-
Notifications
You must be signed in to change notification settings - Fork 28
/
microphone-worker.js
32 lines (30 loc) · 1.06 KB
/
microphone-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class MicrophoneWorker extends EventTarget {
constructor(mediaStream, options = {}) {
super();
const audio = document.createElement('audio');
audio.srcObject = mediaStream;
audio.muted = true;
this.audioContext = new AudioContext();
const mediaStreamSource = this.audioContext.createMediaStreamSource(mediaStream);
this.audioContext.audioWorklet.addModule(options.microphoneWorkletUrl || 'microphone-worklet.js')
.then(() => {
const audioWorkletNode = new AudioWorkletNode(this.audioContext, 'volume-processor');
if (options.muted === false) {
audioWorkletNode.port.postMessage(JSON.stringify({
method: 'muted',
muted: false,
}));
}
audioWorkletNode.port.onmessage = e => {
this.dispatchEvent(new MessageEvent('volume', {
data: e.data,
}));
};
mediaStreamSource.connect(audioWorkletNode).connect(this.audioContext.destination);
});
}
close() {
this.audioContext.close();
}
}
export default MicrophoneWorker;