I2SOut – Output an I2S audio signal

I2S is used to output an audio signal on an I2S bus.

class audiobusio.I2SOut(bit_clock, word_select, data, *, left_justified)

Create a I2SOut object associated with the given pins.

Parameters:
  • bit_clock (Pin) – The bit clock (or serial clock) pin
  • word_select (Pin) – The word select (or left/right clock) pin
  • data (Pin) – The data pin
  • left_justified (bool) – True when data bits are aligned with the word select clock. False when they are shifted by one to match classic I2S protocol.

Simple 8ksps 440 Hz sine wave on Metro M0 Express using UDA1334 Breakout:

import audiobusio
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 / 18) * (2 ** 15) + 2 ** 15)

sine_wave = audiobusio.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 audiobusio
import digitalio


f = open("cplay-5.1-16bit-16khz.wav", "rb")
wav = audioio.WaveFile(f)

a = audiobusio.I2SOut(board.D1, board.D0, board.D9)

print("playing")
a.play(wav)
while a.playing:
  pass
print("stopped")
deinit()

Deinitialises the I2SOut and releases any hardware resources for reuse.

__enter__()

No-op used by Context Managers.

__exit__()

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 playing to block.

Sample must be an audioio.WaveFile or audioio.RawSample.

The sample itself should consist of 8 bit or 16 bit samples.

stop()

Stops playback.

playing

True when the audio sample is being output. (read-only)

pause()

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

resume()

Resumes sample playback after pause().

paused

True when playback is paused. (read-only)