From f41b4f9a3d12cf33f813584b074915a5056d92d4 Mon Sep 17 00:00:00 2001 From: kevinstadler Date: Wed, 20 Sep 2023 18:08:53 +0200 Subject: [PATCH] Prevent ArrayIndexOutOfBoundsException when none of the devices have any inputs --- src/processing/sound/Engine.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/processing/sound/Engine.java b/src/processing/sound/Engine.java index a3c8cbd..c5745d1 100644 --- a/src/processing/sound/Engine.java +++ b/src/processing/sound/Engine.java @@ -138,7 +138,13 @@ private void createSynth(AudioDeviceManager deviceManager) { if (this.synth != null) { this.stopSynth(true); } - this.inputDevice = deviceManager.getDefaultInputDeviceID(); + // this might be -1 if there is no device with inputs. handled below. + System.out.println(deviceManager.USE_DEFAULT_DEVICE); + try { + this.inputDevice = deviceManager.getDefaultInputDeviceID(); + } catch (RuntimeException e) { + // JPortAudioDevice throws an exception if no devices with inputs + } this.outputDevice = deviceManager.getDefaultOutputDeviceID(); this.synth = JSyn.createSynthesizer(deviceManager); } @@ -191,8 +197,11 @@ private void startSynth() { } this.setVolume(1.0f); + // prevent IndexOutOfBoundsException on input-less devices + int inputChannels = this.inputDevice >= 0 ? + this.synth.getAudioDeviceManager().getMaxInputChannels(this.inputDevice) : 0; this.synth.start(this.sampleRate, - this.inputDevice, this.synth.getAudioDeviceManager().getMaxInputChannels(this.inputDevice), + this.inputDevice, inputChannels, this.outputDevice, this.synth.getAudioDeviceManager().getMaxOutputChannels(this.outputDevice)); }