From 799581c08d3a275d08cd4623078d071a67f91157 Mon Sep 17 00:00:00 2001 From: Liz Date: Wed, 5 Jun 2024 15:21:37 -0400 Subject: [PATCH] update for sound box adding ability for multiple "shake" activated sound effects --- CircuitPython_Sound_Box_2/code.py | 70 ++++++++++++++++--------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/CircuitPython_Sound_Box_2/code.py b/CircuitPython_Sound_Box_2/code.py index 1de6df386..56ca83069 100644 --- a/CircuitPython_Sound_Box_2/code.py +++ b/CircuitPython_Sound_Box_2/code.py @@ -28,26 +28,6 @@ # external button switch = keypad.Keys((board.EXTERNAL_BUTTON,), value_when_pressed=False, pull=True) -wavs = [] -wav_names = [] -for filename in os.listdir('/wavs'): - if filename.lower().endswith('.wav') and not filename.startswith('.'): - wavs.append("/wavs/"+filename) - wav_names.append(filename.replace('.wav', '')) - -audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) - -num_wavs = len(wavs) -wav_index = 0 - -def open_audio(num): - n = wavs[num] - f = open(n, "rb") - w = audiocore.WaveFile(f) - wn = wav_names[num] - return w, wn -wave, wave_name = open_audio(wav_index) - colors = [ {'label': "BLUE", 'color': 0x0000FF}, {'label': "RED", 'color': 0xFF0000}, @@ -60,6 +40,33 @@ def open_audio(num): {'label': "WHITE", 'color': 0x555555}, ] +shake_wavs = [] +color_wavs = [] +for filename in os.listdir('/wavs'): + if filename.lower().endswith('.wav') and not filename.startswith('.'): + if "SHAKE" in filename: + shake_wavs.append("/wavs/" + filename) + else: + for color in colors: + if color['label'] in filename: + color_wavs.append("/wavs/" + filename) + break + +audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) + +num_colors = len(color_wavs) +num_shakes = len(shake_wavs) +wav_index = 0 + + +def open_audio(num, wavs): + n = wavs[num] + f = open(n, "rb") + w = audiocore.WaveFile(f) + # wn = wav_names[num] + return w, n +wave, wave_name = open_audio(wav_index, color_wavs) + i2c = board.I2C() int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT) lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) @@ -69,7 +76,7 @@ def open_audio(num): event = switch.events.get() if event: if event.pressed: - wave, wave_name = open_audio(random.randint(0, 8)) + wave, wave_name = open_audio(random.randint(0, num_colors - 1), color_wavs) audio.play(wave) for color in colors: if color['label'] in wave_name: @@ -82,15 +89,12 @@ def open_audio(num): if event.released: pass if lis3dh.shake(shake_threshold=12): - for v in wav_names: - name = wav_names.index(v) - if "SHAKE" in v: - wave, wave_name = open_audio(name) - audio.play(wave) - for i in range(num_pixels): - pixels[i] = colorwheel(hue) - hue = (hue + 30) % 256 - print(hue) - time.sleep(.7) - pixels.fill((0, 0, 0)) - print('shake') + wave, wave_name = open_audio(random.randint(0, num_shakes - 1), shake_wavs) + audio.play(wave) + for i in range(num_pixels): + pixels[i] = colorwheel(hue) + hue = (hue + 30) % 256 + print(hue) + time.sleep(.7) + pixels.fill((0, 0, 0)) + print('shake')