Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4906

General • Play multiple wav sounds simultaneous

$
0
0
Hello,
I have a simple application playing multiple wav sound files from an sd card over a i2s amplifier. The Pico receives a byte over the serial port and accordingly plays one of five sounds. These sounds can overlap and play at the same time. If the same sound is triggered while playing it stops the playback and starts it new.
Basically it all works fine. The problem is that I get sporadic errors. Sometimes it throws an exception "input/output error", sometimes the serial port stops to receive bytes and sometimes the thing hangs itself up and produces a very loud noise (only way to recover is to power cycle). What could be the cause and how could I fix this?

Code:

import boardimport digitalioimport timeimport audiocoreimport audiobusioimport busioimport sdcardioimport storageimport osimport audiomixeruart = busio.UART(board.GP16, board.GP17, baudrate=9600, receiver_buffer_size=1000)led = digitalio.DigitalInOut(board.LED)led.direction = digitalio.Direction.OUTPUTcs = board.GP13audio = audiobusio.I2SOut(board.GP0, board.GP1, board.GP2)spi = busio.SPI(board.GP10, board.GP11, board.GP12)sd = sdcardio.SDCard(spi, cs)vfs = storage.VfsFat(sd)storage.mount(vfs, "/sd")mixer = audiomixer.Mixer(voice_count=5, sample_rate=16000, channel_count=1, bits_per_sample=16, samples_signed=True)w1 = open("/sd/button1.wav", "rb")w2 = open("/sd/music1.wav", "rb")w3 = open("/sd/hit1.wav", "rb")w4 = open("/sd/music2.wav", "rb")w5 = open("/sd/hit2.wav", "rb")a1 = audiocore.WaveFile(w1)a2 = audiocore.WaveFile(w2)a3 = audiocore.WaveFile(w3)a4 = audiocore.WaveFile(w4)a5 = audiocore.WaveFile(w5)audio.play(mixer)mixer.voice[0].level = 0.05mixer.voice[1].level = 0.05mixer.voice[2].level = 0.05mixer.voice[3].level = 0.05mixer.voice[4].level = 0.05while True:    if uart.in_waiting > 0:        c = uart.read(1)        if(c == b'a'):            mixer.stop_voice(0)            mixer.voice[0].play(a1)        elif(c == b'b'):            mixer.stop_voice(1)            mixer.voice[1].play(a2)        elif(c == b'c'):            mixer.stop_voice(2)            mixer.voice[2].play(a3)        elif(c == b'd'):            mixer.stop_voice(3)            mixer.voice[3].play(a4)        elif(c == b'e'):            mixer.stop_voice(4)            mixer.voice[4].play(a5)        elif(c == b'q'):            break;        elif(c == b'z'):            mixer.stop_voice(0)            mixer.stop_voice(1)            mixer.stop_voice(2)            mixer.stop_voice(3)            mixer.stop_voice(4)    time.sleep(0.001)mixer.stop_voice(0)mixer.stop_voice(1)mixer.stop_voice(2)mixer.stop_voice(3)mixer.stop_voice(4)w1.close()w2.close()w3.close()w4.close()w5.close()

Statistics: Posted by raspitin — Wed Mar 13, 2024 6:25 pm



Viewing all articles
Browse latest Browse all 4906

Trending Articles