Reputation: 115
I have a python script that generates an audio buffer every 170 ms. This buffer is sent in real time to my JS program through a socket. I'm now trying to play the sound of the buffers in real time.
I tried to create a new JS AudioBuffer every time I receive something but doing leads to a lot of stuttering.
var net = require('net');
var client = new net.Socket();
client.connect(8484, '127.0.0.1', function() {
console.log('Connected');
});
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var channels = 1;
client.on('data', function(data) {
// clean data (each buffer has a length of 1350)
var array = String(data).split(";");
for (var i = 0; i < array.length; i++)
array[i] = parseFloat(array[i])
// play the small buffer
var buf = audioCtx.createBuffer(channels, array.length, 16000);
var buffering = buf.getChannelData(0);
for (var i = 0; i < array.length; i++)
buffering[i] = array[i];
var source = audioCtx.createBufferSource();
source.buffer = buf;
source.connect(audioCtx.destination);
source.start();
});
I also tried web-audio-stream API but it showed an error message.
Thank you
Upvotes: 2
Views: 1349
Reputation: 237
Hi abcdef or whatever,
You need to time your buffers. So you need to know length of the chunks => 170 ms propably. So be sure the one chunk is finsisehd before you start the other one. You can do this with an array or setTimeout or whatever.
Perhaps the split(";") command is too slow as well. I mean you need hundreds of thousands of splits then. Isn't that too time consuming?
Try logging if that is a bottleneck and you need more time to do all the processing, cause you just have 170 ms.
Perhaps you can generate the data in python in a way it doesn't have to be split and can be decoded directly. Just like an mp3 or something => wav is probably faster.
I don't know it really depends on your setups and the time the different steps take.
Cheers
Kilian
Upvotes: 1