Skip to content

Commit 1893ddd

Browse files
committed
code for soundboard scooter bike project
code for soundboard scooter bike project
1 parent b29b64e commit 1893ddd

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

Soundboard_Scooter_Bike/code.py

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