Using threads for audio computing?

Stefan Behnel stefan_ml at behnel.de
Mon May 12 02:13:32 EDT 2014


lgabiot, 12.05.2014 07:33:
> Le 11/05/14 17:40, lgabiot a écrit :
> 
>> I guess if my calculation had to be performed on a small number of
>> samples (i.e. under the value of the Pyaudio buffer size (2048 samples
>> for instance), and that the calculation would last less than the time it
>> takes to get the next 2048 samples from Pyaudio, I wouldn't need the
>> Queue and Thread system.
>> But in my case where I need a large buffer, it might not work?
>> Unless I ask pyaudio to feed me directly with 5 seconds chunks (instead
>> of the usual buffer sizes: 1024, 2048, etc...), which I didn't try,
>> because I hadn't though of it.
> 
> I guess this solution might probably not work, since it would mean that the
> calculation should be quick enough so it wouldn't last longer than 1 sample
> (1/48000 s for instance), since while doing the calculation, no audio would
> be ingested (unless pyAudio possess some kind of internal concurrency system).
> Which leads me to think that a buffer (queue) and separate threads
> (producer and consumer) are necessary for this task.

This sounds like a use case for double buffering. Use two buffers, start
filling one. When it's full, switch buffers, start filling the second and
process the first. When the second is full, switch again.

Note that you have to make sure that the processing always terminates
within the time it takes to fill the other buffer. If you can't assure
that, however, you have a problem anyway and should see if there's a way to
improve your algorithm.

If the "fill my buffer" call in PyAudio is blocking (i.e. if it returns
only after filling the buffer), then you definitely need two threads for this.


> But AFAIK the python GIL (and in smaller or older computers that have only
> one core) does not permit true paralell execution of two threads.

Not for code that runs in the *interpreter", but it certainly allows I/O
and low-level NumPy array processing to happen in parallel, as they do not
need the interpreter.

Stefan





More information about the Python-list mailing list