Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Other actions while playing a WAV
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Other actions while playing a WAV

by greisinb on Thu Mar 11, 2021 3:53 pm

Hi, I'm currently using the Mu editor and am playing a WAV file with the cp.play_file() command. Is there a way to have the CPX start playing the file and then move on to other tasks while the sound continues to play?

greisinb
 
Posts: 13
Joined: Wed Jul 08, 2015 12:40 am

Re: Other actions while playing a WAV

by tannewt on Fri Mar 12, 2021 4:14 pm

Take a look at the `play_file` implementation: https://github.com/adafruit/Adafruit_Ci ... se.py#L827

It has a loop to wait while the sound is playing. Instead of calling `play_file` you can copy that code block and modify it to not block. The CircuitPlayground library is designed to be easy and the tradeoff is that it can't do everything folks want to do. To do more advanced things, you'll have to work at one level lower.

tannewt
 
Posts: 2390
Joined: Thu Oct 06, 2016 8:48 pm

Re: Other actions while playing a WAV

by greisinb on Fri Mar 12, 2021 4:38 pm

Ahh, excellent. Thank you so much for getting to my question, I appreciate it.

greisinb
 
Posts: 13
Joined: Wed Jul 08, 2015 12:40 am

Re: Other actions while playing a WAV

by greisinb on Fri Mar 12, 2021 5:04 pm

When I put the following in Mu and check it:

Code: Select all | TOGGLE FULL SIZE
 self.stop_tone()
        self._speaker_enable.value = True
        with self._audio_out(board.SPEAKER) as audio:  # pylint: disable=not-callable
            wavefile = audiocore.WaveFile(open(file_name, "rb"))
            audio.play(wavefile)
            while audio.playing:
                pass
        self._speaker_enable.value = False


It tells me that the line "self._speaker_enable.value = True" has a syntax error, Python cannot understand this line. I've tried deleting it and retyping it just to make sure it wasn't a copy/paste error. The rest of it checks out.

greisinb
 
Posts: 13
Joined: Wed Jul 08, 2015 12:40 am

Re: Other actions while playing a WAV

by tannewt on Fri Mar 12, 2021 8:38 pm

Double check all of your indentation. You also want self to be `cp` since you are moving code out of the object.

tannewt
 
Posts: 2390
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.