Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Created July 30, 2024 21:33
Show Gist options
  • Save gallaugher/f1ef0f95b7981e46e6e25f3a70fe9f3a to your computer and use it in GitHub Desktop.
Save gallaugher/f1ef0f95b7981e46e6e25f3a70fe9f3a to your computer and use it in GitHub Desktop.
audio-without-pops.py
import board, time, digitalio
import audiomixer, audiocore
from audiopwmio import PWMAudioOut as AudioOut
# Speaker setup for the CircuitPlayground boards (not for Pico)
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
# use speaker location to create an AudioOut object named audio
audio = AudioOut(board.SPEAKER) # use board.GP# for pin number for audio out on a Pico
# set path where audio files can be found on device
path = "sounds/"
# create Mixer and attach to audio playback
num_voices = 1 # only playing 1 song at a time
mixer = audiomixer.Mixer(voice_count=num_voices, sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True)
audio.play(mixer)
# Setup buttons
button_A = digitalio.DigitalInOut(board.BUTTON_A)
button_A.switch_to_input(digitalio.Pull.DOWN)
def play_voice(filename):
# read in all beats & simultaneously play them at audio sound .level = 0 (no volume)
print(f"About to play: {path+filename}")
wave = audiocore.WaveFile(open(path+filename,"rb"))
mixer.voice[0].play(wave, loop=False )
mixer.voice[0].level = 1.0
while mixer.voice[0].playing:
pass
while True:
if button_A.value:
print("A pressed")
play_voice("encouragement1.wav")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment