วิธีประมวลผลเสียงจากไมโครโฟนของผู้ใช้

François Beaufort
François Beaufort

คุณสามารถเข้าถึงกล้องและไมโครโฟนของผู้ใช้ได้ในแพลตฟอร์มเว็บโดยใช้ Media Recording and Streams API เมธอด getUserMedia() จะแจ้งให้ผู้ใช้เข้าถึงกล้องและ/หรือไมโครโฟนเพื่อจับภาพเป็นสตรีมสื่อ จากนั้นระบบจะประมวลผลสตรีมนี้ในชุดข้อความ Web Audio แยกต่างหากด้วย AudioWorklet ที่มีการประมวลผลเสียงที่มีเวลาในการตอบสนองต่ำมาก

ตัวอย่างด้านล่างแสดงวิธีประมวลผลเสียงจากไมโครโฟนของผู้ใช้อย่างมีประสิทธิภาพ

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()

การสนับสนุนเบราว์เซอร์

  • 53
  • 12
  • 36
  • 11

แหล่งที่มา

เว็บออดิโอ

การสนับสนุนเบราว์เซอร์

  • 35
  • 12
  • 25
  • 14.1

แหล่งที่มา

AudioWorklet

การสนับสนุนเบราว์เซอร์

  • 66
  • 79
  • 76
  • 14.1

แหล่งที่มา

อ่านเพิ่มเติม

ข้อมูลประชากร

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="icon"
      href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🎙️</text></svg>"
    />
    <title>How to process audio from the user's microphone</title>
  </head>
  <body>
    <h1>How to process audio from the user's microphone</h1>
    <button id="startMicrophoneButton">Start using microphone</button>
    <button id="stopMicrophoneButton" disabled>Stop using microphone</button>
    <pre id="logs"></pre>
  </body>
</html>

CSS


        :root {
  color-scheme: dark light;
}
html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
body {
  margin: 1rem;
  font-family: system-ui, sans-serif;
}
button {
  display: block;
  margin-bottom: 4px;
}
pre {
  color: red;
  white-space: pre-line;
}
        

JS


        const startMicrophoneButton = document.querySelector('#startMicrophoneButton');
const stopMicrophoneButton = document.querySelector('#stopMicrophoneButton');

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);

  stopMicrophoneButton.disabled = false;
  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.");
});