Using threads for audio computing?

lgabiot lgabiot at hotmail.com
Sun May 11 11:40:27 EDT 2014


Le 11/05/14 16:40, Roy Smith a écrit :
> In article <536f869c$0$2178$426a74cc at news.free.fr>,
>   lgabiot <lgabiot at hotmail.com> wrote:
>
>> Hello,
>>
Le 11/05/14 16:40, Roy Smith a écrit :
> If you are going to use threads, the architecture you describe seems
> perfectly reasonable.  It's a classic producer-consumer pattern.
>
> But, I wonder if you even need anything this complicated.  Using a queue
> to buffer work between threads makes sense if the workload presented is
> uneven.  Sometimes you'll get a burst of work all at once and don't have
> the capacity to process it in real-time, so you want to buffer it up.
>
> I would think sampling audio would be a steady stream.  Every x ms, you
> get another chunk of samples, like clockwork.  Is this not the case?
>

Thanks for your answer,

yes, I guess I can consider audio as a steady stream. PyAudio gives me 
the audio samples by small chunks (2048 samples at a time for instance, 
while the sound card gives 48 000 samples/seconds). I accumulate the 
samples into a numpy array, and once the numpy array has reached the 
needed size (for instance 5 seconds of audio), I put this numpy array in 
the queue. So I think you are right in thinking that every 5 seconds I 
get a new chunk of audio to work on. Then I perform a calculation on 
this 5 seconds of audio (which needs to be done in less than 5 seconds, 
so that it will be ready to process the next 5 second chunk), but 
meanwhile, I need to still constantly get from Pyaudio a new 5 second 
chunk of audio. Hence my system.

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.







More information about the Python-list mailing list