Skip to content

Commit fe402db

Browse files
authored
Merge pull request #25 from kattni/mp3-playback
Add MP3 playback and example with files.
2 parents 8a496b4 + ba55cb6 commit fe402db

File tree

12 files changed

+249
-10
lines changed

12 files changed

+249
-10
lines changed

LICENSES/CC-BY-SA-4.0.txt

Lines changed: 170 additions & 0 deletions
Large diffs are not rendered by default.

adafruit_macropad.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import displayio
5555
import audiopwmio
5656
import audiocore
57+
import audiomp3
5758
import usb_hid
5859
from adafruit_hid.keyboard import Keyboard
5960
from adafruit_hid.keycode import Keycode
@@ -851,9 +852,11 @@ def stop_tone(self):
851852
self._speaker_enable.value = False
852853

853854
def play_file(self, file_name):
854-
"""Play a .wav file using the onboard speaker.
855+
"""Play a .wav or .mp3 file using the onboard speaker.
855856
856-
:param file_name: The name of your .wav file in quotation marks including .wav
857+
:param file_name: The name of your .wav or .mp3 file in quotation marks including
858+
.wav or .mp3, e.g. "sound.wav" or "sound.mp3". Include location if file
859+
is placed somewhere other than /, e.g. "audio/sound.wav".
857860
858861
The following example plays the file "sound.wav" when the rotary encoder switch is pressed.
859862
@@ -866,17 +869,39 @@ def play_file(self, file_name):
866869
while True:
867870
if macropad.encoder_switch:
868871
macropad.play_file("sound.wav")
872+
873+
The following example plays the file "sound.mp3" when the rotary encoder switch is pressed.
874+
875+
.. code-block:: python
876+
877+
from adafruit_macropad import MacroPad
878+
879+
macropad = MacroPad()
880+
881+
while True:
882+
if macropad.encoder_switch:
883+
macropad.play_file("sound.mp3")
869884
"""
870-
# Play a specified file.
871885
self.stop_tone()
872886
self._speaker_enable.value = True
873-
with audiopwmio.PWMAudioOut(
874-
board.SPEAKER
875-
) as audio: # pylint: disable=not-callable
876-
wavefile = audiocore.WaveFile(open(file_name, "rb"))
877-
audio.play(wavefile)
878-
while audio.playing:
879-
pass
887+
if file_name.lower().endswith(".wav"):
888+
with audiopwmio.PWMAudioOut(
889+
board.SPEAKER
890+
) as audio: # pylint: disable=not-callable
891+
wavefile = audiocore.WaveFile(open(file_name, "rb"))
892+
audio.play(wavefile)
893+
while audio.playing:
894+
pass
895+
elif file_name.lower().endswith(".mp3"):
896+
with audiopwmio.PWMAudioOut(
897+
board.SPEAKER
898+
) as audio: # pylint: disable=not-callable
899+
mp3file = audiomp3.MP3Decoder(open(file_name, "rb"))
900+
audio.play(mp3file)
901+
while audio.playing:
902+
pass
903+
else:
904+
raise ValueError("Filetype must be wav or MP3.")
880905
self._speaker_enable.value = False
881906

882907

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"audiopwmio",
4141
"audiocore",
4242
"adafruit_debouncer",
43+
"audiomp3",
4344
]
4445

4546

examples/macropad_mp3/beats.mp3

13.6 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: CC-BY-SA-4.0

examples/macropad_mp3/happy.mp3

24.1 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: CC-BY-SA-4.0

examples/macropad_mp3/macropad_mp3.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
MacroPad MP3 playback demo. Plays one of four different MP3 files when one of the first four keys
6+
is pressed. All keys light up a color of the rainbow when pressed, but no audio is played for the
7+
rest of the keys.
8+
"""
9+
from rainbowio import colorwheel
10+
from adafruit_macropad import MacroPad
11+
12+
macropad = MacroPad()
13+
14+
# To include more MP3 files, add the names to this list in the same manner as the others.
15+
# Then, press the key associated with the file's position in the list to play the file!
16+
audio_files = ["slow.mp3", "happy.mp3", "beats.mp3", "upbeats.mp3"]
17+
18+
while True:
19+
key_event = macropad.keys.events.get()
20+
21+
if key_event:
22+
if key_event.pressed:
23+
macropad.pixels[key_event.key_number] = colorwheel(
24+
int(255 / 12) * key_event.key_number
25+
)
26+
if key_event.key_number < len(audio_files):
27+
macropad.play_file(audio_files[key_event.key_number])
28+
29+
else:
30+
macropad.pixels.fill((0, 0, 0))
31+
macropad.stop_tone()

examples/macropad_mp3/slow.mp3

12.4 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: CC-BY-SA-4.0

examples/macropad_mp3/upbeats.mp3

34.8 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: CC-BY-SA-4.0

0 commit comments

Comments
 (0)