SoundTrigger语音识别架构是Android5.0才引入,在Android7.0逐步完善。
官方介绍:https://source.android.google.cn/devices/audio/sound-trigger
The Sound Trigger feature provides apps with the ability to listen for certain acoustic events, like hotwords, in a low-power and privacy-sensitive manner. Example use cases of Sound Trigger are Assistant and Now Playing.
应用场景主要有 语音助手(VIA) 或者 唤醒词(HOTWORD),本文主要关注的是右边: 唤醒词方案。至于VIA比较全的说明可参考:https://source.android.google.cn/devices/automotive/voice/voice_interaction_guide
The function of the Sound Trigger stack is to deliver discrete events that represent acoustic, trigger events. In most cases, the Sound Trigger stack does not deal with audio. Upon receipt of the trigger events, apps get access to the actual audio stream, surrounding the time of the events, by opening an AudioRecord object via the Audio framework. The Sound Trigger HAL APIs provide a handle with the triggered event that is used with the Audio Framework. Hence, since the Sound Trigger HAL and Audio HAL are connected under the hood, they typically share a process.
粗体字节部分是关键点,意思就是soundtrigger主要负责的是声学(acoustic)唤醒词的识别,识别后会触发相应的events给到上层。它不负责处理后续音频数据,应用层应该通过AudioRecord的方式来读取后续的音频数据。另外如之前篇介绍:Android Framework源码解读 - Audio - audioserver启动流程(2) - HIDL ,soundtrigger hal层的加载是通过auido hal层加载带入的,因此它们处于同一个进程中,即android.hardware.audio@2.0-service。
官方的sound-trigger的其他说明,看起来也还是不太好理解,不够细节,所以接下来还是继续以源码解读方式来揭开SoundTrigger的面纱。
三大部分
1、SoundTriggerHwService
/frameworks/av/media/audioserver/main_audioserver.cpp 包含 SoundTriggerHwService::instantiate();这部分在之前的文章Android Framework源码解读 - Audio - audioserver启动流程(1),已经有提到,这里再回顾一下:
audioserver进程(main_audioserver.cpp)的main函数里面创建了SoundTriggerHwService, ./frameworks/av/services/soundtrigger/SoundTriggerHwService.cpp
主要工作:1、创建共享内存 new MemoryDealer(1024*1024, “SoundTriggerHwService”) ; 2、对接HAL层connectModule(“primary”);
./frameworks/av/services/soundtrigger/SoundTriggerHalInterface.h, ./frameworks/av/services/soundtrigger/SoundTriggerHalHidl.cpp
SoundTriggerHalHidl类是SoundTriggerHalInterface具体的实现类。