Compress pyaudio stream with zlib

I am trying to send audio from a microphone input between a server and client using pyaudio, I only need voice quality sampled at a rate of 8000. Without compression it works fine and I am trying to add zlib compression to reduce the bandwidth.

In my server the stream_callback function is

def callback(in_data, frame_count, time_info, status):
    for s in read_list[1:]:
        s.send(zlib.compress(in_data))
    return (None, pyaudio.paContinue)

In my client I am trying to decompress like this

try:
    while True:
        data = s.recv(CHUNK)
        stream.write(zlib.decompress(data, zlib.MAX_WBITS | 16))
except KeyboardInterrupt:
    pass

I have tried various parameters with zlib.MAX_WBITS but all return this error:

zlib.error: Error -3 while decompressing data: incorrect header check

Edit: I have also tried with no second parameter with zlib.decompress

Can someone suggest what I am doing wrong please, TIA

1 answer

  • answered 2021-02-23 08:02 Mark Adler

    You don't need the second parameter of zlib.decompress at all. What you have in the question would look for a gzip stream instead of a zlib stream.

    For compressing audio, you should use an audio compressor. Take a look at this answer.