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