audioio

Support for audio output

The audioio module contains classes to provide access to audio IO.

All classes change hardware state and should be deinitialized when they are no longer needed if the program continues after use. To do so, either call deinit() or use a context manager. See Lifetime and ContextManagers for more info.

Since CircuitPython 5, RawSample and WaveFile are moved to audiocore, and Mixer is moved to audiomixer.

For compatibility with CircuitPython 4.x, some builds allow the items in audiocore to be imported from audioio. This will be removed for all boards in a future build of CircuitPython.

class audioio.AudioOut(left_channel: microcontroller.Pin, *, right_channel: Optional[microcontroller.Pin] = None, quiescent_value: int = 32768)

Output an analog audio signal

playing :bool

True when an audio sample is being output even if paused. (read-only)

paused :bool

True when playback is paused. (read-only)

deinit(self)

Deinitialises the AudioOut 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 16 bit samples. Microcontrollers with a lower output resolution will use the highest order bits to output. For example, the SAMD21 has a 10 bit DAC that ignores the lowest 6 bits when playing 16 bit samples.

stop(self)

Stops playback and resets to the start of the sample.

pause(self)

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

resume(self)

Resumes sample playback after pause().