Skip to content

Commit 76d29b8

Browse files
authored
Merge pull request #2568 from adafruit/audio_bff_examples
Adding CP example code and audio files
2 parents 008e4be + 11c5ab9 commit 76d29b8

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed
59.3 KB
Binary file not shown.
40.9 KB
Binary file not shown.
Binary file not shown.
43 KB
Binary file not shown.
38.5 KB
Binary file not shown.

Audio_BFF/code.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# Demo audio player that plays random wav files from internal storage or
6+
# SD card. Default pinout matches the Audio BFF for QT Py S2, S3 and RP2040
7+
8+
import os
9+
import random
10+
import audiocore
11+
import board
12+
import audiobusio
13+
import audiomixer
14+
import adafruit_sdcard
15+
import storage
16+
import digitalio
17+
18+
card_cs = digitalio.DigitalInOut(board.A0)
19+
card_cs.direction = digitalio.Direction.INPUT
20+
card_cs.pull = digitalio.Pull.UP
21+
sdcard = None
22+
23+
DATA = board.A1
24+
LRCLK = board.A2
25+
BCLK = board.A3
26+
audio = audiobusio.I2SOut(BCLK, LRCLK, DATA)
27+
mixer = None
28+
29+
button = digitalio.DigitalInOut(board.BUTTON)
30+
button.switch_to_input(pull=digitalio.Pull.UP)
31+
32+
wave_files = []
33+
for filename in sorted(os.listdir("/")):
34+
filename = filename.lower()
35+
if filename.endswith(".wav") and not filename.startswith("."):
36+
wave_files.append(filename)
37+
38+
def open_audio():
39+
n = random.choice(wave_files)
40+
print("playing", n)
41+
f = open(n, "rb")
42+
w = audiocore.WaveFile(f)
43+
return f, w
44+
45+
wavefile = 0
46+
47+
while True:
48+
if not sdcard:
49+
try:
50+
sdcard = adafruit_sdcard.SDCard(board.SPI(), card_cs)
51+
vfs = storage.VfsFat(sdcard)
52+
storage.mount(vfs, "/sd")
53+
print("Mounted SD card")
54+
wave_files = ["/"+file for file in os.listdir('/') if file.endswith('.wav')]
55+
wave_files += ["/sd/"+file for file in os.listdir('/sd') if file.endswith('.wav')]
56+
print(wave_files)
57+
except OSError:
58+
pass
59+
60+
if not button.value:
61+
if mixer and mixer.voice[0].playing:
62+
print("stopping")
63+
mixer.voice[0].stop()
64+
if wavefile:
65+
wavefile.close()
66+
else:
67+
wavefile, wave = open_audio()
68+
mixer = audiomixer.Mixer(voice_count=1,
69+
sample_rate=wave.sample_rate,
70+
channel_count=wave.channel_count,
71+
bits_per_sample=wave.bits_per_sample,
72+
samples_signed=True)
73+
mixer.voice[0].level = 0.1
74+
audio.play(mixer)
75+
mixer.voice[0].play(wave)
76+
77+
while not button.value:
78+
pass

0 commit comments

Comments
 (0)