1 /*
2  * Copyright (C) 2019 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 package android.media.soundtrigger_middleware;
17 
18 import android.media.soundtrigger_middleware.ModelParameter;
19 import android.media.soundtrigger_middleware.ModelParameterRange;
20 import android.media.soundtrigger_middleware.SoundModel;
21 import android.media.soundtrigger_middleware.PhraseSoundModel;
22 import android.media.soundtrigger_middleware.RecognitionConfig;
23 
24 /**
25  * A sound-trigger module.
26  *
27  * This interface allows a client to operate a sound-trigger device, intended for low-power
28  * detection of various sound patterns, represented by a "sound model".
29  *
30  * Basic operation is to load a sound model (either a generic one or a "phrase" model), then
31  * initiate recognition on this model. A trigger will be delivered asynchronously via a callback
32  * provided by the caller earlier, when attaching to this interface.
33  *
34  * In additon to recognition events, this module will also produce abort events in cases where
35  * recognition has been externally preempted.
36  *
37  * {@hide}
38  */
39 interface ISoundTriggerModule {
40     /**
41      * Load a sound model. Will return a handle to the model on success or will throw a
42      * ServiceSpecificException with one of the {@link Status} error codes upon a recoverable error
43      * (for example, lack of resources of loading a model at the time of call.
44      * Model must eventually be unloaded using {@link #unloadModel(int)} prior to detaching.
45      *
46      * May throw a ServiceSpecificException with an RESOURCE_CONTENTION status to indicate that
47      * resources required for loading the model are currently consumed by other clients.
48      */
loadModel(in SoundModel model)49     int loadModel(in SoundModel model);
50 
51     /**
52      * Load a phrase sound model. Will return a handle to the model on success or will throw a
53      * ServiceSpecificException with one of the {@link Status} error codes upon a recoverable error
54      * (for example, lack of resources of loading a model at the time of call.
55      * Model must eventually be unloaded using unloadModel prior to detaching.
56      *
57      * May throw a ServiceSpecificException with an RESOURCE_CONTENTION status to indicate that
58      * resources required for loading the model are currently consumed by other clients.
59      */
loadPhraseModel(in PhraseSoundModel model)60     int loadPhraseModel(in PhraseSoundModel model);
61 
62     /**
63      * Unload a model, previously loaded with loadModel or loadPhraseModel. After unloading, model
64      * can no longer be used for recognition and the resources occupied by it are released.
65      * Model must not be active at the time of unloading. Cient may call stopRecognition to ensure
66      * that.
67      */
unloadModel(int modelHandle)68     void unloadModel(int modelHandle);
69 
70     /**
71      * Initiate recognition on a previously loaded model.
72      * Recognition event would eventually be delivered via the client-provided callback, typically
73      * supplied during attachment to this interface.
74      *
75      * Once a recognition event is passed to the client, the recognition automatically become
76      * inactive, unless the event is of the RecognitionStatus.FORCED kind. Client can also shut down
77      * the recognition explicitly, via stopRecognition.
78      */
startRecognition(int modelHandle, in RecognitionConfig config)79     void startRecognition(int modelHandle, in RecognitionConfig config);
80 
81     /**
82      * Stop a recognition of a previously active recognition. Will NOT generate a recognition event.
83      * This call is idempotent - calling it on an inactive model has no effect. However, it must
84      * only be used with a loaded model handle.
85      */
stopRecognition(int modelHandle)86     void stopRecognition(int modelHandle);
87 
88     /**
89      * Force generation of a recognition event. Handle must be that of a loaded model. If
90      * recognition is inactive, will do nothing. If recognition is active, will asynchronously
91      * deliever an event with RecognitionStatus.FORCED status and leave recognition in active state.
92      * To avoid any race conditions, once an event signalling the automatic stopping of recognition
93      * is sent, no more forced events will get sent (even if previously requested) until recognition
94      * is explicitly started again.
95      *
96      * Since not all module implementations support this feature, may throw a
97      * ServiceSpecificException with an OPERATION_NOT_SUPPORTED status.
98      */
forceRecognitionEvent(int modelHandle)99     void forceRecognitionEvent(int modelHandle);
100 
101     /**
102      * Set a model specific parameter with the given value. This parameter
103      * will keep its value for the duration the model is loaded regardless of starting and stopping
104      * recognition. Once the model is unloaded, the value will be lost.
105      * It is expected to check if the handle supports the parameter via the
106      * queryModelParameterSupport API prior to calling this method.
107      *
108      * @param modelHandle The sound model handle indicating which model to modify parameters
109      * @param modelParam Parameter to set which will be validated against the
110      *                   ModelParameter type.
111      * @param value The value to set for the given model parameter
112      */
setModelParameter(int modelHandle, ModelParameter modelParam, int value)113     void setModelParameter(int modelHandle, ModelParameter modelParam, int value);
114 
115     /**
116      * Get a model specific parameter. This parameter will keep its value
117      * for the duration the model is loaded regardless of starting and stopping recognition.
118      * Once the model is unloaded, the value will be lost. If the value is not set, a default
119      * value is returned. See ModelParameter for parameter default values.
120      * It is expected to check if the handle supports the parameter via the
121      * queryModelParameterSupport API prior to calling this method.
122      *
123      * @param modelHandle The sound model associated with given modelParam
124      * @param modelParam Parameter to set which will be validated against the
125      *                   ModelParameter type.
126      * @return Value set to the requested parameter.
127      */
getModelParameter(int modelHandle, ModelParameter modelParam)128     int getModelParameter(int modelHandle, ModelParameter modelParam);
129 
130     /**
131      * Determine if parameter control is supported for the given model handle, and its valid value
132      * range if it is.
133      *
134      * @param modelHandle The sound model handle indicating which model to query
135      * @param modelParam Parameter to set which will be validated against the
136      *                   ModelParameter type.
137      * @return If parameter is supported, the return value is its valid range, otherwise null.
138      */
queryModelParameterSupport(int modelHandle, ModelParameter modelParam)139     @nullable ModelParameterRange queryModelParameterSupport(int modelHandle,
140                                                              ModelParameter modelParam);
141 
142     /**
143      * Detach from the module, releasing any active resources.
144      * This will ensure the client callback is no longer called after this call returns.
145      * All models must have been unloaded prior to calling this method.
146      */
detach()147     void detach();
148 }