bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
680 stars 69 forks source link

Simultaneously play and record #48

Closed gonzalocasas closed 5 years ago

gonzalocasas commented 5 years ago

Is it possible to playback audio and record it simultaneously? I didn't find a non-blocking way to call .record() (except perhaps passing None to numframes?) so that I can start playing back sound after the recording is active. For context, I want to playback a sweep signal and record it to later on calculate the impulse response and do some processing, I initially used threading (and then multiprocessing) with a threading.Event to sync them, but identical playback/recording sessions have slight different, non-deterministic offsets in the range of +/-10ms.

I am running this on Linux with Python 3.6.7, and soundcard=0.2.2.

Is there a way to achieve this directly on soundcard without resorting to threading or other ways to parallelize?

Thanks!!

bastibe commented 5 years ago

There is indeed a way, and it is exactly what you thought: record(numframes=None) will return whatever is currently buffered without waiting for a buffer to fill.

At least on Linux, we have a latency attribute, which should be useful for syncing streams. This is entirely up to the backend system, though, and I have no idea how reliable these numbers are across sound cards.

gonzalocasas commented 5 years ago

@bastibe thanks for the reply! I'll give it a try and report here!

gonzalocasas commented 5 years ago

btw...there is no example on using numframes=None, is there?

bastibe commented 5 years ago

Only in the documentation.

bastibe commented 5 years ago

But... pull requests are welcome!

gonzalocasas commented 5 years ago

In the end, I resorted to hardware support instead, using the loopback of the mic, so then it's not so relevant when the record starts (in a different thread/process), because the precise synchronization can be done based on the loopback channel. So, I think this issue could be closed.

bastibe commented 5 years ago

Good to know. Thank you for following up on this!