AudioOut can be used to output an analog audio signal on a given pin.
AudioOut(left_channel, *, right_channel=None, quiescent_value=0x8000)¶
Create a AudioOut object associated with the given pin(s). This allows you to play audio signals out on the given pin(s).
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",  * length) for i in range(length): sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (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")
Deinitialises the AudioOut and releases any hardware resources for reuse.
No-op used by Context Managers.
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
play(sample, *, loop=False)¶
Plays the sample once when loop=False and continuously when loop=True. Does not block. Use
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.
Stops playback and resets to the start of the sample.
True when playback is paused. (read-only)