SoundTriggerMiddleware(黄色)位于 HAL 接口上方。它与 HAL 进行通信,并负责执行一些功能(例如在不同客户端之间共享 HAL、日志记录、强制执行权限以及处理与旧版 HAL 的兼容性)。
SoundTriggerService(蓝色)系统位于中间件上方。它有助于与其他系统功能(例如电话和电池事件)集成。此外,它还维护了一个按唯一 ID 编入索引的声音模型数据库。
在 SoundTriggerService 层上方,该堆栈(棕色)会分别处理 Google 助理和通用应用特有的功能。
声音触发器堆栈的功能是传递代表声音触发器事件的独立事件。在大多数情况下,声音触发器堆栈并不处理音频。收到触发器事件后,应用会通过音频框架打开 AudioRecord 对象,从而在事件发生时访问实际音频流。音频触发器 HAL API 会提供与音频框架一起使用的触发事件的句柄。而由于声音触发器 HAL 和音频 HAL 在后台彼此相连,它们通常会共用一个进程。
声音触发器 HAL 接口
声音触发器 HAL (STHAL) 接口是声音触发器堆栈中的供应商专用组件,用于处理启动指令和其他声音的硬件识别。
STHAL 提供一个或多个引擎,每个引擎都运行不同的算法,用于检测特定类型的声音。当 STHAL 检测到触发器时,会向框架发送事件,然后停止检测。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-09。"],[],[],null,["# Sound Trigger\n\nThe Sound Trigger feature provides apps with the ability to listen for certain\nacoustic events, like hotwords, in a low-power and privacy-sensitive manner.\nExample use cases of Sound Trigger are Assistant and Now Playing.\n\nThis page gives an overview of the Sound Trigger architecture and its HAL\n(Hardware Abstraction Layer) interface.\n\nSound Trigger stack\n-------------------\n\nThe Sound Trigger subsystem is built in layers as shown in Figure 1:\n\n**Figure 1:** Sound Trigger stack\n\nThe following list describes each layer shown in Figure 1 in more detail:\n\n- The **HAL layer** (in green) contains the vendor specific code which\n implements the [Sound Trigger HAL](#sthal-interface) (STHAL) interface.\n\n | **Note:** A direct implementation of the STHAL interface is recommended rather than as an adapter around an implementation of a legacy HAL.\n- The **`SoundTriggerMiddleware`** (in yellow) resides above the HAL\n interface. It communicates with the HAL and is responsible for functionalities\n such as sharing the HAL between different clients, logging, enforcing\n permissions and handling compatibility with older HAL versions.\n\n- The **`SoundTriggerService`** (in blue) system resides above the middleware.\n It facilitates integration with other system features, such as telephony and\n battery events. It also maintains a database of sound models, indexed by unique\n IDs.\n\n- Above the `SoundTriggerService` layer, the stack (in brown) handles features\n specific to Assistant and generic apps separately.\n\nThe function of the Sound Trigger stack is to deliver discrete events that\nrepresent acoustic, trigger events. In most cases, the Sound Trigger stack does\nnot deal with audio. Upon receipt of the trigger events, apps get access to the\nactual audio stream, surrounding the time of the events, by opening an\n`AudioRecord` object via the Audio framework. The Sound Trigger HAL APIs provide\na handle with the triggered event that is used with the Audio Framework. Hence,\nsince the Sound Trigger HAL and Audio HAL are connected under the hood, they\ntypically share a process.\n| **Note:** Since models and data types are opaque to the framework in the Sound Trigger subsystem, it is expected that apps and the HAL implementation maintain a \"hidden contract\", that is, the actual contents, format and semantics of those data types are agreed upon. Hence, apps using Sound Trigger are intended to be vendor provided rather than by independent developers.\n\nSound Trigger HAL interface\n---------------------------\n\nThe Sound Trigger HAL (STHAL) interface is the vendor specific part of the Sound\nTrigger stack and it handles hardware recognition of hotwords and other sounds.\nSTHAL provides one or more engines with each one running a different algorithm\ndesigned to detect a specific class of sounds. When STHAL detects a trigger, it\nsends an event to the framework and then stops the detection.\n\nThe STHAL interface is specified under `/hardware/interfaces/soundtrigger/`.\n\nThe `ISoundTriggerHw` interface supports the ability to have one or more\ndetection sessions running at a given time and to listen to acoustic events.\nA call to `ISoundTriggerHw.getProperties()` returns a `Properties` structure\ncontaining implementation description and capabilities.\n\nThe basic flow of setting up a session is explained as follows in Figure 2:\n\n**Figure 2:** STHAL state diagram\n\nThe following steps describe each state in more detail:\n\n1. The HAL client loads a model using `loadSoundModel()` or\n `loadPhraseSoundModel()`. The provided model object indicates which\n implementation-specific detection algorithm (engine) to use, as well as the\n parameters applicable for this algorithm. Upon success, these methods return a\n handle which is used to reference this model in subsequent calls.\n\n2. Once the model has been successfully loaded, the HAL client calls\n `startRecognition()` to begin detection. Recognition continues to run in the\n background until one of the following events occurs:\n\n 1. A `stopRecognition()` has been called on this model.\n 2. A detection has occurred.\n 3. Detection is aborted due to resource constraints, for example, when a higher priority use case has been initiated.\n\n In the latter two cases, a recognition event is sent via the callback\n interface that is registered by the HAL client upon loading. In all cases,\n after any of these events occur, the detection becomes inactive and no more\n recognition callbacks are allowed.\n\n The same model can be started again at a later time, and this process can be\n repeated as many times as needed.\n3. Finally, an inactive model that is no longer needed is unloaded by the HAL\n client via `unloadModel()`.\n\n| **Note:** When a model is started, the HAL client can call `forceRecognitionEvent()` to generate a forced recognition event. The returned event is similar to a normal recognition event, but has its status field set to `FORCED`. Such recognition events do not automatically stop the recognition. Instead, the model state remains running even after the event has been delivered.\n\nHandle HAL errors\n-----------------\n\nTo ensure reliable and consistent behavior between driver implementations, in\nAndroid 11, any non-success error codes returned from\nthe HAL are treated as programming errors, recovery from which requires\nrestarting the HAL process. This is a last-resort recovery strategy and the\nexpectation is that such cases won't occur in a properly working system.\n| **Note:** Although these errors are being called out as valid in the HAL API documentation, there is ambiguity as to when and under what states these error codes are returned and what the expected error recovery procedure is. Hence, Android 11 mandates stricter conformance of Sound Trigger HAL implementations at runtime than the lower versions of Android."]]