Dockerfile loads Rpi with sounddevice image BUT, program execution is weird

This Github has a project that attempts to install sounddevice and use it within Python3

The Dockerfile and other associated files suffice to create an image that downloads into my Rpi.
The goal is to be able to import sounddevice into Python3 and execute various commands.

After much fiddling, the image loads and if I start Python3 from the Balena-cloud terminal window, the Rpi runs sounddevice examples. Great.

But, if I try to directly execute a python3 script via python3 playFile.py or ./playFile the Rpi fails play the wav file (via the Rpi output jack).

My question is: Why should it run if the code is pasted into python3 via the balena-cloud terminal window but not run when invoked via python3 followed by the script name or ./ followed by the script name.

Thanks for any suggestions,
Val

Hi @val the issue you have here is that sd.play() allows script execution to continue whilst the audio plays in the background.
From the sound device playback docs ( https://python-sounddevice.readthedocs.io/en/0.3.14/usage.html#playback ):

This function returns immediately but continues playing the audio signal in the background.

In your application, when you run the script file, the python interpreter runs the sd.play() method, but then continues executing, gets to the end of the file and exits the process. On the other hand, when you copy and paste your code into the python3 REPL, the process doesn’t exit and you stay in the active python process, so the audio continues to play.
You can use the sd.wait() method to block the python interpreter until playback is finished.
I sent a PR to your GitHub repository showing how this would work in your application https://github.com/veirs/sounddevice/pull/1/files
I tried this locally on a test device and it worked great!
Let me know if this fixes your issue

@Lucian,

Thank you so much for catching my omission. I should have found that myself but I greatly appreciate your suggestion.

Cheers,
Val