Fix font name, temporarily remove subscribe call, and fix app name
[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) !== 'undefined' && typeof (tizen.speech) !== 'undefined' && typeof (tizen.speech.setCBListener) !== 'undefined') {
56                         try {
57
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;
63
64                                                         switch (result[i].toString().trim().toLowerCase()) {
65                                                         case "play":
66                                                                 commandFound = true;
67                                                                 self._callListener("onplay");
68                                                                 break;
69                                                         case "next":
70                                                                 self._callListener("onnext");
71                                                                 commandFound = true;
72                                                                 break;
73                                                         case "previous":
74                                                                 self._callListener("onprevious");
75                                                                 commandFound = true;
76                                                                 break;
77                                                         case "stop":
78                                                         case "pause":
79                                                                 self._callListener("onstop");
80                                                                 commandFound = true;
81                                                                 break;
82                                                         case "launch_homescreen":
83                                                         case "launch_navigation":
84                                                         case "launch_dashboard":
85                                                         case "launch_store":
86                                                         case "launch_multimediaplayer":
87                                                         case "launch_phone":
88                                                         case "launch_hvac":
89                                                         case "launch_sdl":
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";
100                                                                 }
101                                                                 self._callListener("onapplicationlaunch", appName);
102                                                                 commandFound = true;
103                                                                 break;
104                                                         case "install":
105                                                                 self._callListener("onapplicationinstall");
106                                                                 commandFound = true;
107                                                                 break;
108                                                         case "uninstall":
109                                                                 self._callListener("onapplicationuninstall");
110                                                                 commandFound = true;
111                                                                 break;
112                                                         case "call":
113                                                                 self._callListener("oncall");
114                                                                 commandFound = true;
115                                                                 break;
116                                                         default:
117                                                                 break;
118                                                         }
119
120                                                         if (commandFound) {
121                                                                 break;
122                                                         }
123                                                 }
124                                         });
125                         } catch (err) {
126                                 console.log("Speech set callback listener FAILED + " + err.message);
127                                 console.log(err);
128                         }
129                 } else {
130                         console.log("Speech set callback listener not supported.");
131                 }
132         };
133
134         /**
135          * Adds the listener object to receive notifications when the speech recognizer returns a requested speech command.
136          * Following voice commands are recognized:
137          *
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}}
156          *
157          * To attach to these commands provide listener object defining method name:
158          *
159          *     {
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
163      *     }
164      *
165          *
166          * @method addVoiceRecognitionListener
167          * @param aCallbackObject Object with callback functions to be invoked.
168          */
169         Speech.prototype.addVoiceRecognitionListener = function(aCallbackObject) {
170                 this._listeners.push(aCallbackObject);
171         };
172
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);
178                                         break;
179                                 }
180                         }
181                 }
182         };
183
184         /**
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.
187          *
188          * @method vocalizeString
189          * @param string {String} Text to be synthetized.
190          */
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') {
194                         try {
195                                 tizen.speech.vocalizeString(string);
196                         } catch (err) {
197                                 console.log("Speech vocalize string FAILED: " + err.message);
198                                 console.log(err);
199                         }
200                 } else {
201                         console.log("Speech vocalize string not supported.");
202                 }
203         };
204
205         /**
206          * Performs text to speech synthesis of the current application name.
207          *
208          * @method readCurrentAppName
209          */
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";
215                         }
216                         this.vocalizeString(appName);
217                 }
218         };
219
220         window.__speech = undefined === window.__speech ? new Speech() : window.__speech;
221         return window.__speech;
222 })();