-
Notifications
You must be signed in to change notification settings - Fork 585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
apps/examples/wakerec : Update wakerec to support record without KD #6564
Open
Taejun-Kwon
wants to merge
1
commit into
Samsung:master
Choose a base branch
from
Taejun-Kwon:wakerec
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−80
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<WakeRec> | ||
{ | ||
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<media::stream::BufferOutputDataSource>( | ||
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<FocusRequest> 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<unsigned char> 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<media::stream::BufferOutputDataSource>( | ||
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<WakeRec>(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<WakeRec>()); | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if !NULL only work when used like new(std::nothrow) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nullptr is \0 so I think this is okay. |
||
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; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initWareRec -> initWakeRec