Skip to content

Add MP3 playback and example with files. #25

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 3 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
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
170 changes: 170 additions & 0 deletions LICENSES/CC-BY-SA-4.0.txt

Large diffs are not rendered by default.

45 changes: 35 additions & 10 deletions adafruit_macropad.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import displayio
import audiopwmio
import audiocore
import audiomp3
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
Expand Down Expand Up @@ -851,9 +852,11 @@ def stop_tone(self):
self._speaker_enable.value = False

def play_file(self, file_name):
"""Play a .wav file using the onboard speaker.
"""Play a .wav or .mp3 file using the onboard speaker.

:param file_name: The name of your .wav file in quotation marks including .wav
:param file_name: The name of your .wav or .mp3 file in quotation marks including
.wav or .mp3, e.g. "sound.wav" or "sound.mp3". Include location if file
is placed somewhere other than /, e.g. "audio/sound.wav".

The following example plays the file "sound.wav" when the rotary encoder switch is pressed.

Expand All @@ -866,17 +869,39 @@ def play_file(self, file_name):
while True:
if macropad.encoder_switch:
macropad.play_file("sound.wav")

The following example plays the file "sound.mp3" when the rotary encoder switch is pressed.

.. code-block:: python

from adafruit_macropad import MacroPad

macropad = MacroPad()

while True:
if macropad.encoder_switch:
macropad.play_file("sound.mp3")
"""
# Play a specified file.
self.stop_tone()
self._speaker_enable.value = True
with audiopwmio.PWMAudioOut(
board.SPEAKER
) as audio: # pylint: disable=not-callable
wavefile = audiocore.WaveFile(open(file_name, "rb"))
audio.play(wavefile)
while audio.playing:
pass
if file_name.lower().endswith(".wav"):
with audiopwmio.PWMAudioOut(
board.SPEAKER
) as audio: # pylint: disable=not-callable
wavefile = audiocore.WaveFile(open(file_name, "rb"))
audio.play(wavefile)
while audio.playing:
pass
elif file_name.lower().endswith(".mp3"):
with audiopwmio.PWMAudioOut(
board.SPEAKER
) as audio: # pylint: disable=not-callable
mp3file = audiomp3.MP3Decoder(open(file_name, "rb"))
audio.play(mp3file)
while audio.playing:
pass
else:
raise ValueError("Filetype must be wav or MP3.")
self._speaker_enable.value = False


Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"audiopwmio",
"audiocore",
"adafruit_debouncer",
"audiomp3",
]


Expand Down
Binary file added examples/macropad_mp3/beats.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/macropad_mp3/beats.mp3.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
#
# SPDX-License-Identifier: CC-BY-SA-4.0
Binary file added examples/macropad_mp3/happy.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/macropad_mp3/happy.mp3.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
#
# SPDX-License-Identifier: CC-BY-SA-4.0
31 changes: 31 additions & 0 deletions examples/macropad_mp3/macropad_mp3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this file be examples/macropad_mp3/code.py? I am not sure about the new rules.

This comment was marked as duplicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For library code, Bundlefly automatically changes the file to code.py so that the files can continue to stay within library standards and Adabot remains happy.

#
# SPDX-License-Identifier: Unlicense
"""
MacroPad MP3 playback demo. Plays one of four different MP3 files when one of the first four keys
is pressed. All keys light up a color of the rainbow when pressed, but no audio is played for the
rest of the keys.
"""
from rainbowio import colorwheel
from adafruit_macropad import MacroPad

macropad = MacroPad()

# To include more MP3 files, add the names to this list in the same manner as the others.
# Then, press the key associated with the file's position in the list to play the file!
audio_files = ["slow.mp3", "happy.mp3", "beats.mp3", "upbeats.mp3"]

while True:
key_event = macropad.keys.events.get()

if key_event:
if key_event.pressed:
macropad.pixels[key_event.key_number] = colorwheel(
int(255 / 12) * key_event.key_number
)
if key_event.key_number < len(audio_files):
macropad.play_file(audio_files[key_event.key_number])

else:
macropad.pixels.fill((0, 0, 0))
macropad.stop_tone()
Binary file added examples/macropad_mp3/slow.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/macropad_mp3/slow.mp3.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
#
# SPDX-License-Identifier: CC-BY-SA-4.0
Binary file added examples/macropad_mp3/upbeats.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/macropad_mp3/upbeats.mp3.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 Bartlebeats for Adafruit Industries
#
# SPDX-License-Identifier: CC-BY-SA-4.0