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) !== 'undefined' && typeof (tizen.speech) !== 'undefined' && typeof (tizen.speech.setCBListener) !== 'undefined') {
58 tizen.speech.setCBListener(function(result) {
59 console.log("Speech: onresult received");
60 for ( var i = 0; i < result.length; i++) {
61 console.log("Speech: forloop, command = " + result[i]);
62 var commandFound = false;
64 switch (result[i].toString().trim().toLowerCase()) {
67 self._callListener("onplay");
70 self._callListener("onnext");
74 self._callListener("onprevious");
79 self._callListener("onstop");
82 case "launch_homescreen":
83 case "launch_navigation":
84 case "launch_dashboard":
86 case "launch_multimediaplayer":
90 case "launch_simulator":
91 var appName = result[i].toString().trim().toLowerCase().substring(7);
92 if (appName === "homescreen") {
93 appName = "home screen";
94 } else if (appName === "multimediaplayer") {
95 appName = "multimedia player";
96 } else if (appName === "simulator") {
97 appName = "amb simulator";
98 } else if (appName === "sdl") {
99 appName = "smartdevicelink";
101 self._callListener("onapplicationlaunch", appName);
105 self._callListener("onapplicationinstall");
109 self._callListener("onapplicationuninstall");
113 self._callListener("oncall");
126 console.log("Speech set callback listener FAILED + " + err.message);
130 console.log("Speech set callback listener not supported.");
135 * Adds the listener object to receive notifications when the speech recognizer returns a requested speech command.
136 * Following voice commands are recognized:
138 * * `play music` - invokes method `onplay`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
139 * * `pause music` - invokes method `onpause`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
140 * * `play next` - invokes method `onnext`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
141 * * `play previous`- invokes method `onprevious`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
142 * * `stop music` - invokes method `onstop`, used in {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
143 * * `install application` - invokes method `onapplicationinstall`, used in {{#crossLinkModule "StoreApplication"}}{{/crossLinkModule}}
144 * * `uninstall application` - invokes method `onapplicationuninstall`, used in {{#crossLinkModule "StoreApplication"}}{{/crossLinkModule}}
145 * * `call contact` - invokes method `oncall`, used in {{#crossLinkModule "PhoneApplication"}}{{/crossLinkModule}}
146 * * `launch` commands - invokes method `onapplicationlaunch` with parameter indicating application (used globally):
147 * * `launch home screen` - Launches {{#crossLinkModule "HomeScreenApplication"}}{{/crossLinkModule}}
148 * * `launch navigation` - Launches {{#crossLinkModule "NavigationGoogleApplication"}}{{/crossLinkModule}}
149 * * `launch dashboard` - Launches {{#crossLinkModule "DashboardApplication"}}{{/crossLinkModule}}
150 * * `launch store` - Launches {{#crossLinkModule "StoreApplication"}}{{/crossLinkModule}}
151 * * `launch multimedia player` - Launches {{#crossLinkModule "MultimediaPlayerApplication"}}{{/crossLinkModule}}
152 * * `launch phone`, `launch dialer` - Launches {{#crossLinkModule "PhoneApplication"}}{{/crossLinkModule}}
153 * * `launch air conditioning` - Launches {{#crossLinkModule "HVACApplication"}}{{/crossLinkModule}}
154 * * `launch smart device link` - Launches {{#crossLinkModule "SdlApplication"}}{{/crossLinkModule}}
155 * * `launch simulator` - Launches {{#crossLinkModule "AMBSimulatorApplication"}}{{/crossLinkModule}}
157 * To attach to these commands provide listener object defining method name:
160 * onplay : function() { }, // Called when play command was identified
161 * onstop : function() { } // Called when stop command was identified
162 * onapplicationinstall : function() { } // Called when install command was identified
166 * @method addVoiceRecognitionListener
167 * @param aCallbackObject Object with callback functions to be invoked.
169 Speech.prototype.addVoiceRecognitionListener = function(aCallbackObject) {
170 this._listeners.push(aCallbackObject);
173 Speech.prototype._callListener = function(listenerName, arg) {
174 for ( var i = 0; i < this._listeners.length; ++i) {
175 for ( var name in this._listeners[i]) {
176 if (name === listenerName) {
177 this._listeners[i][name](arg);
185 * Performs text to speech synthesis of a given text. This method use api function `tizen.speech.vocalizeString` and currently
186 * all strings are queued without option to cancel TTS before it's finalized.
188 * @method vocalizeString
189 * @param string {String} Text to be synthetized.
191 Speech.prototype.vocalizeString = function(string) {
192 console.log("Speech vocalize string called.");
193 if (typeof (tizen) !== 'undefined' && typeof (tizen.speech) !== 'undefined' && typeof (tizen.speech.vocalizeString) !== 'undefined') {
195 tizen.speech.vocalizeString(string);
197 console.log("Speech vocalize string FAILED: " + err.message);
201 console.log("Speech vocalize string not supported.");
206 * Performs text to speech synthesis of the current application name.
208 * @method readCurrentAppName
210 Speech.prototype.readCurrentAppName = function() {
211 if (typeof (tizen) !== 'undefined') {
212 var appName = tizen.application.getCurrentApplication().appInfo.name.toString().trim().toLowerCase();
213 if (appName === "modello hvac") {
214 appName = "air conditioning";
216 this.vocalizeString(appName);
220 window.__speech = undefined === window.__speech ? new Speech() : window.__speech;
221 return window.__speech;