audio_aaudio.h
Audio in and out on Android, through AAudio.
#include <sonore/audio_aaudio.h>- 2types
- Guides: Platforms and formats
On this page
Design notes
AAudio is the NDK's own low-latency audio API (Android 8, API 26; this SDK's Android floor is 9). It is what Google's Oboe library calls first, and on a device with an MMAP path it hands the app the hardware's buffer directly. The two classes here are the standalone's PlatformAudio and PlatformAudioInput on Android, the same surface WASAPI, ALSA and Core Audio offer on the desktop (scripts/backend-surface.mjs checks it).
What Android does differently:
- There is no device list to choose from. AAudio opens the route the system picked -- the speaker, headphones, a USB interface when one is plugged in -- and follows the user's choice in the system's own UI. listDevices() names that one route.
- The rate is the device's. A stream opened without a rate runs at the hardware's own (48 kHz on nearly every phone), with no resampler in the path, which is the low-latency path; the DSP is prepared at whatever it is.
- Low latency is asked for, and EXCLUSIVE sharing: the MMAP path where the device has one. A device without it gives a shared stream instead, which still works. The buffer is two bursts deep, AAudio's own advice for a stream with no glitches to absorb.
- A stream can be DISCONNECTED -- headphones pulled, a USB interface unplugged, the route changed under it. The error callback says so; the stream may not be closed from inside it, so it is recorded and takeDeviceLost() reports it to the thread that reopens.
- Capture needs the RECORD_AUDIO permission, granted by the user at run time. Without it the input stream does not open, and error() says why.
Types 2
The system output, through an AAudio stream whose data callback runs the standalone's render function on AAudio's own high-priority thread.
The system input -- the microphone, or a plugged-in interface -- through an AAudio capture stream whose callback fills a ring the render reads from.