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

Conversation

kattni
Copy link
Contributor

@kattni kattni commented Jun 15, 2018

Added deinit() to both. You still get a RuntimeError of DAC already in use if you are currently playing a tone using start_tone and then try to play_file before ending the tone. Added try and except to play_file to avoid this error if these functions are used in this manner.

Copy link
Contributor

@caternuson caternuson left a 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:
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.

@kattni
Copy link
Contributor Author

kattni commented Jun 15, 2018

Thanks for the suggestion! Tested successfully with suggested changes.

Copy link
Contributor

@caternuson caternuson left a comment

Choose a reason for hiding this comment

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

Yep. Looks good!

@caternuson caternuson merged commit d0aa6dc into adafruit:master Jun 15, 2018
@kattni kattni deleted the play-file-stop-tone-fix branch June 15, 2018 22:07
tannewt pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jun 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants