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 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ | 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ | 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 namespace speech_input { | 24 namespace speech_input { |
25 | 25 |
26 // Records audio, sends recorded audio to server and translates server response | 26 // Records audio, sends recorded audio to server and translates server response |
27 // to recognition result. | 27 // to recognition result. |
28 class CONTENT_EXPORT SpeechRecognizer | 28 class CONTENT_EXPORT SpeechRecognizer |
29 : public base::RefCountedThreadSafe<SpeechRecognizer>, | 29 : public base::RefCountedThreadSafe<SpeechRecognizer>, |
30 public media::AudioInputController::EventHandler, | 30 public media::AudioInputController::EventHandler, |
31 public SpeechRecognitionRequestDelegate { | 31 public SpeechRecognitionRequestDelegate { |
32 public: | 32 public: |
33 enum ErrorCode { | |
34 RECOGNIZER_NO_ERROR, | |
35 RECOGNIZER_ERROR_CAPTURE, | |
36 RECOGNIZER_ERROR_NO_SPEECH, | |
37 RECOGNIZER_ERROR_NO_RESULTS, | |
38 RECOGNIZER_ERROR_NETWORK, | |
39 }; | |
40 | |
41 // Implemented by the caller to receive recognition events. | 33 // Implemented by the caller to receive recognition events. |
42 class CONTENT_EXPORT Delegate { | 34 class CONTENT_EXPORT Delegate { |
43 public: | 35 public: |
44 virtual void SetRecognitionResult( | 36 virtual void SetRecognitionResult( |
45 int caller_id, | 37 int caller_id, |
46 bool error, | 38 const SpeechInputResult& result) = 0; |
47 const SpeechInputResultArray& result) = 0; | |
48 | 39 |
49 // Invoked when the first audio packet was received from the audio capture | 40 // Invoked when the first audio packet was received from the audio capture |
50 // device. | 41 // device. |
51 virtual void DidStartReceivingAudio(int caller_id) = 0; | 42 virtual void DidStartReceivingAudio(int caller_id) = 0; |
52 | 43 |
53 // Invoked when audio recording stops, either due to the end pointer | 44 // Invoked when audio recording stops, either due to the end pointer |
54 // detecting silence in user input or if |StopRecording| was called. The | 45 // detecting silence in user input or if |StopRecording| was called. The |
55 // delegate has to wait until |DidCompleteRecognition| is invoked before | 46 // delegate has to wait until |DidCompleteRecognition| is invoked before |
56 // destroying the |SpeechRecognizer| object. | 47 // destroying the |SpeechRecognizer| object. |
57 virtual void DidCompleteRecording(int caller_id) = 0; | 48 virtual void DidCompleteRecording(int caller_id) = 0; |
58 | 49 |
59 // This is guaranteed to be the last method invoked in the recognition | 50 // This is guaranteed to be the last method invoked in the recognition |
60 // sequence and the |SpeechRecognizer| object can be freed up if necessary. | 51 // sequence and the |SpeechRecognizer| object can be freed up if necessary. |
61 virtual void DidCompleteRecognition(int caller_id) = 0; | 52 virtual void DidCompleteRecognition(int caller_id) = 0; |
62 | 53 |
| 54 // Informs that the end pointer has started detecting speech. |
| 55 virtual void DidStartReceivingSpeech(int caller_id) = 0; |
| 56 |
| 57 // Informs that the end pointer has stopped detecting speech. |
| 58 virtual void DidStopReceivingSpeech(int caller_id) = 0; |
| 59 |
63 // Invoked if there was an error while recording or recognizing audio. The | 60 // Invoked if there was an error while recording or recognizing audio. The |
64 // session has already been cancelled when this call is made and the DidXxxx | 61 // session has already been cancelled when this call is made and the DidXxxx |
65 // callbacks will not be issued. It is safe to destroy/release the | 62 // callbacks will not be issued. It is safe to destroy/release the |
66 // |SpeechRecognizer| object while processing this call. | 63 // |SpeechRecognizer| object while processing this call. |
67 virtual void OnRecognizerError(int caller_id, | 64 virtual void OnRecognizerError(int caller_id, |
68 SpeechRecognizer::ErrorCode error) = 0; | 65 SpeechInputError error) = 0; |
69 | 66 |
70 // At the start of recognition, a short amount of audio is recorded to | 67 // At the start of recognition, a short amount of audio is recorded to |
71 // estimate the environment/background noise and this callback is issued | 68 // estimate the environment/background noise and this callback is issued |
72 // after that is complete. Typically the delegate brings up any speech | 69 // after that is complete. Typically the delegate brings up any speech |
73 // recognition UI once this callback is received. | 70 // recognition UI once this callback is received. |
74 virtual void DidCompleteEnvironmentEstimation(int caller_id) = 0; | 71 virtual void DidCompleteEnvironmentEstimation(int caller_id) = 0; |
75 | 72 |
76 // Informs of a change in the captured audio level, useful if displaying | 73 // Informs of a change in the captured audio level, useful if displaying |
77 // a microphone volume indicator while recording. | 74 // a microphone volume indicator while recording. |
78 // The value of |volume| and |noise_volume| is in the [0.0, 1.0] range. | 75 // The value of |volume| and |noise_volume| is in the [0.0, 1.0] range. |
79 virtual void SetInputVolume(int caller_id, float volume, | 76 virtual void SetInputVolume(int caller_id, float volume, |
80 float noise_volume) = 0; | 77 float noise_volume) = 0; |
81 | 78 |
82 protected: | 79 protected: |
83 virtual ~Delegate() {} | 80 virtual ~Delegate() {} |
84 }; | 81 }; |
85 | 82 |
86 SpeechRecognizer(Delegate* delegate, | 83 SpeechRecognizer(Delegate* delegate, |
87 int caller_id, | 84 int caller_id, |
88 const std::string& language, | 85 const std::string& language, |
89 const std::string& grammar, | 86 const std::string& grammar, |
90 net::URLRequestContextGetter* context_getter, | 87 net::URLRequestContextGetter* context_getter, |
91 bool censor_results, | 88 bool censor_results, |
92 const std::string& hardware_info, | 89 const std::string& hardware_info, |
93 const std::string& origin_url); | 90 const std::string& origin_url); |
| 91 |
94 virtual ~SpeechRecognizer(); | 92 virtual ~SpeechRecognizer(); |
95 | 93 |
96 // Starts audio recording and does recognition after recording ends. The same | 94 // Starts audio recording and does recognition after recording ends. The same |
97 // SpeechRecognizer instance can be used multiple times for speech recognition | 95 // SpeechRecognizer instance can be used multiple times for speech recognition |
98 // though each recognition request can be made only after the previous one | 96 // though each recognition request can be made only after the previous one |
99 // completes (i.e. after receiving Delegate::DidCompleteRecognition). | 97 // completes (i.e. after receiving Delegate::DidCompleteRecognition). |
100 bool StartRecording(); | 98 bool StartRecording(); |
101 | 99 |
102 // Stops recording audio and starts recognition. | 100 // Stops recording audio and starts recognition. |
103 void StopRecording(); | 101 void StopRecording(); |
104 | 102 |
105 // Stops recording audio and cancels recognition. Any audio recorded so far | 103 // Stops recording audio and cancels recognition. Any audio recorded so far |
106 // gets discarded. | 104 // gets discarded. |
107 void CancelRecognition(); | 105 void CancelRecognition(); |
108 | 106 |
109 // AudioInputController::EventHandler methods. | 107 // AudioInputController::EventHandler methods. |
110 virtual void OnCreated(media::AudioInputController* controller) { } | 108 virtual void OnCreated(media::AudioInputController* controller) { } |
111 virtual void OnRecording(media::AudioInputController* controller) { } | 109 virtual void OnRecording(media::AudioInputController* controller) { } |
112 virtual void OnError(media::AudioInputController* controller, int error_code); | 110 virtual void OnError(media::AudioInputController* controller, int error_code); |
113 virtual void OnData(media::AudioInputController* controller, | 111 virtual void OnData(media::AudioInputController* controller, |
114 const uint8* data, | 112 const uint8* data, |
115 uint32 size); | 113 uint32 size); |
116 | 114 |
117 // SpeechRecognitionRequest::Delegate methods. | 115 // SpeechRecognitionRequest::Delegate methods. |
118 virtual void SetRecognitionResult(bool error, | 116 virtual void SetRecognitionResult(const SpeechInputResult& result); |
119 const SpeechInputResultArray& result); | |
120 | 117 |
121 static const int kAudioSampleRate; | 118 static const int kAudioSampleRate; |
122 static const int kAudioPacketIntervalMs; // Duration of each audio packet. | 119 static const int kAudioPacketIntervalMs; // Duration of each audio packet. |
123 static const ChannelLayout kChannelLayout; | 120 static const ChannelLayout kChannelLayout; |
124 static const int kNumBitsPerAudioSample; | 121 static const int kNumBitsPerAudioSample; |
125 static const int kNoSpeechTimeoutSec; | 122 static const int kNoSpeechTimeoutSec; |
126 static const int kEndpointerEstimationTimeMs; | 123 static const int kEndpointerEstimationTimeMs; |
127 | 124 |
128 private: | 125 private: |
129 void InformErrorAndCancelRecognition(ErrorCode error); | 126 void InformErrorAndCancelRecognition(SpeechInputError error); |
130 void SendRecordedAudioToServer(); | 127 void SendRecordedAudioToServer(); |
131 | 128 |
132 void HandleOnError(int error_code); // Handles OnError in the IO thread. | 129 void HandleOnError(int error_code); // Handles OnError in the IO thread. |
133 | 130 |
134 // Handles OnData in the IO thread. Takes ownership of |data|. | 131 // Handles OnData in the IO thread. Takes ownership of |data|. |
135 void HandleOnData(std::string* data); | 132 void HandleOnData(std::string* data); |
136 | 133 |
137 Delegate* delegate_; | 134 Delegate* delegate_; |
138 int caller_id_; | 135 int caller_id_; |
139 std::string language_; | 136 std::string language_; |
(...skipping 16 matching lines...) Expand all Loading... |
156 | 153 |
157 // This typedef is to workaround the issue with certain versions of | 154 // This typedef is to workaround the issue with certain versions of |
158 // Visual Studio where it gets confused between multiple Delegate | 155 // Visual Studio where it gets confused between multiple Delegate |
159 // classes and gives a C2500 error. (I saw this error on the try bots - | 156 // classes and gives a C2500 error. (I saw this error on the try bots - |
160 // the workaround was not needed for my machine). | 157 // the workaround was not needed for my machine). |
161 typedef SpeechRecognizer::Delegate SpeechRecognizerDelegate; | 158 typedef SpeechRecognizer::Delegate SpeechRecognizerDelegate; |
162 | 159 |
163 } // namespace speech_input | 160 } // namespace speech_input |
164 | 161 |
165 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ | 162 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
OLD | NEW |