OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_ENGINE_H_ |
| 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_ENGINE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 |
| 14 namespace content { |
| 15 struct SpeechRecognitionResult; |
| 16 struct SpeechRecognitionError; |
| 17 } |
| 18 |
| 19 namespace speech { |
| 20 |
| 21 class AudioChunk; |
| 22 |
| 23 // This interface models the basic contract that a speech recognition engine, |
| 24 // either working locally or relying on a remote web-service, must obey. |
| 25 // The expected call sequence for exported methods is: |
| 26 // StartRecognition Mandatory at beginning of SR. |
| 27 // TakeAudioChunk For every audio chunk pushed. |
| 28 // AudioChunksEnded Finalize the audio stream (omitted in case of errors). |
| 29 // EndRecognition Mandatory at end of SR (even on errors). |
| 30 // No delegate callback is allowed before Initialize() or after Cleanup(). |
| 31 class SpeechRecognitionEngine { |
| 32 public: |
| 33 // Interface for receiving callbacks from this object. |
| 34 class Delegate { |
| 35 public: |
| 36 // Called whenever a result is retrieved. It might be issued several times, |
| 37 // (e.g., in the case of continuous speech recognition engine |
| 38 // implementations). |
| 39 virtual void OnSpeechRecognitionEngineResult( |
| 40 const content::SpeechRecognitionResult& result) = 0; |
| 41 virtual void OnSpeechRecognitionEngineError( |
| 42 const content::SpeechRecognitionError& error) = 0; |
| 43 |
| 44 protected: |
| 45 virtual ~Delegate() {} |
| 46 }; |
| 47 |
| 48 virtual ~SpeechRecognitionEngine() {} |
| 49 |
| 50 // Called when the speech recognition begins, before any TakeAudioChunk call. |
| 51 virtual void StartRecognition() = 0; |
| 52 |
| 53 // End any recognition activity and don't make any further callback. |
| 54 // Must be always called to close the corresponding StartRecognition call, |
| 55 // even in case of errors. |
| 56 // No further TakeAudioChunk/AudioChunksEnded calls are allowed after this. |
| 57 virtual void EndRecognition() = 0; |
| 58 |
| 59 // Push a chunk of uncompressed audio data, where the chunk length agrees with |
| 60 // GetDesiredAudioChunkDurationMs(). |
| 61 virtual void TakeAudioChunk(const AudioChunk& data) = 0; |
| 62 |
| 63 // Notifies the engine that audio capture has completed and no more chunks |
| 64 // will be pushed. The engine, however, can still provide further results |
| 65 // using the audio chunks collected so far. |
| 66 virtual void AudioChunksEnded() = 0; |
| 67 |
| 68 // Checks wheter recognition of pushed audio data is pending. |
| 69 virtual bool IsRecognitionPending() const = 0; |
| 70 |
| 71 // Retrieves the desired duration, in milliseconds, of pushed AudioChunk(s). |
| 72 virtual int GetDesiredAudioChunkDurationMs() const = 0; |
| 73 |
| 74 // set_delegate detached from constructor for lazy dependency injection. |
| 75 void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
| 76 |
| 77 protected: |
| 78 Delegate* delegate() const { return delegate_; } |
| 79 |
| 80 private: |
| 81 Delegate* delegate_; |
| 82 }; |
| 83 |
| 84 // This typedef is to workaround the issue with certain versions of |
| 85 // Visual Studio where it gets confused between multiple Delegate |
| 86 // classes and gives a C2500 error. (I saw this error on the try bots - |
| 87 // the workaround was not needed for my machine). |
| 88 typedef SpeechRecognitionEngine::Delegate SpeechRecognitionEngineDelegate; |
| 89 |
| 90 } // namespace speech |
| 91 |
| 92 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_ENGINE_H_ |
OLD | NEW |