Skip to content

Commit 748af66

Browse files
committed
Add an API for querying / enabling network TTS support.
bug:5284966 Change-Id: I01708f40bf0e975449125320dbcd4842210ca168
1 parent b4529b0 commit 748af66

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

api/current.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18808,6 +18808,7 @@ package android.speech.tts {
1880818808
method public boolean areDefaultsEnforced();
1880918809
method public java.lang.String getDefaultEngine();
1881018810
method public java.util.List<android.speech.tts.TextToSpeech.EngineInfo> getEngines();
18811+
method public java.util.Set<java.lang.String> getFeatures(java.util.Locale);
1881118812
method public java.util.Locale getLanguage();
1881218813
method public int isLanguageAvailable(java.util.Locale);
1881318814
method public boolean isSpeaking();
@@ -18853,6 +18854,8 @@ package android.speech.tts {
1885318854
field public static final java.lang.String EXTRA_VOICE_DATA_FILES_INFO = "dataFilesInfo";
1885418855
field public static final java.lang.String EXTRA_VOICE_DATA_ROOT_DIRECTORY = "dataRoot";
1885518856
field public static final java.lang.String INTENT_ACTION_TTS_SERVICE = "android.intent.action.TTS_SERVICE";
18857+
field public static final java.lang.String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
18858+
field public static final java.lang.String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
1885618859
field public static final java.lang.String KEY_PARAM_PAN = "pan";
1885718860
field public static final java.lang.String KEY_PARAM_STREAM = "streamType";
1885818861
field public static final java.lang.String KEY_PARAM_UTTERANCE_ID = "utteranceId";
@@ -18878,6 +18881,7 @@ package android.speech.tts {
1887818881
public abstract class TextToSpeechService extends android.app.Service {
1887918882
ctor public TextToSpeechService();
1888018883
method public android.os.IBinder onBind(android.content.Intent);
18884+
method protected java.util.Set<java.lang.String> onGetFeaturesForLanguage(java.lang.String, java.lang.String, java.lang.String);
1888118885
method protected abstract java.lang.String[] onGetLanguage();
1888218886
method protected abstract int onIsLanguageAvailable(java.lang.String, java.lang.String, java.lang.String);
1888318887
method protected abstract int onLoadLanguage(java.lang.String, java.lang.String, java.lang.String);

core/java/android/speech/tts/ITextToSpeechService.aidl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ interface ITextToSpeechService {
113113
*/
114114
int isLanguageAvailable(in String lang, in String country, in String variant);
115115

116+
/**
117+
* Returns a list of features available for a given language. Elements of the returned
118+
* string array can be passed in as keys to {@link TextToSpeech#speak} and
119+
* {@link TextToSpeech#synthesizeToFile} to select a given feature or features to be
120+
* used during synthesis.
121+
*
122+
* @param lang ISO-3 language code.
123+
* @param country ISO-3 country code. May be empty or null.
124+
* @param variant Language variant. May be empty or null.
125+
* @return An array of strings containing the set of features supported for
126+
* the supplied locale. The array of strings must not contain
127+
* duplicates.
128+
*/
129+
String[] getFeaturesForLanguage(in String lang, in String country, in String variant);
130+
116131
/**
117132
* Notifies the engine that it should load a speech synthesis language.
118133
*

core/java/android/speech/tts/TextToSpeech.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import android.text.TextUtils;
3232
import android.util.Log;
3333

34+
import java.util.Collections;
3435
import java.util.HashMap;
36+
import java.util.HashSet;
3537
import java.util.List;
3638
import java.util.Locale;
3739
import java.util.Map;
40+
import java.util.Set;
3841

3942
/**
4043
*
@@ -147,7 +150,25 @@ public interface OnUtteranceCompletedListener {
147150
}
148151

149152
/**
150-
* Constants and parameter names for controlling text-to-speech.
153+
* Constants and parameter names for controlling text-to-speech. These include:
154+
*
155+
* <ul>
156+
* <li>
157+
* Intents to ask engine to install data or check its data and
158+
* extras for a TTS engine's check data activity.
159+
* </li>
160+
* <li>
161+
* Keys for the parameters passed with speak commands, e.g.
162+
* {@link Engine#KEY_PARAM_UTTERANCE_ID}, {@link Engine#KEY_PARAM_STREAM}.
163+
* </li>
164+
* <li>
165+
* A list of feature strings that engines might support, e.g
166+
* {@link Engine#KEY_FEATURE_NETWORK_SYNTHESIS}). These values may be passed in to
167+
* {@link TextToSpeech#speak} and {@link TextToSpeech#synthesizeToFile} to modify
168+
* engine behaviour. The engine can be queried for the set of features it supports
169+
* through {@link TextToSpeech#getFeatures(java.util.Locale)}.
170+
* </li>
171+
* </ul>
151172
*/
152173
public class Engine {
153174

@@ -435,6 +456,25 @@ public class Engine {
435456
*/
436457
public static final String KEY_PARAM_PAN = "pan";
437458

459+
/**
460+
* Feature key for network synthesis. See {@link TextToSpeech#getFeatures(Locale)}
461+
* for a description of how feature keys work. If set (and supported by the engine
462+
* as per {@link TextToSpeech#getFeatures(Locale)}, the engine must
463+
* use network based synthesis.
464+
*
465+
* @see TextToSpeech#speak(String, int, java.util.HashMap)
466+
* @see TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)
467+
* @see TextToSpeech#getFeatures(java.util.Locale)
468+
*/
469+
public static final String KEY_FEATURE_NETWORK_SYNTHESIS = "networkTts";
470+
471+
/**
472+
* Feature key for embedded synthesis. See {@link TextToSpeech#getFeatures(Locale)}
473+
* for a description of how feature keys work. If set and supported by the engine
474+
* as per {@link TextToSpeech#getFeatures(Locale)}, the engine must synthesize
475+
* text on-device (without making network requests).
476+
*/
477+
public static final String KEY_FEATURE_EMBEDDED_SYNTHESIS = "embeddedTts";
438478
}
439479

440480
private final Context mContext;
@@ -811,6 +851,36 @@ public Integer run(ITextToSpeechService service) throws RemoteException {
811851
}, ERROR, "playSilence");
812852
}
813853

854+
/**
855+
* Queries the engine for the set of features it supports for a given locale.
856+
* Features can either be framework defined, e.g.
857+
* {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific.
858+
* Engine specific keys must be prefixed by the name of the engine they
859+
* are intended for. These keys can be used as parameters to
860+
* {@link TextToSpeech#speak(String, int, java.util.HashMap)} and
861+
* {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}.
862+
*
863+
* Features are boolean flags, and their values in the synthesis parameters
864+
* must be behave as per {@link Boolean#parseBoolean(String)}.
865+
*
866+
* @param locale The locale to query features for.
867+
*/
868+
public Set<String> getFeatures(final Locale locale) {
869+
return runAction(new Action<Set<String>>() {
870+
@Override
871+
public Set<String> run(ITextToSpeechService service) throws RemoteException {
872+
String[] features = service.getFeaturesForLanguage(
873+
locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
874+
if (features != null) {
875+
final Set<String> featureSet = new HashSet<String>();
876+
Collections.addAll(featureSet, features);
877+
return featureSet;
878+
}
879+
return null;
880+
}
881+
}, null, "getFeatures");
882+
}
883+
814884
/**
815885
* Checks whether the TTS engine is busy speaking. Note that a speech item is
816886
* considered complete once it's audio data has been sent to the audio mixer, or
@@ -1017,6 +1087,9 @@ private Bundle getParams(HashMap<String, String> params) {
10171087
copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME);
10181088
copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN);
10191089

1090+
// Copy feature strings defined by the framework.
1091+
copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
1092+
10201093
// Copy over all parameters that start with the name of the
10211094
// engine that we are currently connected to. The engine is
10221095
// free to interpret them as it chooses.

core/java/android/speech/tts/TextToSpeechService.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.IOException;
3737
import java.util.HashMap;
3838
import java.util.Locale;
39+
import java.util.Set;
3940

4041

4142
/**
@@ -67,7 +68,6 @@
6768
* any. Any pending data from the current synthesis will be discarded.
6869
*
6970
*/
70-
// TODO: Add a link to the sample TTS engine once it's done.
7171
public abstract class TextToSpeechService extends Service {
7272

7373
private static final boolean DBG = false;
@@ -196,6 +196,18 @@ public void onDestroy() {
196196
protected abstract void onSynthesizeText(SynthesisRequest request,
197197
SynthesisCallback callback);
198198

199+
/**
200+
* Queries the service for a set of features supported for a given language.
201+
*
202+
* @param lang ISO-3 language code.
203+
* @param country ISO-3 country code. May be empty or null.
204+
* @param variant Language variant. May be empty or null.
205+
* @return A list of features supported for the given language.
206+
*/
207+
protected Set<String> onGetFeaturesForLanguage(String lang, String country, String variant) {
208+
return null;
209+
}
210+
199211
private int getDefaultSpeechRate() {
200212
return getSecureSettingInt(Settings.Secure.TTS_DEFAULT_RATE, Engine.DEFAULT_RATE);
201213
}
@@ -778,6 +790,13 @@ public int isLanguageAvailable(String lang, String country, String variant) {
778790
return onIsLanguageAvailable(lang, country, variant);
779791
}
780792

793+
public String[] getFeaturesForLanguage(String lang, String country, String variant) {
794+
Set<String> features = onGetFeaturesForLanguage(lang, country, variant);
795+
String[] featuresArray = new String[features.size()];
796+
features.toArray(featuresArray);
797+
return featuresArray;
798+
}
799+
781800
/*
782801
* There is no point loading a non default language if defaults
783802
* are enforced.

0 commit comments

Comments
 (0)