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 19 matching lines...) Expand all Loading... |
30 // Private class which encapsulates the audio converter and the | 30 // Private class which encapsulates the audio converter and the |
31 // AudioConverter::InputCallback. It handles resampling, buffering and | 31 // AudioConverter::InputCallback. It handles resampling, buffering and |
32 // channel mixing between input and output parameters. | 32 // channel mixing between input and output parameters. |
33 class SpeechRecognizerImpl::OnDataConverter | 33 class SpeechRecognizerImpl::OnDataConverter |
34 : public media::AudioConverter::InputCallback { | 34 : public media::AudioConverter::InputCallback { |
35 public: | 35 public: |
36 OnDataConverter(const AudioParameters& input_params, | 36 OnDataConverter(const AudioParameters& input_params, |
37 const AudioParameters& output_params); | 37 const AudioParameters& output_params); |
38 virtual ~OnDataConverter(); | 38 virtual ~OnDataConverter(); |
39 | 39 |
40 // Converts input |data| buffer into an AudioChunk where the input format | 40 // Converts input audio |data| bus into an AudioChunk where the input format |
41 // is given by |input_parameters_| and the output format by | 41 // is given by |input_parameters_| and the output format by |
42 // |output_parameters_|. | 42 // |output_parameters_|. |
43 scoped_refptr<AudioChunk> Convert(const uint8* data, size_t size); | 43 scoped_refptr<AudioChunk> Convert(const AudioBus* data); |
44 | 44 |
45 private: | 45 private: |
46 // media::AudioConverter::InputCallback implementation. | 46 // media::AudioConverter::InputCallback implementation. |
47 virtual double ProvideInput(AudioBus* dest, | 47 virtual double ProvideInput(AudioBus* dest, |
48 base::TimeDelta buffer_delay) OVERRIDE; | 48 base::TimeDelta buffer_delay) OVERRIDE; |
49 | 49 |
50 // Handles resampling, buffering, and channel mixing between input and output | 50 // Handles resampling, buffering, and channel mixing between input and output |
51 // parameters. | 51 // parameters. |
52 AudioConverter audio_converter_; | 52 AudioConverter audio_converter_; |
53 | 53 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 audio_converter_.AddInput(this); | 125 audio_converter_.AddInput(this); |
126 } | 126 } |
127 | 127 |
128 SpeechRecognizerImpl::OnDataConverter::~OnDataConverter() { | 128 SpeechRecognizerImpl::OnDataConverter::~OnDataConverter() { |
129 // It should now be safe to unregister the converter since no more OnData() | 129 // It should now be safe to unregister the converter since no more OnData() |
130 // callbacks are outstanding at this point. | 130 // callbacks are outstanding at this point. |
131 audio_converter_.RemoveInput(this); | 131 audio_converter_.RemoveInput(this); |
132 } | 132 } |
133 | 133 |
134 scoped_refptr<AudioChunk> SpeechRecognizerImpl::OnDataConverter::Convert( | 134 scoped_refptr<AudioChunk> SpeechRecognizerImpl::OnDataConverter::Convert( |
135 const uint8* data, size_t size) { | 135 const AudioBus* data) { |
136 CHECK_EQ(size, static_cast<size_t>(input_parameters_.GetBytesPerBuffer())); | 136 CHECK_EQ(data->frames(), input_parameters_.frames_per_buffer()); |
137 | 137 |
138 input_bus_->FromInterleaved( | 138 data->CopyTo(input_bus_.get()); |
139 data, input_bus_->frames(), input_parameters_.bits_per_sample() / 8); | |
140 | 139 |
141 waiting_for_input_ = true; | 140 waiting_for_input_ = true; |
142 audio_converter_.Convert(output_bus_.get()); | 141 audio_converter_.Convert(output_bus_.get()); |
143 | 142 |
144 output_bus_->ToInterleaved( | 143 output_bus_->ToInterleaved( |
145 output_bus_->frames(), output_parameters_.bits_per_sample() / 8, | 144 output_bus_->frames(), output_parameters_.bits_per_sample() / 8, |
146 converted_data_.get()); | 145 converted_data_.get()); |
147 | 146 |
148 // TODO(primiano): Refactor AudioChunk to avoid the extra-copy here | 147 // TODO(primiano): Refactor AudioChunk to avoid the extra-copy here |
149 // (see http://crbug.com/249316 for details). | 148 // (see http://crbug.com/249316 for details). |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 // Invoked in the audio thread. | 264 // Invoked in the audio thread. |
266 void SpeechRecognizerImpl::OnError(AudioInputController* controller, | 265 void SpeechRecognizerImpl::OnError(AudioInputController* controller, |
267 media::AudioInputController::ErrorCode error_code) { | 266 media::AudioInputController::ErrorCode error_code) { |
268 FSMEventArgs event_args(EVENT_AUDIO_ERROR); | 267 FSMEventArgs event_args(EVENT_AUDIO_ERROR); |
269 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 268 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
270 base::Bind(&SpeechRecognizerImpl::DispatchEvent, | 269 base::Bind(&SpeechRecognizerImpl::DispatchEvent, |
271 this, event_args)); | 270 this, event_args)); |
272 } | 271 } |
273 | 272 |
274 void SpeechRecognizerImpl::OnData(AudioInputController* controller, | 273 void SpeechRecognizerImpl::OnData(AudioInputController* controller, |
275 const uint8* data, uint32 size) { | 274 const AudioBus* data) { |
276 if (size == 0) // This could happen when audio capture stops and is normal. | |
277 return; | |
278 | |
279 // Convert audio from native format to fixed format used by WebSpeech. | 275 // Convert audio from native format to fixed format used by WebSpeech. |
280 FSMEventArgs event_args(EVENT_AUDIO_DATA); | 276 FSMEventArgs event_args(EVENT_AUDIO_DATA); |
281 event_args.audio_data = audio_converter_->Convert(data, size); | 277 event_args.audio_data = audio_converter_->Convert(data); |
282 | 278 |
283 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 279 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
284 base::Bind(&SpeechRecognizerImpl::DispatchEvent, | 280 base::Bind(&SpeechRecognizerImpl::DispatchEvent, |
285 this, event_args)); | 281 this, event_args)); |
286 } | 282 } |
287 | 283 |
288 void SpeechRecognizerImpl::OnAudioClosed(AudioInputController*) {} | 284 void SpeechRecognizerImpl::OnAudioClosed(AudioInputController*) {} |
289 | 285 |
290 void SpeechRecognizerImpl::OnSpeechRecognitionEngineResults( | 286 void SpeechRecognizerImpl::OnSpeechRecognitionEngineResults( |
291 const SpeechRecognitionResults& results) { | 287 const SpeechRecognitionResults& results) { |
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 SpeechRecognizerImpl::FSMEventArgs::FSMEventArgs(FSMEvent event_value) | 808 SpeechRecognizerImpl::FSMEventArgs::FSMEventArgs(FSMEvent event_value) |
813 : event(event_value), | 809 : event(event_value), |
814 audio_data(NULL), | 810 audio_data(NULL), |
815 engine_error(SPEECH_RECOGNITION_ERROR_NONE) { | 811 engine_error(SPEECH_RECOGNITION_ERROR_NONE) { |
816 } | 812 } |
817 | 813 |
818 SpeechRecognizerImpl::FSMEventArgs::~FSMEventArgs() { | 814 SpeechRecognizerImpl::FSMEventArgs::~FSMEventArgs() { |
819 } | 815 } |
820 | 816 |
821 } // namespace content | 817 } // namespace content |
OLD | NEW |