OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import android.content.Context; | |
8 import android.speech.tts.TextToSpeech; | |
9 import android.speech.tts.UtteranceProgressListener; | |
10 import java.lang.Double; | |
11 import java.lang.Integer; | |
12 import java.util.ArrayList; | |
13 import java.util.HashMap; | |
14 import java.util.Locale; | |
15 import org.chromium.base.CalledByNative; | |
16 import org.chromium.base.ThreadUtils; | |
17 | |
18 /** | |
19 * This class is the Java counterpart to the C++ TtsPlatformImplAndroid class. | |
20 * It implements the Android-native text-to-speech code to support the web | |
21 * speech synthesis API. | |
22 */ | |
23 public class TtsPlatformImpl { | |
24 private class TtsVoice { | |
25 public String name; | |
26 public String language; | |
27 }; | |
28 | |
29 private int mNativeTtsPlatformImplAndroid; | |
30 private TextToSpeech mTextToSpeech; | |
31 private boolean mInitialized; | |
32 private ArrayList<TtsVoice> mVoices; | |
33 private String mCurrentLanguage; | |
34 | |
35 private TtsPlatformImpl(int nativeTtsPlatformImplAndroid, Context context) { | |
36 mInitialized = false; | |
37 mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid; | |
38 mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListene r() { | |
39 public void onInit(int status) { | |
40 if (status == TextToSpeech.SUCCESS) | |
41 initialize(); | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
move this up on the same line as the if() or put {
dmazzoni
2013/05/16 07:35:13
Done.
| |
42 } | |
43 }); | |
44 mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListen er() { | |
45 public void onDone(final String utteranceId) { | |
46 ThreadUtils.runOnUiThread(new Runnable() { | |
47 @Override | |
48 public void run() { | |
49 nativeOnEndEvent(mNativeTtsPlatformImplAndroid, | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
Maybe check mNativeTtsPlatformImplAndroid for 0?
dmazzoni
2013/05/16 07:35:13
Done.
| |
50 Integer.parseInt(utteranceId)); | |
51 } | |
52 }); | |
53 } | |
54 | |
55 public void onError(final String utteranceId) { | |
56 ThreadUtils.runOnUiThread(new Runnable() { | |
57 @Override | |
58 public void run() { | |
59 nativeOnErrorEvent(mNativeTtsPlatformImplAndroid, | |
60 Integer.parseInt(utteranceId)); | |
61 } | |
62 }); | |
63 } | |
64 | |
65 public void onStart(final String utteranceId) { | |
66 ThreadUtils.runOnUiThread(new Runnable() { | |
67 @Override | |
68 public void run() { | |
69 nativeOnStartEvent(mNativeTtsPlatformImplAndroid, | |
70 Integer.parseInt(utteranceId)); | |
71 } | |
72 }); | |
73 } | |
74 }); | |
75 }; | |
76 | |
77 @CalledByNative | |
78 public static TtsPlatformImpl create(int nativeTtsPlatformImplAndroid, | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
Put javadocs on the public methods.
dmazzoni
2013/05/16 07:35:13
Done.
| |
79 Context context) { | |
80 return new TtsPlatformImpl(nativeTtsPlatformImplAndroid, context); | |
81 } | |
82 | |
83 @CalledByNative | |
84 public boolean isInitialized() { | |
85 return mInitialized; | |
86 } | |
87 | |
88 @CalledByNative | |
89 public int getVoiceCount() { | |
90 return mVoices.size(); | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
Would it just be safe to return 0 if mVoices is nu
dmazzoni
2013/05/16 07:35:13
Sure
dmazzoni
2013/05/16 07:35:13
How about I just assert mInitialized == true every
| |
91 } | |
92 | |
93 @CalledByNative | |
94 public String getVoiceName(int voiceIndex) { | |
95 return mVoices.get(voiceIndex).name; | |
96 } | |
97 | |
98 @CalledByNative | |
99 public String getVoiceLanguage(int voiceIndex) { | |
100 return mVoices.get(voiceIndex).language; | |
101 } | |
102 | |
103 @CalledByNative | |
104 public boolean speak(int utteranceId, String text, String lang, | |
105 float rate, float pitch, float volume) { | |
106 if (lang != mCurrentLanguage) { | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
return false if !isInitialized() (and/or assert)?
dmazzoni
2013/05/16 07:35:13
I like asserting better, that doesn't imply there'
| |
107 mTextToSpeech.setLanguage(new Locale(lang)); | |
108 mCurrentLanguage = lang; | |
109 } | |
110 | |
111 mTextToSpeech.setSpeechRate(rate); | |
112 mTextToSpeech.setPitch(pitch); | |
113 HashMap<String, String> params = new HashMap<String, String>(); | |
114 if (volume != 1.0) | |
115 params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Double.toString(vol ume)); | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
{ } around this even though it's one line.
dmazzoni
2013/05/16 07:35:13
Done.
| |
116 params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString( utteranceId)); | |
117 int result = mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params) ; | |
118 return (result == TextToSpeech.SUCCESS); | |
119 } | |
120 | |
121 @CalledByNative | |
122 public void stop() { | |
123 } | |
124 | |
125 private void initialize() { | |
126 // Note: Android supports multiple speech engines, but querying the | |
127 // metadata about all of them is expensive. So we deliberately only | |
128 // support the default speech engine, and expose the different | |
129 // supported languages for the default engine as different voices. | |
130 String defaultEngineName = mTextToSpeech.getDefaultEngine(); | |
131 String engineLabel = defaultEngineName; | |
132 for (TextToSpeech.EngineInfo info : mTextToSpeech.getEngines()) { | |
133 if (info.name.equals(defaultEngineName)) | |
134 engineLabel = info.label; | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
Move up to same line as if.
dmazzoni
2013/05/16 07:35:13
Done.
| |
135 } | |
136 Locale[] locales = Locale.getAvailableLocales(); | |
137 mVoices = new ArrayList<TtsVoice>(); | |
138 for (int i = 0; i < locales.length; ++i) { | |
139 if (!locales[i].getVariant().isEmpty()) | |
140 continue; | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
Move up to same line as if.
dmazzoni
2013/05/16 07:35:13
Done.
| |
141 if (mTextToSpeech.isLanguageAvailable(locales[i]) > 0) { | |
142 TtsVoice voice = new TtsVoice(); | |
143 voice.name = locales[i].getDisplayLanguage(); | |
144 if (!locales[i].getCountry().isEmpty()) | |
145 voice.name += " " + locales[i].getDisplayCountry(); | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
Add { } around this line or move up.
If this is g
dmazzoni
2013/05/16 07:35:13
Done.
| |
146 voice.language = locales[i].toString(); | |
147 mVoices.add(voice); | |
148 } | |
149 } | |
150 | |
151 mInitialized = true; | |
152 | |
153 ThreadUtils.runOnUiThread(new Runnable() { | |
154 @Override | |
155 public void run() { | |
156 nativeVoicesChanged(mNativeTtsPlatformImplAndroid); | |
David Trainor- moved to gerrit
2013/05/15 20:55:28
Maybe add if (mNativeTtsPlatformImplAndroid != 0)?
dmazzoni
2013/05/16 07:35:13
Done.
| |
157 } | |
158 }); | |
159 } | |
160 | |
161 private native void nativeVoicesChanged(int nativeTtsPlatformImplAndroid); | |
162 private native void nativeOnEndEvent(int nativeTtsPlatformImplAndroid, int u tteranceId); | |
163 private native void nativeOnStartEvent(int nativeTtsPlatformImplAndroid, int utteranceId); | |
164 private native void nativeOnErrorEvent(int nativeTtsPlatformImplAndroid, int utteranceId); | |
165 } | |
OLD | NEW |