<alp/audio.h> — PDM In + I²S Out
Block-based microphone capture and speaker playback. Both directions share one config struct, alp_audio_config_t.
Header
#include <alp/audio.h>
Capture (PDM microphone)
alp_audio_in_t *mic = alp_audio_in_open(&(alp_audio_config_t){
.peripheral_id = ALP_E1M_PDM0,
.sample_rate_hz = 16000,
.channels = 1,
.format = ALP_AUDIO_FMT_S16_LE,
.frames_per_block = 256,
});
alp_audio_in_start(mic);
int16_t samples[256];
size_t got = 0;
alp_audio_in_read(mic, samples, 256, &got, /* timeout_ms */ 100);
alp_audio_in_stop(mic);
alp_audio_in_close(mic);
buf holds interleaved frames and must be at least frames × channels × sample size bytes.
Samples delivered right after alp_audio_in_start() — or after a restart following a stop or error — can contain a settling transient from the capture path's decimation and filtering. Treat at least the first block as non-representative.
Read errors
alp_audio_in_read() returns ALP_OK, ALP_ERR_NOT_READY, ALP_ERR_INVAL, ALP_ERR_TIMEOUT or — since v0.17.0 — ALP_ERR_IO:
ALP_ERR_TIMEOUTonly means no data arrived withintimeout_ms(including atimeout_ms = 0poll that found nothing queued). It does not by itself indicate loss.ALP_ERR_IOmeans the backend reported dropped data mid-session (e.g. a slab, queue or hardware-FIFO overflow in the Zephyr PDM driver). It is sticky: every read returns it until you stop and restart the stream.
Software resampling (Zephyr PDM)
New in v0.17.0. If the microphone cannot be clocked for the requested sample_rate_hz — for example 8 or 16 kHz on MEMS mics whose minimum PDM clock is above those modes — the Zephyr PDM backend opens at the lowest supported native rate that is an integer multiple of the request (32 kHz, then 48 kHz) and decimates in software with alp_dsp_decimator_t. alp_audio_in_read() still delivers frames at the requested rate, and frames_per_block still counts frames at that rate.
The cost: the native block is ratio times larger and must fit CONFIG_ALP_SDK_AUDIO_BLOCK_BYTES (otherwise open fails with ALP_ERR_OUT_OF_RANGE); the anti-alias filter adds 67 native-rate samples of group delay (about 2.1 ms at 32 kHz, 1.4 ms at 48 kHz); and every read filters ratio input frames per output frame. S16 only; a rate that no native rate divides evenly still fails.
Playback (I²S speaker)
alp_audio_out_t *spkr = alp_audio_out_open(&(alp_audio_config_t){
.peripheral_id = ALP_E1M_I2S0,
.sample_rate_hz = 48000,
.channels = 2,
.format = ALP_AUDIO_FMT_S16_LE,
.frames_per_block = 256,
});
alp_audio_out_set_volume(spkr, 200); // linear 0..255, not dB
alp_audio_out_start(spkr);
alp_audio_out_write(spkr, pcm_buffer, n_frames, NULL, /* timeout_ms */ 100);
alp_audio_out_write()refusesframeslarger than theframes_per_blocknegotiated at open withALP_ERR_OUT_OF_RANGEinstead of truncating (v0.17.0).out_framescounts only frames the driver genuinely accepted.alp_audio_out_set_volume()takes a linear0..255. A value other than255returnsALP_ERR_NOSUPPORTwhen the open format cannot be software-scaled — the Yocto and Zephyr backends only scaleS16_LE, notS24_LE/S32_LE.
Start, underrun and stop on the Zephyr I²S backend
These behaviours are new in v0.17.0:
- Deferred start. Calling
alp_audio_out_start()before the first write is legal. The DesignWare I²S driver cannot trigger with an empty queue, so the real start is deferred to the first write that queues a block — and a trigger failure then surfaces from thatalp_audio_out_write()call. If that retry fails, the just-queued block is released, so a failing write never leaves data queued. - Underrun recovery. A gap between writes long enough to underrun is recovered transparently: the next write (or a start) resumes the stream. An external codec or amplifier that shut itself down when the bit clock stopped is not re-armed by this layer — see its chip driver (e.g.
tas2563_resume()in<alp/chips/tas2563.h>). - Stop. Stopping a stream that underran still returns
ALP_OKand releases what was queued.ALP_ERR_IOis returned only if both the stop trigger and its internal fallback were refused. - A second
start()while already playing is backend-specific (ALP_ERR_IOon Zephyr I²S, idempotentALP_OKon Yocto / ALSA) — do not rely on either.
What an external amplifier does when the I²S clock stops while it is active is chip-specific. Mute it before alp_audio_out_stop(), and expect it may need re-arming after the first successful write following a restart.
Backends
| Backend | Implementation |
|---|---|
| Zephyr | audio_dmic for PDM in (CONFIG_ALP_SDK_AUDIO_IN); output delegates to <alp/i2s.h> (CONFIG_ALP_SDK_AUDIO_OUT). With either option off, the matching *_open() returns NULL with ALP_ERR_NOSUPPORT. |
| Yocto | ALSA (snd_pcm_*). peripheral_id == 0 → "default"; peripheral_id == N → "hw:N-1,0". |
| Bare-metal | No backend: the build links a stub, so the *_open() calls return NULL with ALP_ERR_NOSUPPORT. |
The application API is identical across backends. A default DSP stage (DC-block) sits in-line on the input path.
Default-initialiser macro
alp_audio_config_t cfg = ALP_AUDIO_CONFIG_DEFAULT(ALP_E1M_PDM0);
cfg.sample_rate_hz = 48000u;
ALP_AUDIO_CONFIG_DEFAULT(id) (v0.10) sets peripheral_id from id plus: sample_rate_hz = 16000, channels = 1 (mono), format = ALP_AUDIO_FMT_S16_LE, frames_per_block = 256.
See the shared macro contract for the C++ compound-literal caveat that applies to every ALP_*_CONFIG_DEFAULT.
See also
<alp/dsp.h>— FIR/IIR/FFT chains and the streaming decimator<alp/i2s.h>— raw I²S