diff --git a/adafruit_circuitplayground/bluefruit.py b/adafruit_circuitplayground/bluefruit.py index 04623d4..b48feb4 100755 --- a/adafruit_circuitplayground/bluefruit.py +++ b/adafruit_circuitplayground/bluefruit.py @@ -42,6 +42,7 @@ import digitalio import board import audiopwmio +import audiomp3 import audiobusio from adafruit_circuitplayground.circuit_playground_base import CircuitPlaygroundBase @@ -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. diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index d0f61fb..73d0f9a 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -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 diff --git a/docs/conf.py b/docs/conf.py index 7c9853b..5f84980 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -40,6 +40,7 @@ "audiocore", "audiopwmio", "audiobusio", + "audiomp3", ] # Add any paths that contain templates here, relative to this directory. diff --git a/examples/circuitplayground_bluefruit_play_mp3.py b/examples/circuitplayground_bluefruit_play_mp3.py new file mode 100644 index 0000000..eccfb94 --- /dev/null +++ b/examples/circuitplayground_bluefruit_play_mp3.py @@ -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") diff --git a/examples/dip.mp3 b/examples/dip.mp3 new file mode 100644 index 0000000..1e4e595 Binary files /dev/null and b/examples/dip.mp3 differ diff --git a/examples/rise.mp3 b/examples/rise.mp3 new file mode 100644 index 0000000..f944814 Binary files /dev/null and b/examples/rise.mp3 differ