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.

Available on these boards
  • Adafruit BLM Badge
  • Adafruit Circuit Playground Express 4-H
  • Adafruit CircuitPlayground Express
  • Adafruit CircuitPlayground Express with Crickit libraries
  • Adafruit CircuitPlayground Express with displayio
  • Adafruit EdgeBadge
  • Adafruit Feather M0 Express
  • Adafruit Feather M0 Express with Crickit libraries
  • Adafruit Feather M4 CAN
  • Adafruit Feather M4 Express
  • Adafruit Grand Central M4 Express
  • Adafruit Hallowing M4 Express
  • Adafruit ItsyBitsy M0 Express
  • Adafruit ItsyBitsy M4 Express
  • Adafruit Matrix Portal M4
  • Adafruit Metro M0 Express
  • Adafruit Metro M4 Airlift Lite
  • Adafruit Metro M4 Express
  • Adafruit Monster M4SK
  • Adafruit PyGamer
  • Adafruit PyPortal
  • Adafruit PyPortal Pynt
  • Adafruit PyPortal Titano
  • Adafruit Pybadge
  • Adafruit QT Py M0 Haxpress
  • Adafruit Trellis M4 Express
  • AloriumTech Evo M51
  • BDMICRO VINA-D21
  • BDMICRO VINA-D51
  • CP32-M4
  • Cedar Grove StringCar M0 Express
  • Circuit Playground Express Digi-Key PyCon 2019
  • CircuitBrains Basic
  • CircuitBrains Deluxe
  • DynOSSAT-EDU-EPS
  • DynOSSAT-EDU-OBC
  • Electronic Cats Hunter Cat NFC
  • Hacked Feather M0 Express with 8Mbyte SPI flash
  • HalloWing M0 Express
  • Mini SAM M4
  • PewPew M4
  • PyCubedv04
  • PyCubedv04-MRAM
  • PyCubedv05
  • PyCubedv05-MRAM
  • Robo HAT MM1 M4
  • SAM E54 Xplained Pro
  • SAM32v26
  • Seeeduino Wio Terminal
  • Serpente
  • Silicognition LLC M4-Shim
  • SparkFun MicroMod SAMD51 Processor
  • SparkFun RedBoard Turbo
  • SparkFun Thing Plus - SAMD51
  • StackRduino M0 PRO
  • TG-Boards' Datalore IP M4
  • The Open Book Feather
  • Trinket M0 Haxpress
  • UARTLogger II
  • Winterbloom Big Honking Button
  • keithp.com snekboard
  • uGame10

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

Output an analog audio signal

Create a AudioOut object associated with the given pin(s). This allows you to play audio signals out on the given pin(s).

Parameters
  • left_channel (Pin) – The pin to output the left channel to

  • right_channel (Pin) – The pin to output the right channel to

  • quiescent_value (int) – The output value when no signal is present. Samples should start and end with this value to prevent audible popping.

Simple 8ksps 440 Hz sin wave:

import audiocore
import audioio
import board
import array
import time
import math

# Generate one period of sine wav.
length = 8000 // 440
sine_wave = array.array("H", [0] * length)
for i in range(length):
    sine_wave[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15) + 2 ** 15)

dac = audioio.AudioOut(board.SPEAKER)
sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)
dac.play(sine_wave, loop=True)
time.sleep(1)
dac.stop()

Playing a wave file from flash:

import board
import audioio
import digitalio

# Required for CircuitPlayground Express
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.switch_to_output(value=True)

data = open("cplay-5.1-16bit-16khz.wav", "rb")
wav = audiocore.WaveFile(data)
a = audioio.AudioOut(board.A0)

print("playing")
a.play(wav)
while a.playing:
  pass
print("stopped")
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() None

Deinitialises the AudioOut and releases any hardware resources for reuse.

__enter__() AudioOut

No-op used by Context Managers.

__exit__() None

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

play(sample: circuitpython_typing.AudioSample, *, loop: bool = False) None

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, audiomixer.Mixer or audiomp3.MP3Decoder.

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() None

Stops playback and resets to the start of the sample.

pause() None

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

resume() None

Resumes sample playback after pause().