audiobusio

Support for audio input and output over digital buses

The audiobusio module contains classes to provide access to audio IO over digital buses. These protocols are used to communicate audio to other chips in the same circuit. It doesn’t include audio interconnect protocols such as S/PDIF.

All libraries change hardware state and should be deinitialized when they are no longer needed. To do so, either call deinit() or use a context manager.

class audiobusio.I2SOut(bit_clock: microcontroller.Pin, word_select: microcontroller.Pin, data: microcontroller.Pin, *, left_justified: bool)

Output an I2S audio signal

playing :bool

True when the audio sample is being output. (read-only)

paused :bool

True when playback is paused. (read-only)

deinit(self)

Deinitialises the I2SOut and releases any hardware resources for reuse.

__enter__(self)

No-op used by Context Managers.

__exit__(self)

Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.

play(self, sample: audiocore._AudioSample, *, loop: bool = False)

Plays the sample once when loop=False and continuously when loop=True. Does not block. Use playing to block.

Sample must be an audiocore.WaveFile, audiocore.RawSample, or audiomixer.Mixer.

The sample itself should consist of 8 bit or 16 bit samples.

stop(self)

Stops playback.

pause(self)

Stops playback temporarily while remembering the position. Use resume to resume playback.

resume(self)

Resumes sample playback after pause().

class audiobusio.PDMIn(clock_pin: microcontroller.Pin, data_pin: microcontroller.Pin, *, sample_rate: int = 16000, bit_depth: int = 8, mono: bool = True, oversample: int = 64, startup_delay: float = 0.11)

Record an input PDM audio stream

sample_rate :int

The actual sample_rate of the recording. This may not match the constructed sample rate due to internal clock limitations.

deinit(self)

Deinitialises the PDMIn and releases any hardware resources for reuse.

__enter__(self)

No-op used by Context Managers.

__exit__(self)

Automatically deinitializes the hardware when exiting a context.

record(self, destination: WriteableBuffer, destination_length: int)

Records destination_length bytes of samples to destination. This is blocking.

An IOError may be raised when the destination is too slow to record the audio at the given rate. For internal flash, writing all 1s to the file before recording is recommended to speed up writes.

Returns:The number of samples recorded. If this is less than destination_length, some samples were missed due to processing time.