OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "chrome/common/net/test_url_fetcher_factory.h" | 7 #include "chrome/common/net/test_url_fetcher_factory.h" |
8 #include "content/browser/browser_thread.h" | 8 #include "content/browser/browser_thread.h" |
9 #include "content/browser/speech/speech_recognizer.h" | 9 #include "content/browser/speech/speech_recognizer.h" |
10 #include "media/audio/test_audio_input_controller_factory.h" | 10 #include "media/audio/test_audio_input_controller_factory.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 } | 55 } |
56 | 56 |
57 virtual void DidCompleteEnvironmentEstimation(int caller_id) { | 57 virtual void DidCompleteEnvironmentEstimation(int caller_id) { |
58 } | 58 } |
59 | 59 |
60 virtual void OnRecognizerError(int caller_id, | 60 virtual void OnRecognizerError(int caller_id, |
61 SpeechRecognizer::ErrorCode error) { | 61 SpeechRecognizer::ErrorCode error) { |
62 error_ = error; | 62 error_ = error; |
63 } | 63 } |
64 | 64 |
65 virtual void SetInputVolume(int caller_id, float volume) { | 65 virtual void SetInputVolume(int caller_id, float volume, float noise_volume) { |
66 volume_ = volume; | 66 volume_ = volume; |
| 67 noise_volume_ = noise_volume; |
67 } | 68 } |
68 | 69 |
69 // testing::Test methods. | 70 // testing::Test methods. |
70 virtual void SetUp() { | 71 virtual void SetUp() { |
71 URLFetcher::set_factory(&url_fetcher_factory_); | 72 URLFetcher::set_factory(&url_fetcher_factory_); |
72 AudioInputController::set_factory(&audio_input_controller_factory_); | 73 AudioInputController::set_factory(&audio_input_controller_factory_); |
73 } | 74 } |
74 | 75 |
75 virtual void TearDown() { | 76 virtual void TearDown() { |
76 URLFetcher::set_factory(NULL); | 77 URLFetcher::set_factory(NULL); |
77 AudioInputController::set_factory(NULL); | 78 AudioInputController::set_factory(NULL); |
78 } | 79 } |
79 | 80 |
80 void FillPacketWithTestWaveform() { | 81 void FillPacketWithTestWaveform() { |
81 // Fill the input with a simple pattern, a 125Hz sawtooth waveform. | 82 // Fill the input with a simple pattern, a 125Hz sawtooth waveform. |
82 for (size_t i = 0; i < audio_packet_.size(); ++i) | 83 for (size_t i = 0; i < audio_packet_.size(); ++i) |
83 audio_packet_[i] = static_cast<uint8>(i); | 84 audio_packet_[i] = static_cast<uint8>(i); |
84 } | 85 } |
85 | 86 |
| 87 void FillPacketWithNoise() { |
| 88 int value = 0; |
| 89 int factor = 175; |
| 90 for (size_t i = 0; i < audio_packet_.size(); ++i) { |
| 91 value += factor; |
| 92 audio_packet_[i] = value % 100; |
| 93 } |
| 94 } |
| 95 |
86 protected: | 96 protected: |
87 MessageLoopForIO message_loop_; | 97 MessageLoopForIO message_loop_; |
88 BrowserThread io_thread_; | 98 BrowserThread io_thread_; |
89 scoped_refptr<SpeechRecognizer> recognizer_; | 99 scoped_refptr<SpeechRecognizer> recognizer_; |
90 bool recording_complete_; | 100 bool recording_complete_; |
91 bool recognition_complete_; | 101 bool recognition_complete_; |
92 bool result_received_; | 102 bool result_received_; |
93 SpeechRecognizer::ErrorCode error_; | 103 SpeechRecognizer::ErrorCode error_; |
94 TestURLFetcherFactory url_fetcher_factory_; | 104 TestURLFetcherFactory url_fetcher_factory_; |
95 TestAudioInputControllerFactory audio_input_controller_factory_; | 105 TestAudioInputControllerFactory audio_input_controller_factory_; |
96 std::vector<uint8> audio_packet_; | 106 std::vector<uint8> audio_packet_; |
97 float volume_; | 107 float volume_; |
| 108 float noise_volume_; |
98 }; | 109 }; |
99 | 110 |
100 TEST_F(SpeechRecognizerTest, StopNoData) { | 111 TEST_F(SpeechRecognizerTest, StopNoData) { |
101 // Check for callbacks when stopping record before any audio gets recorded. | 112 // Check for callbacks when stopping record before any audio gets recorded. |
102 EXPECT_TRUE(recognizer_->StartRecording()); | 113 EXPECT_TRUE(recognizer_->StartRecording()); |
103 recognizer_->CancelRecognition(); | 114 recognizer_->CancelRecognition(); |
104 EXPECT_FALSE(recording_complete_); | 115 EXPECT_FALSE(recording_complete_); |
105 EXPECT_FALSE(recognition_complete_); | 116 EXPECT_FALSE(recognition_complete_); |
106 EXPECT_FALSE(result_received_); | 117 EXPECT_FALSE(result_received_); |
107 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 118 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 EXPECT_TRUE(recognizer_->StartRecording()); | 276 EXPECT_TRUE(recognizer_->StartRecording()); |
266 TestAudioInputController* controller = | 277 TestAudioInputController* controller = |
267 audio_input_controller_factory_.controller(); | 278 audio_input_controller_factory_.controller(); |
268 ASSERT_TRUE(controller); | 279 ASSERT_TRUE(controller); |
269 controller = audio_input_controller_factory_.controller(); | 280 controller = audio_input_controller_factory_.controller(); |
270 ASSERT_TRUE(controller); | 281 ASSERT_TRUE(controller); |
271 | 282 |
272 // Feed some samples to begin with for the endpointer to do noise estimation. | 283 // Feed some samples to begin with for the endpointer to do noise estimation. |
273 int num_packets = SpeechRecognizer::kEndpointerEstimationTimeMs / | 284 int num_packets = SpeechRecognizer::kEndpointerEstimationTimeMs / |
274 SpeechRecognizer::kAudioPacketIntervalMs; | 285 SpeechRecognizer::kAudioPacketIntervalMs; |
| 286 FillPacketWithNoise(); |
275 for (int i = 0; i < num_packets; ++i) { | 287 for (int i = 0; i < num_packets; ++i) { |
276 controller->event_handler()->OnData(controller, &audio_packet_[0], | 288 controller->event_handler()->OnData(controller, &audio_packet_[0], |
277 audio_packet_.size()); | 289 audio_packet_.size()); |
278 } | 290 } |
279 MessageLoop::current()->RunAllPending(); | 291 MessageLoop::current()->RunAllPending(); |
280 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. | 292 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. |
281 | 293 |
282 // The vector is already filled with zero value samples on create. | 294 // The vector is already filled with zero value samples on create. |
283 controller->event_handler()->OnData(controller, &audio_packet_[0], | 295 controller->event_handler()->OnData(controller, &audio_packet_[0], |
284 audio_packet_.size()); | 296 audio_packet_.size()); |
285 MessageLoop::current()->RunAllPending(); | 297 MessageLoop::current()->RunAllPending(); |
286 EXPECT_EQ(0, volume_); | 298 EXPECT_FLOAT_EQ(0.51877826f, volume_); |
287 | 299 |
288 FillPacketWithTestWaveform(); | 300 FillPacketWithTestWaveform(); |
289 controller->event_handler()->OnData(controller, &audio_packet_[0], | 301 controller->event_handler()->OnData(controller, &audio_packet_[0], |
290 audio_packet_.size()); | 302 audio_packet_.size()); |
291 MessageLoop::current()->RunAllPending(); | 303 MessageLoop::current()->RunAllPending(); |
292 EXPECT_FLOAT_EQ(0.9f, volume_); | 304 EXPECT_FLOAT_EQ(0.81907868f, volume_); |
| 305 EXPECT_FLOAT_EQ(0.52143687f, noise_volume_); |
293 | 306 |
294 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 307 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
295 EXPECT_FALSE(recording_complete_); | 308 EXPECT_FALSE(recording_complete_); |
296 EXPECT_FALSE(recognition_complete_); | 309 EXPECT_FALSE(recognition_complete_); |
297 recognizer_->CancelRecognition(); | 310 recognizer_->CancelRecognition(); |
298 } | 311 } |
299 | 312 |
300 } // namespace speech_input | 313 } // namespace speech_input |
OLD | NEW |