-
Notifications
You must be signed in to change notification settings - Fork 71
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are right that its just a library issue. Its a simple fix though. Concurrency is tricky. :-)
audio.play() | ||
while audio.playing: | ||
pass | ||
except RuntimeError: |
There was a problem hiding this comment.
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.
Thanks for the suggestion! Tested successfully with suggested changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. Looks good!
Updating https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground to 2.0.2 from 2.0.1: > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#36 from kattni/play-file-stop-tone-fix
Added
deinit()
to both. You still get aRuntimeError
ofDAC already in use
if you are currently playing a tone usingstart_tone
and then try toplay_file
before ending the tone. Addedtry
andexcept
toplay_file
to avoid this error if these functions are used in this manner.