SoundTriggerMiddleware (以黃色顯示) 位於 HAL 介面上方。它會與 HAL 進行通訊,並負責執行各種功能,例如在不同用戶端之間共用 HAL、記錄、強制執行權限,以及處理與舊版 HAL 的相容性。
SoundTriggerService (以藍色表示) 系統位於中介軟體之上。這有助於整合其他系統功能,例如電話和電池事件。它也會維護音效模型資料庫,並以獨特 ID 建立索引。
在 SoundTriggerService 層之上,堆疊 (以棕色表示) 會分別處理 Google 助理和一般應用程式的特定功能。
聲響觸發事件堆疊的功能是提供代表聲響觸發事件的個別事件。在大多數情況下,音訊觸發堆疊不會處理音訊。收到觸發事件後,應用程式會透過 Audio 架構開啟 AudioRecord 物件,取得事件發生前後的實際音訊串流。Sound Trigger HAL API 會提供一個句柄,其中包含與音訊架構搭配使用的觸發事件。因此,由於 Sound Trigger HAL 和 Audio 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"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],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."]]