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: Simple 8ksps 440 Hz sine wave on Metro M0 Express using UDA1334 Breakout:
import audiobusio import audiocore import board import array import time import math # Generate one period of sine wave. 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 = 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")
-
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
audiocore.WaveFile
,audiocore.RawSample
, oraudiomixer.Mixer
.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)
-
paused
¶ True when playback is paused. (read-only)
-