Skip to content

Resolves issues using play_file and start/stop_tone #36

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 5 commits into from
Jun 15, 2018
Merged
Changes from 4 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
28 changes: 17 additions & 11 deletions adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ def stop_tone(self):
# Stop playing any tones.
if self._sample is not None and self._sample.playing:
self._sample.stop()
self._sample.deinit()
self._sample = None
self._speaker_enable.value = False

def play_file(self, file_name):
Expand All @@ -671,17 +673,21 @@ def play_file(self, file_name):
"""
# Play a specified file.
self._speaker_enable.value = True
if sys.implementation.version[0] >= 3:
audio = audioio.AudioOut(board.SPEAKER)
file = audioio.WaveFile(open(file_name, "rb"))
audio.play(file)
while audio.playing:
pass
else:
audio = audioio.AudioOut(board.SPEAKER, open(file_name, "rb"))
audio.play()
while audio.playing:
pass
try:
if sys.implementation.version[0] >= 3:
audio = audioio.AudioOut(board.SPEAKER)
file = audioio.WaveFile(open(file_name, "rb"))
audio.play(file)
while audio.playing:
pass
audio.deinit()
else:
audio = audioio.AudioOut(board.SPEAKER, open(file_name, "rb"))
audio.play()
while audio.playing:
pass
except RuntimeError:
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't require the try: except:. I think you need a stop_tone() call at the start of this in case one does:

cpx.start_tone(440)
cpx.play_file("sample.wav")

I'd also suggest using with to manage making sure deinit is called:

            if sys.implementation.version[0] >= 3:
                with audioio.AudioOut(board.SPEAKER) as audio:
                    wavefile = audioio.WaveFile(open(file_name, "rb"))
                    audio.play(wavefile)
                    while audio.playing:
                        pass
            else:
                with audioio.AudioOut(board.SPEAKER, open(file_name, "rb")) as audio:
                    audio.play()
                    while audio.playing:
                        pass

I also renamed file to wavefile because file is/was a python keyword which can make things weird later.

pass
self._speaker_enable.value = False


Expand Down