36#define MAX_BUFFER 262144
49 DWORD fileAttributes = GetFileAttributes(filePath.c_str());
50 return (fileAttributes != INVALID_FILE_ATTRIBUTES);
61 return (stat(filePath.c_str(), &buffer) == 0);
67 size_t found = filePath.find_last_of(
"/\\");
69 if (found != std::string::npos)
70 return filePath.substr(0, found);
77 size_t lastSlash = filePath.find_last_of(
"/\\");
78 size_t lastDot = filePath.find_last_of(
".");
79 if (lastDot != std::string::npos && (lastSlash == std::string::npos || lastDot > lastSlash))
80 return filePath.substr(lastSlash + 1, lastDot - lastSlash - 1);
82 return filePath.substr(lastSlash + 1);
87 std::istringstream iss(text);
115 mConfig = ps_config_init(NULL);
122 ps_config_set_int(mConfig,
"samprate", 16000);
124 ps_config_set_int(mConfig,
"pl_window", 3);
125 ps_config_set_int(mConfig,
"topn", 3);
126 ps_config_set_str(mConfig,
"hmm", (pathDir +
P_SLASH + lang).c_str());
127 ps_config_set_str(mConfig,
"lm", pathtobin.c_str());
128 ps_config_set_str(mConfig,
"dict", pathToDict.c_str());
130 mDecoder = ps_init(mConfig);
131 mEndPointer = ps_endpointer_init(0, 0.0, PS_VAD_MEDIUM_LOOSE, 0, 0);
132 if ((mDecoder != NULL) && (mEndPointer != NULL))
134 mFrameSize = ps_endpointer_frame_size(mEndPointer);
141 if (mEndPointer != NULL)
142 ps_endpointer_free(mEndPointer);
145 if (mDecoder != NULL)
150 ps_config_free(mConfig);
164 if (mThread.joinable())
167 if (mEndPointer != NULL)
168 ps_endpointer_free(mEndPointer);
170 if (mDecoder != NULL)
174 ps_config_free(mConfig);
176 SAFE_DELETE(mBuffer);
185 const std::lock_guard<std::mutex> lock(mMutex);
186 mBuffer->
fill(data, lenght);
197 const std::lock_guard<std::mutex> lock(mMutexConfig);
198 int prev_in_speech = ps_endpointer_in_speech(mEndPointer);
201 if (mBuffer->
getCount() < mFrameSize)
203 std::this_thread::sleep_for(std::chrono::milliseconds(10));
209 const std::lock_guard<std::mutex> lock(mMutex);
210 mBuffer->
getInt16(buffer, mFrameSize);
212 speech = ps_endpointer_process(mEndPointer, buffer);
220 ps_start_utt(mDecoder);
223 if (ps_process_raw(mDecoder, speech, mFrameSize, FALSE, FALSE) < 0)
225 std::this_thread::sleep_for(std::chrono::milliseconds(10));
229 if (!ps_endpointer_in_speech(mEndPointer))
231 ps_end_utt(mDecoder);
233 if ((hyp = ps_get_hyp(mDecoder, NULL)) != NULL)
243 std::list<pTextRec*> lPhraseRec;
244 ps_nbest_t* nbest = ps_nbest(mDecoder);
246 for (
int n = 0; nbest && n < 10; nbest = ps_nbest_next(nbest), n++)
248 hyp = ps_nbest_hyp(nbest, &score);
253 paramTextRec->
altText = from_utf8(hyp);
255 lPhraseRec.push_back(paramTextRec);
258 if (!lPhraseRec.empty())
268 std::this_thread::sleep_for(std::chrono::milliseconds(10));
276 const std::lock_guard<std::mutex> lock(mMutexConfig);
277 mKeyWords.push_back(s_Word);
280 for (
unsigned int i = 0; i < mKeyWords.size(); i++)
282 std::string w = mKeyWords[i];
283 std::transform(w.begin(), w.end(), w.begin(), ::tolower);
285 wlist = wlist + to_utf8(w) +
" /1e-" + std::to_string(std::min(wcnt * 10, 60)) +
"/\n";
288 ps_add_keyphrase(mDecoder,
"keyword", wlist.c_str());
289 ps_activate_search(mDecoder,
"keyword");
void getInt16(int16_t *int16Array, size_t length)
void fill(const char *newData, size_t newDataSize)
void setVolume(int volume)
Set The volume for the recognition.
void AddWord(std::string s_Rule, std::string s_Word)
Add word to recognition.
void fillAudioBuffer(const char *data, size_t lenght)
Recognition()
utils libraries
bool initializeObjects()
Init Recognition Objects.
int getVolume()
Get The volume from recognition.
~Recognition()
Recognition Destructor.
int RECOGNITION_TEXTS_ALT_CB
bool fileExists(const std::string &filePath)
std::string getDirectoryFromFilePath(const std::string &filePath)
int countWords(const std::string &text)
std::string getFileNameWithoutExtension(const std::string &filePath)