| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_impl.h" | 5 #include "content/browser/speech/speech_recognizer_impl.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/time.h" | 9 #include "base/time/time.h" |
| 10 #include "content/browser/browser_main_loop.h" | 10 #include "content/browser/browser_main_loop.h" |
| (...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 DCHECK(audio_manager != NULL); | 496 DCHECK(audio_manager != NULL); |
| 497 | 497 |
| 498 DVLOG(1) << "SpeechRecognizerImpl starting audio capture."; | 498 DVLOG(1) << "SpeechRecognizerImpl starting audio capture."; |
| 499 num_samples_recorded_ = 0; | 499 num_samples_recorded_ = 0; |
| 500 audio_level_ = 0; | 500 audio_level_ = 0; |
| 501 listener()->OnRecognitionStart(session_id()); | 501 listener()->OnRecognitionStart(session_id()); |
| 502 | 502 |
| 503 // TODO(xians): Check if the OS has the device with |device_id_|, return | 503 // TODO(xians): Check if the OS has the device with |device_id_|, return |
| 504 // |SPEECH_AUDIO_ERROR_DETAILS_NO_MIC| if the target device does not exist. | 504 // |SPEECH_AUDIO_ERROR_DETAILS_NO_MIC| if the target device does not exist. |
| 505 if (!audio_manager->HasAudioInputDevices()) { | 505 if (!audio_manager->HasAudioInputDevices()) { |
| 506 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO, | 506 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, |
| 507 SPEECH_AUDIO_ERROR_DETAILS_NO_MIC)); | 507 SPEECH_AUDIO_ERROR_DETAILS_NO_MIC)); |
| 508 } | 508 } |
| 509 | 509 |
| 510 int chunk_duration_ms = recognition_engine_->GetDesiredAudioChunkDurationMs(); | 510 int chunk_duration_ms = recognition_engine_->GetDesiredAudioChunkDurationMs(); |
| 511 | 511 |
| 512 AudioParameters in_params = audio_manager->GetInputStreamParameters( | 512 AudioParameters in_params = audio_manager->GetInputStreamParameters( |
| 513 device_id_); | 513 device_id_); |
| 514 if (!in_params.IsValid() && !unit_test_is_active) { | 514 if (!in_params.IsValid() && !unit_test_is_active) { |
| 515 DLOG(ERROR) << "Invalid native audio input parameters"; | 515 DLOG(ERROR) << "Invalid native audio input parameters"; |
| 516 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); | 516 return Abort( |
| 517 SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE)); |
| 517 } | 518 } |
| 518 | 519 |
| 519 // Audio converter shall provide audio based on these parameters as output. | 520 // Audio converter shall provide audio based on these parameters as output. |
| 520 // Hard coded, WebSpeech specific parameters are utilized here. | 521 // Hard coded, WebSpeech specific parameters are utilized here. |
| 521 int frames_per_buffer = (kAudioSampleRate * chunk_duration_ms) / 1000; | 522 int frames_per_buffer = (kAudioSampleRate * chunk_duration_ms) / 1000; |
| 522 AudioParameters output_parameters = AudioParameters( | 523 AudioParameters output_parameters = AudioParameters( |
| 523 AudioParameters::AUDIO_PCM_LOW_LATENCY, kChannelLayout, kAudioSampleRate, | 524 AudioParameters::AUDIO_PCM_LOW_LATENCY, kChannelLayout, kAudioSampleRate, |
| 524 kNumBitsPerAudioSample, frames_per_buffer); | 525 kNumBitsPerAudioSample, frames_per_buffer); |
| 525 | 526 |
| 526 // Audio converter will receive audio based on these parameters as input. | 527 // Audio converter will receive audio based on these parameters as input. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 557 | 558 |
| 558 // Create an audio converter which converts data between native input format | 559 // Create an audio converter which converts data between native input format |
| 559 // and WebSpeech specific output format. | 560 // and WebSpeech specific output format. |
| 560 audio_converter_.reset( | 561 audio_converter_.reset( |
| 561 new OnDataConverter(input_parameters, output_parameters)); | 562 new OnDataConverter(input_parameters, output_parameters)); |
| 562 | 563 |
| 563 audio_controller_ = AudioInputController::Create( | 564 audio_controller_ = AudioInputController::Create( |
| 564 audio_manager, this, input_parameters, device_id_, NULL); | 565 audio_manager, this, input_parameters, device_id_, NULL); |
| 565 | 566 |
| 566 if (!audio_controller_.get()) { | 567 if (!audio_controller_.get()) { |
| 567 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); | 568 return Abort( |
| 569 SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE)); |
| 568 } | 570 } |
| 569 | 571 |
| 570 audio_log_->OnCreated(0, input_parameters, device_id_); | 572 audio_log_->OnCreated(0, input_parameters, device_id_); |
| 571 | 573 |
| 572 // The endpointer needs to estimate the environment/background noise before | 574 // The endpointer needs to estimate the environment/background noise before |
| 573 // starting to treat the audio as user input. We wait in the state | 575 // starting to treat the audio as user input. We wait in the state |
| 574 // ESTIMATING_ENVIRONMENT until such interval has elapsed before switching | 576 // ESTIMATING_ENVIRONMENT until such interval has elapsed before switching |
| 575 // to user input mode. | 577 // to user input mode. |
| 576 endpointer_.SetEnvironmentEstimationMode(); | 578 endpointer_.SetEnvironmentEstimationMode(); |
| 577 audio_controller_->Record(); | 579 audio_controller_->Record(); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 SpeechRecognizerImpl::FSMState | 644 SpeechRecognizerImpl::FSMState |
| 643 SpeechRecognizerImpl::AbortSilently(const FSMEventArgs& event_args) { | 645 SpeechRecognizerImpl::AbortSilently(const FSMEventArgs& event_args) { |
| 644 DCHECK_NE(event_args.event, EVENT_AUDIO_ERROR); | 646 DCHECK_NE(event_args.event, EVENT_AUDIO_ERROR); |
| 645 DCHECK_NE(event_args.event, EVENT_ENGINE_ERROR); | 647 DCHECK_NE(event_args.event, EVENT_ENGINE_ERROR); |
| 646 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_NONE)); | 648 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_NONE)); |
| 647 } | 649 } |
| 648 | 650 |
| 649 SpeechRecognizerImpl::FSMState | 651 SpeechRecognizerImpl::FSMState |
| 650 SpeechRecognizerImpl::AbortWithError(const FSMEventArgs& event_args) { | 652 SpeechRecognizerImpl::AbortWithError(const FSMEventArgs& event_args) { |
| 651 if (event_args.event == EVENT_AUDIO_ERROR) { | 653 if (event_args.event == EVENT_AUDIO_ERROR) { |
| 652 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO)); | 654 return Abort( |
| 655 SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE)); |
| 653 } else if (event_args.event == EVENT_ENGINE_ERROR) { | 656 } else if (event_args.event == EVENT_ENGINE_ERROR) { |
| 654 return Abort(event_args.engine_error); | 657 return Abort(event_args.engine_error); |
| 655 } | 658 } |
| 656 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_ABORTED)); | 659 return Abort(SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_ABORTED)); |
| 657 } | 660 } |
| 658 | 661 |
| 659 SpeechRecognizerImpl::FSMState SpeechRecognizerImpl::Abort( | 662 SpeechRecognizerImpl::FSMState SpeechRecognizerImpl::Abort( |
| 660 const SpeechRecognitionError& error) { | 663 const SpeechRecognitionError& error) { |
| 661 if (IsCapturingAudio()) | 664 if (IsCapturingAudio()) |
| 662 CloseAudioControllerAsynchronously(); | 665 CloseAudioControllerAsynchronously(); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 SpeechRecognizerImpl::FSMEventArgs::FSMEventArgs(FSMEvent event_value) | 812 SpeechRecognizerImpl::FSMEventArgs::FSMEventArgs(FSMEvent event_value) |
| 810 : event(event_value), | 813 : event(event_value), |
| 811 audio_data(NULL), | 814 audio_data(NULL), |
| 812 engine_error(SPEECH_RECOGNITION_ERROR_NONE) { | 815 engine_error(SPEECH_RECOGNITION_ERROR_NONE) { |
| 813 } | 816 } |
| 814 | 817 |
| 815 SpeechRecognizerImpl::FSMEventArgs::~FSMEventArgs() { | 818 SpeechRecognizerImpl::FSMEventArgs::~FSMEventArgs() { |
| 816 } | 819 } |
| 817 | 820 |
| 818 } // namespace content | 821 } // namespace content |
| OLD | NEW |