Skip to content

update for sound box #2831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions CircuitPython_Sound_Box_2/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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')