| 1 |
export const requestMicrophoneNode = (microphoneCallback) => { |
| 2 |
const getUserMedia = |
| 3 |
typeof navigator.mediaDevices !== "undefined" |
| 4 |
? navigator.mediaDevices.getUserMedia |
| 5 |
: navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; |
| 6 |
|
| 7 |
console.log("requesting microphone access"); |
| 8 |
typeof navigator.mediaDevices !== "undefined" |
| 9 |
? getUserMedia |
| 10 |
.call(navigator.mediaDevices, { |
| 11 |
audio: { echoCancellation: false, sampleSize: 32 }, |
| 12 |
}) |
| 13 |
.then(microphoneCallback) |
| 14 |
.catch(console.error) |
| 15 |
: getUserMedia.call( |
| 16 |
navigator, |
| 17 |
{ |
| 18 |
audio: { |
| 19 |
optional: [{ echoCancellation: false, sampleSize: 32 }], |
| 20 |
}, |
| 21 |
}, |
| 22 |
microphoneCallback, |
| 23 |
console.error, |
| 24 |
); |
| 25 |
}; |
| 26 |
|
| 27 |
// rebind this to exportApi instance to use |
| 28 |
export async function enableAudioInput() { |
| 29 |
console.log("enabling audio input"); |
| 30 |
requestMicrophoneNode(async (stream) => { |
| 31 |
if (stream) { |
| 32 |
const audioContext = await this.getAudioContext(); |
| 33 |
const liveInput = audioContext.createMediaStreamSource(stream); |
| 34 |
this.inputsCount = liveInput.channelCount; |
| 35 |
|
| 36 |
// if (this.autoConnect) { |
| 37 |
const node = await this.getNode(); |
| 38 |
liveInput.connect(node); |
| 39 |
// } |
| 40 |
} |
| 41 |
}); |
| 42 |
} |