Skip to content

Commit b3e1d9f

Browse files
authored
Merge pull request #2151 from adafruit/soundboard_scooter
code for soundboard scooter bike project
2 parents 679c909 + dbfe115 commit b3e1d9f

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

Soundboard_Scooter_Bike/code.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# put samples in "/wav" folder
5+
6+
import time
7+
import board
8+
import keypad
9+
import audiocore
10+
import audiomixer
11+
import audiobusio
12+
13+
# wait a little bit so USB can stabilize and not glitch audio
14+
time.sleep(3)
15+
16+
# list of (samples to play, mixer gain level)
17+
wav_files = (
18+
('wav/airhorn.wav', 1.0),
19+
('wav/bike-horn.wav', 1.0),
20+
('wav/chime.wav', 1.0)
21+
)
22+
23+
# pins used by keyboard
24+
KEY_PINS = (
25+
board.D5, board.D6, board.D12
26+
)
27+
28+
km = keypad.Keys( KEY_PINS, value_when_pressed=False, pull=True)
29+
30+
audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)
31+
32+
mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=1,
33+
bits_per_sample=16, samples_signed=True)
34+
audio.play(mixer) # attach mixer to audio playback
35+
36+
for i in range(len(wav_files)): # start all samples at once for use w handle_mixer
37+
wave = audiocore.WaveFile(open(wav_files[i][0],"rb"))
38+
mixer.voice[i].play(wave, loop=True)
39+
mixer.voice[i].level = 0
40+
41+
def handle_mixer(num, pressed):
42+
voice = mixer.voice[num] # get mixer voice
43+
if pressed:
44+
voice.level = wav_files[num][1] # play at level in wav_file list
45+
else: # released
46+
voice.level = 0 # mute it
47+
48+
49+
while True:
50+
event = km.events.get()
51+
if event:
52+
if event.key_number < len(wav_files):
53+
if event.pressed:
54+
handle_mixer(event.key_number, True)
55+
56+
if event.released:
57+
handle_mixer( event.key_number, False )
69.4 KB
Binary file not shown.
16.9 KB
Binary file not shown.

Soundboard_Scooter_Bike/wav/chime.wav

154 KB
Binary file not shown.

0 commit comments

Comments
 (0)