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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_
6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "content/browser/speech/endpointer/endpointer.h"
11 #include "content/browser/speech/speech_recognition_engine.h"
12 #include "content/browser/speech/speech_recognizer.h"
13 #include "content/public/common/speech_recognition_error.h"
14 #include "content/public/common/speech_recognition_result.h"
15 #include "media/audio/audio_input_controller.h"
16 #include "net/url_request/url_request_context_getter.h"
24 class SpeechRecognitionEventListener
;
26 // Handles speech recognition for a session (identified by |session_id|), taking
27 // care of audio capture, silence detection/endpointer and interaction with the
28 // SpeechRecognitionEngine.
29 class CONTENT_EXPORT SpeechRecognizerImpl
30 : public SpeechRecognizer
,
31 public media::AudioInputController::EventHandler
,
32 public NON_EXPORTED_BASE(SpeechRecognitionEngineDelegate
) {
34 static const int kAudioSampleRate
;
35 static const media::ChannelLayout kChannelLayout
;
36 static const int kNumBitsPerAudioSample
;
37 static const int kNoSpeechTimeoutMs
;
38 static const int kEndpointerEstimationTimeMs
;
40 static void SetAudioManagerForTests(media::AudioManager
* audio_manager
);
42 SpeechRecognizerImpl(SpeechRecognitionEventListener
* listener
,
45 SpeechRecognitionEngine
* engine
);
47 virtual void StartRecognition(const std::string
& device_id
) OVERRIDE
;
48 virtual void AbortRecognition() OVERRIDE
;
49 virtual void StopAudioCapture() OVERRIDE
;
50 virtual bool IsActive() const OVERRIDE
;
51 virtual bool IsCapturingAudio() const OVERRIDE
;
52 const SpeechRecognitionEngine
& recognition_engine() const;
55 friend class SpeechRecognizerTest
;
60 STATE_ESTIMATING_ENVIRONMENT
,
61 STATE_WAITING_FOR_SPEECH
,
63 STATE_WAITING_FINAL_RESULT
,
65 STATE_MAX_VALUE
= STATE_ENDED
76 EVENT_MAX_VALUE
= EVENT_AUDIO_ERROR
80 explicit FSMEventArgs(FSMEvent event_value
);
84 scoped_refptr
<AudioChunk
> audio_data
;
85 SpeechRecognitionResults engine_results
;
86 SpeechRecognitionError engine_error
;
89 virtual ~SpeechRecognizerImpl();
91 // Entry point for pushing any new external event into the recognizer FSM.
92 void DispatchEvent(const FSMEventArgs
& event_args
);
94 // Defines the behavior of the recognizer FSM, selecting the appropriate
95 // transition according to the current state and event.
96 FSMState
ExecuteTransitionAndGetNextState(const FSMEventArgs
& args
);
98 // Process a new audio chunk in the audio pipeline (endpointer, vumeter, etc).
99 void ProcessAudioPipeline(const AudioChunk
& raw_audio
);
101 // The methods below handle transitions of the recognizer FSM.
102 FSMState
StartRecording(const FSMEventArgs
& event_args
);
103 FSMState
StartRecognitionEngine(const FSMEventArgs
& event_args
);
104 FSMState
WaitEnvironmentEstimationCompletion(const FSMEventArgs
& event_args
);
105 FSMState
DetectUserSpeechOrTimeout(const FSMEventArgs
& event_args
);
106 FSMState
StopCaptureAndWaitForResult(const FSMEventArgs
& event_args
);
107 FSMState
ProcessIntermediateResult(const FSMEventArgs
& event_args
);
108 FSMState
ProcessFinalResult(const FSMEventArgs
& event_args
);
109 FSMState
AbortSilently(const FSMEventArgs
& event_args
);
110 FSMState
AbortWithError(const FSMEventArgs
& event_args
);
111 FSMState
Abort(const SpeechRecognitionError
& error
);
112 FSMState
DetectEndOfSpeech(const FSMEventArgs
& event_args
);
113 FSMState
DoNothing(const FSMEventArgs
& event_args
) const;
114 FSMState
NotFeasible(const FSMEventArgs
& event_args
);
116 // Returns the time span of captured audio samples since the start of capture.
117 int GetElapsedTimeMs() const;
119 // Calculates the input volume to be displayed in the UI, triggering the
120 // OnAudioLevelsChange event accordingly.
121 void UpdateSignalAndNoiseLevels(const float& rms
, bool clip_detected
);
123 void CloseAudioControllerAsynchronously();
125 // Callback called on IO thread by audio_controller->Close().
126 void OnAudioClosed(media::AudioInputController
*);
128 // AudioInputController::EventHandler methods.
129 virtual void OnCreated(media::AudioInputController
* controller
) OVERRIDE
{}
130 virtual void OnRecording(media::AudioInputController
* controller
) OVERRIDE
{}
131 virtual void OnError(media::AudioInputController
* controller
) OVERRIDE
;
132 virtual void OnData(media::AudioInputController
* controller
,
133 const uint8
* data
, uint32 size
) OVERRIDE
;
135 // SpeechRecognitionEngineDelegate methods.
136 virtual void OnSpeechRecognitionEngineResults(
137 const SpeechRecognitionResults
& results
) OVERRIDE
;
138 virtual void OnSpeechRecognitionEngineError(
139 const SpeechRecognitionError
& error
) OVERRIDE
;
141 static media::AudioManager
* audio_manager_for_tests_
;
143 scoped_ptr
<SpeechRecognitionEngine
> recognition_engine_
;
144 Endpointer endpointer_
;
145 scoped_refptr
<media::AudioInputController
> audio_controller_
;
146 int num_samples_recorded_
;
148 bool is_dispatching_event_
;
149 bool is_single_shot_
;
151 std::string device_id_
;
153 class OnDataConverter
;
155 // Converts data between native input format and a WebSpeech specific
157 scoped_ptr
<SpeechRecognizerImpl::OnDataConverter
> audio_converter_
;
159 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizerImpl
);
162 } // namespace content
164 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_