I am currently working on a program for my Raspberry Pi Pico (CircuitPython) which could make it play songs to a speaker through I2S.
So, I decided to use the Audiomixer built-in module in CircuitPython, which has a gain control.
import board
import audiomp3
import audiobusio
import busio
import sdcardio
import storage
import audiomixer
# Gets SD's pins
spi = busio.SPI(board.GP10, MOSI = board.GP11, MISO = board.GP12)
cs = board.GP15
sd = sdcardio.SDCard(spi, cs)
# Mount SD card
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')
# Setup Audio I2S Out
audio = audiobusio.I2SOut(board.GP16, board.GP17, board.GP18)
# Setup the volume control
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True)
# attaches the mixer to audio play
audio.play(mixer)
def play_sound(fileName):
mixer.voice[0].level = 0.5 # MP3 file playing at half volume
music = audiomp3.MP3Decoder(open(f"/sd/{fileName}", "rb"))
mixer.voice[0].play(music, loop=True) # starts the audio
print(f"playing {fileName}")
while audio.playing:
pass
if __name__ == "__main__":
while True:
play_sound("0_1.mp3")
The code works perfectly fine when ran. However, when I import the function like this, it doesn’t play anything, and shows no error…
from audioI2S import play_sound
while True:
play_sound("0_1.mp3")
Thanks in advance for your help !