Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 1 | /* |
| 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; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.UUID; |
| 23 | |
| 24 | /** |
| 25 | * The SoundTrigger class provides access via JNI to the native service managing |
| 26 | * the sound trigger HAL. |
| 27 | * |
| 28 | * @hide |
| 29 | */ |
| 30 | public class SoundTrigger { |
| 31 | |
| 32 | public static final int STATUS_OK = 0; |
| 33 | public static final int STATUS_ERROR = Integer.MIN_VALUE; |
| 34 | public static final int STATUS_PERMISSION_DENIED = -1; |
| 35 | public static final int STATUS_NO_INIT = -19; |
| 36 | public static final int STATUS_BAD_VALUE = -22; |
| 37 | public static final int STATUS_DEAD_OBJECT = -32; |
| 38 | public static final int STATUS_INVALID_OPERATION = -38; |
| 39 | |
| 40 | /***************************************************************************** |
| 41 | * A ModuleProperties describes a given sound trigger hardware module |
| 42 | * managed by the native sound trigger service. Each module has a unique |
| 43 | * ID used to target any API call to this paricular module. Module |
| 44 | * properties are returned by listModules() method. |
| 45 | ****************************************************************************/ |
| 46 | public static class ModuleProperties { |
| 47 | /** Unique module ID provided by the native service */ |
| 48 | public final int id; |
| 49 | |
| 50 | /** human readable voice detection engine implementor */ |
| 51 | public final String implementor; |
| 52 | |
| 53 | /** human readable voice detection engine description */ |
| 54 | public final String description; |
| 55 | |
| 56 | /** Unique voice engine Id (changes with each version) */ |
| 57 | public final UUID uuid; |
| 58 | |
| 59 | /** Voice detection engine version */ |
| 60 | public final int version; |
| 61 | |
| 62 | /** Maximum number of active sound models */ |
| 63 | public final int maxSoundModels; |
| 64 | |
| 65 | /** Maximum number of key phrases */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 66 | public final int maxKeyphrases; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 67 | |
| 68 | /** Maximum number of users per key phrase */ |
| 69 | public final int maxUsers; |
| 70 | |
| 71 | /** Supported recognition modes (bit field, RECOGNITION_MODE_VOICE_TRIGGER ...) */ |
| 72 | public final int recognitionModes; |
| 73 | |
| 74 | /** Supports seamless transition to capture mode after recognition */ |
| 75 | public final boolean supportsCaptureTransition; |
| 76 | |
| 77 | /** Maximum buffering capacity in ms if supportsCaptureTransition() is true */ |
| 78 | public final int maxBufferMs; |
| 79 | |
| 80 | /** Supports capture by other use cases while detection is active */ |
| 81 | public final boolean supportsConcurrentCapture; |
| 82 | |
| 83 | /** Rated power consumption when detection is active with TDB silence/sound/speech ratio */ |
| 84 | public final int powerConsumptionMw; |
| 85 | |
| 86 | ModuleProperties(int id, String implementor, String description, |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 87 | String uuid, int version, int maxSoundModels, int maxKeyphrases, |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 88 | int maxUsers, int recognitionModes, boolean supportsCaptureTransition, |
| 89 | int maxBufferMs, boolean supportsConcurrentCapture, |
| 90 | int powerConsumptionMw) { |
| 91 | this.id = id; |
| 92 | this.implementor = implementor; |
| 93 | this.description = description; |
| 94 | this.uuid = UUID.fromString(uuid); |
| 95 | this.version = version; |
| 96 | this.maxSoundModels = maxSoundModels; |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 97 | this.maxKeyphrases = maxKeyphrases; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 98 | this.maxUsers = maxUsers; |
| 99 | this.recognitionModes = recognitionModes; |
| 100 | this.supportsCaptureTransition = supportsCaptureTransition; |
| 101 | this.maxBufferMs = maxBufferMs; |
| 102 | this.supportsConcurrentCapture = supportsConcurrentCapture; |
| 103 | this.powerConsumptionMw = powerConsumptionMw; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /***************************************************************************** |
| 108 | * A SoundModel describes the attributes and contains the binary data used by the hardware |
| 109 | * implementation to detect a particular sound pattern. |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 110 | * A specialized version {@link KeyphraseSoundModel} is defined for key phrase |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 111 | * sound models. |
| 112 | ****************************************************************************/ |
| 113 | public static class SoundModel { |
| 114 | /** Undefined sound model type */ |
| 115 | public static final int TYPE_UNKNOWN = -1; |
| 116 | |
| 117 | /** Keyphrase sound model */ |
| 118 | public static final int TYPE_KEYPHRASE = 0; |
| 119 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 120 | /** Unique sound model identifier */ |
| 121 | public final UUID uuid; |
| 122 | |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 123 | /** Sound model type (e.g. TYPE_KEYPHRASE); */ |
| 124 | public final int type; |
| 125 | |
| 126 | /** Opaque data. For use by vendor implementation and enrollment application */ |
| 127 | public final byte[] data; |
| 128 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 129 | public SoundModel(UUID uuid, int type, byte[] data) { |
| 130 | this.uuid = uuid; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 131 | this.type = type; |
| 132 | this.data = data; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /***************************************************************************** |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 137 | * A Keyphrase describes a key phrase that can be detected by a |
| 138 | * {@link KeyphraseSoundModel} |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 139 | ****************************************************************************/ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 140 | public static class Keyphrase { |
| 141 | /** Unique identifier for this keyphrase */ |
| 142 | public final int id; |
| 143 | |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 144 | /** Recognition modes supported for this key phrase in the model */ |
| 145 | public final int recognitionModes; |
| 146 | |
| 147 | /** Locale of the keyphrase. JAVA Locale string e.g en_US */ |
| 148 | public final String locale; |
| 149 | |
| 150 | /** Key phrase text */ |
| 151 | public final String text; |
| 152 | |
| 153 | /** Number of users this key phrase has been trained for */ |
| 154 | public final int numUsers; |
| 155 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 156 | public Keyphrase(int id, int recognitionModes, String locale, String text, int numUsers) { |
| 157 | this.id = id; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 158 | this.recognitionModes = recognitionModes; |
| 159 | this.locale = locale; |
| 160 | this.text = text; |
| 161 | this.numUsers = numUsers; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /***************************************************************************** |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 166 | * A KeyphraseSoundModel is a specialized {@link SoundModel} for key phrases. |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 167 | * 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^] | 168 | * and the list of corresponding {@link Keyphrase} descriptors. |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 169 | ****************************************************************************/ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 170 | public static class KeyphraseSoundModel extends SoundModel { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 171 | /** Key phrases in this sound model */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 172 | public final Keyphrase[] keyphrases; // keyword phrases in model |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 173 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 174 | public KeyphraseSoundModel(UUID id, byte[] data, Keyphrase[] keyphrases) { |
| 175 | super(id, TYPE_KEYPHRASE, data); |
| 176 | this.keyphrases = keyphrases; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Modes for key phrase recognition |
| 182 | */ |
| 183 | /** Simple recognition of the key phrase */ |
| 184 | public static final int RECOGNITION_MODE_VOICE_TRIGGER = 0x1; |
| 185 | /** Trigger only if one user is identified */ |
| 186 | public static final int RECOGNITION_MODE_USER_IDENTIFICATION = 0x2; |
| 187 | /** Trigger only if one user is authenticated */ |
| 188 | public static final int RECOGNITION_MODE_USER_AUTHENTICATION = 0x4; |
| 189 | |
| 190 | /** |
| 191 | * Status codes for {@link RecognitionEvent} |
| 192 | */ |
| 193 | /** Recognition success */ |
| 194 | public static final int RECOGNITION_STATUS_SUCCESS = 0; |
| 195 | /** Recognition aborted (e.g. capture preempted by anotehr use case */ |
| 196 | public static final int RECOGNITION_STATUS_ABORT = 1; |
| 197 | /** Recognition failure */ |
| 198 | public static final int RECOGNITION_STATUS_FAILURE = 2; |
| 199 | |
| 200 | /** |
| 201 | * A RecognitionEvent is provided by the |
| 202 | * {@link StatusListener#onRecognition(RecognitionEvent)} |
| 203 | * callback upon recognition success or failure. |
| 204 | */ |
| 205 | public static class RecognitionEvent { |
| 206 | /** Recognition status e.g {@link #RECOGNITION_STATUS_SUCCESS} */ |
| 207 | public final int status; |
| 208 | /** Sound Model corresponding to this event callback */ |
| 209 | public final int soundModelHandle; |
| 210 | /** True if it is possible to capture audio from this utterance buffered by the hardware */ |
| 211 | public final boolean captureAvailable; |
| 212 | /** Audio session ID to be used when capturing the utterance with an AudioRecord |
| 213 | * if captureAvailable() is true. */ |
| 214 | public final int captureSession; |
| 215 | /** Delay in ms between end of model detection and start of audio available for capture. |
| 216 | * A negative value is possible (e.g. if keyphrase is also available for capture) */ |
| 217 | public final int captureDelayMs; |
| 218 | /** Opaque data for use by system applications who know about voice engine internals, |
| 219 | * typically during enrollment. */ |
| 220 | public final byte[] data; |
| 221 | |
| 222 | RecognitionEvent(int status, int soundModelHandle, boolean captureAvailable, |
| 223 | int captureSession, int captureDelayMs, byte[] data) { |
| 224 | this.status = status; |
| 225 | this.soundModelHandle = soundModelHandle; |
| 226 | this.captureAvailable = captureAvailable; |
| 227 | this.captureSession = captureSession; |
| 228 | this.captureDelayMs = captureDelayMs; |
| 229 | this.data = data; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 234 | * Additional data conveyed by a {@link KeyphraseRecognitionEvent} |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 235 | * for a key phrase detection. |
| 236 | */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 237 | public static class KeyphraseRecognitionExtra { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 238 | /** Confidence level for each user defined in the key phrase in the same order as |
| 239 | * users in the key phrase. The confidence level is expressed in percentage (0% -100%) */ |
| 240 | public final int[] confidenceLevels; |
| 241 | |
| 242 | /** Recognition modes matched for this event */ |
| 243 | public final int recognitionModes; |
| 244 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 245 | KeyphraseRecognitionExtra(int[] confidenceLevels, int recognitionModes) { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 246 | this.confidenceLevels = confidenceLevels; |
| 247 | this.recognitionModes = recognitionModes; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Specialized {@link RecognitionEvent} for a key phrase detection. |
| 253 | */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 254 | public static class KeyphraseRecognitionEvent extends RecognitionEvent { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 255 | /** 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^] | 256 | public final KeyphraseRecognitionExtra[] keyphraseExtras; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 257 | |
| 258 | /** Additional data available for each recognized key phrases in the model */ |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 259 | public final boolean keyphraseInCapture; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 260 | |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 261 | KeyphraseRecognitionEvent(int status, int soundModelHandle, boolean captureAvailable, |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 262 | int captureSession, int captureDelayMs, byte[] data, |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 263 | boolean keyphraseInCapture, KeyphraseRecognitionExtra[] keyphraseExtras) { |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 264 | super(status, soundModelHandle, captureAvailable, captureSession, captureDelayMs, data); |
Sandeep Siddhartha | d4233c6 | 2014-06-12 18:31:19 -0700 | [diff] [blame^] | 265 | this.keyphraseInCapture = keyphraseInCapture; |
| 266 | this.keyphraseExtras = keyphraseExtras; |
Eric Laurent | e48188c | 2014-04-18 17:44:11 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Returns a list of descriptors for all harware modules loaded. |
| 272 | * @param modules A ModuleProperties array where the list will be returned. |
| 273 | * @return - {@link #STATUS_OK} in case of success |
| 274 | * - {@link #STATUS_ERROR} in case of unspecified error |
| 275 | * - {@link #STATUS_PERMISSION_DENIED} if the caller does not have system permission |
| 276 | * - {@link #STATUS_NO_INIT} if the native service cannot be reached |
| 277 | * - {@link #STATUS_BAD_VALUE} if modules is null |
| 278 | * - {@link #STATUS_DEAD_OBJECT} if the binder transaction to the native service fails |
| 279 | */ |
| 280 | public static native int listModules(ArrayList <ModuleProperties> modules); |
| 281 | |
| 282 | /** |
| 283 | * Get an interface on a hardware module to control sound models and recognition on |
| 284 | * this module. |
| 285 | * @param moduleId Sound module system identifier {@link ModuleProperties#id}. mandatory. |
| 286 | * @param listener {@link StatusListener} interface. Mandatory. |
| 287 | * @param handler the Handler that will receive the callabcks. Can be null if default handler |
| 288 | * is OK. |
| 289 | * @return a valid sound module in case of success or null in case of error. |
| 290 | */ |
| 291 | public static SoundTriggerModule attachModule(int moduleId, |
| 292 | StatusListener listener, |
| 293 | Handler handler) { |
| 294 | if (listener == null) { |
| 295 | return null; |
| 296 | } |
| 297 | SoundTriggerModule module = new SoundTriggerModule(moduleId, listener, handler); |
| 298 | return module; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Interface provided by the client application when attaching to a {@link SoundTriggerModule} |
| 303 | * to received recognition and error notifications. |
| 304 | */ |
| 305 | public static interface StatusListener { |
| 306 | /** |
| 307 | * Called when recognition succeeds of fails |
| 308 | */ |
| 309 | public abstract void onRecognition(RecognitionEvent event); |
| 310 | |
| 311 | /** |
| 312 | * Called when the sound trigger native service dies |
| 313 | */ |
| 314 | public abstract void onServiceDied(); |
| 315 | } |
| 316 | } |