I2S is used to output an audio signal on an I2S bus.
I2SOut(bit_clock, word_select, data, *, left_justified)¶
Create a I2SOut object associated with the given pins.
import audiobusio import audiocore 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) sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000) i2s = audiobusio.I2SOut(board.D1, board.D0, board.D9) i2s.play(sine_wave, loop=True) time.sleep(1) i2s.stop()
Playing a wave file from flash:
import board import audioio import audiocore import audiobusio import digitalio f = open("cplay-5.1-16bit-16khz.wav", "rb") wav = audiocore.WaveFile(f) a = audiobusio.I2SOut(board.D1, board.D0, board.D9) print("playing") a.play(wav) while a.playing: pass print("stopped")
Deinitialises the I2SOut 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 8 bit or 16 bit samples.
True when the audio sample is being output. (read-only)
True when playback is paused. (read-only)