|
31 | 31 | import android.text.TextUtils; |
32 | 32 | import android.util.Log; |
33 | 33 |
|
| 34 | +import java.util.Collections; |
34 | 35 | import java.util.HashMap; |
| 36 | +import java.util.HashSet; |
35 | 37 | import java.util.List; |
36 | 38 | import java.util.Locale; |
37 | 39 | import java.util.Map; |
| 40 | +import java.util.Set; |
38 | 41 |
|
39 | 42 | /** |
40 | 43 | * |
@@ -147,7 +150,25 @@ public interface OnUtteranceCompletedListener { |
147 | 150 | } |
148 | 151 |
|
149 | 152 | /** |
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> |
151 | 172 | */ |
152 | 173 | public class Engine { |
153 | 174 |
|
@@ -435,6 +456,25 @@ public class Engine { |
435 | 456 | */ |
436 | 457 | public static final String KEY_PARAM_PAN = "pan"; |
437 | 458 |
|
| 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"; |
438 | 478 | } |
439 | 479 |
|
440 | 480 | private final Context mContext; |
@@ -811,6 +851,36 @@ public Integer run(ITextToSpeechService service) throws RemoteException { |
811 | 851 | }, ERROR, "playSilence"); |
812 | 852 | } |
813 | 853 |
|
| 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 | + |
814 | 884 | /** |
815 | 885 | * Checks whether the TTS engine is busy speaking. Note that a speech item is |
816 | 886 | * 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) { |
1017 | 1087 | copyFloatParam(bundle, params, Engine.KEY_PARAM_VOLUME); |
1018 | 1088 | copyFloatParam(bundle, params, Engine.KEY_PARAM_PAN); |
1019 | 1089 |
|
| 1090 | + // Copy feature strings defined by the framework. |
| 1091 | + copyStringParam(bundle, params, Engine.KEY_FEATURE_NETWORK_SYNTHESIS); |
| 1092 | + |
1020 | 1093 | // Copy over all parameters that start with the name of the |
1021 | 1094 | // engine that we are currently connected to. The engine is |
1022 | 1095 | // free to interpret them as it chooses. |
|
0 commit comments