Dries
Dries

Reputation: 1005

Web Audio getUserMedia constraints have no effect

I have some Javascript that outputs an input stream (from a mic or something else) with some AudioNodes inbetween back to the default output but I'm having trouble with a large amount of latency.
To be a bit more precise, I've connected my guitar to a Behringer UCG102 USB Interface, and I've captured the stream of that device on a simple web page that displays the waveform of the input.

var constraints = {
  audio: {
    deviceId: selectedDevice.deviceId,
    sampleSize: 16,
    sampleRate: 16000,
    latency: 4
  }
};
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
  var audioCtx = new AudioContext();
  var source = audioCtx.createMediaStreamSource(stream);
  var gainNode = audioCtx.createGain();
  var compressor = audioCtx.createDynamicsCompressor();
  compressor.threshold.value = -50;
  compressor.knee.value = 40;
  compressor.ratio.value = 12;
  compressor.attack.value = 0;
  compressor.release.value = 0.25;
  source.connect(compressor);

  // debug audio analyser created here
  var analyser = audioCtx.createAnalyser();
  compressor.connect(analyser);
  analyser.connect(gainNode);
  analyser.fftSize = 2048;
  var bufferLength = analyser.frequencyBinCount;
  var dataArray = new Uint8Array(bufferLength);
  analyser.getByteTimeDomainData(dataArray);

  // Drawing the analyser code was here...

  gainNode.connect(audioCtx.destination);
});

As you can see I have created a 'constraints' variable that defines some audio settings, but except for the deviceId property it seems to not change anything. Even if I set {audio: {volume: 0}} I get the same sound output.

I've been reading the MDN documentation about MediaStreamConstraints and such but cannot figure out why I can't seem to control volume, latency,...

Upvotes: 2

Views: 796

Answers (1)

Muttley
Muttley

Reputation: 512

Short answer:

the constraints you specified may be either not handled by your browser, or changed by the system since values are interpreted by default as ideal.

Long answer:

Constraints accepted by navigator.mediaDevices.getUserMedia are discussed here (check here also the browser compatibility). The value of each constraint can be specified as min, max, exact, or ideal (which is the default), as discussed here.

"Constraints which are specified using any or all of max, min, or exact are always treated as mandatory. If any constraint which uses one or more of those can't be met [...], the promise will be rejected.".

A constraint specified as ideal (remember this is the default!) can be adjusted by the system, resulting in a value which differs from the value you requested.

Upvotes: 2

Related Questions