From fc0150cc74aa068f45413d3e8674cd6db01a9fb6 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 10 Apr 2018 13:53:57 +0200 Subject: [PATCH] [lib.audio-manager] maybe latency minfix ?? --- parcel/lib/audio-manager.js | 98 ++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/parcel/lib/audio-manager.js b/parcel/lib/audio-manager.js index be439d5..e32ed3f 100644 --- a/parcel/lib/audio-manager.js +++ b/parcel/lib/audio-manager.js @@ -211,7 +211,49 @@ export default class AudioManager{ ---------------------------------------------------------*/ send(_audioprocess){ - /* (1) Manage analyser + /* Exit here if not connected */ + if( this.ws === null || this.ws.readyState !== 1 ) + return; + + + /* (1) WebSocket send packet + ---------------------------------------------------------*/ + /* (1) Initialize buffer (Float32Array) */ + let buf32 = new Float32Array(AudioManager.BUFFER_SIZE); + + /* (2) Extract stream into buffer */ + _audioprocess.inputBuffer.copyFromChannel(buf32, 0); + + /* (4) Convert for WS connection (Int16Array) */ + let buf16 = this.f32toi16(buf32); + + /* (5) Send buffer through websocket */ + this.ws.send(buf16); + + + + /* (2) WebSocket buffer stack read + ---------------------------------------------------------*/ + setTimeout(function(){ + + /* (1) Pop too large stack */ + this.stack.length > this.stack_size && this.stack.pop(); + + /* (2) Read input buffer stack */ + if( this.stack.length > 0 ){ + + // 1. extract our source + let source_node = this.stack.shift(); + + // 2. Play source node + source_node.start(); + + } + + }.bind(this), 0); + + + /* (3) Manage analyser ---------------------------------------------------------*/ /* (1) Process only if 'freq_drawer' is set */ if( this.freq_drawer instanceof Function ){ @@ -223,7 +265,7 @@ export default class AudioManager{ this.analyser.getByteFrequencyData(freqArray); // 3. Send to callback - this.freq_drawer(freqArray); + setTimeout(this.freq_drawer.bind(this,freqArray), 0); } @@ -237,61 +279,17 @@ export default class AudioManager{ this.analyser.getByteTimeDomainData(waveArray); // 3. Send to callback - this.wave_drawer(waveArray); + setTimeout(this.wave_drawer.bind(this,waveArray), 0); } - /* (2) WebSocket send packet - ---------------------------------------------------------*/ - /* (1) Exit here if not connected */ - if( this.ws === null || this.ws.readyState !== 1 ) - return; - - /* (2) Initialize buffer (Float32Array) */ - let buf32 = new Float32Array(AudioManager.BUFFER_SIZE); - - /* (3) Extract stream into buffer */ - _audioprocess.inputBuffer.copyFromChannel(buf32, 0); - - /* (4) Convert for WS connection (Int16Array) */ - let buf16 = this.f32toi16(buf32); - - /* (5) Send buffer through websocket */ - this.ws.send(buf16); - - /* (6) Pop too large stack */ - this.stack.length > this.stack_size && this.stack.pop(); - - /* (7) Read input buffer stack */ - if( this.stack.length > 0 ){ - - // 1. extract our nodes - let tmp = this.stack.shift(); - let source_node = tmp[0]; - let gain_node = tmp[1]; - - // 2. set default gain to fade-in - // ( this.test === true ) && gain_node.gain.setValueAtTime(0.1, this.ctx.currentTime); - // ( this.test === true ) && gain_node.gain.exponentialRampToValueAtTime(1, this.ctx.currentTime+AudioManager.CHUNK_DURATION*this.fade_in); - - // 3. set final gain to fadeout - // ( this.test === true ) && gain_node.gain.setValueAtTime(1, this.ctx.currentTime+AudioManager.CHUNK_DURATION*(1-this.fade_out)); - // ( this.test === true ) && gain_node.gain.exponentialRampToValueAtTime(0.1, this.ctx.currentTime+AudioManager.CHUNK_DURATION ); - - source_node.start(this.ctx.currentTime); - } - - /* (7) Pop stack if exceeded */ - - - - - // DEBUG this.dbg.data.packets_sent++; this.dbg.data.kB_sent += buf16.length * 16. / 8 / 1024; + + } /* (5) Play received chunks (Int16Array) @@ -320,7 +318,7 @@ export default class AudioManager{ gain.connect(this.master); /* (7) Push in buffer stack */ - this.stack.push([source, gain]); + this.stack.push(source); }