Template
reco_sphinx.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25
34#ifndef __RECO_H__
35#define __RECO_H__
36
38#include <pocketsphinx.h>
39#include <thread>
40#include <mutex>
41
44#include <cstring>
45#include <iostream>
46
47#include <unordered_map>
48
49#define MAX_BUFFER 262144
50
51class Buffer {
52public:
53 Buffer(size_t bufferSize) : size(bufferSize), data(new char[size]), count(0) {}
54
56 {
57 delete[] data;
58 }
59
60 void fill(const char* newData, size_t newDataSize)
61 {
62 if (newDataSize > size - count)
63 {
64 // "Buffer overflow. Data not filled completely."
65 return;
66 }
67
68 std::memcpy(data + count, newData, newDataSize);
69 count += newDataSize;
70 }
71
72 void consume(size_t consumeSize)
73 {
74 if (consumeSize >= count)
75 {
76 count = 0;
77 }
78 else
79 {
80 std::memmove(data, data + consumeSize, count - consumeSize);
81 count -= consumeSize;
82 }
83 }
84
85 void getInt16(int16_t* int16Array, size_t length)
86 {
87 const int16_t* audioData = reinterpret_cast<const int16_t*>(data);
88 for (size_t i = 0; i < length; ++i)
89 {
90 int16Array[i] = audioData[i];
91 }
92 consume(length);
93 }
94
95 const char* getData() const
96 {
97 return data;
98 }
99
100 size_t getCount() const
101 {
102 return count;
103 }
104
105private:
106 size_t size;
107 char* data;
108 size_t count;
109};
110
111
112class Recognition
113{
114public:
115protected:
116private:
117 std::thread mThread;
118 std::mutex mMutex;
119 std::mutex mMutexConfig;
120 bool mValid;
121 ps_config_t* mConfig;
122 ps_decoder_t* mDecoder;
123 ps_endpointer_t* mEndPointer;
124 size_t mFrameSize;
125 Buffer* mBuffer;
126 bool mKeySearch;
127 std::vector<std::string> mKeyWords;
128public:
132
135 Recognition(std::string pathtobin, std::string lang);
136
140
143 void cbThread();
144
145 void fillAudioBuffer(const char* data, size_t lenght);
146
150
154
157 void setVolume(int volume);
158
161 void AddWord(std::string s_Rule, std::string s_Word);
162protected:
163private:
164};
165
166#endif
void consume(size_t consumeSize)
Definition reco_sphinx.h:72
Buffer(size_t bufferSize)
Definition reco_sphinx.h:53
void getInt16(int16_t *int16Array, size_t length)
Definition reco_sphinx.h:85
size_t getCount() const
const char * getData() const
Definition reco_sphinx.h:95
void fill(const char *newData, size_t newDataSize)
Definition reco_sphinx.h:60
Management of the recognition class .
Definition reco.h:50
void setVolume(int volume)
void AddWord(std::string s_Rule, std::string s_Word)
void fillAudioBuffer(const char *data, size_t lenght)
bool initializeObjects()
int getVolume()
Recognition(std::string pathtobin, std::string lang)