Skip to content

add play_mp3 function to bluefruit module #95

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 4 commits into from
Jun 17, 2020
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
36 changes: 36 additions & 0 deletions adafruit_circuitplayground/bluefruit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import digitalio
import board
import audiopwmio
import audiomp3
import audiobusio
from adafruit_circuitplayground.circuit_playground_base import CircuitPlaygroundBase

Expand Down Expand Up @@ -151,6 +152,41 @@ def loud_sound(self, sound_threshold=200):

return self.sound_level > sound_threshold

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

:param file_name: The name of your .mp3 file in quotation marks including .mp3

.. image :: ../docs/_static/speaker.jpg
:alt: Onboard speaker

To use with the Circuit Playground Bluefruit:

.. code-block:: python

from adafruit_circuitplayground import cp

while True:
if cp.button_a:
cp.play_mp3("laugh.mp3")
elif cp.button_b:
cp.play_mp3("rimshot.mp3")
"""
if file_name.lower().endswith(".mp3"):
# Play a specified file.
self.stop_tone()
self._speaker_enable.value = True
with self._audio_out(
board.SPEAKER
) as audio: # pylint: disable=not-callable
mp3file = audiomp3.MP3Decoder(open(file_name, "rb"))
audio.play(mp3file)
while audio.playing:
pass
self._speaker_enable.value = False
else:
raise ValueError("Filetype must be mp3")


cpb = Bluefruit() # pylint: disable=invalid-name
"""Object that is automatically created on import.
Expand Down
1 change: 1 addition & 0 deletions adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def _unsupported(self):
# Express, they will result in the NotImplementedError raised in the property above.
sound_level = _unsupported
loud_sound = _unsupported
play_mp3 = _unsupported


cpx = Express() # pylint: disable=invalid-name
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 @@
"audiocore",
"audiopwmio",
"audiobusio",
"audiomp3",
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
12 changes: 12 additions & 0 deletions examples/circuitplayground_bluefruit_play_mp3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
This example plays mp3 audio files from the built-in speaker when the A or B buttons are pressed.

NOTE: This example does NOT support Circuit Playground Express.
"""
from adafruit_circuitplayground import cp

while True:
if cp.button_a:
cp.play_mp3("dip.mp3")
if cp.button_b:
cp.play_mp3("rise.mp3")
Binary file added examples/dip.mp3
Binary file not shown.
Binary file added examples/rise.mp3
Binary file not shown.