Skip to content

Commit 799581c

Browse files
committed
update for sound box
adding ability for multiple "shake" activated sound effects
1 parent 8a08dcf commit 799581c

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

CircuitPython_Sound_Box_2/code.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@
2828
# external button
2929
switch = keypad.Keys((board.EXTERNAL_BUTTON,), value_when_pressed=False, pull=True)
3030

31-
wavs = []
32-
wav_names = []
33-
for filename in os.listdir('/wavs'):
34-
if filename.lower().endswith('.wav') and not filename.startswith('.'):
35-
wavs.append("/wavs/"+filename)
36-
wav_names.append(filename.replace('.wav', ''))
37-
38-
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
39-
40-
num_wavs = len(wavs)
41-
wav_index = 0
42-
43-
def open_audio(num):
44-
n = wavs[num]
45-
f = open(n, "rb")
46-
w = audiocore.WaveFile(f)
47-
wn = wav_names[num]
48-
return w, wn
49-
wave, wave_name = open_audio(wav_index)
50-
5131
colors = [
5232
{'label': "BLUE", 'color': 0x0000FF},
5333
{'label': "RED", 'color': 0xFF0000},
@@ -60,6 +40,33 @@ def open_audio(num):
6040
{'label': "WHITE", 'color': 0x555555},
6141
]
6242

43+
shake_wavs = []
44+
color_wavs = []
45+
for filename in os.listdir('/wavs'):
46+
if filename.lower().endswith('.wav') and not filename.startswith('.'):
47+
if "SHAKE" in filename:
48+
shake_wavs.append("/wavs/" + filename)
49+
else:
50+
for color in colors:
51+
if color['label'] in filename:
52+
color_wavs.append("/wavs/" + filename)
53+
break
54+
55+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
56+
57+
num_colors = len(color_wavs)
58+
num_shakes = len(shake_wavs)
59+
wav_index = 0
60+
61+
62+
def open_audio(num, wavs):
63+
n = wavs[num]
64+
f = open(n, "rb")
65+
w = audiocore.WaveFile(f)
66+
# wn = wav_names[num]
67+
return w, n
68+
wave, wave_name = open_audio(wav_index, color_wavs)
69+
6370
i2c = board.I2C()
6471
int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT)
6572
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
@@ -69,7 +76,7 @@ def open_audio(num):
6976
event = switch.events.get()
7077
if event:
7178
if event.pressed:
72-
wave, wave_name = open_audio(random.randint(0, 8))
79+
wave, wave_name = open_audio(random.randint(0, num_colors - 1), color_wavs)
7380
audio.play(wave)
7481
for color in colors:
7582
if color['label'] in wave_name:
@@ -82,15 +89,12 @@ def open_audio(num):
8289
if event.released:
8390
pass
8491
if lis3dh.shake(shake_threshold=12):
85-
for v in wav_names:
86-
name = wav_names.index(v)
87-
if "SHAKE" in v:
88-
wave, wave_name = open_audio(name)
89-
audio.play(wave)
90-
for i in range(num_pixels):
91-
pixels[i] = colorwheel(hue)
92-
hue = (hue + 30) % 256
93-
print(hue)
94-
time.sleep(.7)
95-
pixels.fill((0, 0, 0))
96-
print('shake')
92+
wave, wave_name = open_audio(random.randint(0, num_shakes - 1), shake_wavs)
93+
audio.play(wave)
94+
for i in range(num_pixels):
95+
pixels[i] = colorwheel(hue)
96+
hue = (hue + 30) % 256
97+
print(hue)
98+
time.sleep(.7)
99+
pixels.fill((0, 0, 0))
100+
print('shake')

0 commit comments

Comments
 (0)