웹 플랫폼에서는 Media Capture and Streams API를 사용하여 사용자의 카메라와 마이크에 액세스할 수 있습니다. getUserMedia()
메서드는 사용자에게 카메라 또는 마이크에 액세스하여 미디어 스트림으로 캡처하라는 메시지를 표시합니다. 그런 다음 이 스트림은 매우 낮은 지연 시간 오디오 처리를 제공하는 AudioWorklet을 사용하여 별도의 Web Audio 스레드에서 처리할 수 있습니다.
아래 예는 성능이 우수한 방식으로 사용자의 마이크에서 오디오를 처리하는 방법을 보여줍니다.
let stream;
startMicrophoneButton.addEventListener("click", async () => {
// Prompt the user to use their microphone.
stream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
const context = new AudioContext();
const source = context.createMediaStreamSource(stream);
// Load and execute the module script.
await context.audioWorklet.addModule("processor.js");
// Create an AudioWorkletNode. The name of the processor is the
// one passed to registerProcessor() in the module script.
const processor = new AudioWorkletNode(context, "processor");
source.connect(processor).connect(context.destination);
log("Your microphone audio is being used.");
});
stopMicrophoneButton.addEventListener("click", () => {
// Stop the stream.
stream.getTracks().forEach(track => track.stop());
log("Your microphone audio is not used anymore.");
});
// processor.js
// This file is evaluated in the audio rendering thread
// upon context.audioWorklet.addModule() call.
class Processor extends AudioWorkletProcessor {
process([input], [output]) {
// Copy inputs to outputs.
output[0].set(input[0]);
return true;
}
}
registerProcessor("processor", Processor);
브라우저 지원
MediaDevices.getUserMedia()
Web Audio
AudioWorklet