Switch audio device type automatically

Hi all,

I’ve got a JUCE audio app that runs only on Windows, with an external ASIO soundcard. This is what I would like to do:

  • When app starts up, it selects ASIO as device type and a specific audio interface as device, for example a Behringer 404HD. Then set a specific buffer size and a specific sample rate. I’ve managed to do so, by adding this in the constructor of the MainComponent class:

    deviceManager.setCurrentAudioDeviceType("ASIO", true);
    AudioDeviceManager::AudioDeviceSetup stp = deviceManager.getAudioDeviceSetup();
    stp.bufferSize = DEFAULT_DEVICE_BUFFER_SIZE;
    stp.sampleRate = DEFAULT_DEVICE_SAMPLE_RATE;
    deviceManager.setAudioDeviceSetup(stp, true);

by doing this the app starts with the correct device type and device (I don’t set directly the device because I’ve installed the ASIO drivers of only one soundcard, the only one that this app will ever use)

  • If the soundcard is disconnected the app should print an error message in the terminal, and switch to “windows audio”. Right now instead, if the soundcard is disconnected, the app freezes and then crashes. I need that the app does not crash is the soundcard is disconnected.

  • When the soundcard is connected again to the PC, the app should switch back to ASIO, and set again buffer size and sample rate if needed

Basically what I need is:

  • no crash when ASIO soundcard is disconnected
  • switch back to ASIO when the sound card is connected again

How can I do that? I’ve tried with a changeListener on the deviceManager, but I didn’t find a way to do what I need to do.

Thank you guys for your help!