STT and TTS for mobile applications
With Adobe AIR and AS3
Hi there. I hope you are well.
In this tutorial, we will see how to use the two native libraries that I wrote for Adobe AIR to create a “kind of parrot” mobile application.
Using natural language processing functions, we can bring other types of interaction, imagine other modes of operation.
The concept
To operate, this application must access the microphone (Android / iOS) and speech recognition (iOS) functions. It is therefore necessary to request authorization from the user when using this function for the first time.
Once the permissions are obtained, the program is ready to retranscribe a sentence uttered by the user.
The latter can then choose the language of the various speech engines and start listening. He will see his sentence appear in the result panel as he speaks. When he stops app listening (or stops speaking), he hears the application repeating his sentence.
He can choose to modify the different settings to re-listen to his utterance or to repeat the operation.
Environment: SDKs and Extensions
To run this application should be compiled with:
- AIR SDK 26
- Feathers SDK 3.3.0
- MetalWorks Mobile Theme
- EzSTT ANE
- EzSpeech ANE
The manifest file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?xml version="1.0" encoding="utf-8" ?> <application xmlns="http://ns.adobe.com/air/application/26.0"> <id>com.fabricemontfort.stt-tts-sample</id> <filename>stt-tts-sample</filename> <name>STT - TTS</name> <versionNumber>0.0.1</versionNumber> <extensions> <extensionID>com.fabricemontfort.air.ezSpeech</extensionID> <extensionID>com.fabricemontfort.air.ezSTT</extensionID> </extensions> <initialWindow> <content>SWF file name is set automatically at compile time</content> <visible>true</visible> <aspectRatio>portrait</aspectRatio> <autoOrients>false</autoOrients> <fullScreen>true</fullScreen> <renderMode>direct</renderMode> <depthAndStencil>true</depthAndStencil> </initialWindow> <supportedLanguages>en de cs es fr it ja ko nl pl pt ru sv tr zh</supportedLanguages> <iPhone> <InfoAdditions> <![CDATA[ <key>UIDeviceFamily</key> <array> <string>1</string> <string>2</string> </array> <key>NSSpeechRecognitionUsageDescription</key> <string>NEED VOICE RECOGNITION</string> <key>NSMicrophoneUsageDescription</key> <string>NEED MICROPHONE</string> <key>MinimumOSVersion</key> <string>10.0</string> ]]> </InfoAdditions> <requestedDisplayResolution>high</requestedDisplayResolution> </iPhone> <android> <manifestAdditions> <![CDATA[ <manifest android:installLocation="auto"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> </manifest> ]]> </manifestAdditions> </android> </application> |
The application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
<?xml version="1.0" encoding="utf-8"?> <f:Application xmlns:f="library://ns.feathersui.com/mxml" xmlns:fx="http://ns.adobe.com/mxml/2009" theme="feathers.themes.MetalWorksMobileTheme"> <fx:Script> <![CDATA[ import feathers.controls.Alert; import starling.events.Event; /** * STT and TTS AIR Native Extensions */ import com.fabricemontfort.air.ezSTT; import com.fabricemontfort.air.ezSpeech; import com.fabricemontfort.air.ezspeech.languages; import com.fabricemontfort.air.ezstt.STTEvent; import com.fabricemontfort.air.ezstt.languages; /** * Text To Speech engine */ private var tts:ezSpeech = ezSpeech.instance; /** * Speech To Text engine */ private var stt:ezSTT = ezSTT.instance; /** * actionButton Labels */ private static const OUCH:String = "NOT SUPPORTED"; private static const AUTH:String = "ASK PERMISSIONS"; private static const START:String = "START LISTENING"; private static const STOP:String = "STOP LISTENING"; /** * STT/TTS utterance */ private var _utterance:String = ""; [Bindable] public function get utterance():String { return _utterance; } public function set utterance(value:String):void { _utterance = value; resultLabel.text = _utterance; } /** * STT Microphone volume */ private var _volume:Number = 0; [Bindable] public function get volume():Number { return _volume; } public function set volume(value:Number):void { _volume = value; } /** * Initialize TTS speed */ private var _speed:Number = 0.48; [Bindable] public function get speed():Number { return _speed; } public function set speed(value:Number):void { _speed = value; tts.setSpeed(speed); } /** * Initialize TTS pitch */ private var _pitch:Number = 0.65; [Bindable] public function get pitch():Number { return _pitch; } public function set pitch(value:Number):void { _pitch = value; tts.setPitch(pitch); } override protected function initialize():void { super.initialize(); // Hide debug messages for ezSTT and ezSpeech stt.debug = false; tts.debug = false; // Set TTS voice speed tts.setSpeed(speed); // Set TTS voice pitch tts.setPitch(pitch); // Set default language for engines stt.setLanguage(com.fabricemontfort.air.ezstt.languages.EN); tts.setLanguage(com.fabricemontfort.air.ezspeech.languages.US); // Check if STT is supported if (stt.isSupported()) { // Check if STT is authorized if (stt.isAuthorized()) { // Add STT listeners for final result, partial result, volume, end of speech stt.addEventListener(STTEvent.PARTIAL, onSTTResult); stt.addEventListener(STTEvent.FINAL, onSTTResult); stt.addEventListener(STTEvent.VOL, onSTTVolume); stt.addEventListener(STTEvent.STOP, onSTTStop); // Everything is fine, lets start actionButton.label = START; // STT is not autorized } else { // Initialize STT listener for permissions stt.addEventListener(STTEvent.AUTH, onAuth); actionButton.label = AUTH; } // STT is not supported } else { actionButton.label = OUCH; var alert:Alert = Alert.show("STT is not supported", "Error", new ListCollection( [ {label: "OK"} ])); } } /** * STT engine recognized words */ private function onSTTResult(event:STTEvent):void { // Set utterance with partial and final result utterance = event.message; } /** * The microphone volume changed */ private function onSTTVolume(event:STTEvent):void { // Set STT microphone volume volume = parseInt(event.message); } /** * User stopped speaking or clicked the stop button */ private function onSTTStop(event:STTEvent):void { actionButton.label = START; volume = 0; // Wait 1 second before repeating last utterance setTimeout(repeatUtterance, 1000); } private function onAuth(event:STTEvent):void { // Check if STT is authorized if (stt.isAuthorized()) { // Remove this listener stt.removeEventListener(STTEvent.AUTH, onAuth); // Add STT listeners for final result, partial result, volume, end of speech stt.addEventListener(STTEvent.PARTIAL, onSTTResult); stt.addEventListener(STTEvent.FINAL, onSTTResult); stt.addEventListener(STTEvent.VOL, onSTTVolume); stt.addEventListener(STTEvent.STOP, onSTTStop); // Everything is fine, lets start actionButton.label = START; // STT is not autorized } else { // Show an error message var alert:Alert = Alert.show("Please give permissions and retry", "Error", new ListCollection( [ {label: "OK"} ])); } } private function actionButton_triggeredHandler(event:Event):void { // Check actionButton label switch (actionButton.label) { // Ask for permissions case AUTH: { stt.askUserAuthorization(); break; } // Start STT engine and change label case START: { stt.start(); actionButton.label = STOP; break; } // Stop STT engine and change label case STOP: { stt.stop(); actionButton.label = START; break; } } } /** * User changed the current language */ private function langPickerList_changeHandler(event:Event):void { // Set current language for STT and TTS engines stt.setLanguage(langPickerList.selectedItem.code); tts.setLanguage(langPickerList.selectedItem.codeSpeech); } /** * User asked to enforce a specific language with country code */ private function forceLangTextInput_changeHandler(event:Event):void { // Try to enfore current language for STT and TTS engines (experimental) stt.forceLanguage(forceLangTextInput.text); tts.forceLanguage(forceLangTextInput.text); } /** * User asked to repeat the utterance */ private function sayButton_triggeredHandler(event:starling.events.Event):void { repeatUtterance(); } private function repeatUtterance():void { // if utterence is not empty if (utterance != "" && utterance != "[]") { // and tts is supported if (tts.isSupported()) { // Let's talk tts.say(utterance); // tts is not supported } else { // show a error message var alert:Alert = Alert.show("TTS is not supported", "Error", new ListCollection( [ {label: "OK"} ])); } } } /** * User changed the speed Slider */ private function speedSlider_changeHandler(event:Event):void { speed = speedSlider.value; } /** * User changed the pitch Slider */ private function pitchSlider_changeHandler(event:starling.events.Event):void { pitch = pitchSlider.value; } ]]> </fx:Script> <!-- This is a vertical layout with padding and gapping of 10 --> <f:layout> <f:VerticalLayout gap="10" padding="10"/> </f:layout> <fx:Declarations> <!-- Fit the target component to the maximum available space --> <f:VerticalLayoutData id="fitScreen" percentWidth="100" percentHeight="100"/> <!-- Fit the target component to me maximum width --> <f:VerticalLayoutData id="fitWidth" percentWidth="100"/> <!-- List of all supported languages --> <f:ListCollection id="langListCollection"> <fx:Object label="English" code="{com.fabricemontfort.air.ezstt.languages.EN}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.US}"/> <fx:Object label="French" code="{com.fabricemontfort.air.ezstt.languages.FR}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.FR}"/> <fx:Object label="German" code="{com.fabricemontfort.air.ezstt.languages.DE}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.DE}"/> <fx:Object label="Italian" code="{com.fabricemontfort.air.ezstt.languages.IT}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.IT}"/> <fx:Object label="Spanish" code="{com.fabricemontfort.air.ezstt.languages.ES}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.ES}"/> <fx:Object label="Chinese" code="{com.fabricemontfort.air.ezstt.languages.ZH}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.ZH}"/> <fx:Object label="Japanese" code="{com.fabricemontfort.air.ezstt.languages.JA}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.JA}"/> <fx:Object label="Russian" code="{com.fabricemontfort.air.ezstt.languages.RU}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.RU}"/> <fx:Object label="Korean" code="{com.fabricemontfort.air.ezstt.languages.KO}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.KO}"/> <fx:Object label="Portuguese" code="{com.fabricemontfort.air.ezstt.languages.PT}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.PT}"/> <fx:Object label="Czech" code="{com.fabricemontfort.air.ezstt.languages.CS}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.CS}"/> <fx:Object label="Dutch" code="{com.fabricemontfort.air.ezstt.languages.NL}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.NL}"/> <fx:Object label="Polish" code="{com.fabricemontfort.air.ezstt.languages.PL}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.PL}"/> <fx:Object label="Swedish" code="{com.fabricemontfort.air.ezstt.languages.SV}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.SV}"/> <fx:Object label="Turkish" code="{com.fabricemontfort.air.ezstt.languages.TR}" codeSpeech="{com.fabricemontfort.air.ezspeech.languages.TR}"/> </f:ListCollection> </fx:Declarations> <f:Panel id="resultPanel" title="RESULTS" layoutData="{fitScreen}"> <f:layout> <f:VerticalLayout padding="10"/> </f:layout> <!-- This is where user can read the utterance --> <f:Label id="resultLabel" wordWrap="true" text="{utterance}" layoutData="{fitScreen}"/> </f:Panel> <f:Label text="TTS VOICE SPEED" layoutData="{fitWidth}"/> <!-- This is where the user can change the TTS voice speed --> <f:Slider id="speedSlider" change="speedSlider_changeHandler(event)" layoutData="{fitWidth}" minimum="0" maximum="1" step="0.05" value="{speed}"/> <f:Label text="TTS VOICE PITCH" layoutData="{fitWidth}"/> <!-- This is where the user can change the TTS voice pitch --> <f:Slider id="pitchSlider" change="pitchSlider_changeHandler(event)" layoutData="{fitWidth}" minimum="0.5" maximum="1.5" step="0.05" value="{pitch}"/> <f:Label text="STT MICROPHONE VOLUME"/> <!-- This is where the user can see the STT microphone volume --> <f:ProgressBar id="volumeBar" minimum="0" maximum="12" value="{volume}" layoutData="{fitWidth}"/> <!-- This is where the user can pick a language --> <f:PickerList id="langPickerList" change="langPickerList_changeHandler(event)" focusPadding="20" dataProvider="{langListCollection}" layoutData="{fitWidth}"/> <f:Label id="forceLangLabel" text="TEST A COUNTRY CODE (EXPERIMENTAL)" layoutData="{fitWidth}"/> <!-- This is where the user can enforce a language with country code --> <f:TextInput id="forceLangTextInput" padding="20" change="forceLangTextInput_changeHandler(event)" layoutData="{fitWidth}"/> <!-- This is where the user have to click to give permissions, start speaking, stop speaking --> <f:Button id="actionButton" padding="20" triggered="actionButton_triggeredHandler(event)" layoutData="{fitWidth}"/> <!-- This is where the user have to click to repeat the utterance --> <f:Button id="sayButton" padding="20" label="REPEAT THE LAST UTTERANCE" triggered="sayButton_triggeredHandler(event)" layoutData="{fitWidth}"/> </f:Application> |
The result in video
Sorry for my so bad english accent (it makes my ears bleed too).
Complete source code
You can find the complete source code and the assets here.
You can also find a very ugly but fully fonctionnal version of this application for Animate CC on github.
The conclusion
If you want to create new types of interactive content with ActionScript 3.0 for mobile projects, voice processing may well open up new horizons for you.
I hope you enjoyed this demonstration. Perhaps this gives you new application ideas. Do not hesitate to let me discover them via Twitter.
Happy coding.
Where to go from here:
Making a chatterbot with AIR, AS3 and PHP