export default class AudioManager{ static get BUFFER_SIZE(){ return 8192; } constructor(){ /* (1) Initialise our AudioNodes ---------------------------------------------------------*/ /* (1) Build Audio Context */ this.ctx = new (window.AudioContext || window.webkitAudioContext)(); /* (2) Create the MASTER gain */ this.master = this.ctx.createGain(); /* (3) Initialise input (typically bound from recorder) */ this.input = null; /* (4) Shortcut our output */ this.output = this.ctx.destination; /* (5) Connect MASTER gain to output */ this.master.connect(this.output); /* (2) Initialise processing attributes ---------------------------------------------------------*/ /* (1) Container for our recorder */ this.recorder = null; /* (2) Initialise filters */ this.filters = { voice_clarity: this.ctx.createBiquadFilter(), voice_fullness: this.ctx.createBiquadFilter(), voice_presence: this.ctx.createBiquadFilter(), voice_sss: this.ctx.createBiquadFilter() }; /* (3) Create network I/O controller (WebSocket) */ this.network = { out: this.ctx.createScriptProcessor(AudioManager.BUFFER_SIZE, 1, 1) }; /* (4) Initialise websocket */ this.ws = null; /* (5) Bind network controller to send() function */ this.network.out.onaudioprocess = this.send.bind(this); /* (6) Set up our filters' parameters */ this.setUpFilters(); /* (9) Debug data */ this.dbg = { interval: 10, // debug every ... second def: { packets_received: 0, packets_sent: 0, kB_received: 0, kB_sent: 0 }, data: { packets_received: 0, packets_sent: 0, kB_received: 0, kB_sent: 0 } }; setInterval(function(){ console.group('debug'); for( let k in this.data ){ console.log(`${this.data[k]} ${k}`) this.data[k] = this.def[k] } console.groupEnd('debug'); }.bind(this.dbg), this.dbg.interval*1000); } /* (2) Setup filters * ---------------------------------------------------------*/ setUpFilters(){ /* (1) Setup filter parameters ---------------------------------------------------------*/ /* (1) Setup EQ#1 -> voice clarity */ this.filters.voice_clarity.type = 'peaking'; this.filters.voice_clarity.frequency.value = 3000; this.filters.voice_clarity.Q.value = .8; this.filters.voice_clarity.gain.value = 2; /* (2) Setup EQ#2 -> voice fullness */ this.filters.voice_fullness.type = 'peaking'; this.filters.voice_fullness.frequency.value = 200; this.filters.voice_fullness.Q.value = .8; this.filters.voice_fullness.gain.value = 2; /* (3) Setup EQ#3 -> reduce voice presence */ this.filters.voice_presence.type = 'peaking'; this.filters.voice_presence.frequency.value = 5000; this.filters.voice_presence.Q.value = .8; this.filters.voice_presence.gain.value = -2; /* (4) Setup EQ#3 -> reduce 'sss' metallic sound */ this.filters.voice_sss.type = 'peaking'; this.filters.voice_sss.frequency.value = 7000; this.filters.voice_sss.Q.value = .8; this.filters.voice_sss.gain.value = -8; /* (2) Connect filters ---------------------------------------------------------*/ /* (1) Connect clarity to fullness */ this.filters.voice_clarity.connect( this.filters.voice_fullness ); /* (2) Connect fullness to presence reduction */ this.filters.voice_fullness.connect( this.filters.voice_presence ); /* (3) Connect presence reduction to 'ss' removal */ this.filters.voice_presence.connect( this.filters.voice_sss ); } /* (3) Filter toggle * * @unlink Whether to unlink filters (directly bind to output) * ---------------------------------------------------------*/ linkFilters(unlink=false){ /* (1) Disconnect all by default */ this.input.disconnect(); /* (2) Get first filter */ let first_filter = this.filters.voice_clarity; let last_filter = this.filters.voice_sss; /* (3) If unlink -> connect directly to NETWORK output */ if( unlink === true ) return this.input.connect(this.network.out); /* (4) If linking -> connect input to filter stack */ this.input.connect(first_filter); /* (5) If linking -> connect stack end to network.out */ last_filter.connect(this.network.out); } /* (2) Binds an input stream * ---------------------------------------------------------*/ bindRecorderStream(_stream){ /* (1) Bind audio stream ---------------------------------------------------------*/ this.input = this.ctx.createMediaStreamSource(_stream); /* (2) By default: link through filters to output ---------------------------------------------------------*/ this.linkFilters(); } /* (3) Send chunks (Float32Array) * ---------------------------------------------------------*/ send(_audioprocess){ let buf32 = new Float32Array(AudioManager.BUFFER_SIZE); _audioprocess.inputBuffer.copyFromChannel(buf32, 0); let buf16 = this.f32toi16(buf32); // exit if no connection if( this.ws === null || this.ws.readyState !== 1 ) return; this.ws.send(buf16); this.dbg.data.packets_sent++; this.dbg.data.kB_sent += buf16.length * 16. / 8 / 1024; } /* (4) Play received chunks (Int16Array) * ---------------------------------------------------------*/ receive(_buffer){ /* (1) Convert to Float32Array */ let buf32 = this.i16tof32(_buffer); /* (2) Create source node */ let source = this.ctx.createBufferSource(); /* (3) Create buffer and dump data */ let input_buffer = this.ctx.createBuffer(1, AudioManager.BUFFER_SIZE, this.ctx.sampleRate); input_buffer.getChannelData(0).set(buf32); /* (4) Bind buffer to source node */ source.buffer = input_buffer; /* (5) Create a dedicated *muted* gain */ let gain = this.ctx.createGain(); /* (6) source -> gain -> MASTER + play() */ source.connect(gain); gain.connect(this.master); /* (7) Start playing */ source.start(this.ctx.currentTime); this.dbg.data.packets_received++; this.dbg.data.kB_received += _buffer.length * 16. / 8 / 1024; } /* (4) Convert Float32Array to Int16Array * * @buf32 Input * * @return buf16 Converted output * ---------------------------------------------------------*/ f32toi16(buf32){ /* (1) Initialise output */ let buf16 = new Int16Array(buf32.length); /* (2) Initialize loop */ let i = 0, l = buf32.length; /* (3) Convert each value */ for( ; i < l ; i++ ) buf16[i] = (buf32[i] < 0) ? 0x8000 * buf32[i] : 0x7FFF * buf32[i]; return buf16; } /* (2) Convert Int16Array to Float32Array * * @buf16 Input * * @return buf32 Converted output * ---------------------------------------------------------*/ i16tof32(buf16){ /* (1) Initialise output */ let buf32 = new Float32Array(buf16.length); /* (2) Initialize loop */ let i = 0, l = buf16.length; /* (3) Convert each value */ for( ; i < l ; i++ ) buf32[i] = (buf16[i] >= 0x8000) ? -(0x10000 * buf16[i])/0x8000 : buf16[i] / 0x7FFF; return buf32; } /* (8) Connect websocket * * @address Websocket address * ---------------------------------------------------------*/ wsconnect(_addr){ /* (1) Create websocket connection */ this.ws = new WebSocket(_addr); /* (2) Manage websocket responses */ this.ws.onmessage = function(_msg){ if( !(_msg.data instanceof Blob) ) return console.warn('[NaB] Not A Blob'); let fr = new FileReader(); fr.onload = function(){ let buf16 = new Int16Array(fr.result); this.receive(buf16); }.bind(this); fr.readAsArrayBuffer(_msg.data); }.bind(this); /* (3) Debug */ this.ws.onopen = () => console.warn('[audio] websocket connected'); this.ws.onclose = () => console.warn('[audio] websocket closed'); } /* (x) Access microphone + launch all * ---------------------------------------------------------*/ launch(wsAddress='wss://ws.douscord.xdrm.io/audio/2'){ /* (1) Start websocket */ this.wsconnect(wsAddress); if( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ){ navigator.mediaDevices.getUserMedia({ audio: true }) .then( stream => { this.recorder = new MediaRecorder(stream); this.bindRecorderStream(stream); this.recorder.onstart = () => console.warn('[audio] recording'); this.recorder.onstop = () => { this.recorder.stream.getTracks().map( t => t.stop() ); this.recorder = null; console.warn('[audio] stopped recording'); }; // start recording this.recorder.start(); }) .catch( e => console.warn('[audio] microphone permission issue', e) ); }else console.warn('[audio] microphone not supported'); } /* (x) Shut down microphone + kill all * ---------------------------------------------------------*/ kill(){ /* (1) Close websocket */ this.ws.close(); /* (2) Stop recording */ this.recorder.stop(); /* (3) Volume 0 */ this.master.gain.setValueAtTime(0, this.ctx.currentTime); } }