Overte C++ Documentation
SpeechRecognizer.h
1 //
2 // SpeechRecognizer.h
3 // interface/src
4 //
5 // Created by Ryan Huffman on 07/31/14.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_SpeechRecognizer_h
13 #define hifi_SpeechRecognizer_h
14 
15 #include <QObject>
16 #include <QSet>
17 #include <QString>
18 
19 #if defined(Q_OS_WIN)
20 #include <QWinEventNotifier>
21 #endif
22 
23 #include <DependencyManager.h>
24 
25 /*@jsdoc
26  * The <code>SpeechRecognizer</code> API provides facilities to recognize voice commands.
27  * <p>Speech recognition is enabled or disabled via the Developer &gt; Scripting &gt; Enable Speech Control API menu item or
28  * the {@link SpeechRecognizer.setEnabled} method.</p>
29  *
30  * @namespace SpeechRecognizer
31  *
32  * @hifi-interface
33  * @hifi-client-entity
34  * @hifi-avatar
35  */
36 class SpeechRecognizer : public QObject, public Dependency {
37  Q_OBJECT
38  SINGLETON_DEPENDENCY
39 
40 public:
41  void handleCommandRecognized(const char* command);
42  bool getEnabled() const { return _enabled; }
43 
44 public slots:
45 
46  /*@jsdoc
47  * Enables or disables speech recognition.
48  * @function SpeechRecognizer.setEnabled
49  * @param {boolean} enabled - <code>true</code> to enable speech recognition, <code>false</code> to disable.
50  */
51  void setEnabled(bool enabled);
52 
53  /*@jsdoc
54  * Adds a voice command to the speech recognizer.
55  * @function SpeechRecognizer.addCommand
56  * @param {string} command - The voice command to recognize.
57  */
58  void addCommand(const QString& command);
59 
60  /*@jsdoc
61  * Removes a voice command from the speech recognizer.
62  * @function SpeechRecognizer.removeCommand
63  * @param {string} command - The voice command to stop recognizing.
64  */
65  void removeCommand(const QString& command);
66 
67 signals:
68 
69  /*@jsdoc
70  * Triggered when a voice command has been recognized.
71  * @function SpeechRecognizer.commandRecognized
72  * @param {string} command - The voice command recognized.
73  * @returns {Signal}
74  * @example <caption>Turn your avatar upon voice command.</caption>
75  * var TURN_LEFT = "turn left";
76  * var TURN_RIGHT = "turn right";
77  * var TURN_RATE = 0.5;
78  * var TURN_DURATION = 1000; // ms
79  * var turnRate = 0;
80  *
81  * function getTurnRate() {
82  * return turnRate;
83  * }
84  *
85  * var MAPPING_NAME = "org.overte.controllers.example.speechRecognizer";
86  * var mapping = Controller.newMapping(MAPPING_NAME);
87  *
88  * mapping.from(getTurnRate).to(Controller.Actions.Yaw);
89  * Controller.enableMapping(MAPPING_NAME);
90  *
91  * function onCommandRecognized(command) {
92  * print("Speech command: " + command);
93  * switch (command) {
94  * case TURN_LEFT:
95  * turnRate = -TURN_RATE;
96  * break;
97  * case TURN_RIGHT:
98  * turnRate = TURN_RATE;
99  * break;
100  * }
101  * Script.setTimeout(function () {
102  * turnRate = 0;
103  * }, TURN_DURATION);
104  * }
105  *
106  * SpeechRecognizer.addCommand(TURN_LEFT);
107  * SpeechRecognizer.addCommand(TURN_RIGHT);
108  * SpeechRecognizer.commandRecognized.connect(onCommandRecognized);
109  *
110  * Script.scriptEnding.connect(function () {
111  * Controller.disableMapping(MAPPING_NAME);
112  * SpeechRecognizer.removeCommand(TURN_LEFT);
113  * SpeechRecognizer.removeCommand(TURN_RIGHT);
114  * });
115  */
116  void commandRecognized(const QString& command);
117 
118  /*@jsdoc
119  * Triggered when speech recognition is enabled or disabled.
120  * @function SpeechRecognizer.enabledUpdated
121  * @param {boolean} enabled - <code>true</code> if speech recognition is enabled, <code>false</code> if it is disabled.
122  * @returns {Signal}
123  * @example <caption>Report when speech recognition is enabled or disabled.</caption>
124  * SpeechRecognizer.enabledUpdated.connect(function (enabled) {
125  * print("Speech recognition: " + (enabled ? "enabled" : "disabled"));
126  * });
127  */
128  void enabledUpdated(bool enabled);
129 
130 protected:
131  void reloadCommands();
132 
133 private:
134  SpeechRecognizer();
135  virtual ~SpeechRecognizer();
136 
137  bool _enabled;
138  QSet<QString> _commands;
139 #if defined(Q_OS_MAC)
140  void* _speechRecognizerDelegate;
141  void* _speechRecognizer;
142 #elif defined(Q_OS_WIN)
143  bool _comInitialized;
144  // Use void* instead of ATL CComPtr<> for speech recognizer in order to avoid linker errors with Visual Studio Express.
145  void* _speechRecognizer;
146  void* _speechRecognizerContext;
147  void* _speechRecognizerGrammar;
148  void* _commandRecognizedEvent;
149  QWinEventNotifier* _commandRecognizedNotifier;
150 #endif
151 
152 #if defined(Q_OS_WIN)
153 private slots:
154  void notifyCommandRecognized(void* handle);
155 #endif
156 };
157 
158 #endif // hifi_SpeechRecognizer_h