0

Continuiing execution while using cp.play_file()
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Continuiing execution while using cp.play_file()

by HowdyMoto on Mon Jul 26, 2021 4:19 pm

Hi all,

I'm using the adafruit_circuitpython libraries, and would like to be able to play a sound file while also doing other things. the cp.play_file() function appears to wait until teh wav file is done playing before the next line will execute. Is there a way to make Python do that function and keep executing the code?

I know I can use audiocore to do this, but using audiocore and the other libs I need to manipulate audio conflicts with the use of adafruit_circuitpython, and I'd love to keep my code as dead simple as possible.

Thanks!

HowdyMoto
 
Posts: 14
Joined: Sat Jun 05, 2021 11:41 pm

Re: Continuiing execution while using cp.play_file()

by mikeysklar on Tue Jul 27, 2021 6:18 pm

Howdy Moto,

I looked at the source code for the CircuitPlayground and it was quite basic for play_file(). It is going to play through without any sort of background options until the file is finished.

https://github.com/adafruit/Adafruit_Ci ... nd_base.py

Code: Select all | TOGGLE FULL SIZE
    def play_file(self, file_name):
        """Play a .wav file using the onboard speaker.
        :param file_name: The name of your .wav file in quotation marks including .wav
        .. image :: ../docs/_static/speaker.jpg
          :alt: Onboard speaker
        To use with the Circuit Playground Express or Bluefruit:
        .. code-block:: python
             from adafruit_circuitplayground import cp
             while True:
                 if cp.button_a:
                     cp.play_file("laugh.wav")
                 elif cp.button_b:
                     cp.play_file("rimshot.wav")
        """
        # Play a specified file.
        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

mikeysklar
 
Posts: 5574
Joined: Mon Aug 01, 2016 8:10 pm

Re: Continuiing execution while using cp.play_file()

by HowdyMoto on Wed Jul 28, 2021 1:21 am

Thanks for taking a look, mikeysklar. I discovered that if my code is in the same with block as the play() command, it will keep executing while the sound plays. Here's the code that works as expected: a sound plays while the LEDS light up on my CircuitPlayground Express.

Code: Select all | TOGGLE FULL SIZE
wav_data = open("Tones.wav", "rb")
wav_file = WaveFile(wav_data)

def boot_up():
    with AudioOut(board.SPEAKER) as audio:
        audio.play(wav_file)
        pixels[0] = WHITE
        pixels[9] = WHITE
        time.sleep(0.05)
        pixels[1] = WHITE
        pixels[8] = WHITE
        time.sleep(0.05)
        pixels[2] = WHITE
        pixels[7] = WHITE
        time.sleep(0.05)
        pixels[3] = WHITE
        pixels[6] = WHITE
        time.sleep(0.05)
        pixels[4] = WHITE
        pixels[5] = WHITE
        time.sleep(0.05)

HowdyMoto
 
Posts: 14
Joined: Sat Jun 05, 2021 11:41 pm

Re: Continuiing execution while using cp.play_file()

by mikeysklar on Wed Jul 28, 2021 5:38 pm

Nicely done. Glad to see audio was a workable solution for you.

mikeysklar
 
Posts: 5574
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.