Capturing several devices simultaneously
See original GitHub issueIs it possible to record from several devices simultaneously?
I have 4 devices and I want to capture simultaneaously from them. I have tried 3 approaches in this order:
1. Consecutive calls to sd.rec()
:
Tried something like:
sd.rec( ..., device=1)
sd.rec( ..., device=2)
sd.rec( ..., device=3)
sd.rec( ..., device=4)
sd.wait()
But plotting the data (4 outputs concatenated) I got this. Just device 4 is fully recorded:
2. Multiprocess:
I wrapped sd.rec() function in a new one, and tried to use it via multiprocess, but got the “freeze_support” error:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
3. subprocess (isolated recording script)
Finally I opt to create a python script for recording having as input the device number, and use a “master” script to call it with the required devices, and it works. Something like:
for i in [1,2,3,4]:
p = Popen("python record_script.py " + str(i))
The only caveat here is that I can’t stop or handle the recording once it has started because, obviously, this is a new process in the OS.
Is there any approach more related to my attempts 1 and 2 for recording multiple devices?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
I do not have any real experience with using the
sounddevice
module in multiple threads/processes at the same time. Here are a few related issues: #118, #120, #147.Your option 1 will definitely not work. The
rec()
function is made for exclusive use. If you startrec()
while another invocation is still running, the previous recording is stopped, as you have noticed.Your option 2 might work. If you need help avoiding the exception you were mentioning, you should provide some code. If I can reproduce the problem I might be able to help, but no guarantees!
If your option 3 does what you need, that’s fine. But I think if option 3 works, the same thing should also work with
multiprocessing
.Depending on your OS and host API, it might also be possible to create 4
InputStream
objects in the same thread, one for each input device. This is not officially supported, but it might still work for you. In this case you would have 4 callback functions running at the same time (the underlying PortAudio library will create a separate thread for each of them). You could e.g. have a queue for each callback and the main thread would read the input data from the 4 queues.Yet another option would be to try to combine the input devices with some OS tool. For example, on macOS it is possible to make an “aggregate device” by combining multiple existing devices. With a custom ALSA setup, a similar thing might be possible. There might be other tools available.
OK, thanks for the update!
I guess I can close this issue now. If you have further questions, feel free to open further issues or add comments here.