[lib.audio-manager] added dynamic mic volume

This commit is contained in:
xdrm-brackets 2018-04-10 14:20:27 +02:00
parent fc0150cc74
commit bdbfa902fb
1 changed files with 25 additions and 4 deletions

View File

@ -13,7 +13,8 @@ export default class AudioManager{
/* (2) Create the MASTER gain */
this.master = this.ctx.createGain();
this.volume = this.ctx.createGain();
this.volume.gain.setValueAtTime(.65, 0); // input volume 65%
this.peaks = { low: 0, high: 0 };
this.volume_value = 1;
/* (3) Initialise input (typically bound from recorder) */
this.input = null;
@ -225,11 +226,24 @@ export default class AudioManager{
_audioprocess.inputBuffer.copyFromChannel(buf32, 0);
/* (4) Convert for WS connection (Int16Array) */
this.peaks.low = 0;
this.peaks.high = 0;
let buf16 = this.f32toi16(buf32);
/* (5) Send buffer through websocket */
this.ws.send(buf16);
/* (5) Adapt microphone volume if had peaks */
if( this.peaks.high > .01 ) // 30% saturation -> decrease
this.volume_value *= .8;
else if( this.peaks.low > .99 && this.volume_value*1.01 < 1 ) // 90% too low volume + less than 30% saturation -> increase
this.volume_value *= 1.01;
// apply new volume
this.volume.gain.setValueAtTime(this.volume_value, this.ctx.currentTime);
/* (2) WebSocket buffer stack read
@ -339,8 +353,15 @@ export default class AudioManager{
let i = 0, l = buf32.length;
/* (3) Convert each value */
for( ; i < l ; i++ )
for( ; i < l ; i++ ){
buf16[i] = (buf32[i] < 0) ? 0x8000 * buf32[i] : 0x7FFF * buf32[i];
( buf32[i] > 0.9 ) && ( this.peaks.high++ );
( buf32[i] < 0.1 ) && ( this.peaks.low++ );
}
/* (4) Report peaks in percentage */
this.peaks.high /= l;
this.peaks.low /= l;
return buf16;
}