spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
980 stars 145 forks source link

Running rec_unlimited.py within other Python code #447

Open bwinsto2 opened 1 year ago

bwinsto2 commented 1 year ago

I am trying to write/use your code to make a recording that terminates when either 1) there is a keyboard press, or 2) there is a (configurable) number of seconds that have passed without any speech.

Normally when I want to run one python file within another, I will use the subprocess function. But when I try to use subprocess to run rec_unlimited.py, I can't use KeyboardInterrupt to stop the recording b/c it will simply stop the subprocess rather than the recording.

Does this make sense? Sorry I am a psychologist - not a computer scientist. Do you have any suggestions or resources you can point me towards to get what I described in the first paragraph?

Thanks :)

HaHeho commented 1 year ago

It should be possible to catch the KeyboardInterrupt in the parent application (similar to within rec_unlimited.py) and react accordingly, e.g. kill or send a terminate signals to the subprocess (I haven't used subprocess, so not sure which is the best way to do this). You might want to prevent the parent application from exciting by the subprocess startup, exception handling, and subprocess termination within an infinite loop in the parent application, which is exciting differently. I hope this helps as a first idea.

mgeier commented 1 year ago

Any updates @bwinsto2?

I just wanted to add that running a second Python interpreter with subprocess is normally not the way to re-use a Python file.

The typical way to use a third-party Python file/library/module is with import.

However, the examples here are not really made to be used like this (except maybe the two asyncio_*.py examples). I would recommend that you just copy the code that you find interesting to your own file and start experimenting with it. In the beginning, you can write everything into a single file.