Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 1 | /** |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.hardware.soundtrigger; |
| 18 | |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 19 | import android.os.Handler; |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.ArrayList; |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 24 | import java.util.Arrays; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 25 | import java.util.UUID; |
| 26 | |
| 27 | /** |
| 28 | * The SoundTrigger class provides access via JNI to the native service managing |
| 29 | * the sound trigger HAL. |
| 30 | * |
| 31 | * @hide |
| 32 | */ |
| 33 | public class SoundTrigger { |
| 34 | |
| 35 | public static final int STATUS_OK = 0; |
| 36 | public static final int STATUS_ERROR = Integer.MIN_VALUE; |
| 37 | public static final int STATUS_PERMISSION_DENIED = -1; |
| 38 | public static final int STATUS_NO_INIT = -19; |
| 39 | public static final int STATUS_BAD_VALUE = -22; |
| 40 | public static final int STATUS_DEAD_OBJECT = -32; |
| 41 | public static final int STATUS_INVALID_OPERATION = -38; |
| 42 | |
| 43 | /***************************************************************************** |
| 44 | * A ModuleProperties describes a given sound trigger hardware module |
| 45 | * managed by the native sound trigger service. Each module has a unique |
| 46 | * ID used to target any API call to this paricular module. Module |
| 47 | * properties are returned by listModules() method. |
| 48 | ****************************************************************************/ |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 49 | public static class ModuleProperties implements Parcelable { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 50 | /** Unique module ID provided by the native service */ |
| 51 | public final int id; |
| 52 | |
| 53 | /** human readable voice detection engine implementor */ |
| 54 | public final String implementor; |
| 55 | |
| 56 | /** human readable voice detection engine description */ |
| 57 | public final String description; |
| 58 | |
| 59 | /** Unique voice engine Id (changes with each version) */ |
| 60 | public final UUID uuid; |
| 61 | |
| 62 | /** Voice detection engine version */ |
| 63 | public final int version; |
| 64 | |
| 65 | /** Maximum number of active sound models */ |
| 66 | public final int maxSoundModels; |
| 67 | |
| 68 | /** Maximum number of key phrases */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 69 | public final int maxKeyphrases; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 70 | |
| 71 | /** Maximum number of users per key phrase */ |
| 72 | public final int maxUsers; |
| 73 | |
| 74 | /** Supported recognition modes (bit field, RECOGNITION_MODE_VOICE_TRIGGER ...) */ |
| 75 | public final int recognitionModes; |
| 76 | |
| 77 | /** Supports seamless transition to capture mode after recognition */ |
| 78 | public final boolean supportsCaptureTransition; |
| 79 | |
| 80 | /** Maximum buffering capacity in ms if supportsCaptureTransition() is true */ |
| 81 | public final int maxBufferMs; |
| 82 | |
| 83 | /** Supports capture by other use cases while detection is active */ |
| 84 | public final boolean supportsConcurrentCapture; |
| 85 | |
| 86 | /** Rated power consumption when detection is active with TDB silence/sound/speech ratio */ |
| 87 | public final int powerConsumptionMw; |
| 88 | |
| 89 | ModuleProperties(int id, String implementor, String description, |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 90 | String uuid, int version, int maxSoundModels, int maxKeyphrases, |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 91 | int maxUsers, int recognitionModes, boolean supportsCaptureTransition, |
| 92 | int maxBufferMs, boolean supportsConcurrentCapture, |
| 93 | int powerConsumptionMw) { |
| 94 | this.id = id; |
| 95 | this.implementor = implementor; |
| 96 | this.description = description; |
| 97 | this.uuid = UUID.fromString(uuid); |
| 98 | this.version = version; |
| 99 | this.maxSoundModels = maxSoundModels; |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 100 | this.maxKeyphrases = maxKeyphrases; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 101 | this.maxUsers = maxUsers; |
| 102 | this.recognitionModes = recognitionModes; |
| 103 | this.supportsCaptureTransition = supportsCaptureTransition; |
| 104 | this.maxBufferMs = maxBufferMs; |
| 105 | this.supportsConcurrentCapture = supportsConcurrentCapture; |
| 106 | this.powerConsumptionMw = powerConsumptionMw; |
| 107 | } |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 108 | |
| 109 | public static final Parcelable.Creator<ModuleProperties> CREATOR |
| 110 | = new Parcelable.Creator<ModuleProperties>() { |
| 111 | public ModuleProperties createFromParcel(Parcel in) { |
| 112 | return ModuleProperties.fromParcel(in); |
| 113 | } |
| 114 | |
| 115 | public ModuleProperties[] newArray(int size) { |
| 116 | return new ModuleProperties[size]; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | private static ModuleProperties fromParcel(Parcel in) { |
| 121 | int id = in.readInt(); |
| 122 | String implementor = in.readString(); |
| 123 | String description = in.readString(); |
| 124 | String uuid = in.readString(); |
| 125 | int version = in.readInt(); |
| 126 | int maxSoundModels = in.readInt(); |
| 127 | int maxKeyphrases = in.readInt(); |
| 128 | int maxUsers = in.readInt(); |
| 129 | int recognitionModes = in.readInt(); |
| 130 | boolean supportsCaptureTransition = in.readByte() == 1; |
| 131 | int maxBufferMs = in.readInt(); |
| 132 | boolean supportsConcurrentCapture = in.readByte() == 1; |
| 133 | int powerConsumptionMw = in.readInt(); |
| 134 | return new ModuleProperties(id, implementor, description, uuid, version, |
| 135 | maxSoundModels, maxKeyphrases, maxUsers, recognitionModes, |
| 136 | supportsCaptureTransition, maxBufferMs, supportsConcurrentCapture, |
| 137 | powerConsumptionMw); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void writeToParcel(Parcel dest, int flags) { |
| 142 | dest.writeInt(id); |
| 143 | dest.writeString(implementor); |
| 144 | dest.writeString(description); |
| 145 | dest.writeString(uuid.toString()); |
| 146 | dest.writeInt(version); |
| 147 | dest.writeInt(maxSoundModels); |
| 148 | dest.writeInt(maxKeyphrases); |
| 149 | dest.writeInt(maxUsers); |
| 150 | dest.writeInt(recognitionModes); |
| 151 | dest.writeByte((byte) (supportsCaptureTransition ? 1 : 0)); |
| 152 | dest.writeInt(maxBufferMs); |
| 153 | dest.writeByte((byte) (supportsConcurrentCapture ? 1 : 0)); |
| 154 | dest.writeInt(powerConsumptionMw); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public int describeContents() { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public String toString() { |
| 164 | return "ModuleProperties [id=" + id + ", implementor=" + implementor + ", description=" |
| 165 | + description + ", uuid=" + uuid + ", version=" + version + ", maxSoundModels=" |
| 166 | + maxSoundModels + ", maxKeyphrases=" + maxKeyphrases + ", maxUsers=" |
| 167 | + maxUsers + ", recognitionModes=" + recognitionModes |
| 168 | + ", supportsCaptureTransition=" + supportsCaptureTransition + ", maxBufferMs=" |
| 169 | + maxBufferMs + ", supportsConcurrentCapture=" + supportsConcurrentCapture |
| 170 | + ", powerConsumptionMw=" + powerConsumptionMw + "]"; |
| 171 | } |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /***************************************************************************** |
| 175 | * A SoundModel describes the attributes and contains the binary data used by the hardware |
| 176 | * implementation to detect a particular sound pattern. |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 177 | * A specialized version {@link KeyphraseSoundModel} is defined for key phrase |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 178 | * sound models. |
| 179 | ****************************************************************************/ |
| 180 | public static class SoundModel { |
| 181 | /** Undefined sound model type */ |
| 182 | public static final int TYPE_UNKNOWN = -1; |
| 183 | |
| 184 | /** Keyphrase sound model */ |
| 185 | public static final int TYPE_KEYPHRASE = 0; |
| 186 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 187 | /** Unique sound model identifier */ |
| 188 | public final UUID uuid; |
| 189 | |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 190 | /** Sound model type (e.g. TYPE_KEYPHRASE); */ |
| 191 | public final int type; |
| 192 | |
| 193 | /** Opaque data. For use by vendor implementation and enrollment application */ |
| 194 | public final byte[] data; |
| 195 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 196 | public SoundModel(UUID uuid, int type, byte[] data) { |
| 197 | this.uuid = uuid; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 198 | this.type = type; |
| 199 | this.data = data; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /***************************************************************************** |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 204 | * A Keyphrase describes a key phrase that can be detected by a |
| 205 | * {@link KeyphraseSoundModel} |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 206 | ****************************************************************************/ |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 207 | public static class Keyphrase implements Parcelable { |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 208 | /** Unique identifier for this keyphrase */ |
| 209 | public final int id; |
| 210 | |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 211 | /** Recognition modes supported for this key phrase in the model */ |
| 212 | public final int recognitionModes; |
| 213 | |
| 214 | /** Locale of the keyphrase. JAVA Locale string e.g en_US */ |
| 215 | public final String locale; |
| 216 | |
| 217 | /** Key phrase text */ |
| 218 | public final String text; |
| 219 | |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 220 | /** Users this key phrase has been trained for. countains sound trigger specific user IDs |
| 221 | * derived from system user IDs {@link android.os.UserHandle#getIdentifier()}. */ |
| 222 | public final int[] users; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 223 | |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 224 | public Keyphrase(int id, int recognitionModes, String locale, String text, int[] users) { |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 225 | this.id = id; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 226 | this.recognitionModes = recognitionModes; |
| 227 | this.locale = locale; |
| 228 | this.text = text; |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 229 | this.users = users; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 230 | } |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 231 | |
| 232 | public static final Parcelable.Creator<Keyphrase> CREATOR |
| 233 | = new Parcelable.Creator<Keyphrase>() { |
| 234 | public Keyphrase createFromParcel(Parcel in) { |
| 235 | return Keyphrase.fromParcel(in); |
| 236 | } |
| 237 | |
| 238 | public Keyphrase[] newArray(int size) { |
| 239 | return new Keyphrase[size]; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | private static Keyphrase fromParcel(Parcel in) { |
| 244 | int id = in.readInt(); |
| 245 | int recognitionModes = in.readInt(); |
| 246 | String locale = in.readString(); |
| 247 | String text = in.readString(); |
| 248 | int[] users = null; |
| 249 | int numUsers = in.readInt(); |
Sandeep Siddhartha | 110f569 | 2014-07-20 12:22:56 -0700 | [diff] [blame] | 250 | if (numUsers >= 0) { |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 251 | users = new int[numUsers]; |
| 252 | in.readIntArray(users); |
| 253 | } |
| 254 | return new Keyphrase(id, recognitionModes, locale, text, users); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void writeToParcel(Parcel dest, int flags) { |
| 259 | dest.writeInt(id); |
| 260 | dest.writeInt(recognitionModes); |
| 261 | dest.writeString(locale); |
| 262 | dest.writeString(text); |
| 263 | if (users != null) { |
| 264 | dest.writeInt(users.length); |
| 265 | dest.writeIntArray(users); |
| 266 | } else { |
Sandeep Siddhartha | 110f569 | 2014-07-20 12:22:56 -0700 | [diff] [blame] | 267 | dest.writeInt(-1); |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public int describeContents() { |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public int hashCode() { |
| 278 | final int prime = 31; |
| 279 | int result = 1; |
| 280 | result = prime * result + ((text == null) ? 0 : text.hashCode()); |
| 281 | result = prime * result + id; |
| 282 | result = prime * result + ((locale == null) ? 0 : locale.hashCode()); |
| 283 | result = prime * result + recognitionModes; |
| 284 | result = prime * result + Arrays.hashCode(users); |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public boolean equals(Object obj) { |
| 290 | if (this == obj) |
| 291 | return true; |
| 292 | if (obj == null) |
| 293 | return false; |
| 294 | if (getClass() != obj.getClass()) |
| 295 | return false; |
| 296 | Keyphrase other = (Keyphrase) obj; |
| 297 | if (text == null) { |
| 298 | if (other.text != null) |
| 299 | return false; |
| 300 | } else if (!text.equals(other.text)) |
| 301 | return false; |
| 302 | if (id != other.id) |
| 303 | return false; |
| 304 | if (locale == null) { |
| 305 | if (other.locale != null) |
| 306 | return false; |
| 307 | } else if (!locale.equals(other.locale)) |
| 308 | return false; |
| 309 | if (recognitionModes != other.recognitionModes) |
| 310 | return false; |
| 311 | if (!Arrays.equals(users, other.users)) |
| 312 | return false; |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public String toString() { |
| 318 | return "Keyphrase [id=" + id + ", recognitionModes=" + recognitionModes + ", locale=" |
| 319 | + locale + ", text=" + text + ", users=" + Arrays.toString(users) + "]"; |
| 320 | } |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /***************************************************************************** |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 324 | * A KeyphraseSoundModel is a specialized {@link SoundModel} for key phrases. |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 325 | * It contains data needed by the hardware to detect a certain number of key phrases |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 326 | * and the list of corresponding {@link Keyphrase} descriptors. |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 327 | ****************************************************************************/ |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 328 | public static class KeyphraseSoundModel extends SoundModel implements Parcelable { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 329 | /** Key phrases in this sound model */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 330 | public final Keyphrase[] keyphrases; // keyword phrases in model |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 331 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 332 | public KeyphraseSoundModel(UUID id, byte[] data, Keyphrase[] keyphrases) { |
| 333 | super(id, TYPE_KEYPHRASE, data); |
| 334 | this.keyphrases = keyphrases; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 335 | } |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 336 | |
| 337 | public static final Parcelable.Creator<KeyphraseSoundModel> CREATOR |
| 338 | = new Parcelable.Creator<KeyphraseSoundModel>() { |
| 339 | public KeyphraseSoundModel createFromParcel(Parcel in) { |
| 340 | return KeyphraseSoundModel.fromParcel(in); |
| 341 | } |
| 342 | |
| 343 | public KeyphraseSoundModel[] newArray(int size) { |
| 344 | return new KeyphraseSoundModel[size]; |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | private static KeyphraseSoundModel fromParcel(Parcel in) { |
| 349 | UUID uuid = UUID.fromString(in.readString()); |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame^] | 350 | byte[] data = in.readBlob(); |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 351 | Keyphrase[] keyphrases = in.createTypedArray(Keyphrase.CREATOR); |
| 352 | return new KeyphraseSoundModel(uuid, data, keyphrases); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public int describeContents() { |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void writeToParcel(Parcel dest, int flags) { |
| 362 | dest.writeString(uuid.toString()); |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame^] | 363 | dest.writeBlob(data); |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 364 | dest.writeTypedArray(keyphrases, 0); |
| 365 | } |
Sandeep Siddhartha | 110f569 | 2014-07-20 12:22:56 -0700 | [diff] [blame] | 366 | |
| 367 | @Override |
| 368 | public String toString() { |
| 369 | return "KeyphraseSoundModel [keyphrases=" + Arrays.toString(keyphrases) + ", uuid=" |
| 370 | + uuid + ", type=" + type + ", data? " + (data != null) + "]"; |
| 371 | } |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Modes for key phrase recognition |
| 376 | */ |
| 377 | /** Simple recognition of the key phrase */ |
| 378 | public static final int RECOGNITION_MODE_VOICE_TRIGGER = 0x1; |
| 379 | /** Trigger only if one user is identified */ |
| 380 | public static final int RECOGNITION_MODE_USER_IDENTIFICATION = 0x2; |
| 381 | /** Trigger only if one user is authenticated */ |
| 382 | public static final int RECOGNITION_MODE_USER_AUTHENTICATION = 0x4; |
| 383 | |
| 384 | /** |
| 385 | * Status codes for {@link RecognitionEvent} |
| 386 | */ |
| 387 | /** Recognition success */ |
| 388 | public static final int RECOGNITION_STATUS_SUCCESS = 0; |
| 389 | /** Recognition aborted (e.g. capture preempted by anotehr use case */ |
| 390 | public static final int RECOGNITION_STATUS_ABORT = 1; |
| 391 | /** Recognition failure */ |
| 392 | public static final int RECOGNITION_STATUS_FAILURE = 2; |
| 393 | |
| 394 | /** |
| 395 | * A RecognitionEvent is provided by the |
| 396 | * {@link StatusListener#onRecognition(RecognitionEvent)} |
| 397 | * callback upon recognition success or failure. |
| 398 | */ |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame^] | 399 | public static class RecognitionEvent implements Parcelable { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 400 | /** Recognition status e.g {@link #RECOGNITION_STATUS_SUCCESS} */ |
| 401 | public final int status; |
| 402 | /** Sound Model corresponding to this event callback */ |
| 403 | public final int soundModelHandle; |
| 404 | /** True if it is possible to capture audio from this utterance buffered by the hardware */ |
| 405 | public final boolean captureAvailable; |
| 406 | /** Audio session ID to be used when capturing the utterance with an AudioRecord |
| 407 | * if captureAvailable() is true. */ |
| 408 | public final int captureSession; |
| 409 | /** Delay in ms between end of model detection and start of audio available for capture. |
| 410 | * A negative value is possible (e.g. if keyphrase is also available for capture) */ |
| 411 | public final int captureDelayMs; |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 412 | /** Duration in ms of audio captured before the start of the trigger. 0 if none. */ |
| 413 | public final int capturePreambleMs; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 414 | /** Opaque data for use by system applications who know about voice engine internals, |
| 415 | * typically during enrollment. */ |
| 416 | public final byte[] data; |
| 417 | |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame^] | 418 | public RecognitionEvent(int status, int soundModelHandle, boolean captureAvailable, |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 419 | int captureSession, int captureDelayMs, int capturePreambleMs, byte[] data) { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 420 | this.status = status; |
| 421 | this.soundModelHandle = soundModelHandle; |
| 422 | this.captureAvailable = captureAvailable; |
| 423 | this.captureSession = captureSession; |
| 424 | this.captureDelayMs = captureDelayMs; |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 425 | this.capturePreambleMs = capturePreambleMs; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 426 | this.data = data; |
| 427 | } |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame^] | 428 | |
| 429 | public static final Parcelable.Creator<RecognitionEvent> CREATOR |
| 430 | = new Parcelable.Creator<RecognitionEvent>() { |
| 431 | public RecognitionEvent createFromParcel(Parcel in) { |
| 432 | return RecognitionEvent.fromParcel(in); |
| 433 | } |
| 434 | |
| 435 | public RecognitionEvent[] newArray(int size) { |
| 436 | return new RecognitionEvent[size]; |
| 437 | } |
| 438 | }; |
| 439 | |
| 440 | private static RecognitionEvent fromParcel(Parcel in) { |
| 441 | int status = in.readInt(); |
| 442 | int soundModelHandle = in.readInt(); |
| 443 | boolean captureAvailable = in.readByte() == 1; |
| 444 | int captureSession = in.readInt(); |
| 445 | int captureDelayMs = in.readInt(); |
| 446 | int capturePreambleMs = in.readInt(); |
| 447 | byte[] data = in.readBlob(); |
| 448 | return new RecognitionEvent(status, soundModelHandle, captureAvailable, captureSession, |
| 449 | captureDelayMs, capturePreambleMs, data); |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public int describeContents() { |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public void writeToParcel(Parcel dest, int flags) { |
| 459 | dest.writeInt(status); |
| 460 | dest.writeInt(soundModelHandle); |
| 461 | dest.writeByte((byte) (captureAvailable ? 1 : 0)); |
| 462 | dest.writeInt(captureSession); |
| 463 | dest.writeInt(captureDelayMs); |
| 464 | dest.writeInt(capturePreambleMs); |
| 465 | dest.writeBlob(data); |
| 466 | } |
| 467 | |
| 468 | @Override |
| 469 | public int hashCode() { |
| 470 | final int prime = 31; |
| 471 | int result = 1; |
| 472 | result = prime * result + (captureAvailable ? 1231 : 1237); |
| 473 | result = prime * result + captureDelayMs; |
| 474 | result = prime * result + capturePreambleMs; |
| 475 | result = prime * result + captureSession; |
| 476 | result = prime * result + Arrays.hashCode(data); |
| 477 | result = prime * result + soundModelHandle; |
| 478 | result = prime * result + status; |
| 479 | return result; |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public boolean equals(Object obj) { |
| 484 | if (this == obj) |
| 485 | return true; |
| 486 | if (obj == null) |
| 487 | return false; |
| 488 | if (getClass() != obj.getClass()) |
| 489 | return false; |
| 490 | RecognitionEvent other = (RecognitionEvent) obj; |
| 491 | if (captureAvailable != other.captureAvailable) |
| 492 | return false; |
| 493 | if (captureDelayMs != other.captureDelayMs) |
| 494 | return false; |
| 495 | if (capturePreambleMs != other.capturePreambleMs) |
| 496 | return false; |
| 497 | if (captureSession != other.captureSession) |
| 498 | return false; |
| 499 | if (!Arrays.equals(data, other.data)) |
| 500 | return false; |
| 501 | if (soundModelHandle != other.soundModelHandle) |
| 502 | return false; |
| 503 | if (status != other.status) |
| 504 | return false; |
| 505 | return true; |
| 506 | } |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /** |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 510 | * A RecognitionConfig is provided to |
| 511 | * {@link SoundTriggerModule#startRecognition(int, RecognitionConfig)} to configure the |
| 512 | * recognition request. |
| 513 | */ |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 514 | public static class RecognitionConfig implements Parcelable { |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 515 | /** True if the DSP should capture the trigger sound and make it available for further |
| 516 | * capture. */ |
| 517 | public final boolean captureRequested; |
| 518 | /** List of all keyphrases in the sound model for which recognition should be performed with |
| 519 | * options for each keyphrase. */ |
| 520 | public final KeyphraseRecognitionExtra keyphrases[]; |
| 521 | /** Opaque data for use by system applications who know about voice engine internals, |
| 522 | * typically during enrollment. */ |
| 523 | public final byte[] data; |
| 524 | |
| 525 | public RecognitionConfig(boolean captureRequested, |
| 526 | KeyphraseRecognitionExtra keyphrases[], byte[] data) { |
| 527 | this.captureRequested = captureRequested; |
| 528 | this.keyphrases = keyphrases; |
| 529 | this.data = data; |
| 530 | } |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 531 | |
| 532 | public static final Parcelable.Creator<RecognitionConfig> CREATOR |
| 533 | = new Parcelable.Creator<RecognitionConfig>() { |
| 534 | public RecognitionConfig createFromParcel(Parcel in) { |
| 535 | return RecognitionConfig.fromParcel(in); |
| 536 | } |
| 537 | |
| 538 | public RecognitionConfig[] newArray(int size) { |
| 539 | return new RecognitionConfig[size]; |
| 540 | } |
| 541 | }; |
| 542 | |
| 543 | private static RecognitionConfig fromParcel(Parcel in) { |
| 544 | boolean captureRequested = in.readByte() == 1; |
| 545 | KeyphraseRecognitionExtra[] keyphrases = |
| 546 | in.createTypedArray(KeyphraseRecognitionExtra.CREATOR); |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame^] | 547 | byte[] data = in.readBlob(); |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 548 | return new RecognitionConfig(captureRequested, keyphrases, data); |
| 549 | } |
| 550 | |
| 551 | @Override |
| 552 | public void writeToParcel(Parcel dest, int flags) { |
| 553 | dest.writeByte((byte) (captureRequested ? 1 : 0)); |
| 554 | dest.writeTypedArray(keyphrases, 0); |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame^] | 555 | dest.writeBlob(data); |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public int describeContents() { |
| 560 | return 0; |
| 561 | } |
Sandeep Siddhartha | 110f569 | 2014-07-20 12:22:56 -0700 | [diff] [blame] | 562 | |
| 563 | @Override |
| 564 | public String toString() { |
| 565 | return "RecognitionConfig [captureRequested=" + captureRequested + ", keyphrases=" |
| 566 | + Arrays.toString(keyphrases) + ", data? " + (data != null) + "]"; |
| 567 | } |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Confidence level for users defined in a keyphrase. |
| 572 | * - The confidence level is expressed in percent (0% -100%). |
| 573 | * When used in a {@link KeyphraseRecognitionEvent} it indicates the detected confidence level |
| 574 | * When used in a {@link RecognitionConfig} it indicates the minimum confidence level that |
| 575 | * should trigger a recognition. |
| 576 | * - The user ID is derived from the system ID {@link android.os.UserHandle#getIdentifier()}. |
| 577 | */ |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 578 | public static class ConfidenceLevel implements Parcelable { |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 579 | public final int userId; |
| 580 | public final int confidenceLevel; |
| 581 | |
| 582 | public ConfidenceLevel(int userId, int confidenceLevel) { |
| 583 | this.userId = userId; |
| 584 | this.confidenceLevel = confidenceLevel; |
| 585 | } |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 586 | |
| 587 | public static final Parcelable.Creator<ConfidenceLevel> CREATOR |
| 588 | = new Parcelable.Creator<ConfidenceLevel>() { |
| 589 | public ConfidenceLevel createFromParcel(Parcel in) { |
| 590 | return ConfidenceLevel.fromParcel(in); |
| 591 | } |
| 592 | |
| 593 | public ConfidenceLevel[] newArray(int size) { |
| 594 | return new ConfidenceLevel[size]; |
| 595 | } |
| 596 | }; |
| 597 | |
| 598 | private static ConfidenceLevel fromParcel(Parcel in) { |
| 599 | int userId = in.readInt(); |
| 600 | int confidenceLevel = in.readInt(); |
| 601 | return new ConfidenceLevel(userId, confidenceLevel); |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void writeToParcel(Parcel dest, int flags) { |
| 606 | dest.writeInt(userId); |
| 607 | dest.writeInt(confidenceLevel); |
| 608 | } |
| 609 | |
| 610 | @Override |
| 611 | public int describeContents() { |
| 612 | return 0; |
| 613 | } |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 617 | * Additional data conveyed by a {@link KeyphraseRecognitionEvent} |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 618 | * for a key phrase detection. |
| 619 | */ |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 620 | public static class KeyphraseRecognitionExtra implements Parcelable { |
| 621 | /** The keyphrase ID */ |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 622 | public final int id; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 623 | |
| 624 | /** Recognition modes matched for this event */ |
| 625 | public final int recognitionModes; |
| 626 | |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 627 | /** Confidence levels for all users recognized (KeyphraseRecognitionEvent) or to |
| 628 | * be recognized (RecognitionConfig) */ |
| 629 | public final ConfidenceLevel[] confidenceLevels; |
| 630 | |
| 631 | public KeyphraseRecognitionExtra(int id, int recognitionModes, |
| 632 | ConfidenceLevel[] confidenceLevels) { |
| 633 | this.id = id; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 634 | this.recognitionModes = recognitionModes; |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 635 | this.confidenceLevels = confidenceLevels; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 636 | } |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 637 | |
| 638 | public static final Parcelable.Creator<KeyphraseRecognitionExtra> CREATOR |
| 639 | = new Parcelable.Creator<KeyphraseRecognitionExtra>() { |
| 640 | public KeyphraseRecognitionExtra createFromParcel(Parcel in) { |
| 641 | return KeyphraseRecognitionExtra.fromParcel(in); |
| 642 | } |
| 643 | |
| 644 | public KeyphraseRecognitionExtra[] newArray(int size) { |
| 645 | return new KeyphraseRecognitionExtra[size]; |
| 646 | } |
| 647 | }; |
| 648 | |
| 649 | private static KeyphraseRecognitionExtra fromParcel(Parcel in) { |
| 650 | int id = in.readInt(); |
| 651 | int recognitionModes = in.readInt(); |
| 652 | ConfidenceLevel[] confidenceLevels = in.createTypedArray(ConfidenceLevel.CREATOR); |
| 653 | return new KeyphraseRecognitionExtra(id, recognitionModes, confidenceLevels); |
| 654 | } |
| 655 | |
| 656 | @Override |
| 657 | public void writeToParcel(Parcel dest, int flags) { |
| 658 | dest.writeInt(id); |
| 659 | dest.writeInt(recognitionModes); |
| 660 | dest.writeTypedArray(confidenceLevels, 0); |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public int describeContents() { |
| 665 | return 0; |
| 666 | } |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Specialized {@link RecognitionEvent} for a key phrase detection. |
| 671 | */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 672 | public static class KeyphraseRecognitionEvent extends RecognitionEvent { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 673 | /** Indicates if the key phrase is present in the buffered audio available for capture */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 674 | public final KeyphraseRecognitionExtra[] keyphraseExtras; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 675 | |
| 676 | /** Additional data available for each recognized key phrases in the model */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 677 | public final boolean keyphraseInCapture; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 678 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 679 | KeyphraseRecognitionEvent(int status, int soundModelHandle, boolean captureAvailable, |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 680 | int captureSession, int captureDelayMs, int capturePreambleMs, byte[] data, |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 681 | boolean keyphraseInCapture, KeyphraseRecognitionExtra[] keyphraseExtras) { |
Eric Laurent | 013f66b | 2014-07-06 16:35:00 -0700 | [diff] [blame] | 682 | super(status, soundModelHandle, captureAvailable, captureSession, captureDelayMs, |
| 683 | capturePreambleMs, data); |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame] | 684 | this.keyphraseInCapture = keyphraseInCapture; |
| 685 | this.keyphraseExtras = keyphraseExtras; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Returns a list of descriptors for all harware modules loaded. |
| 691 | * @param modules A ModuleProperties array where the list will be returned. |
| 692 | * @return - {@link #STATUS_OK} in case of success |
| 693 | * - {@link #STATUS_ERROR} in case of unspecified error |
| 694 | * - {@link #STATUS_PERMISSION_DENIED} if the caller does not have system permission |
| 695 | * - {@link #STATUS_NO_INIT} if the native service cannot be reached |
| 696 | * - {@link #STATUS_BAD_VALUE} if modules is null |
| 697 | * - {@link #STATUS_DEAD_OBJECT} if the binder transaction to the native service fails |
| 698 | */ |
| 699 | public static native int listModules(ArrayList <ModuleProperties> modules); |
| 700 | |
| 701 | /** |
| 702 | * Get an interface on a hardware module to control sound models and recognition on |
| 703 | * this module. |
| 704 | * @param moduleId Sound module system identifier {@link ModuleProperties#id}. mandatory. |
| 705 | * @param listener {@link StatusListener} interface. Mandatory. |
| 706 | * @param handler the Handler that will receive the callabcks. Can be null if default handler |
| 707 | * is OK. |
| 708 | * @return a valid sound module in case of success or null in case of error. |
| 709 | */ |
| 710 | public static SoundTriggerModule attachModule(int moduleId, |
| 711 | StatusListener listener, |
| 712 | Handler handler) { |
| 713 | if (listener == null) { |
| 714 | return null; |
| 715 | } |
| 716 | SoundTriggerModule module = new SoundTriggerModule(moduleId, listener, handler); |
| 717 | return module; |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Interface provided by the client application when attaching to a {@link SoundTriggerModule} |
| 722 | * to received recognition and error notifications. |
| 723 | */ |
| 724 | public static interface StatusListener { |
| 725 | /** |
| 726 | * Called when recognition succeeds of fails |
| 727 | */ |
| 728 | public abstract void onRecognition(RecognitionEvent event); |
| 729 | |
| 730 | /** |
| 731 | * Called when the sound trigger native service dies |
| 732 | */ |
| 733 | public abstract void onServiceDied(); |
| 734 | } |
| 735 | } |