เลเยอร์ HAL (สีเขียว) มีโค้ดเฉพาะของผู้ให้บริการซึ่งใช้อินเทอร์เฟซ Sound Trigger HAL (STHAL)
SoundTriggerMiddleware (สีเหลือง) จะอยู่เหนืออินเทอร์เฟซ HAL
โดยจะสื่อสารกับ HAL และมีหน้าที่รับผิดชอบด้านฟังก์ชันการทำงาน เช่น การแชร์ HAL ระหว่างไคลเอ็นต์ต่างๆ การบันทึก การบังคับใช้สิทธิ์ และการจัดการความเข้ากันได้กับ HAL เวอร์ชันเก่า
ฟังก์ชันของสแต็กทริกเกอร์เสียงคือการส่งเหตุการณ์แบบแยกต่างหากซึ่งแสดงถึงเหตุการณ์ทริกเกอร์เสียง ในกรณีส่วนใหญ่ สแต็กทริกเกอร์เสียงจะไม่จัดการกับเสียง เมื่อได้รับเหตุการณ์ทริกเกอร์ แอปจะได้รับสิทธิ์เข้าถึงสตรีมเสียงจริงในช่วงเวลาของเหตุการณ์ดังกล่าวโดยการเปิดออบเจ็กต์ AudioRecord ผ่านเฟรมเวิร์กเสียง Sound Trigger HAL API มีตัวแฮนเดิลที่มีเหตุการณ์ที่ทริกเกอร์ซึ่งใช้กับเฟรมเวิร์กเสียง ดังนั้น HAL ของทริกเกอร์เสียงและ HAL ของเสียงจึงมักจะใช้กระบวนการเดียวกันเนื่องจากเชื่อมต่อกันอยู่เบื้องหลัง
สุดท้าย HAL จะยกเลิกการโหลดโมเดลที่ไม่ใช้งานซึ่งไม่จําเป็นต้องใช้แล้วผ่าน unloadModel()
จัดการข้อผิดพลาด HAL
ใน Android 11 ระบบจะถือว่ารหัสข้อผิดพลาดที่ไม่สำเร็จซึ่งแสดงผลจาก HAL เป็นข้อผิดพลาดในการเขียนโปรแกรม ซึ่งการกู้คืนจากข้อผิดพลาดดังกล่าวจะต้องรีสตาร์ทกระบวนการ HAL นี่เป็นกลยุทธ์การกู้คืนที่ใช้เป็นทางเลือกสุดท้าย และเราหวังว่ากรณีเช่นนี้จะไม่เกิดขึ้นในระบบที่ทำงานได้อย่างถูกต้อง
[[["เข้าใจง่าย","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 UTC"],[],[],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."]]