I am streaming audio in my flask app from a client to the server, but the received audio has very low quality.
On the client, I preprocess the audio buffer as follows:
this.node.onaudioprocess = function(e){
var buf = e.inputBuffer.getChannelData(0);
var out = new Int16Array(buf.length);
for (var i = 0; i < buf.length; i++){
var s = Math.max(-1, Math.min(1, buf[i]));
out[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
}
socket.emit('audio event',{data: out})
return;
}
On the server side, I receive the audio as follows:
audio_file = open('tempfile.raw', 'w')
@socketio.on('audio event')
def audio_message(message):
dat = [v[1] for v in sorted(message['data'].iteritems())]
n = len(dat)
byteval = struct.pack('<'+str(n)+'h',*dat)
audio_file.write(byteval)
But the resulting audio sounds metallic, interrupted and noisy. Here's how the resulting waveform looks:
Where in my code is the audio quality lost? How can I stream audio without quality loss?