---
title: "Audio"
description: "Block-based microphone capture and speaker playback. Both directions share one config struct, alpaudioconfig_t."
source: https://docs.alplab.ai/sdk/api/audio
---

# `<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

```c
#include <alp/audio.h>
```

## Capture (PDM microphone)

```c
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.

:::note[Discard the first block after a start]
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_TIMEOUT` only means no data arrived within `timeout_ms` (including a `timeout_ms = 0` poll that found nothing queued). It does not by itself indicate loss.
- `ALP_ERR_IO` means 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`](https://docs.alplab.ai/sdk/api/dsp#streaming-decimator). `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)

```c
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()` refuses `frames` larger than the `frames_per_block` negotiated at open with `ALP_ERR_OUT_OF_RANGE` instead of truncating (v0.17.0). `out_frames` counts only frames the driver genuinely accepted.
- `alp_audio_out_set_volume()` takes a linear `0..255`. A value other than `255` returns `ALP_ERR_NOSUPPORT` when the open format cannot be software-scaled — the Yocto and Zephyr backends only scale `S16_LE`, not `S24_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 that `alp_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_OK` and releases what was queued. `ALP_ERR_IO` is returned only if both the stop trigger and its internal fallback were refused.
- A second `start()` while already playing is backend-specific (`ALP_ERR_IO` on Zephyr I²S, idempotent `ALP_OK` on Yocto / ALSA) — do not rely on either.

:::caution[Mute the amplifier before stopping]
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>`](https://docs.alplab.ai/sdk/api/i2s) (`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

```c
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](https://docs.alplab.ai/sdk/api/peripheral#default-initialiser-macros) for the C++ compound-literal caveat that applies to every `ALP_*_CONFIG_DEFAULT`.

## See also

- [`<alp/dsp.h>`](https://docs.alplab.ai/sdk/api/dsp) — FIR/IIR/FFT chains and the streaming decimator
- [`<alp/i2s.h>`](https://docs.alplab.ai/sdk/api/i2s) — raw I²S
