audio — Audio Module
The audio module is used to record audio samples from a microphone. PDM
samples captured from the microphone are filtered and decimated to PCM samples
which can be passed to a user callback or read directly into a buffer.
Functions
- audio.init(channels: int = 1, frequency: int = 16000, gain_db: int = 24, highpass: float | bool = 0.9883, samples: int = -1, buffers: int = 16, overflow: bool = True, clkdiv: int = 0) None
Initializes the audio module. Must be called first before using the audio module.
channelsis the number of audio channels. May be1or2. Audio samples are interleaved when more than one channel is used. Multi-channel capture is only supported on boards with more than one microphone.frequencyis the PCM sample frequency in Hz. The set of supported frequencies is port/board specific.gain_dbis the microphone gain to apply, in decibels.highpassis the high pass filter coefficient (STM32) or a boolean enabling the high pass filter (Alif). Ignored on ports that do not implement a high pass filter.samplesis the number of PCM samples to accumulate per callback. If set to-1the value is computed automatically from the decimation factor and number of channels. Must be a multiple of 16. Available on the STM32 and Alif ports.buffersis the number of internal PCM buffers used to queue samples between the DMA ISR and the user. Available on the Alif and RP2 ports.overflowcontrols whether a buffer overflow raisesRuntimeError. WhenFalsethe oldest buffer is overwritten and streaming continues. Available on the Alif and RP2 ports.clkdivoverrides the PIO clock divider used to drive the PDM clock. When0the divider is computed from the requested frequency. Available on the RP2 port.
- audio.start_streaming(callback: Callable[[bytearray], None] | None) None
Starts audio capture.
callbackis called from the scheduler with a single argumentpcmbufeach time a new PCM buffer is ready.pcmbufis a signed 16-bitbytearrayof PCM samples whose length is determined by the decimation factor, the number of channels and thesamplesargument passed toaudio.init(). In single-channel mode each entry is one 16-bit sample; in dual-channel mode samples are interleaved in pairs.On ports that support
audio.get_buffer()(Alif and RP2), passing a non-callable value (e.g.None) starts capture without a callback so buffers can be drained withaudio.get_buffer()instead.
- audio.get_buffer(timeout: int = 0) bytearray
Returns the next available PCM buffer. Blocks until a buffer is ready or until
timeoutmilliseconds have elapsed (0means wait forever). RaisesRuntimeErrorif streaming is not enabled, if a buffer overflow has occurred whileoverflowisTrue, or if a streaming callback is installed.Available on the Alif and RP2 ports.
- audio.read_pdm(buf: bytearray) None
Reads raw PDM samples from the microphone directly into
buf.bufmust be anarray/bytearraywhose element size matches the number of channels (1 byte for mono, 2 bytes for stereo).Available on the STM32 port (SAI-based boards) only.
- audio.samples() int
Returns the total number of PCM samples captured since the last call to
audio.start_streaming().Available on the RP2 port only.
- audio.overflow() bool
Returns
Trueif a buffer overflow has occurred since the last call toaudio.start_streaming().Available on the RP2 port only.