Skip to content

Commit

Permalink
core: fix SpeechRecognizer to support WIN32
Browse files Browse the repository at this point in the history
fix following codes to support WIN32
- change variable-length array to `calloc()`
- change `nanosleep` to `Sleep()`

Signed-off-by: Inho Oh <[email protected]>
  • Loading branch information
webispy authored and kimhyungrok committed Jun 3, 2024
1 parent ef30872 commit 9ae7c40
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/core/speech_recognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include "nugu_timer.hh"
#include "speech_recognizer.hh"

#ifdef _WIN32
#include <windows.h>
#endif

#define EPD_MODEL_FILE "skt_epd_model.raw"
#define OUT_DATA_SIZE (1024 * 9)

Expand Down Expand Up @@ -192,6 +196,7 @@ void SpeechRecognizer::loop()
std::string id = listening_id;

while (g_atomic_int_get(&destroy) == 0) {
char *pcm_buf = nullptr;
std::unique_lock<std::mutex> lock(mutex);
cond.wait(lock);
lock.unlock();
Expand Down Expand Up @@ -237,18 +242,26 @@ void SpeechRecognizer::loop()
prev_epd_ret = 0;
is_epd_end = false;
pcm_size = recorder->getAudioFrameSize();
pcm_buf = (char *)calloc(1, pcm_size);
if (!pcm_buf) {
nugu_error_nomem();
continue;
}

while (is_running) {
char pcm_buf[pcm_size];

if (!recorder->isRecording()) {
#ifndef _WIN32
struct timespec ts;

ts.tv_sec = 0;
ts.tv_nsec = 10 * 1000 * 1000; /* 10ms */

nugu_dbg("Listening Thread: not recording state");
nanosleep(&ts, NULL);
#else
nugu_dbg("Listening Thread: not recording state (10ms wait)");
Sleep(10);
#endif
continue;
}

Expand Down Expand Up @@ -345,6 +358,11 @@ void SpeechRecognizer::loop()
if (g_atomic_int_get(&destroy) == 0)
sendListeningEvent(ListeningState::DONE, id);

if (pcm_buf) {
free(pcm_buf);
pcm_buf = nullptr;
}

is_running = false;
recorder->stop();
epd_client_release();
Expand Down Expand Up @@ -390,6 +408,7 @@ void SpeechRecognizer::loop()
std::string id = listening_id;

while (g_atomic_int_get(&destroy) == 0) {
char *pcm_buf = nullptr;
std::unique_lock<std::mutex> lock(mutex);
cond.wait(lock);
lock.unlock();
Expand Down Expand Up @@ -423,18 +442,26 @@ void SpeechRecognizer::loop()
sendListeningEvent(ListeningState::LISTENING, id);

pcm_size = recorder->getAudioFrameSize();
pcm_buf = (char *)calloc(1, pcm_size);
if (!pcm_buf) {
nugu_error_nomem();
continue;
}

while (is_running) {
char pcm_buf[pcm_size];

if (!recorder->isRecording()) {
#ifndef _WIN32
struct timespec ts;

ts.tv_sec = 0;
ts.tv_nsec = 10 * 1000 * 1000; /* 10ms */

nugu_dbg("Listening Thread: not recording state");
nanosleep(&ts, NULL);
#else
nugu_dbg("Listening Thread: not recording state (10ms wait)");
Sleep(10);
#endif
continue;
}

Expand Down Expand Up @@ -492,6 +519,11 @@ void SpeechRecognizer::loop()
if (g_atomic_int_get(&destroy) == 0)
sendListeningEvent(ListeningState::DONE, id);

if (pcm_buf) {
free(pcm_buf);
pcm_buf = nullptr;
}

is_running = false;
is_first = true;
is_end = false;
Expand Down

0 comments on commit 9ae7c40

Please sign in to comment.