Continuiing execution while using cp.play_file()

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
HowdyMoto
 
Posts: 23
Joined: Sat Jun 05, 2021 11:41 pm

Continuiing execution while using cp.play_file()

Post by HowdyMoto »

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!

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Continuiing execution while using cp.play_file()

Post by mikeysklar »

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

    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

User avatar
HowdyMoto
 
Posts: 23
Joined: Sat Jun 05, 2021 11:41 pm

Re: Continuiing execution while using cp.play_file()

Post by HowdyMoto »

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

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)

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Continuiing execution while using cp.play_file()

Post by mikeysklar »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”