Skip to content

Commit 425afbd

Browse files
kattnitannewt
authored andcommitted
Added play_file
1 parent e87cfdd commit 425afbd

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

adafruit_circuitplayground/express.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def __init__(self):
4949
# Define audio:
5050
self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
5151
self._speaker_enable.switch_to_output(value=False)
52-
5352
self.sample = None
5453
self.sine_wave = None
5554

@@ -445,6 +444,32 @@ def stop_tone(self):
445444
self.sample.stop()
446445
self._speaker_enable.value = False
447446

447+
def play_file(self, file_name):
448+
""" Play a .wav file using the onboard speaker.
449+
450+
:param file_name: The name of your .wav file in quotation marks including .wav
451+
452+
.. image :: /_static/speaker.jpg
453+
454+
.. code-block:: python
455+
456+
from adafruit_circuitplayground.express import cpx
457+
458+
while True:
459+
if cpx.button_a:
460+
cpx.play_file("laugh.wav")
461+
elif cpx.button_b:
462+
cpx.play_file("rimshot.wav")
463+
"""
464+
# Play a specified file.
465+
self._speaker_enable.value = True
466+
self.a = audioio.AudioOut(board.SPEAKER, open(file_name, "rb"))
467+
468+
self.a.play()
469+
while self.a.playing:
470+
pass
471+
self._speaker_enable.value = False
472+
448473

449474
cpx = Express()
450475
"""Object that is automatically created on import.

0 commit comments

Comments
 (0)