Skip to content

Instantly share code, notes, and snippets.

@RobCranfill
Last active July 26, 2023 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobCranfill/3f2dd091e15ceeb13cde5c61a40f9e2a to your computer and use it in GitHub Desktop.
Save RobCranfill/3f2dd091e15ceeb13cde5c61a40f9e2a to your computer and use it in GitHub Desktop.
Output stereo audio via a RPi Pico and a PCM5102 board
# Test outputting stereo audio via a PCM5102 board
# cran, based on:
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import audiocore, audiobusio, audiomixer, synthio
# Pico
#
# I2S clock: CircuitPython GP16 = Pico pin 21 -> "BCK" on 5102 board
PIN_BIT_CLOCK = board.GP16
# I2S LR clock (aka WS clock): GP17 = pin 22 -> "LCK" on board
PIN_WORD_SELECT = board.GP17
# I2S data: GP18 = pin 24 -> "DIN" on board
PIN_DATA = board.GP18
# NOTE: The 5102 board's "SCK" pin must be pulled to ground!
# wav file from https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples.html
wave_file = audiocore.WaveFile(open("M1F1-int16-AFsp.wav", "rb"))
audio = audiobusio.I2SOut(PIN_BIT_CLOCK, PIN_WORD_SELECT, PIN_DATA)
# As per https://github.com/todbot/circuitpython-synthio-tricks use a mixer:
_mixer = audiomixer.Mixer(channel_count=2, sample_rate=8000, buffer_size=2040)
_mixer.voice[0].level = 0 # 10% volume to start seems plenty
_synth = synthio.Synthesizer(channel_count=2, sample_rate=8000)
audio.play(_mixer)
lev = 0
while True:
lev += .2
_mixer.voice[0].level = lev
print(f"playing at {lev}....")
_mixer.voice[0].play(wave_file)
while _mixer.voice[0].playing:
pass
print("Done!")
# ---- No mixer - not so good.
#
# wave_file = audiocore.WaveFile(open("M1F1-int16-AFsp.wav", "rb"))
# wave = audiocore.WaveFile(wave_file)
# mixer = audiomixer.Mixer(voice_count=1, sample_rate=16000, channel_count=2,
# bits_per_sample=16, samples_signed=True)
# while True:
# print("playing....")
# mixer.voice[0].play(wave_file)
# while audio.playing:
# pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment