Voice.java revision ad6df74ada7c478257425b746588f22eeec199a6
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package android.speech.tts; 18 19import android.os.Parcel; 20import android.os.Parcelable; 21 22import java.util.ArrayList; 23import java.util.Collections; 24import java.util.HashSet; 25import java.util.Locale; 26import java.util.Set; 27 28/** 29 * Characteristics and features of a Text-To-Speech Voice. Each TTS Engine can expose 30 * multiple voices for each locale, with different set of features. 31 */ 32public class Voice implements Parcelable { 33 /** Very low, but still intelligible quality of speech synthesis */ 34 public static final int QUALITY_VERY_LOW = 100; 35 36 /** Low, not human-like quality of speech synthesis */ 37 public static final int QUALITY_LOW = 200; 38 39 /** Normal quality of speech synthesis */ 40 public static final int QUALITY_NORMAL = 300; 41 42 /** High, human-like quality of speech synthesis */ 43 public static final int QUALITY_HIGH = 400; 44 45 /** Very high, almost human-indistinguishable quality of speech synthesis */ 46 public static final int QUALITY_VERY_HIGH = 500; 47 48 /** Very low expected synthesizer latency (< 20ms) */ 49 public static final int LATENCY_VERY_LOW = 100; 50 51 /** Low expected synthesizer latency (~20ms) */ 52 public static final int LATENCY_LOW = 200; 53 54 /** Normal expected synthesizer latency (~50ms) */ 55 public static final int LATENCY_NORMAL = 300; 56 57 /** Network based expected synthesizer latency (~200ms) */ 58 public static final int LATENCY_HIGH = 400; 59 60 /** Very slow network based expected synthesizer latency (> 200ms) */ 61 public static final int LATENCY_VERY_HIGH = 500; 62 63 private final String mName; 64 private final Locale mLocale; 65 private final int mQuality; 66 private final int mLatency; 67 private final boolean mRequiresNetworkConnection; 68 private final Set<String> mFeatures; 69 70 public Voice(String name, 71 Locale locale, 72 int quality, 73 int latency, 74 boolean requiresNetworkConnection, 75 Set<String> features) { 76 this.mName = name; 77 this.mLocale = locale; 78 this.mQuality = quality; 79 this.mLatency = latency; 80 this.mRequiresNetworkConnection = requiresNetworkConnection; 81 this.mFeatures = features; 82 } 83 84 private Voice(Parcel in) { 85 this.mName = in.readString(); 86 this.mLocale = (Locale)in.readSerializable(); 87 this.mQuality = in.readInt(); 88 this.mLatency = in.readInt(); 89 this.mRequiresNetworkConnection = (in.readByte() == 1); 90 this.mFeatures = new HashSet<String>(); 91 Collections.addAll(this.mFeatures, in.readStringArray()); 92 } 93 94 /** 95 * @hide 96 */ 97 @Override 98 public void writeToParcel(Parcel dest, int flags) { 99 dest.writeString(mName); 100 dest.writeSerializable(mLocale); 101 dest.writeInt(mQuality); 102 dest.writeInt(mLatency); 103 dest.writeByte((byte) (mRequiresNetworkConnection ? 1 : 0)); 104 dest.writeStringList(new ArrayList<String>(mFeatures)); 105 } 106 107 /** 108 * @hide 109 */ 110 @Override 111 public int describeContents() { 112 return 0; 113 } 114 115 /** 116 * @hide 117 */ 118 public static final Parcelable.Creator<Voice> CREATOR = new Parcelable.Creator<Voice>() { 119 @Override 120 public Voice createFromParcel(Parcel in) { 121 return new Voice(in); 122 } 123 124 @Override 125 public Voice[] newArray(int size) { 126 return new Voice[size]; 127 } 128 }; 129 130 131 /** 132 * @return The voice's locale 133 */ 134 public Locale getLocale() { 135 return mLocale; 136 } 137 138 /** 139 * @return The voice's quality (higher is better) 140 * @see #QUALITY_VERY_HIGH 141 * @see #QUALITY_HIGH 142 * @see #QUALITY_NORMAL 143 * @see #QUALITY_LOW 144 * @see #QUALITY_VERY_LOW 145 */ 146 public int getQuality() { 147 return mQuality; 148 } 149 150 /** 151 * @return The voice's latency (lower is better) 152 * @see #LATENCY_VERY_LOW 153 * @see #LATENCY_LOW 154 * @see #LATENCY_NORMAL 155 * @see #LATENCY_HIGH 156 * @see #LATENCY_VERY_HIGH 157 */ 158 public int getLatency() { 159 return mLatency; 160 } 161 162 /** 163 * @return Does the Voice require a network connection to work. 164 */ 165 public boolean getRequiresNetworkConnection() { 166 return mRequiresNetworkConnection; 167 } 168 169 /** 170 * @return Unique voice name. 171 */ 172 public String getName() { 173 return mName; 174 } 175 176 /** 177 * Returns the set of features it supports for a given voice. 178 * Features can either be framework defined, e.g. 179 * {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_TIMEOUT_MS} or engine specific. 180 * Engine specific keys must be prefixed by the name of the engine they 181 * are intended for. These keys can be used as parameters to 182 * {@link TextToSpeech#speak(String, int, java.util.HashMap)} and 183 * {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}. 184 * 185 * Features values are strings and their values must met restrictions described in their 186 * documentation. 187 * 188 * @return Set instance. May return {@code null} on error. 189 */ 190 public Set<String> getFeatures() { 191 return mFeatures; 192 } 193 194 @Override 195 public String toString() { 196 StringBuilder builder = new StringBuilder(64); 197 return builder.append("Voice[Name: ").append(mName) 198 .append(", locale: ").append(mLocale) 199 .append(", quality: ").append(mQuality) 200 .append(", latency: ").append(mLatency) 201 .append(", requiresNetwork: ").append(mRequiresNetworkConnection) 202 .append(", features: ").append(mFeatures.toString()) 203 .append("]").toString(); 204 } 205 206 @Override 207 public int hashCode() { 208 final int prime = 31; 209 int result = 1; 210 result = prime * result + ((mFeatures == null) ? 0 : mFeatures.hashCode()); 211 result = prime * result + mLatency; 212 result = prime * result + ((mLocale == null) ? 0 : mLocale.hashCode()); 213 result = prime * result + ((mName == null) ? 0 : mName.hashCode()); 214 result = prime * result + mQuality; 215 result = prime * result + (mRequiresNetworkConnection ? 1231 : 1237); 216 return result; 217 } 218 219 @Override 220 public boolean equals(Object obj) { 221 if (this == obj) { 222 return true; 223 } 224 if (obj == null) { 225 return false; 226 } 227 if (getClass() != obj.getClass()) { 228 return false; 229 } 230 Voice other = (Voice) obj; 231 if (mFeatures == null) { 232 if (other.mFeatures != null) { 233 return false; 234 } 235 } else if (!mFeatures.equals(other.mFeatures)) { 236 return false; 237 } 238 if (mLatency != other.mLatency) { 239 return false; 240 } 241 if (mLocale == null) { 242 if (other.mLocale != null) { 243 return false; 244 } 245 } else if (!mLocale.equals(other.mLocale)) { 246 return false; 247 } 248 if (mName == null) { 249 if (other.mName != null) { 250 return false; 251 } 252 } else if (!mName.equals(other.mName)) { 253 return false; 254 } 255 if (mQuality != other.mQuality) { 256 return false; 257 } 258 if (mRequiresNetworkConnection != other.mRequiresNetworkConnection) { 259 return false; 260 } 261 return true; 262 } 263} 264