question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Capturing several devices simultaneously

See original GitHub issue

Is 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:

wrong

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.

error.txt

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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mgeiercommented, Oct 18, 2018

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 start rec() 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.

0reactions
mgeiercommented, Sep 30, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Record multiple devices at the same time - Reflector
Recording multiple devices at the same time works very similarly to recording a single device. When mirroring a device, begin recording by ...
Read more >
'DoubleTake' app that lets iPhone users record video from two ...
'DoubleTake' app that lets iPhone users record video from two devices at the same time to more easily create multi-angle films rolls out...
Read more >
Collecting data from Multiple Devices Simultaneously | TALtech
WinWedge and TCPWedge can easily be used to collect data from multiple devices simultaneously. There are several ways to do this, but perhaps...
Read more >
Create an Aggregate Device to combine multiple audio devices
With an Aggregate Device, you can use audio inputs and outputs from different devices at the same time with Logic Pro, GarageBand, ...
Read more >
MULTIPLE Capture Cards on ONE System - YouTube
In this video find out how to correctly install TWO Elgato HDMI capture cards on a single machine. It may sound simple, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found