48#include <unordered_map>
52 Buffer(
size_t bufferSize) : size(bufferSize), data(new char[size]), count(0) {}
59 void fill(
const char* newData,
size_t newDataSize)
61 const std::lock_guard<std::mutex> lock(mMutex);
62 if (newDataSize > size - count)
70 std::memcpy(data + count, newData, newDataSize);
76 if (consumeSize >= count)
82 std::memmove(data, data + consumeSize, count - consumeSize);
89 const std::lock_guard<std::mutex> lock(mMutex);
90 if (consumeSize >= count)
96 std::memmove(data, data + consumeSize, count - consumeSize);
103 const std::lock_guard<std::mutex> lock(mMutex);
104 size_t cpl = std::min(length, count);
105 std::memcpy(buffer, data, cpl);
112 const int16_t* audioData =
reinterpret_cast<const int16_t*
>(data);
113 for (
size_t i = 0; i < length; ++i)
115 int16Array[i] = audioData[i];
133 const int16_t* samples =
reinterpret_cast<const int16_t*
>(data);
134 size_t numSamples = length /
sizeof(int16_t);
138 for (
size_t i = 0; i < numSamples; ++i)
140 double sample =
static_cast<double>(samples[i]) /
static_cast<double>(INT16_MAX);
141 sum += sample * sample;
145 double rmsLevel = std::sqrt(sum / numSamples);
150 void RemoveNoise(
size_t length,
float noiseLevel,
float reductionFactor)
153 const size_t sampleSize = 2;
156 const float threshold = noiseLevel * std::numeric_limits<short>::max();
159 for (
size_t i = 0; i < length; i += sampleSize)
162 short* sample =
reinterpret_cast<short*
>(&data[i]);
165 if (std::abs(*sample) < threshold)
168 *sample *= reductionFactor;
175 const std::lock_guard<std::mutex> lock(mMutex);
177 size_t cpl = std::min(length, count);
181 if (audioLevel < threshold)
187 std::memcpy(buffer, data, cpl);
206 std::mutex mMutexConfig;
209 VoskRecognizer* mRecognizer;
214 std::vector<std::string> mKeyWords;
248 void AddWord(std::string s_Rule, std::string s_Word);
void consume(size_t consumeSize)
void consume_safe(size_t consumeSize)
size_t getBufferCopy(char *buffer, size_t length)
Buffer(size_t bufferSize)
void getInt16(int16_t *int16Array, size_t length)
float calculateAudioLevel(size_t length)
const char * getData() const
size_t ProcessBuffer(char *buffer, size_t length, float threshold)
void RemoveNoise(size_t length, float noiseLevel, float reductionFactor)
void fill(const char *newData, size_t newDataSize)
Management of the recognition class .
void setVolume(int volume)
void AddWord(std::string s_Rule, std::string s_Word)
void fillAudioBuffer(const char *data, size_t lenght)
Recognition(std::string pathtobin, std::string lang)