6 * Speech class provides text to speech (TTS) and speech to text (STT) or speech recognition functionality utilizing
7 * [tizen.speech API](https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html).
8 * This component is usually initialized by {{#crossLink "Bootstrap"}}{{/crossLink}} class and can be
9 * later accessed using global {{#crossLink "Speech"}}{{/crossLink}} object.
11 * Due to limitation of [tizen.speech API](https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html) that allows to set
12 * only one listener, class implements custom event dispatcher that provides unified way for registering listener objects to
13 * receive notification when the speech recognizer returns a result.
15 * To attach to particular recognized speech commands register new callback object using {{#crossLink "Speech/addVoiceRecognitionListener:method"}}{{/crossLink}} method.
17 * To execute TTS for particular sentence use {{#crossLink "Speech/vocalizeString:method"}}{{/crossLink}} method.
22 var Speech = (function() {
26 console.info("Starting up service Speech");
29 this.vocalizeString("");
32 if (typeof (tizen) !== 'undefined' && typeof (tizen.content) !== 'undefined' && typeof (tizen.speech.find) !== 'undefined') {
33 tizen.content.find(function(content) {
38 this._initVoiceRecognition();
41 * Array of registered listeners
43 * @property _listeners
46 Speech.prototype._listeners = [];
48 * This method initialize voice recognition , for recognition use tizen.speech.setCBListener function from tizen api.
49 * @method _initVoiceRecognition
52 Speech.prototype._initVoiceRecognition = function() {
54 console.log("Speech init voice recognition called.");
55 if (typeof (tizen.speech.setCBListener) !== 'undefined') {
57 tizen.speech.setCBListener(function(result) {
58 console.log("Speech: onresult received");
59 for ( var i = 0; i < result.length; i++) {
60 console.log("Speech: forloop, command = " + result[i]);
61 var commandFound = false;
63 switch (result[i].toString().trim().toLowerCase()) {
66 self._callListener("onplay");
69 self._callListener("onnext");
73 self._callListener("onprevious");
78 self._callListener("onstop");
81 case "launch_homescreen":
82 case "launch_navigation":
83 case "launch_dashboard":
85 case "launch_multimediaplayer":
89 case "launch_simulator":
90 var appName = result[i].toString().trim().toLowerCase().substring(7);
91 if (appName === "homescreen") {
92 appName = "home screen";
93 } else if (appName === "multimediaplayer") {
94 appName = "multimedia player";
95 } else if (appName === "simulator") {
96 appName = "amb simulator";
97 } else if (appName === "sdl") {
98 appName = "smartdevicelink";
100 self._callListener("onapplicationlaunch", appName);
104 self._callListener("onapplicationinstall");
108 self._callListener("onapplicationuninstall");
112 self._callListener("oncall");
125 console.log("Speech set callback listener FAILED + " + err.message);
129 console.log("Speech set callback listener not supported.");
134 * Adds the listener object to receive notifications when the speech recognizer returns a requested speech command.
135 * Following voice commands are recognized:
137 * * `play music` - invokes method `onplay`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
138 * * `pause music` - invokes method `onpause`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
139 * * `play next` - invokes method `onnext`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
140 * * `play previous`- invokes method `onprevious`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
141 * * `stop music` - invokes method `onstop`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
142 * * `install application` - invokes method `onapplicationinstall`, used in {{#crossLinkModule "StoreApplication"}}{{/crossLinkModule}}
143 * * `uninstall application` - invokes method `onapplicationuninstall`, used in {{#crossLinkModule "StoreApplication"}}{{/crossLinkModule}}
144 * * `call contact` - invokes method `oncall`, used in {{#crossLinkModule "PhoneApplication"}}{{/crossLinkModule}}
145 * * `launch` commands - invokes method `onapplicationlaunch` with parameter indicating application (used globally):
146 * * `launch home screen` - Launches {{#crossLinkModule "HomeScreenApplication"}}{{/crossLinkModule}}
147 * * `launch navigation` - Launches {{#crossLinkModule "NavigationGoogleApplication"}}{{/crossLinkModule}}
148 * * `launch dashboard` - Launches {{#crossLinkModule "DashboardApplication"}}{{/crossLinkModule}}
149 * * `launch store` - Launches {{#crossLinkModule "StoreApplication"}}{{/crossLinkModule}}
150 * * `launch multimedia player` - Launches {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
151 * * `launch phone`, `launch dialer` - Launches {{#crossLinkModule "PhoneApplication"}}{{/crossLinkModule}}
152 * * `launch air conditioning` - Launches {{#crossLinkModule "HVACApplication"}}{{/crossLinkModule}}
153 * * `launch smart device link` - Launches {{#crossLinkModule "SdlApplication"}}{{/crossLinkModule}}
154 * * `launch simulator` - Launches {{#crossLinkModule "AMBSimulatorApplication"}}{{/crossLinkModule}}
156 * To attach to these commands provide listener object defining method name:
159 * onplay : function() { }, // Called when play command was identified
160 * onstop : function() { } // Called when stop command was identified
161 * onapplicationinstall : function() { } // Called when install command was identified
165 * @method addVoiceRecognitionListener
166 * @param aCallbackObject Object with callback functions to be invoked.
168 Speech.prototype.addVoiceRecognitionListener = function(aCallbackObject) {
169 this._listeners.push(aCallbackObject);
172 Speech.prototype._callListener = function(listenerName, arg) {
173 for ( var i = 0; i < this._listeners.length; ++i) {
174 for ( var name in this._listeners[i]) {
175 if (name === listenerName) {
176 this._listeners[i][name](arg);
184 * Performs text to speech synthesis of a given text. This method use api function `tizen.speech.vocalizeString` and currently
185 * all strings are queued without option to cancel TTS before it's finalized.
187 * @method vocalizeString
188 * @param string {String} Text to be synthetized.
190 Speech.prototype.vocalizeString = function(string) {
191 console.log("Speech vocalize string called.");
192 if (typeof (tizen.speech.vocalizeString) !== 'undefined') {
194 tizen.speech.vocalizeString(string);
196 console.log("Speech vocalize string FAILED: " + err.message);
200 console.log("Speech vocalize string not supported.");
205 * Performs text to speech synthesis of the current application name.
207 * @method readCurrentAppName
209 Speech.prototype.readCurrentAppName = function() {
210 if (typeof (tizen.application.getCurrentApplication) !== 'undefined') {
211 var appName = tizen.application.getCurrentApplication().appInfo.name.toString().trim().toLowerCase();
212 if (appName === "modello hvac") {
213 appName = "air conditioning";
215 this.vocalizeString(appName);
219 window.__speech = undefined === window.__speech ? new Speech() : window.__speech;
220 return window.__speech;