blob: 4b5385f0e5f6ae5fcf34b14f2bbc9f0edc5b0f6f [file] [log] [blame]
Przemyslaw Szczepaniak90d15d22013-06-14 12:02:53 +01001package android.speech.tts;
2
3import android.media.AudioManager;
4import android.os.Bundle;
5
6/**
7 * Synthesis request configuration.
8 *
9 * This class is immutable, and can only be constructed using
Nick Kralevicheb337052013-10-24 15:56:04 -070010 * {@link RequestConfig.Builder}.
Przemyslaw Szczepaniak90d15d22013-06-14 12:02:53 +010011 */
12public final class RequestConfig {
13
14 /** Builder for constructing RequestConfig objects. */
15 public static final class Builder {
16 private VoiceInfo mCurrentVoiceInfo;
17 private Bundle mVoiceParams;
18 private Bundle mAudioParams;
19
20 Builder(VoiceInfo currentVoiceInfo, Bundle voiceParams, Bundle audioParams) {
21 mCurrentVoiceInfo = currentVoiceInfo;
22 mVoiceParams = voiceParams;
23 mAudioParams = audioParams;
24 }
25
26 /**
27 * Create new RequestConfig builder.
28 */
29 public static Builder newBuilder() {
30 return new Builder(null, new Bundle(), new Bundle());
31 }
32
33 /**
34 * Create new RequestConfig builder.
35 * @param prototype
36 * Prototype of new RequestConfig. Copies all fields of the
37 * prototype to the constructed object.
38 */
39 public static Builder newBuilder(RequestConfig prototype) {
40 return new Builder(prototype.mCurrentVoiceInfo,
41 (Bundle)prototype.mVoiceParams.clone(),
42 (Bundle)prototype.mAudioParams.clone());
43 }
44
45 /** Set voice for request. Will reset voice parameters to the defaults. */
46 public Builder setVoice(VoiceInfo voice) {
47 mCurrentVoiceInfo = voice;
48 mVoiceParams = (Bundle)voice.getParamsWithDefaults().clone();
49 return this;
50 }
51
52 /**
53 * Set request voice parameter.
54 *
55 * @param paramName
56 * The name of the parameter. It has to be one of the keys
Nick Kralevicheb337052013-10-24 15:56:04 -070057 * from {@link VoiceInfo#getParamsWithDefaults()}
Przemyslaw Szczepaniak90d15d22013-06-14 12:02:53 +010058 * @param value
59 * Value of the parameter. Its type can be one of: Integer, Float,
Przemyslaw Szczepaniak97cd6472013-10-25 12:04:47 +010060 * Boolean, String, VoiceInfo (will be set as a String, result of a call to
61 * the {@link VoiceInfo#getName()}) or byte[]. It has to be of the same type
Nick Kralevicheb337052013-10-24 15:56:04 -070062 * as the default value from {@link VoiceInfo#getParamsWithDefaults()}
Przemyslaw Szczepaniak90d15d22013-06-14 12:02:53 +010063 * for that parameter.
64 * @throws IllegalArgumentException
65 * If paramName is not a valid parameter name or its value is of a wrong
66 * type.
67 * @throws IllegalStateException
68 * If no voice is set.
69 */
70 public Builder setVoiceParam(String paramName, Object value){
71 if (mCurrentVoiceInfo == null) {
72 throw new IllegalStateException(
73 "Couldn't set voice parameter, no voice is set");
74 }
75 Object defaultValue = mCurrentVoiceInfo.getParamsWithDefaults().get(paramName);
76 if (defaultValue == null) {
77 throw new IllegalArgumentException(
Przemyslaw Szczepaniak97cd6472013-10-25 12:04:47 +010078 "Parameter \"" + paramName + "\" is not available in set voice with " +
79 "name: " + mCurrentVoiceInfo.getName());
Przemyslaw Szczepaniak90d15d22013-06-14 12:02:53 +010080 }
81
Przemyslaw Szczepaniak97cd6472013-10-25 12:04:47 +010082 // If it's VoiceInfo, get its name
Przemyslaw Szczepaniak90d15d22013-06-14 12:02:53 +010083 if (value instanceof VoiceInfo) {
Przemyslaw Szczepaniak97cd6472013-10-25 12:04:47 +010084 value = ((VoiceInfo)value).getName();
Przemyslaw Szczepaniak90d15d22013-06-14 12:02:53 +010085 }
86
87 // Check type information
88 if (!defaultValue.getClass().equals(value.getClass())) {
89 throw new IllegalArgumentException(
90 "Parameter \"" + paramName +"\" is of different type. Value passed has " +
91 "type " + value.getClass().getSimpleName() + " but should have " +
92 "type " + defaultValue.getClass().getSimpleName());
93 }
94
95 setParam(mVoiceParams, paramName, value);
96 return this;
97 }
98
99 /**
100 * Set request audio parameter.
101 *
102 * Doesn't requires a set voice.
103 *
104 * @param paramName
105 * Name of parameter.
106 * @param value
107 * Value of parameter. Its type can be one of: Integer, Float, Boolean, String
108 * or byte[].
109 */
110 public Builder setAudioParam(String paramName, Object value) {
111 setParam(mAudioParams, paramName, value);
112 return this;
113 }
114
115 /**
116 * Set the {@link TextToSpeechClient.Params#AUDIO_PARAM_STREAM} audio parameter.
117 *
118 * @param streamId One of the STREAM_ constants defined in {@link AudioManager}.
119 */
120 public void setAudioParamStream(int streamId) {
121 setAudioParam(TextToSpeechClient.Params.AUDIO_PARAM_STREAM, streamId);
122 }
123
124 /**
125 * Set the {@link TextToSpeechClient.Params#AUDIO_PARAM_VOLUME} audio parameter.
126 *
127 * @param volume Float in range of 0.0 to 1.0.
128 */
129 public void setAudioParamVolume(float volume) {
130 setAudioParam(TextToSpeechClient.Params.AUDIO_PARAM_VOLUME, volume);
131 }
132
133 /**
134 * Set the {@link TextToSpeechClient.Params#AUDIO_PARAM_PAN} audio parameter.
135 *
136 * @param pan Float in range of -1.0 to +1.0.
137 */
138 public void setAudioParamPan(float pan) {
139 setAudioParam(TextToSpeechClient.Params.AUDIO_PARAM_PAN, pan);
140 }
141
142 private void setParam(Bundle bundle, String featureName, Object value) {
143 if (value instanceof String) {
144 bundle.putString(featureName, (String)value);
145 } else if(value instanceof byte[]) {
146 bundle.putByteArray(featureName, (byte[])value);
147 } else if(value instanceof Integer) {
148 bundle.putInt(featureName, (Integer)value);
149 } else if(value instanceof Float) {
150 bundle.putFloat(featureName, (Float)value);
151 } else if(value instanceof Double) {
152 bundle.putFloat(featureName, (Float)value);
153 } else if(value instanceof Boolean) {
154 bundle.putBoolean(featureName, (Boolean)value);
155 } else {
156 throw new IllegalArgumentException("Illegal type of object");
157 }
158 return;
159 }
160
161 /**
162 * Build new RequestConfig instance.
163 */
164 public RequestConfig build() {
165 RequestConfig config =
166 new RequestConfig(mCurrentVoiceInfo, mVoiceParams, mAudioParams);
167 return config;
168 }
169 }
170
171 private RequestConfig(VoiceInfo voiceInfo, Bundle voiceParams, Bundle audioParams) {
172 mCurrentVoiceInfo = voiceInfo;
173 mVoiceParams = voiceParams;
174 mAudioParams = audioParams;
175 }
176
177 /**
178 * Currently set voice.
179 */
180 private final VoiceInfo mCurrentVoiceInfo;
181
182 /**
183 * Voice parameters bundle.
184 */
185 private final Bundle mVoiceParams;
186
187 /**
188 * Audio parameters bundle.
189 */
190 private final Bundle mAudioParams;
191
192 /**
193 * @return Currently set request voice.
194 */
195 public VoiceInfo getVoice() {
196 return mCurrentVoiceInfo;
197 }
198
199 /**
200 * @return Request audio parameters.
201 */
202 public Bundle getAudioParams() {
203 return mAudioParams;
204 }
205
206 /**
207 * @return Request voice parameters.
208 */
209 public Bundle getVoiceParams() {
210 return mVoiceParams;
211 }
212
213}