Updating tizen api checks to avoid failure if they don't exist
[profile/ivi/Modello_Common.git] / js / services / speech.js
1 /**
2  * @module Services
3  */
4
5 /**
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.
10  *
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.
14  *
15  * To attach to particular recognized speech commands register new callback object using {{#crossLink "Speech/addVoiceRecognitionListener:method"}}{{/crossLink}} method.
16  *
17  * To execute TTS for particular sentence use {{#crossLink "Speech/vocalizeString:method"}}{{/crossLink}} method.
18  *
19  * @class Speech
20  * @constructor
21  */
22 var Speech = (function() {
23         "use strict";
24
25         function Speech() {
26                 console.info("Starting up service Speech");
27
28                 // workaround for TTS
29                 this.vocalizeString("");
30
31                 // workaround for STT
32                 if (typeof (tizen) !== 'undefined' && typeof (tizen.content) !== 'undefined' && typeof (tizen.speech.find) !== 'undefined') {
33                         tizen.content.find(function(content) {
34                         }, function(error) {
35                         }, null);
36                 }
37
38                 this._initVoiceRecognition();
39         }
40 /**
41 * Array of registered listeners
42 * @type Object
43 * @property _listeners
44 * @private
45 */
46         Speech.prototype._listeners = [];
47 /**
48  * This method initialize voice recognition , for recognition use tizen.speech.setCBListener function from tizen api.
49  * @method _initVoiceRecognition
50  * @private
51  */
52         Speech.prototype._initVoiceRecognition = function() {
53                 var self = this;
54                 console.log("Speech init voice recognition called.");
55                 if (typeof (tizen.speech.setCBListener) !== 'undefined') {
56                         try {
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;
62
63                                                         switch (result[i].toString().trim().toLowerCase()) {
64                                                         case "play":
65                                                                 commandFound = true;
66                                                                 self._callListener("onplay");
67                                                                 break;
68                                                         case "next":
69                                                                 self._callListener("onnext");
70                                                                 commandFound = true;
71                                                                 break;
72                                                         case "previous":
73                                                                 self._callListener("onprevious");
74                                                                 commandFound = true;
75                                                                 break;
76                                                         case "stop":
77                                                         case "pause":
78                                                                 self._callListener("onstop");
79                                                                 commandFound = true;
80                                                                 break;
81                                                         case "launch_homescreen":
82                                                         case "launch_navigation":
83                                                         case "launch_dashboard":
84                                                         case "launch_store":
85                                                         case "launch_multimediaplayer":
86                                                         case "launch_phone":
87                                                         case "launch_hvac":
88                                                         case "launch_sdl":
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";
99                                                                 }
100                                                                 self._callListener("onapplicationlaunch", appName);
101                                                                 commandFound = true;
102                                                                 break;
103                                                         case "install":
104                                                                 self._callListener("onapplicationinstall");
105                                                                 commandFound = true;
106                                                                 break;
107                                                         case "uninstall":
108                                                                 self._callListener("onapplicationuninstall");
109                                                                 commandFound = true;
110                                                                 break;
111                                                         case "call":
112                                                                 self._callListener("oncall");
113                                                                 commandFound = true;
114                                                                 break;
115                                                         default:
116                                                                 break;
117                                                         }
118
119                                                         if (commandFound) {
120                                                                 break;
121                                                         }
122                                                 }
123                                         });
124                         } catch (err) {
125                                 console.log("Speech set callback listener FAILED + " + err.message);
126                                 console.log(err);
127                         }
128                 } else {
129                         console.log("Speech set callback listener not supported.");
130                 }
131         };
132
133         /**
134          * Adds the listener object to receive notifications when the speech recognizer returns a requested speech command.
135          * Following voice commands are recognized:
136          *
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}}
155          *
156          * To attach to these commands provide listener object defining method name:
157          *
158          *     {
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
162      *     }
163      *
164          *
165          * @method addVoiceRecognitionListener
166          * @param aCallbackObject Object with callback functions to be invoked.
167          */
168         Speech.prototype.addVoiceRecognitionListener = function(aCallbackObject) {
169                 this._listeners.push(aCallbackObject);
170         };
171
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);
177                                         break;
178                                 }
179                         }
180                 }
181         };
182
183         /**
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.
186          *
187          * @method vocalizeString
188          * @param string {String} Text to be synthetized.
189          */
190         Speech.prototype.vocalizeString = function(string) {
191                 console.log("Speech vocalize string called.");
192                 if (typeof (tizen.speech.vocalizeString) !== 'undefined') {
193                         try {
194                                 tizen.speech.vocalizeString(string);
195                         } catch (err) {
196                                 console.log("Speech vocalize string FAILED: " + err.message);
197                                 console.log(err);
198                         }
199                 } else {
200                         console.log("Speech vocalize string not supported.");
201                 }
202         };
203
204         /**
205          * Performs text to speech synthesis of the current application name.
206          *
207          * @method readCurrentAppName
208          */
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";
214                         }
215                         this.vocalizeString(appName);
216                 }
217         };
218
219         window.__speech = undefined === window.__speech ? new Speech() : window.__speech;
220         return window.__speech;
221 })();