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.
For more information on working with this module, refer to the
CircuitPython Essentials Learn Guide.
Since CircuitPython 5, RawSample
and WaveFile
are moved
to audiocore
, and Mixer
is moved to audiomixer
.
Available on these boards
- ATMegaZero ESP32-S2
- 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 ESP32 V2
- Adafruit Feather ESP32-S2 Reverse TFT
- Adafruit Feather ESP32-S2 TFT
- Adafruit Feather ESP32S2
- Adafruit Feather HUZZAH32
- Adafruit Feather M0 Express
- Adafruit Feather M0 Express with Crickit libraries
- Adafruit Feather M4 CAN
- Adafruit Feather M4 Express
- Adafruit FunHouse
- Adafruit Grand Central M4 Express
- Adafruit HUZZAH32 Breakout
- Adafruit Hallowing M4 Express
- Adafruit ItsyBitsy ESP32
- Adafruit ItsyBitsy M0 Express
- Adafruit ItsyBitsy M4 Express
- Adafruit MagTag
- Adafruit Matrix Portal M4
- Adafruit Metro ESP32S2
- Adafruit Metro M0 Express
- Adafruit Metro M4 Airlift Lite
- Adafruit Metro M4 Express
- Adafruit Mini Sparkle Motion
- Adafruit Monster M4SK
- Adafruit PyGamer
- Adafruit PyPortal
- Adafruit PyPortal Pynt
- Adafruit PyPortal Titano
- Adafruit Pybadge
- Adafruit QT Py ESP32 PICO
- Adafruit QT Py ESP32S2
- Adafruit QT Py M0 Haxpress
- Adafruit Sparkle Motion
- Adafruit Trellis M4 Express
- Adafruit Vindie S2
- Ai Thinker ESP32-CAM
- AloriumTech Evo M51
- Artisense Reference Design RD00
- BDMICRO VINA-D51
- BPI-Bit-S2
- BastWiFi
- Bradán Lane STUDIO Coin M0
- CP32-M4
- CRCibernetica IdeaBoard
- Cedar Grove StringCar M0 Express
- Circuit Playground Express Digi-Key PyCon 2019
- CircuitBrains Basic
- CircuitBrains Deluxe
- CrumpS2
- Deneyap Kart
- Deneyap Kart 1A
- Deneyap Mini
- Deneyap Mini v2
- DynOSSAT-EDU-EPS
- DynOSSAT-EDU-OBC
- ESP 12k NodeMCU
- ESP32 Devkit V1
- ESP32-S2-DevKitC-1-N4
- ESP32-S2-DevKitC-1-N4R2
- ESP32-S2-DevKitC-1-N8R2
- Electronic Cats Hunter Cat NFC
- Espressif ESP32 DevKitc V4 WROOM-32E
- Espressif ESP32 DevKitc V4 WROVER
- Espressif ESP32 TTGO T8 v1.7
- Espressif ESP32-EYE
- Espressif ESP32-LyraT
- FeatherS2
- FeatherS2 Neo
- FeatherS2 PreRelease
- Flipper Zero Wi-Fi Dev
- Franzininho WIFI w/Wroom
- Franzininho WIFI w/Wrover
- Freenove ESP32-WROVER-DEV-CAM
- Gravitech Cucumber M
- Gravitech Cucumber MS
- Gravitech Cucumber R
- Gravitech Cucumber RS
- HMI-DevKit-1.1
- Hacked Feather M0 Express with 8Mbyte SPI flash
- HalloWing M0 Express
- Hardkernel Odroid Go
- HexKyS2
- IoTs2
- Kaluga 1
- LILYGO TTGO T-DISPLAY v1.1
- LILYGO TTGO T-DISPLAY v1.1 4M
- LILYGO TTGO T8 ESP32-S2
- LILYGO TTGO T8 ESP32-S2 w/Display
- Lilygo T-watch 2020 V3
- M5Stack Atom Echo
- M5Stack Atom Lite
- M5Stack Atom Matrix
- M5Stack Atom U
- M5Stack Core Basic
- M5Stack Core Fire
- M5Stack Core2
- M5Stack M5Paper
- M5Stack Stick C
- M5Stack Stick C Plus
- M5Stack Stick C Plus2
- M5Stack Timer Camera X
- MORPHEANS MorphESP-240
- Maker badge by Czech maker
- MicroDev microS2
- Mini SAM M4
- MixGo CE
- Oak Dev Tech PixelWing ESP32S2
- Oxocard Artwork
- Oxocard Connect
- Oxocard Galaxy
- Oxocard Science
- P1AM-200
- PewPew M4
- PyCubedv04
- PyCubedv04-MRAM
- PyCubedv05
- PyCubedv05-MRAM
- Red S2-WROOM
- Robo HAT MM1 M4
- S2Mini
- S2Pico
- SAM E54 Xplained Pro
- SAM32v26
- SQFMI Watchy
- Saola 1 w/Wroom
- Saola 1 w/Wrover
- 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
- TTGO T8 ESP32-S2-WROOM
- Targett Module Clip w/Wroom
- Targett Module Clip w/Wrover
- The Open Book Feather
- TinyPICO
- TinyPICO Nano
- TinyS2
- Trinket M0 Haxpress
- UARTLogger II
- VIDI X V1.1
- Waveshare ESP32-S2-Pico
- Waveshare ESP32-S2-Pico-LCD
- WeMos LOLIN32 Lite
- Winterbloom Big Honking Button
- keithp.com snekboard
- nanoESP32-S2 w/Wrover
- nanoESP32-S2 w/Wroom
- senseBox MCU-S2 ESP32S2
- sunton_esp32_2432S024C
- sunton_esp32_2432S028
- sunton_esp32_2432S032C
- uGame10
-
class audioio.AudioOut(left_channel: microcontroller.Pin, *, right_channel: microcontroller.Pin | None = 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) – Output left channel data to this pin
right_channel (Pin) – Output right channel data to this pin. May be None
.
quiescent_value (int) – The output value when no signal is present. Samples should start
and end with this value to prevent audible popping.
Note
On ESP32 and ESP32-S2, the DAC channels are usually designated
as DAC_1
(right stereo channel) and DAC_2 (left stereo channel).
These pins are sometimes labelled as A0
and A1
, but they may be assigned
in either order. Check your board’s pinout to verify which pin is which channel.
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")
-
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.
-
playing: bool
True when an audio sample is being output even if paused
. (read-only)
-
pause() → None
Stops playback temporarily while remembering the position. Use resume
to resume playback.
-
resume() → None
Resumes sample playback after pause()
.
-
paused: bool
True when playback is paused. (read-only)