porcupine_recognizer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021, Rein Appeldoorn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <stdexcept>
19 
20 #include "./porcupine_recognizer.h"
21 #include "./util.h"
22 
23 namespace picovoice_driver
24 {
25 std::ostream& operator<<(std::ostream& os, const PorcupineRecognizerData::Parameters& p)
26 {
27  os << "Parameters(access_key=" << p.access_key_ << ", model_path=" << p.model_path_
28  << ", keywords=" << toString(p.keywords_) << ", sensitivity=" << p.sensitivity_ << ")";
29  return os;
30 }
31 
32 std::ostream& operator<<(std::ostream& os, const PorcupineRecognizerData::Result& r)
33 {
34  os << "Result(is_understood=" << r.is_understood_ << ", keyword=" << r.keyword_name_ << ")";
35  return os;
36 }
37 
39 {
40  if (porcupine_ != NULL)
41  {
43  }
44 }
45 
47 {
48  keyword_names_.clear();
49  keyword_paths_.clear();
50  keyword_sensitivities_.clear();
51  for (const auto& kv : parameters.keywords_)
52  {
53  keyword_names_.push_back(kv.first);
54  keyword_paths_.push_back(kv.second.c_str());
55  keyword_sensitivities_.push_back(static_cast<float>(parameters.sensitivity_));
56  }
57 
58  pv_status_t status =
59  pv_porcupine_init(parameters.access_key_.data(), parameters.model_path_.data(), keyword_names_.size(),
61  if (status != PV_STATUS_SUCCESS)
62  {
63  throw std::runtime_error("Failed to initialize picovoice porcupine with parameters " + toString(parameters) + ": " +
64  std::string(pv_status_to_string(status)));
65  }
66 }
67 
69 {
70  return result_;
71 }
72 
74 {
77  settings.sample_rate_ = pv_sample_rate();
78  return settings;
79 }
80 
82 {
84 }
85 
87 {
88  int32_t keyword_index = -1;
89  pv_status_t status = pv_porcupine_process(porcupine_, frames, &keyword_index);
90  if (status != PV_STATUS_SUCCESS)
91  {
92  throw std::runtime_error("Porcupine process failed: " + std::string(pv_status_to_string(status)));
93  }
94  if (keyword_index < 0)
95  {
96  return false;
97  }
98  if (static_cast<size_t>(keyword_index) >= keyword_names_.size())
99  {
100  throw std::runtime_error("Keyword index " + std::to_string(keyword_index) + " out of bound for keywords " +
102  }
103 
104  result_.is_understood_ = true;
105  result_.keyword_name_ = keyword_names_.at(keyword_index);
106  return true;
107 }
108 } // namespace picovoice_driver
pv_porcupine_init
PV_API pv_status_t pv_porcupine_init(const char *access_key, const char *model_path, int32_t num_keywords, const char *const *keyword_paths, const float *sensitivities, pv_porcupine_t **object)
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
picovoice_driver::PorcupineRecognizer::porcupine_
pv_porcupine_t * porcupine_
Definition: porcupine_recognizer.h:81
picovoice_driver::operator<<
std::ostream & operator<<(std::ostream &os, const PorcupineRecognizerData::Parameters &p)
Definition: porcupine_recognizer.cpp:25
picovoice_driver::PorcupineRecognizer::getRecordSettings
RecordSettings getRecordSettings() override
Definition: porcupine_recognizer.cpp:73
picovoice_driver::PorcupineRecognizer::~PorcupineRecognizer
~PorcupineRecognizer()
Definition: porcupine_recognizer.cpp:38
picovoice_driver::PorcupineRecognizer::keyword_names_
std::vector< std::string > keyword_names_
Definition: porcupine_recognizer.h:76
picovoice_driver::PorcupineRecognizer::recognizeProcess
bool recognizeProcess(int16_t *frames) override
Definition: porcupine_recognizer.cpp:86
pv_porcupine_process
PV_API pv_status_t pv_porcupine_process(pv_porcupine_t *object, const int16_t *pcm, int32_t *keyword_index)
picovoice_driver::RecognizerData::Parameters::model_path_
std::string model_path_
model_path_ Path to the Picovoice model parameters
Definition: recognizer.h:38
picovoice_driver::PorcupineRecognizer::getResult
PorcupineRecognizerData::Result getResult() override
getResult Get the recognition result
Definition: porcupine_recognizer.cpp:68
picovoice_driver::PorcupineRecognizer::result_
PorcupineRecognizerData::Result result_
Definition: porcupine_recognizer.h:80
PV_STATUS_SUCCESS
@ PV_STATUS_SUCCESS
Definition: porcupine/include/picovoice.h:34
picovoice_driver::PorcupineRecognizer::keyword_paths_
std::vector< const char * > keyword_paths_
Definition: porcupine_recognizer.h:77
pv_porcupine_frame_length
PV_API int32_t pv_porcupine_frame_length(void)
pv_status_t
pv_status_t
Definition: porcupine/include/picovoice.h:33
picovoice_driver::PorcupineRecognizer::keyword_sensitivities_
std::vector< float > keyword_sensitivities_
Definition: porcupine_recognizer.h:78
picovoice_driver
Definition: porcupine_node.cpp:24
picovoice_driver::Recognizer::RecordSettings::frame_length_
size_t frame_length_
Definition: recognizer.h:80
pv_porcupine_delete
PV_API void pv_porcupine_delete(pv_porcupine_t *object)
pv_sample_rate
PV_API int32_t pv_sample_rate(void)
picovoice_driver::PorcupineRecognizerData::Parameters
Definition: porcupine_recognizer.h:33
porcupine_recognizer.h
picovoice_driver::Recognizer::RecordSettings::sample_rate_
size_t sample_rate_
Definition: recognizer.h:79
picovoice_driver::PorcupineRecognizerData::Parameters::keywords_
std::map< std::string, std::string > keywords_
keywords_ Map from a keyword names to a keyword.ppn paths
Definition: porcupine_recognizer.h:38
picovoice_driver::PorcupineRecognizer::configure
void configure(const PorcupineRecognizerData::Parameters &parameters) override
Definition: porcupine_recognizer.cpp:46
picovoice_driver::PorcupineRecognizerData::Parameters::sensitivity_
double sensitivity_
sensitivity_ Recognizer sensitivity
Definition: porcupine_recognizer.h:43
picovoice_driver::Recognizer::RecordSettings
Definition: recognizer.h:77
picovoice_driver::toString
std::string toString(const T &v)
Definition: util.h:28
picovoice_driver::PorcupineRecognizerData::Result::is_understood_
bool is_understood_
is_understood_ Whether the recognizer understood a keyword
Definition: porcupine_recognizer.h:51
util.h
pv_status_to_string
const PV_API char * pv_status_to_string(pv_status_t status)
picovoice_driver::PorcupineRecognizerData::Result::keyword_name_
std::string keyword_name_
keyword_name_ Detected keyword name
Definition: porcupine_recognizer.h:56
picovoice_driver::RecognizerData::Parameters::access_key_
std::string access_key_
access_key_ Picovoice access key
Definition: recognizer.h:33
picovoice_driver::PorcupineRecognizer::recognizeInit
void recognizeInit() override
Definition: porcupine_recognizer.cpp:81
picovoice_driver::PorcupineRecognizerData::Result
Definition: porcupine_recognizer.h:46


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50