spatialaudio / python-sounddevice

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

**rec_unlimited.py** but at different files per minute #425

Open ghost opened 1 year ago

ghost commented 1 year ago

Hello! I have a big doubt, after trying several of my own codes and the examples in the repository, I can't adapt the code from the rec_unlimited.py file. My idea is to execute this file but every X minute to create a new audio file without stopping the stream process so as not to lose information. For example, every minute a new file is created. I don't know how to approach this because in the rec_unlimited.py example a tempor file is defined before opening the stream and sf.SoundFile, so if I need to create another file I don't know how to redefine the name and have the stream process not cut, any suggestions? Thanks in advance

mgeier commented 1 year ago

in the rec_unlimited.py example a tempor file is defined before opening the stream and sf.SoundFile

You will have to swap the sound file and the stream: open the stream first, and then, within a loop, open a sound file (each time with a new name).

Then, read audio data from the queue and write it to the current file. Once you have written the desired amount of data, finish writing to the file and then continue with the next loop iteration.

The important thing to get no gaps is that the audio callback always keeps writing to the queue without interruption.

davemac327 commented 2 months ago

I'm curious if @ghost ever got it to work. I want to do a very similar thing but also don't want any gaps between what each file has recorded. I'm hoping that I can switch files before the input queue overflows. Any advice on the best way to ensure no gaps would be great. I plan to know in advance how many files I will need, so could in theory open them all in advance, then close and move on to the next one as the previous one fills up, but I don't like that idea.

mgeier commented 2 months ago

@ghost is a dummy user name, so you won't get an answer from there.

Any advice on the best way to ensure no gaps would be great.

You'll never get a guarantee (on a non-realtime operating system), because there will always be things that might take longer than expected.

My advice would be to use conservative settings, test it thoroughly in worst-case conditions, and hope for the best!