html5 - Setting getChannelData causing socket.io to crash in web audio -
html5 - Setting getChannelData causing socket.io to crash in web audio -
i'm having issue whenever transcode sound file , send sound buffer client via socket.io played web sound connection dies perform
source.buffer.getchanneldata(0).set(audio);
i'm assuming isn't socket.io problem , i'm seeing socket.io issue result of real problem. in client i'm piping sound file stdin of ffmpeg , listening stderr of ffmpeg determine when it's safe send buffer. client receiving buffer , doing until line stated above. here sample test code reproduce issue.
server side:
var express = require('express'); var http = require('http'); var spawn = require('child_process').spawn; var app = express(); var webserver = http.createserver(app); var io = require('socket.io').listen(webserver, {log: false}); app.use(express.static(__dirname + '/public')); app.get('/', function(req, res){ res.send( "<script src='/socket.io/socket.io.js'></script>\n"+ "<script>var socket=io.connect('http://127.0.0.1:3000');</script>\n"+ "<script src='/webaudio_file_cli.js'></script>" ); }); webserver.listen(3000); io.sockets.on('connection', function(websocket) { var disconnect = '0'; var count = 0; var audbuf = new buffer([]); if (disconnect == '0') { console.log('new connection...'); var inputstream = spawn('wget', ['-o','-','http://www.noiseaddicts.com/samples/4353.mp3']); var ffmpeg = spawn('ffmpeg', [ '-i', 'pipe:0', // input on stdin '-acodec', 'pcm_s16le', // pcm 16bits, little-endian '-ar', '24000', // sampling rate '-ac', 1, // mono '-f', 'wav', 'pipe:1' // output on stdout ], {stdio: ['pipe','pipe','pipe']}); inputstream.stdout.pipe(ffmpeg.stdin); ffmpeg.stdout.on('data', function(data) { audbuf = buffer.concat([audbuf,data]); }); ffmpeg.stderr.on('data', function(data) { var _line = data.tostring('utf8'); if (_line.substring(0,5) == 'size=' && _line.indexof('headers:') > -1) { console.log('completed...'); websocket.emit('audio',audbuf); } }); } websocket.on('disconnect', function() { console.log('disconnecting...'); disconnect=1; }); });
client side (webaudio_file_cli.js):
window.audiocontext = window.audiocontext || window.webkitaudiocontext; var context = new audiocontext(); var source = context.createbuffersource(); var audiostack = [], sound = []; socket.on('audio', function(data) { playaudio(data); }); function playaudio(data) { // playback starting... audiostack = int16array(data); (var = 0; < audiostack.length; i++) { audio[i] = (audiostack[i]>0)?audiostack[i]/32767:audiostack[i]/32768; // convert buffer within range -1.0 -> +1.0 } var audiobuffer = context.createbuffer(1, audio.length, 24000); source.buffer.getchanneldata(0).set(audio); source.buffer = audiobuffer; source.connect(context.destination); source.start(0); }
in example, you're accessing source.buffer.getchanneldata before set source.buffer = audiobuffer. flip order of 2 lines, maybe?
html5 ffmpeg socket.io html5-audio web-audio
Comments
Post a Comment