Skip to content

Commit

Permalink
Prevent ArrayIndexOutOfBoundsException when none of the devices have …
Browse files Browse the repository at this point in the history
…any inputs
  • Loading branch information
kevinstadler committed Sep 20, 2023
1 parent 3306f82 commit 2afba78
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/processing/sound/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,20 @@ private void createSynth(AudioDeviceManager deviceManager) {
if (this.synth != null) {
this.stopSynth(true);
}
this.inputDevice = deviceManager.getDefaultInputDeviceID();
try {
// this might be -1 if there is no device with inputs. handled below.
this.inputDevice = deviceManager.getDefaultInputDeviceID();
} catch (RuntimeException e) {
// JPortAudioDevice even throws an exception if none of the devices have
// inputs...
}
this.outputDevice = deviceManager.getDefaultOutputDeviceID();
this.synth = JSyn.createSynthesizer(deviceManager);
}

// called in three different cases:
// 1. explicitly by the user
// 2. automatically by selectOutputDevice when it fails to open a line using
// called in two different cases:
// 1. explicitly by the user (through MultiChannel.usePortAudio())
// 2. automatically by selectOutputDevice() when it fails to open a line using
// JavaSound
protected boolean usePortAudio(boolean portAudio) {
if (portAudio != this.synth.getAudioDeviceManager() instanceof JPortAudioDevice) {
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 2afba78

Please sign in to comment.