diff options
author | leandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 13:29:35 +0000 |
---|---|---|
committer | leandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 13:29:35 +0000 |
commit | d1ecb010a30afb74dfd33294771c9a1559f62141 (patch) | |
tree | 259e50648ff9445ff7c43f713c960a31f55e7b2b /content/browser/speech/speech_recognizer.cc | |
parent | b773cee71532fbce19c1b92f256a950979764c8c (diff) | |
download | chromium_src-d1ecb010a30afb74dfd33294771c9a1559f62141.zip chromium_src-d1ecb010a30afb74dfd33294771c9a1559f62141.tar.gz chromium_src-d1ecb010a30afb74dfd33294771c9a1559f62141.tar.bz2 |
Applying changes to the existing speech input code to support the extension API.
This includes extended error management by handling the status response code and the DidStartReceivingSpeech/DidStopReceivingSpeech events.
BUG=97388
TEST=none
Review URL: http://codereview.chromium.org/8137005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104471 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/speech/speech_recognizer.cc')
-rw-r--r-- | content/browser/speech/speech_recognizer.cc | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/content/browser/speech/speech_recognizer.cc b/content/browser/speech/speech_recognizer.cc index 295f156..f81128c 100644 --- a/content/browser/speech/speech_recognizer.cc +++ b/content/browser/speech/speech_recognizer.cc @@ -145,6 +145,7 @@ void SpeechRecognizer::StopRecording() { audio_controller_->Close(); audio_controller_ = NULL; // Releases the ref ptr. + delegate_->DidStopReceivingSpeech(caller_id_); delegate_->DidCompleteRecording(caller_id_); // UploadAudioChunk requires a non-empty final buffer. So we encode a packet @@ -186,7 +187,7 @@ void SpeechRecognizer::HandleOnError(int error_code) { if (!audio_controller_.get()) return; - InformErrorAndCancelRecognition(RECOGNIZER_ERROR_CAPTURE); + InformErrorAndCancelRecognition(kErrorAudio); } void SpeechRecognizer::OnData(AudioInputController* controller, @@ -210,6 +211,8 @@ void SpeechRecognizer::HandleOnData(string* data) { return; } + bool speech_was_heard_before_packet = endpointer_.DidStartReceivingSpeech(); + const short* samples = reinterpret_cast<const short*>(data->data()); DCHECK((data->length() % sizeof(short)) == 0); int num_samples = data->length() / sizeof(short); @@ -246,12 +249,16 @@ void SpeechRecognizer::HandleOnData(string* data) { } // Check if we have waited too long without hearing any speech. - if (!endpointer_.DidStartReceivingSpeech() && + bool speech_was_heard_after_packet = endpointer_.DidStartReceivingSpeech(); + if (!speech_was_heard_after_packet && num_samples_recorded_ >= kNoSpeechTimeoutSec * kAudioSampleRate) { - InformErrorAndCancelRecognition(RECOGNIZER_ERROR_NO_SPEECH); + InformErrorAndCancelRecognition(kErrorNoSpeech); return; } + if (!speech_was_heard_before_packet && speech_was_heard_after_packet) + delegate_->DidStartReceivingSpeech(caller_id_); + // Calculate the input volume to display in the UI, smoothing towards the // new level. float level = (rms - kAudioMeterMinDb) / @@ -271,30 +278,26 @@ void SpeechRecognizer::HandleOnData(string* data) { delegate_->SetInputVolume(caller_id_, did_clip ? 1.0f : audio_level_, noise_level); - if (endpointer_.speech_input_complete()) { + if (endpointer_.speech_input_complete()) StopRecording(); - } - - // TODO(satish): Once we have streaming POST, start sending the data received - // here as POST chunks. } void SpeechRecognizer::SetRecognitionResult( - bool error, const SpeechInputResultArray& result) { - if (error || result.empty()) { - InformErrorAndCancelRecognition(error ? RECOGNIZER_ERROR_NETWORK : - RECOGNIZER_ERROR_NO_RESULTS); + const SpeechInputResult& result) { + if (result.error != kErrorNone) { + InformErrorAndCancelRecognition(result.error); return; } - delegate_->SetRecognitionResult(caller_id_, error, result); - // Guard against the delegate freeing us until we finish our job. scoped_refptr<SpeechRecognizer> me(this); + delegate_->SetRecognitionResult(caller_id_, result); delegate_->DidCompleteRecognition(caller_id_); } -void SpeechRecognizer::InformErrorAndCancelRecognition(ErrorCode error) { +void SpeechRecognizer::InformErrorAndCancelRecognition( + SpeechInputError error) { + DCHECK_NE(error, kErrorNone); CancelRecognition(); // Guard against the delegate freeing us until we finish our job. |