adafruit / Adafruit_CircuitPython_PyPortal

CircuitPython driver for Adafruit PyPortal.
MIT License
46 stars 57 forks source link

play_file() with wait_to_finish=False does not play #118

Closed FoamyGuy closed 2 years ago

FoamyGuy commented 2 years ago

In the currently released version of this library calling pyportal.peripherals.play_file("somefile.wav", wait_to_finish=False) results in no audio playing.

Full reproducer script:

import board
from adafruit_pyportal import PyPortal

# Create the PyPortal object
pyportal = PyPortal()

# Set display to show REPL
board.DISPLAY.show(None)

pyportal.peripherals.play_file("piano2.wav", wait_to_finish=False)

while True:
    pass

Running the same code on version 6.0.0 of this library does play the wav file.

I think the change to use with context processor is what is causing it not to play the file when wait_to_finish is False

In this section of code it returns in the case of False for wait_to_finish the return causes it to close the with context which closes the file and causes it not to be able to be played.

https://github.com/adafruit/Adafruit_CircuitPython_PyPortal/blob/a160fd188a66e0f91f445e899a9a38876700a541/adafruit_pyportal/peripherals.py#L149-L157

Unless there is some way to make the lifetime of the with block continue to remain open after the play_file() function returns I think we would need to remove the usage of the with. We could store the reference to the open file on self and close it on subsequent calls before opening a new one.

FoamyGuy commented 2 years ago

I think it may be good to merge #116 to fix this. That PR was already implementing similar behavior by adding a stop_play() function which does close the file. Afterwards it could also close the file at the beginning of play_file() if It's still open.

FoamyGuy commented 2 years ago

resolved by #116