Getting the audio to work in such a large game had a few challenges. Most of the challenges was centered around getting the audio module to load in memory before the rest of the game. This is accomplished by initializing the audio first and attempting to play a sound. If this was not done, the video would briefly cut out while the audio was playing.
The audio board this project is centered around is the TLV320DAC3100 breakout board, but you can use any I2S DAC that is supported by CircuitPython. For more information, check out the Wiring page. The audio device is initialized in code.py and is passed in to the Audio
class along with a dictionary of sound effects. The sound effects are provided in code.py so that you can customize them with your own sounds.
The Audio class is pretty simple and involved initializing the class and a single public method to play a sound by passing a key from the SOUND_EFFECTS
dictionary.
# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams # # SPDX-License-Identifier: MIT import audiocore from definitions import PLAY_SOUNDS class Audio: def __init__(self, audio_bus, sounds): self._audio = audio_bus self._wav_files = {} for sound_name, file in sounds.items(): self._add_sound(sound_name, file) # Play the first sound in the list to initialize the audio system self.play(tuple(self._wav_files.keys())[0], wait=True) def play(self, sound_name, wait=False): if not PLAY_SOUNDS: return if sound_name in self._wav_files: with open(self._wav_files[sound_name], "rb") as wave_file: wav = audiocore.WaveFile(wave_file) self._audio.play(wav) if wait: while self._audio.playing: pass def _add_sound(self, sound_name, file): self._wav_files[sound_name] = file
Page last edited April 09, 2025
Text editor powered by tinymce.