1

I'm building a low-latency sampler in the browser (using Javascript, WASM, etc.).

How to choose, from Javascript, a specific audio output device for Chrome or Firefox?

I see there is Audio Output API but there are not many examples (and by the way navigator.mediaDevices.selectAudioOutput is undefined on my Chrome 109).

For example, how to make Chrome use Asio4All as main output device?

(Note: using an ASIO device such as the free Asio4All driver can make the latency drop from 30 milliseconds to 5 milliseconds, even on an old computer).

Basj
  • 41,386
  • 99
  • 383
  • 673
  • It remember me a [question](https://stackoverflow.com/q/31765358/5156280) I post some time ago... – TGrif Feb 09 '23 at 16:41

1 Answers1

1

There is also Audio Output Devices API which can be used to achieved the similar functionality.

// First Create a new audio element
    var audio = new Audio("https://samplelib.com/lib/preview/mp3/sample-3s.mp3");
    
    // Get the list of available audio output devices
    navigator.mediaDevices.enumerateDevices()
      .then(function(devices) {
        // Filter the devices to get only the audio output ones
        var audioOutputDevices = devices.filter(function(device) {
          return device.kind === "audiooutput";
        });
        // Log the devices to the console
        console.log(audioOutputDevices);
        // If there is at least one audio output device, use the first one as the output device
        if (audioOutputDevices.length > 0) {
          // Set the sink ID of the audio element to the device ID of the first audio output device
          audio.setSinkId(audioOutputDevices[0].deviceId)
            .then(function() {
              // Play the audio
              audio.play();
            })
            .catch(function(error) {
              // Handle any errors
              console.error(error);
            });
        }
      })

The audioOutputDevices[] can be leveraged to choose between vaious output devices.

  • Do you get various devices on this list? I don't see low-latency devices like ASIO4ALL (https://www.asio4all.org/), I only see 1 device. BTW for future readers it would be great to edit your answer to convert it into a runnable snippet. – Basj Feb 15 '23 at 14:10
  • sure i will do that. Unfortunately i am unable to get ASIO4ALL working on my Windows 11 machine – Avinash Chandravansi Feb 15 '23 at 14:43
  • Thanks @AvinashChandravansi. How many devices do you see in this list on your Windows 11 computer? I would be interested in knowing how many devices you see and if there are multiple variations for the same device, based on the audio backend: DirectX, WASAPI, etc. – Basj Feb 15 '23 at 14:48
  • Myself, I only see `[{ "deviceId": "", "kind": "audiooutput", "label": "", "groupId": "bbb59..." }]` on my Windows laptop. – Basj Feb 15 '23 at 14:49
  • I am able to see only one audio output device in my system setting as well – Avinash Chandravansi Feb 15 '23 at 14:52
  • [Permissions are gated](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices) by the [Permission API](https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API). Also, enumerateDevices() must be used in a [secure context](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts). – Code4R7 Feb 22 '23 at 08:59