77 */
88package ti .tts ;
99
10- import java .util .List ;
11- import java .util .ArrayList ;
12-
1310import android .annotation .SuppressLint ;
1411import android .os .Bundle ;
1512import android .speech .tts .TextToSpeech ;
2421import org .appcelerator .titanium .io .TiBaseFile ;
2522import org .appcelerator .titanium .io .TiFileFactory ;
2623
24+ import java .util .ArrayList ;
25+ import java .util .List ;
2726import java .util .Locale ;
2827
2928
@@ -32,16 +31,15 @@ public class TiTtsModule extends KrollModule {
3231
3332 private static final String LCAT = "TiTtsModule" ;
3433 private static final String defaultID = "utteranceID" ;
34+ TextToSpeech tts ;
3535 private String lastBlobId = null ;
3636 private TiBaseFile lastBlobFile ;
37- private KrollDict config =null ;
38-
39- TextToSpeech tts ;
37+ private KrollDict config = null ;
4038
4139 public TiTtsModule () {
4240 super ();
4341 }
44-
42+
4543 @ Kroll .onAppCreate
4644 public static void onAppCreate (TiApplication app ) {
4745 }
@@ -50,7 +48,7 @@ public static void onAppCreate(TiApplication app) {
5048 @ Deprecated
5149 @ Kroll .method
5250 public TiTtsModule separateSpeaker () {
53- return new TiTtsModule ();
51+ return new TiTtsModule ();
5452 }
5553
5654 @ SuppressLint ("NewApi" )
@@ -62,11 +60,11 @@ public String getVoices(@Kroll.argument(optional = true) String value) {
6260 if (value != null ) {
6361 if (tmpVoice .getName ().contains (value )) {
6462 out += cnc + tmpVoice .getName ();
65- cnc = "|" ;
63+ cnc = "|" ;
6664 }
6765 } else {
6866 out += cnc + tmpVoice .getName ();
69- cnc = "|" ;
67+ cnc = "|" ;
7068 }
7169 }
7270 return out ;
@@ -156,7 +154,7 @@ public void init(@Kroll.argument(optional = true) String engine) {
156154 }, engine );
157155 }
158156 }
159-
157+
160158 @ SuppressLint ("NewApi" )
161159 @ Kroll .method
162160 public void initSilent () {
@@ -172,47 +170,49 @@ public void initSilent() {
172170 public void speak (KrollDict params ) {
173171 String value = params .getString ("text" );
174172 String uid = getStringKey (params , "uid" , TiTtsModule .defaultID );
175- int mode = getBooleanKey (params ,"flush" ,false ) ? TextToSpeech .QUEUE_FLUSH : TextToSpeech .QUEUE_ADD ;
176- Bundle bundle = null ;
173+ int mode = getBooleanKey (params , "flush" , false ) ? TextToSpeech .QUEUE_FLUSH : TextToSpeech .QUEUE_ADD ;
174+ Bundle bundle = new Bundle () ;
177175 Double vol = getDoubleKey (params , "volume" , null );
178176 Double pan = getDoubleKey (params , "pan" , null );
179177 if ((vol != null ) && (pan != null )) {
180- bundle =new Bundle ();
181- if (vol != null )
182- bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_VOLUME , vol .floatValue ()); // [ from 0.0 to 1.0 ]
183- if (pan != null )
184- bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_PAN , pan .floatValue ()); // [ from -1.0 to 1.0 ]
178+ if (vol != null )
179+ bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_VOLUME , vol .floatValue ()); // [ from 0.0 to 1.0 ]
180+ if (pan != null )
181+ bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_PAN , pan .floatValue ()); // [ from -1.0 to 1.0 ]
185182 }
186183 tts .speak (value , mode , bundle , uid );
187184 }
188-
185+
189186 private Double getDoubleKey (KrollDict params , String key ) {
190187 return getDoubleKey (params , key , null );
191188 }
189+
192190 private Double getDoubleKey (KrollDict params , String key , Double def ) {
193- if ((params != null )&& (params .containsKeyAndNotNull (key )))
191+ if ((params != null ) && (params .containsKeyAndNotNull (key )))
194192 return params .getDouble (key );
195- if ((config != null )&& (config .containsKeyAndNotNull (key )))
193+ if ((config != null ) && (config .containsKeyAndNotNull (key )))
196194 return config .getDouble (key );
197195 return def ;
198196 }
197+
199198 private String getStringKey (KrollDict params , String key , String def ) {
200- if ((params != null )&& (params .containsKeyAndNotNull (key )))
199+ if ((params != null ) && (params .containsKeyAndNotNull (key )))
201200 return params .getString (key );
202- if ((config != null )&& (config .containsKeyAndNotNull (key )))
201+ if ((config != null ) && (config .containsKeyAndNotNull (key )))
203202 return config .getString (key );
204203 return def ;
205204 }
205+
206206 private boolean getBooleanKey (KrollDict params , String key , boolean def ) {
207- if ((params != null )&& (params .containsKeyAndNotNull (key )))
207+ if ((params != null ) && (params .containsKeyAndNotNull (key )))
208208 return params .getBoolean (key );
209- if ((config != null )&& (config .containsKeyAndNotNull (key )))
209+ if ((config != null ) && (config .containsKeyAndNotNull (key )))
210210 return config .getBoolean (key );
211211 return def ;
212212 }
213213
214214 public void setup (KrollDict params ) {
215- config = params ;
215+ config = params ;
216216 if (params .containsKeyAndNotNull ("pitch" ))
217217 setPitch (params .getDouble ("pitch" ).floatValue ());
218218 if (params .containsKeyAndNotNull ("speed" ))
@@ -236,11 +236,11 @@ public String getLanguages() {
236236 String cnc = "" ;
237237 for (Locale tmpLang : tts .getAvailableLanguages ()) {
238238 out += cnc + tmpLang .toLanguageTag ();
239- cnc = "|" ;
239+ cnc = "|" ;
240240 }
241241 return out ;
242242 }
243-
243+
244244 @ SuppressLint ("NewApi" )
245245 @ Kroll .method
246246 public Object [] getLanguageList () {
@@ -290,13 +290,13 @@ public Object[] getEnginePackageList() {
290290 public void setEngine (String engine ) {
291291 tts .setEngineByPackageName (engine );
292292 }
293-
293+
294294 @ SuppressLint ("NewApi" )
295295 @ Kroll .getProperty
296296 public int getBufferlen () {
297297 return TextToSpeech .getMaxSpeechInputLength ();
298298 }
299-
299+
300300 @ SuppressLint ("NewApi" )
301301 @ Kroll .getProperty
302302 public boolean getSpeaking () {
@@ -309,24 +309,23 @@ public String synthesizeToFile(KrollDict params) {
309309 String value = params .getString ("text" );
310310
311311 String uid = getStringKey (params , "uid" , TiTtsModule .defaultID );
312- String fileName = getStringKey (params , "filename" , System .currentTimeMillis () + ".wav" );
313- Bundle bundle = null ;
312+ String fileName = getStringKey (params , "filename" , System .currentTimeMillis () + ".wav" );
313+ Bundle bundle = new Bundle () ;
314314 bundle .putString (TextToSpeech .Engine .KEY_PARAM_UTTERANCE_ID , uid );
315315 Double vol = getDoubleKey (params , "volume" , null );
316316 Double pan = getDoubleKey (params , "pan" , null );
317317 if ((vol != null ) && (pan != null )) {
318- bundle =new Bundle ();
319- if (vol != null )
320- bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_VOLUME , vol .floatValue ()); // [ from 0.0 to 1.0 ]
321- if (pan != null )
322- bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_PAN , pan .floatValue ()); // [ from -1.0 to 1.0 ]
318+ if (vol != null )
319+ bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_VOLUME , vol .floatValue ()); // [ from 0.0 to 1.0 ]
320+ if (pan != null )
321+ bundle .putFloat (TextToSpeech .Engine .KEY_PARAM_PAN , pan .floatValue ()); // [ from -1.0 to 1.0 ]
323322 }
324323 try {
325324 TiBaseFile outfile = TiFileFactory .createTitaniumFile (fileName , true );
326325 tts .synthesizeToFile (value , bundle , outfile .getNativeFile (), uid );
327- if (getBooleanKey (params ,"blob" ,true )) {
328- lastBlobId = uid ;
329- lastBlobFile = outfile ;
326+ if (getBooleanKey (params , "blob" , true )) {
327+ lastBlobId = uid ;
328+ lastBlobFile = outfile ;
330329 }
331330 return outfile .nativePath ();
332331 } catch (Exception e ) {
@@ -338,50 +337,50 @@ public String synthesizeToFile(KrollDict params) {
338337 @ Kroll .method
339338 public int emitEvents () {
340339 return tts .setOnUtteranceProgressListener (new UtteranceProgressListener () {
341- @ Override
342- public void onStart (String utteranceId ) {
343- KrollDict kd = new KrollDict ();
344- kd .put ("uid" , utteranceId );
345- fireEvent ("start" , kd );
346- }
340+ @ Override
341+ public void onStart (String utteranceId ) {
342+ KrollDict kd = new KrollDict ();
343+ kd .put ("uid" , utteranceId );
344+ fireEvent ("start" , kd );
345+ }
347346
348- @ Override
349- public void onDone (String utteranceId ) {
350- KrollDict kd = new KrollDict ();
351- kd .put ("uid" , utteranceId );
352- if (utteranceId .equals (lastBlobId ))
353- kd .put ("blob" , TiBlob .blobFromFile (lastBlobFile ));
354- lastBlobFile = null ;
355- lastBlobId = null ;
356- fireEvent ("done" , kd );
357- }
347+ @ Override
348+ public void onDone (String utteranceId ) {
349+ KrollDict kd = new KrollDict ();
350+ kd .put ("uid" , utteranceId );
351+ if (utteranceId .equals (lastBlobId ))
352+ kd .put ("blob" , TiBlob .blobFromFile (lastBlobFile ));
353+ lastBlobFile = null ;
354+ lastBlobId = null ;
355+ fireEvent ("done" , kd );
356+ }
358357
359- @ Override
360- public void onError (String utteranceId , int code ) {
361- KrollDict kd = new KrollDict ();
362- kd .put ("uid" , utteranceId );
363- kd .put ("code" , code );
364- fireEvent ("error" , kd );
365- }
358+ @ Override
359+ public void onError (String utteranceId , int code ) {
360+ KrollDict kd = new KrollDict ();
361+ kd .put ("uid" , utteranceId );
362+ kd .put ("code" , code );
363+ fireEvent ("error" , kd );
364+ }
366365
367- @ Override
368- @ Deprecated
369- public void onError (String utteranceId ) {
370- KrollDict kd = new KrollDict ();
371- kd .put ("uid" , utteranceId );
372- fireEvent ("error" , kd );
373- }
366+ @ Override
367+ @ Deprecated
368+ public void onError (String utteranceId ) {
369+ KrollDict kd = new KrollDict ();
370+ kd .put ("uid" , utteranceId );
371+ fireEvent ("error" , kd );
372+ }
374373
375- @ Override
376- public void onStop (String utteranceId , boolean interrupted ) {
377- KrollDict kd = new KrollDict ();
378- kd .put ("uid" , utteranceId );
379- kd .put ("interrupted" , interrupted );
380- fireEvent ("stop" , kd );
381- }
382- });
374+ @ Override
375+ public void onStop (String utteranceId , boolean interrupted ) {
376+ KrollDict kd = new KrollDict ();
377+ kd .put ("uid" , utteranceId );
378+ kd .put ("interrupted" , interrupted );
379+ fireEvent ("stop" , kd );
380+ }
381+ });
383382 }
384-
383+
385384 @ SuppressLint ("NewApi" )
386385 @ Kroll .method
387386 public void shutdown () {
0 commit comments