OLD | NEW |
---|---|
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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "content/browser/browser_main_loop.h" | |
9 #include "content/public/browser/speech_recognizer_delegate.h" | 10 #include "content/public/browser/speech_recognizer_delegate.h" |
10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/common/speech_input_result.h" | 12 #include "content/public/common/speech_input_result.h" |
12 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
13 | 14 |
15 using content::BrowserMainLoop; | |
14 using content::BrowserThread; | 16 using content::BrowserThread; |
15 using media::AudioInputController; | 17 using media::AudioInputController; |
16 using std::string; | 18 using std::string; |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 // The following constants are related to the volume level indicator shown in | 22 // The following constants are related to the volume level indicator shown in |
21 // the UI for recorded audio. | 23 // the UI for recorded audio. |
22 // Multiplier used when new volume is greater than previous level. | 24 // Multiplier used when new volume is greater than previous level. |
23 const float kUpSmoothingFactor = 1.0f; | 25 const float kUpSmoothingFactor = 1.0f; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 const ChannelLayout SpeechRecognizer::kChannelLayout = CHANNEL_LAYOUT_MONO; | 57 const ChannelLayout SpeechRecognizer::kChannelLayout = CHANNEL_LAYOUT_MONO; |
56 const int SpeechRecognizer::kNumBitsPerAudioSample = 16; | 58 const int SpeechRecognizer::kNumBitsPerAudioSample = 16; |
57 const int SpeechRecognizer::kNoSpeechTimeoutSec = 8; | 59 const int SpeechRecognizer::kNoSpeechTimeoutSec = 8; |
58 const int SpeechRecognizer::kEndpointerEstimationTimeMs = 300; | 60 const int SpeechRecognizer::kEndpointerEstimationTimeMs = 300; |
59 | 61 |
60 SpeechRecognizer::SpeechRecognizer(content::SpeechRecognizerDelegate* delegate, | 62 SpeechRecognizer::SpeechRecognizer(content::SpeechRecognizerDelegate* delegate, |
61 int caller_id, | 63 int caller_id, |
62 const std::string& language, | 64 const std::string& language, |
63 const std::string& grammar, | 65 const std::string& grammar, |
64 net::URLRequestContextGetter* context_getter, | 66 net::URLRequestContextGetter* context_getter, |
65 AudioManager* audio_manager, | |
66 bool filter_profanities, | 67 bool filter_profanities, |
67 const std::string& hardware_info, | 68 const std::string& hardware_info, |
68 const std::string& origin_url) | 69 const std::string& origin_url) |
69 : delegate_(delegate), | 70 : delegate_(delegate), |
70 caller_id_(caller_id), | 71 caller_id_(caller_id), |
71 language_(language), | 72 language_(language), |
72 grammar_(grammar), | 73 grammar_(grammar), |
73 filter_profanities_(filter_profanities), | 74 filter_profanities_(filter_profanities), |
74 hardware_info_(hardware_info), | 75 hardware_info_(hardware_info), |
75 origin_url_(origin_url), | 76 origin_url_(origin_url), |
76 context_getter_(context_getter), | 77 context_getter_(context_getter), |
77 audio_manager_(audio_manager), | |
78 codec_(AudioEncoder::CODEC_FLAC), | 78 codec_(AudioEncoder::CODEC_FLAC), |
79 encoder_(NULL), | 79 encoder_(NULL), |
80 endpointer_(kAudioSampleRate), | 80 endpointer_(kAudioSampleRate), |
81 num_samples_recorded_(0), | 81 num_samples_recorded_(0), |
82 audio_level_(0.0f) { | 82 audio_level_(0.0f), |
83 audio_manager_(NULL) { | |
83 endpointer_.set_speech_input_complete_silence_length( | 84 endpointer_.set_speech_input_complete_silence_length( |
84 base::Time::kMicrosecondsPerSecond / 2); | 85 base::Time::kMicrosecondsPerSecond / 2); |
85 endpointer_.set_long_speech_input_complete_silence_length( | 86 endpointer_.set_long_speech_input_complete_silence_length( |
86 base::Time::kMicrosecondsPerSecond); | 87 base::Time::kMicrosecondsPerSecond); |
87 endpointer_.set_long_speech_length(3 * base::Time::kMicrosecondsPerSecond); | 88 endpointer_.set_long_speech_length(3 * base::Time::kMicrosecondsPerSecond); |
88 endpointer_.StartSession(); | 89 endpointer_.StartSession(); |
89 } | 90 } |
90 | 91 |
91 SpeechRecognizer::~SpeechRecognizer() { | 92 SpeechRecognizer::~SpeechRecognizer() { |
92 // Recording should have stopped earlier due to the endpointer or | 93 // Recording should have stopped earlier due to the endpointer or |
(...skipping 14 matching lines...) Expand all Loading... | |
107 // starting to treat the audio as user input. In |HandleOnData| we wait until | 108 // starting to treat the audio as user input. In |HandleOnData| we wait until |
108 // such time has passed before switching to user input mode. | 109 // such time has passed before switching to user input mode. |
109 endpointer_.SetEnvironmentEstimationMode(); | 110 endpointer_.SetEnvironmentEstimationMode(); |
110 | 111 |
111 encoder_.reset(AudioEncoder::Create(codec_, kAudioSampleRate, | 112 encoder_.reset(AudioEncoder::Create(codec_, kAudioSampleRate, |
112 kNumBitsPerAudioSample)); | 113 kNumBitsPerAudioSample)); |
113 int samples_per_packet = (kAudioSampleRate * kAudioPacketIntervalMs) / 1000; | 114 int samples_per_packet = (kAudioSampleRate * kAudioPacketIntervalMs) / 1000; |
114 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, | 115 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, |
115 kAudioSampleRate, kNumBitsPerAudioSample, | 116 kAudioSampleRate, kNumBitsPerAudioSample, |
116 samples_per_packet); | 117 samples_per_packet); |
117 audio_controller_ = AudioInputController::Create(audio_manager_, this, | 118 audio_controller_ = AudioInputController::Create( |
118 params); | 119 audio_manager_ ? audio_manager_ : BrowserMainLoop::GetAudioManager(), |
tommi (sloooow) - chröme
2012/02/23 07:50:59
when will audio_manager_ not be the same as GetAud
| |
120 this, params); | |
119 DCHECK(audio_controller_.get()); | 121 DCHECK(audio_controller_.get()); |
120 VLOG(1) << "SpeechRecognizer starting record."; | 122 VLOG(1) << "SpeechRecognizer starting record."; |
121 num_samples_recorded_ = 0; | 123 num_samples_recorded_ = 0; |
122 audio_controller_->Record(); | 124 audio_controller_->Record(); |
123 | 125 |
124 return true; | 126 return true; |
125 } | 127 } |
126 | 128 |
127 void SpeechRecognizer::CancelRecognition() { | 129 void SpeechRecognizer::CancelRecognition() { |
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 // TODO(satish): investigate the possibility to utilize the closure | 314 // TODO(satish): investigate the possibility to utilize the closure |
313 // and switch to async. version of this method. Compare with how | 315 // and switch to async. version of this method. Compare with how |
314 // it's done in e.g. the AudioRendererHost. | 316 // it's done in e.g. the AudioRendererHost. |
315 base::WaitableEvent closed_event(true, false); | 317 base::WaitableEvent closed_event(true, false); |
316 audio_controller_->Close(base::Bind(&base::WaitableEvent::Signal, | 318 audio_controller_->Close(base::Bind(&base::WaitableEvent::Signal, |
317 base::Unretained(&closed_event))); | 319 base::Unretained(&closed_event))); |
318 closed_event.Wait(); | 320 closed_event.Wait(); |
319 audio_controller_ = NULL; // Releases the ref ptr. | 321 audio_controller_ = NULL; // Releases the ref ptr. |
320 } | 322 } |
321 | 323 |
324 void SpeechRecognizer::SetAudioManagerForTesting(AudioManager* audio_manager) { | |
325 audio_manager_ = audio_manager; | |
tommi (sloooow) - chröme
2012/02/23 07:50:59
ah, I see...
Instead of adding a 'ForTesting' meth
jam
2012/02/23 08:05:09
I tend to avoid inheritance unless necessary, whic
| |
326 } | |
327 | |
322 } // namespace speech_input | 328 } // namespace speech_input |
OLD | NEW |