Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(880)

Side by Side Diff: content/browser/speech/speech_recognizer.cc

Issue 11421103: Update the Speech Api to support array(s) of result items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix issue with page reloads and add a TODO. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/speech/speech_recognizer.h" 5 #include "content/browser/speech/speech_recognizer.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/time.h" 9 #include "base/time.h"
10 #include "content/browser/browser_main_loop.h" 10 #include "content/browser/browser_main_loop.h"
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 FSMEventArgs event_args(EVENT_AUDIO_DATA); 179 FSMEventArgs event_args(EVENT_AUDIO_DATA);
180 event_args.audio_data = new AudioChunk(data, static_cast<size_t>(size), 180 event_args.audio_data = new AudioChunk(data, static_cast<size_t>(size),
181 kNumBitsPerAudioSample / 8); 181 kNumBitsPerAudioSample / 8);
182 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 182 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
183 base::Bind(&SpeechRecognizer::DispatchEvent, 183 base::Bind(&SpeechRecognizer::DispatchEvent,
184 this, event_args)); 184 this, event_args));
185 } 185 }
186 186
187 void SpeechRecognizer::OnAudioClosed(AudioInputController*) {} 187 void SpeechRecognizer::OnAudioClosed(AudioInputController*) {}
188 188
189 void SpeechRecognizer::OnSpeechRecognitionEngineResult( 189 void SpeechRecognizer::OnSpeechRecognitionEngineResults(
190 const SpeechRecognitionResult& result) { 190 const SpeechRecognitionResults& results) {
191 FSMEventArgs event_args(EVENT_ENGINE_RESULT); 191 FSMEventArgs event_args(EVENT_ENGINE_RESULT);
192 event_args.engine_result = result; 192 event_args.engine_results = results;
193 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 193 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
194 base::Bind(&SpeechRecognizer::DispatchEvent, 194 base::Bind(&SpeechRecognizer::DispatchEvent,
195 this, event_args)); 195 this, event_args));
196 } 196 }
197 197
198 void SpeechRecognizer::OnSpeechRecognitionEngineError( 198 void SpeechRecognizer::OnSpeechRecognitionEngineError(
199 const SpeechRecognitionError& error) { 199 const SpeechRecognitionError& error) {
200 FSMEventArgs event_args(EVENT_ENGINE_ERROR); 200 FSMEventArgs event_args(EVENT_ENGINE_ERROR);
201 event_args.engine_error = error; 201 event_args.engine_error = error;
202 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 202 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 if (state_ == STATE_ESTIMATING_ENVIRONMENT) { 547 if (state_ == STATE_ESTIMATING_ENVIRONMENT) {
548 DCHECK(endpointer_.IsEstimatingEnvironment()); 548 DCHECK(endpointer_.IsEstimatingEnvironment());
549 endpointer_.SetUserInputMode(); 549 endpointer_.SetUserInputMode();
550 listener_->OnEnvironmentEstimationComplete(session_id_); 550 listener_->OnEnvironmentEstimationComplete(session_id_);
551 } else if (state_ == STATE_WAITING_FOR_SPEECH) { 551 } else if (state_ == STATE_WAITING_FOR_SPEECH) {
552 listener_->OnSoundStart(session_id_); 552 listener_->OnSoundStart(session_id_);
553 } else { 553 } else {
554 DCHECK_EQ(STATE_RECOGNIZING, state_); 554 DCHECK_EQ(STATE_RECOGNIZING, state_);
555 } 555 }
556 556
557 const SpeechRecognitionResult& result = event_args.engine_result; 557 listener_->OnRecognitionResults(session_id_, event_args.engine_results);
558 listener_->OnRecognitionResult(session_id_, result);
559 return STATE_RECOGNIZING; 558 return STATE_RECOGNIZING;
560 } 559 }
561 560
562 SpeechRecognizer::FSMState 561 SpeechRecognizer::FSMState
563 SpeechRecognizer::ProcessFinalResult(const FSMEventArgs& event_args) { 562 SpeechRecognizer::ProcessFinalResult(const FSMEventArgs& event_args) {
564 const SpeechRecognitionResult& result = event_args.engine_result; 563 const SpeechRecognitionResults& results = event_args.engine_results;
565 if (result.is_provisional) { 564 SpeechRecognitionResults::const_iterator i = results.begin();
566 DCHECK(!is_single_shot_); 565 bool provisional_results_pending = false;
567 listener_->OnRecognitionResult(session_id_, result); 566 bool results_are_empty = true;
567 for (; i != results.end(); ++i) {
568 const SpeechRecognitionResult& result = (*i);
hans 2012/11/28 14:15:35 ultra nit: parenthesis around *i seems a bit overk
tommi (sloooow) - chröme 2012/11/29 09:30:20 Done.
569 if (result.is_provisional) {
570 provisional_results_pending = true;
571 DCHECK(!is_single_shot_);
572 } else if (results_are_empty) {
573 results_are_empty = result.hypotheses.empty();
574 }
575 }
576
577 if (provisional_results_pending) {
578 listener_->OnRecognitionResults(session_id_, results);
568 // We don't end the recognition if a provisional result is received in 579 // We don't end the recognition if a provisional result is received in
569 // STATE_WAITING_FINAL_RESULT. A definitive result will come next and will 580 // STATE_WAITING_FINAL_RESULT. A definitive result will come next and will
570 // end the recognition. 581 // end the recognition.
571 return state_; 582 return state_;
572 } else { 583 }
573 recognition_engine_->EndRecognition(); 584
585 recognition_engine_->EndRecognition();
586
587 if (!results_are_empty) {
574 // We could receive an empty result (which we won't propagate further) 588 // We could receive an empty result (which we won't propagate further)
575 // in the following (continuous) scenario: 589 // in the following (continuous) scenario:
576 // 1. The caller start pushing audio and receives some results; 590 // 1. The caller start pushing audio and receives some results;
577 // 2. A |StopAudioCapture| is issued later; 591 // 2. A |StopAudioCapture| is issued later;
578 // 3. The final audio frames captured in the interval ]1,2] do not lead to 592 // 3. The final audio frames captured in the interval ]1,2] do not lead to
579 // any result (nor any error); 593 // any result (nor any error);
580 // 4. The speech recognition engine, therefore, emits an empty result to 594 // 4. The speech recognition engine, therefore, emits an empty result to
581 // notify that the recognition is ended with no error, yet neither any 595 // notify that the recognition is ended with no error, yet neither any
582 // further result. 596 // further result.
583 if (result.hypotheses.size() > 0) 597 listener_->OnRecognitionResults(session_id_, results);
584 listener_->OnRecognitionResult(session_id_, result);
585 listener_->OnRecognitionEnd(session_id_);
586 return STATE_IDLE;
587 } 598 }
599
600 listener_->OnRecognitionEnd(session_id_);
601 return STATE_IDLE;
588 } 602 }
589 603
590 SpeechRecognizer::FSMState 604 SpeechRecognizer::FSMState
591 SpeechRecognizer::DoNothing(const FSMEventArgs&) const { 605 SpeechRecognizer::DoNothing(const FSMEventArgs&) const {
592 return state_; // Just keep the current state. 606 return state_; // Just keep the current state.
593 } 607 }
594 608
595 SpeechRecognizer::FSMState 609 SpeechRecognizer::FSMState
596 SpeechRecognizer::NotFeasible(const FSMEventArgs& event_args) { 610 SpeechRecognizer::NotFeasible(const FSMEventArgs& event_args) {
597 NOTREACHED() << "Unfeasible event " << event_args.event 611 NOTREACHED() << "Unfeasible event " << event_args.event
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 : event(event_value), 660 : event(event_value),
647 audio_error_code(0), 661 audio_error_code(0),
648 audio_data(NULL), 662 audio_data(NULL),
649 engine_error(SPEECH_RECOGNITION_ERROR_NONE) { 663 engine_error(SPEECH_RECOGNITION_ERROR_NONE) {
650 } 664 }
651 665
652 SpeechRecognizer::FSMEventArgs::~FSMEventArgs() { 666 SpeechRecognizer::FSMEventArgs::~FSMEventArgs() {
653 } 667 }
654 668
655 } // namespace content 669 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698