I am using pyaudio in callback mode with paFloat32
format, 2 channels, 1024 frames per buffer, and I am interested in a more efficient input/output audio buffer data exchange.
To unpack an input audio buffer and get the list of float samples, I use:
fmt = str( N_CHANNELS * BUFFER_SIZE ) + 'f'
in_floats = struct.unpack( fmt, in_data )
Using struct.pack()
and struct.unpack()
is quite inefficient and it takes significant CPU resources, almost the same as the audio signal processing itself. Since most sound cards are 16 bit, I also tried to use the paInt16
format, but the results are almost identical.
What would be the most efficient format and pack/unpack method to use in callback mode (of course maintaining full resolution)?
Edit: PyAudio exchanges data using binary streams or buffers similar to the C data structures used with Portaudio. I need to unpack the in_data
input buffer to get the float samples and analyze them. Everyting is OK, except the unpack is a bit slow.
struct
is relatively fast. If you need something faster, you probably don't want to use python for the job. You can try to write the callback in C, or avoid unpacking altogether – loopbackbeenumpy
, but since you're dealing with relatively small buffers, the overhead may be big enough to negate the benefit – loopbackbeefrombuffer
might be useful to you here.. docs.scipy.org/doc/numpy/reference/generated/… If you want to follow up, please post some minimal but functional code so we can see what you're trying to do. – tom10