[lib.audio-manager] maybe latency minfix ??

This commit is contained in:
xdrm-brackets 2018-04-10 13:53:57 +02:00
parent 95d3487820
commit fc0150cc74
1 changed files with 48 additions and 50 deletions

View File

@ -211,7 +211,49 @@ export default class AudioManager{
---------------------------------------------------------*/ ---------------------------------------------------------*/
send(_audioprocess){ 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 */ /* (1) Process only if 'freq_drawer' is set */
if( this.freq_drawer instanceof Function ){ if( this.freq_drawer instanceof Function ){
@ -223,7 +265,7 @@ export default class AudioManager{
this.analyser.getByteFrequencyData(freqArray); this.analyser.getByteFrequencyData(freqArray);
// 3. Send to callback // 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); this.analyser.getByteTimeDomainData(waveArray);
// 3. Send to callback // 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 // DEBUG
this.dbg.data.packets_sent++; this.dbg.data.packets_sent++;
this.dbg.data.kB_sent += buf16.length * 16. / 8 / 1024; this.dbg.data.kB_sent += buf16.length * 16. / 8 / 1024;
} }
/* (5) Play received chunks (Int16Array) /* (5) Play received chunks (Int16Array)
@ -320,7 +318,7 @@ export default class AudioManager{
gain.connect(this.master); gain.connect(this.master);
/* (7) Push in buffer stack */ /* (7) Push in buffer stack */
this.stack.push([source, gain]); this.stack.push(source);
} }