Search code examples
safariwebkitweb-audio-apivolumemicrophone

Audio from mic in Safari (Mac) unexpectedly diminishes in volume after several seconds


I am capturing audio from the computer mic in Safari. The audio I am capturing is of me humming a fixed pitch. For the first few seconds, the audio is recorded at the expected volume. After maybe 5-10 seconds, the volume of the audio greatly diminishes. It is perhaps 50% or less of the initial audio.

This happens both when I record and then play back the audio, and also when I view the audio data in real time with an AnalyserNode.

I have tried setting the navigator.audioSession.type to "play-and-record" and I have also tried leaving this parameter with its default setting.

When I speak normally into the microphone, this issue does not occur.

Below is the code I am using to create the mic audio stream:

const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
const isSafari = navigator.userAgent.indexOf("Safari") != -1;

const createStream = async () => {
  // For iOS Safari, reset to play-and-record to allow mic to work
  if ((isIOS || isSafari) && navigator.audioSession) {
    navigator.audioSession.type = "play-and-record";
  }

  const stream = await navigator.mediaDevices.getUserMedia({
    video: false,
    audio: true,
  });
  addStream(stream);
  return stream;
};

Safari Version 16.6 Mac (Apple Silicon)


Solution

  • It's probably some pre-processing that causes the issue. You can disable all pre-processing when calling getUserMedia() with the following constraints:

    const stream = await navigator.mediaDevices.getUserMedia({
        audio: {
            autoGainControl: false,
            echoCancellation: false,
            noiseSuppression: false,
        }
    });