Fast way to compress audio data?
<p>I'm trying to build (Inspirated by teamspeak) a voip program that comunicate via UDP.</p>

<p>Here is my source (Server):</p>

<pre><code>import pyaudio
import socket

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels = CHANNELS,
                rate = RATE,
                output = True,
                frames_per_buffer = CHUNK,
                )

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind(("0.0.0.0", 4444))

while True:
    soundData, addr = udp.recvfrom(CHUNK * CHANNELS * 2)
    stream.write(soundData, CHUNK)
    print len(soundData)

udp.close()
</code></pre>

<p>Client:</p>

<pre><code>import pyaudio
import socket


CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                frames_per_buffer = CHUNK,
                )

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while True:
    udp.sendto(stream.read(CHUNK), ("127.0.0.1", 4444))

udp.close()
</code></pre>

<p>It works great on LAN, but in WAN the audio is very bad.</p>

<p>I assumed that raw audio is not good for voip, and I'm looking for a way to compress the audio using a lossy algorithm or an encoder (mp3, AAC, ogg)</p>

<p>I tried LZMA, but I don't need lossless compression, lossy is better in my case.</p>

<p>I have two rules to follow:</p>

<ol>
<li><p>The program must be cross platform, so I need a way to compress/decompress in a "cross platform" way (Inside python)</p></li>
<li><p>Audio quality should be good (not below 50% of original)</p></li>
</ol>

<p>FFmpeg works on Pipe protocols, and the same functionality has been ported to ffmpy so data can be written to stdin and read from stdout. You would likely have to provide some timing constructs as well to handle synchronization and appropriate buffer managment, but I see no reason this couldn't be made to work. </p>

<p>ffmpy: <a href="http://ffmpy.readthedocs.io/en/latest/examples.html#using-pipe-protocol" rel="nofollow">using the <code>pipe</code> protocol</a></p>

<p>live audio streaming with <a href="http://raspberrypi.stackexchange.com/a/4660">FFmpeg on a rasberry pi</a></p>

python audio encoding compression