From fe26781cc3bf164e70dae15ea2c7bbf6e4833c0a Mon Sep 17 00:00:00 2001 From: tjkwon Date: Fri, 6 Dec 2024 18:12:59 +0900 Subject: [PATCH] apps/examples/wakerec : Update wakerec to support record without KD --- apps/examples/wakerec/wakerec.cxx | 178 ++++++++++++++++-------------- 1 file changed, 98 insertions(+), 80 deletions(-) diff --git a/apps/examples/wakerec/wakerec.cxx b/apps/examples/wakerec/wakerec.cxx index c4a30c39d7..1fed400ace 100644 --- a/apps/examples/wakerec/wakerec.cxx +++ b/apps/examples/wakerec/wakerec.cxx @@ -53,22 +53,66 @@ static const char *filePath = "/tmp/record.pcm"; uint8_t *gBuffer = NULL; uint32_t bufferSize = 0; -static bool isRecording = true; - -static void playRecordVoice(void); -static void startRecord(void); - class WakeRec : public media::voice::SpeechDetectorListenerInterface,public FocusChangeListener, public media::MediaRecorderObserverInterface, public media::MediaPlayerObserverInterface, public std::enable_shared_from_this { +public: + void initWareRec(bool set) + { + mKDEnabled = set; + printf("open file path : %s\n", filePath); + fp = fopen(filePath, "wb"); + if (fp == NULL) { + printf("FILE OPEN FAILED\n"); + return; + } + } + + void startRecord(void) + { + media::recorder_result_t mret = mr.create(); + if (mret == media::RECORDER_OK) { + printf("#### [MR] create succeeded.\n"); + } else { + printf("#### [MR] create failed.\n"); + return; + } + + mret = mr.setDataSource(std::unique_ptr( + new media::stream::BufferOutputDataSource(1, 16000, media::AUDIO_FORMAT_TYPE_S16_LE))); + if (mret == media::RECORDER_OK) { + printf("#### [MR] setDataSource succeeded.\n"); + } else { + printf("#### [MR] setDataSource failed.\n"); + return; + } + + mret = mr.setObserver(shared_from_this()); + if (mret == media::RECORDER_OK) { + printf("#### [MR] setObserver succeeded.\n"); + } else { + printf("#### [MR] setObserver failed.\n"); + return; + } + + if (mr.setDuration(7) == RECORDER_ERROR_NONE && mr.prepare() == RECORDER_ERROR_NONE) { + printf("#### [MR] prepare succeeded.\n"); + } else { + printf("#### [MR] prepare failed.\n"); + return; + } + + mr.start(); + } + private: MediaPlayer mp; MediaRecorder mr; shared_ptr mFocusRequest; - FILE *fp; bool mPaused; - + bool mKDEnabled; + FILE *fp; void onRecordStarted(media::MediaRecorder &mediaRecorder) override { printf("##################################\n"); @@ -121,9 +165,6 @@ class WakeRec : public media::voice::SpeechDetectorListenerInterface,public Focu void onRecordBufferDataReached(media::MediaRecorder &mediaRecorder, std::shared_ptr data, size_t size) override { - if (!isRecording) { - return; - } printf("###########################################\n"); printf("#### onRecordBufferDataReached ####\n"); printf("###########################################\n"); @@ -132,6 +173,8 @@ class WakeRec : public media::voice::SpeechDetectorListenerInterface,public Focu if (fp != NULL) { int sz_written = fwrite(sdata, sizeof(short), size / 2, fp); printf("\n********Size written to file= %d *********\n", sz_written); + } else { + printf("fp is null!!\n"); } } @@ -157,16 +200,17 @@ class WakeRec : public media::voice::SpeechDetectorListenerInterface,public Focu printf("##################################\n"); printf("#### Playback done!! ####\n"); printf("##################################\n"); - - printf("###################################\n"); - printf("#### Wait for wakeup triggered ####\n"); - printf("###################################\n"); - - sd->startKeywordDetect(); + if (mKDEnabled) { + printf("###################################\n"); + printf("#### Wait for wakeup triggered ####\n"); + printf("###################################\n"); + sd->startKeywordDetect(); + } /* Now that we finished playback, we can go to sleep */ sleep(3); //for test, add sleep. - - pm_resume(PM_IDLE_DOMAIN); + if (mKDEnabled) { + pm_resume(PM_IDLE_DOMAIN); + } } void onPlaybackStopped(media::MediaPlayer &mediaPlayer) override @@ -206,12 +250,6 @@ class WakeRec : public media::voice::SpeechDetectorListenerInterface,public Focu pm_suspend(PM_IDLE_DOMAIN); printf("Event SPEECH_DETECT_KD\n"); printf("#### [SD] keyword detected.\n"); - fp = fopen(filePath, "wb"); - if (fp == NULL) { - printf("FILE OPEN FAILED\n"); - return; - } - if (gBuffer) { if (sd->getKeywordData(gBuffer) == true) { /* consume buffer */ @@ -267,43 +305,6 @@ class WakeRec : public media::voice::SpeechDetectorListenerInterface,public Focu } } - void startRecord(void) - { - media::recorder_result_t mret = mr.create(); - if (mret == media::RECORDER_OK) { - printf("#### [MR] create succeeded.\n"); - } else { - printf("#### [MR] create failed.\n"); - return; - } - - mret = mr.setDataSource(std::unique_ptr( - new media::stream::BufferOutputDataSource(1, 16000, media::AUDIO_FORMAT_TYPE_S16_LE))); - if (mret == media::RECORDER_OK) { - printf("#### [MR] setDataSource succeeded.\n"); - } else { - printf("#### [MR] setDataSource failed.\n"); - return; - } - - mret = mr.setObserver(shared_from_this()); - if (mret == media::RECORDER_OK) { - printf("#### [MR] setObserver succeeded.\n"); - } else { - printf("#### [MR] setObserver failed.\n"); - return; - } - - if (mr.setDuration(7) == RECORDER_ERROR_NONE && mr.prepare() == RECORDER_ERROR_NONE) { - printf("#### [MR] prepare succeeded.\n"); - } else { - printf("#### [MR] prepare failed.\n"); - return; - } - - mr.start(); - } - void playRecordVoice(void) { mp.create(); @@ -327,34 +328,51 @@ extern "C" { int wakerec_main(int argc, char *argv[]) { printf("wakerec_main Entry\n"); - sd = media::voice::SpeechDetector::instance(); - if (!sd->initKeywordDetect(16000, 1)) { - printf("#### [SD] init failed.\n"); - return 0; + + if (argc > 3) { + printf("invalid input\n"); + printf("Usage : wakerec [mode]\n"); + printf("mode is optional 0 Disable wakeup\n"); + return -1; } + auto recorder = std::shared_ptr(new WakeRec()); + if (argc == 2 && atoi(argv[1]) == 0) { + printf("disable KD!!\n"); + recorder->initWareRec(false); + recorder->startRecord(); + } else { + recorder->initWareRec(true); + sd = media::voice::SpeechDetector::instance(); + if (!sd->initKeywordDetect(16000, 1)) { + printf("#### [SD] init failed.\n"); + return 0; + } - sd->addListener(std::make_shared()); + sd->addListener(recorder); - printf("###################################\n"); - printf("#### Wait for wakeup triggered ####\n"); - printf("###################################\n"); - - if (sd->getKeywordBufferSize(&bufferSize) == true) { - printf("KD buffer size %d\n", bufferSize); - gBuffer = new uint8_t[bufferSize]; - if (!gBuffer) { - printf("memory allocation failed\n"); + printf("###################################\n"); + printf("#### Wait for wakeup triggered ####\n"); + printf("###################################\n"); + + if (sd->getKeywordBufferSize(&bufferSize) == true) { + printf("KD buffer size %d\n", bufferSize); + gBuffer = new uint8_t[bufferSize]; + if (!gBuffer) { + printf("memory allocation failed\n"); + } } + sd->startKeywordDetect(); + /* similar to wake lock, we release wake lock as we started our thread */ + pm_resume(PM_IDLE_DOMAIN); } - sd->startKeywordDetect(); - /* similar to wake lock, we release wake lock as we started our thread */ - pm_resume(PM_IDLE_DOMAIN); while (1) { sleep(67); } - delete[] gBuffer; - gBuffer = NULL; + if (gBuffer) { + delete[] gBuffer; + gBuffer = NULL; + } return 0; } }