Skip to content

Commit

Permalink
Merge pull request #83 from kshoji/feature/fix-javax-sound-midi-20240506
Browse files Browse the repository at this point in the history
Update MidiSynthesizer.setReceiver method
  • Loading branch information
kshoji authored May 5, 2024
2 parents 45ecd09 + 01459f8 commit 3e954df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
5 changes: 5 additions & 0 deletions MIDIDriver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ android {
targetSdkVersion 34
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

namespace "jp.kshoji.driver.midi"

sourceSets {
Expand Down
14 changes: 12 additions & 2 deletions MIDIDriver/src/jp/kshoji/javax/sound/midi/UsbMidiSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ public void initialize() {
*/
public void terminate() {
synchronized (midiDeviceMap) {
for (final Map.Entry<String, UsbMidiDevice> midiDeviceEntry : midiDeviceMap.entrySet()) {
midiDeviceEntry.getValue().close();
for (final UsbMidiDevice midiDevice : midiDeviceMap.values()) {
midiDevice.close();
MidiSystem.removeMidiDevice(midiDevice);
}

midiDeviceMap.clear();
}

synchronized (midiSynthesizerMap) {
for (final UsbMidiSynthesizer usbMidiSynthesizer : midiSynthesizerMap.values()) {
usbMidiSynthesizer.close();
MidiSystem.removeSynthesizer(usbMidiSynthesizer);
}
midiSynthesizerMap.clear();
}

if (deviceConnectionWatcher != null) {
deviceConnectionWatcher.stop();
deviceConnectionWatcher = null;
Expand Down Expand Up @@ -131,6 +140,7 @@ public void onMidiOutputDeviceAttached(@NonNull final MidiOutputDevice midiOutpu
try {
existingSynthesizer.setReceiver(addedDevice.getReceiver());
} catch (final MidiUnavailableException ignored) {
existingSynthesizer.setReceiver(null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,7 @@ public UsbMidiSynthesizer(final UsbMidiDevice usbMidiDevice) {
} catch (final MidiUnavailableException ignored) {
}

if (receiver == null) {
// empty
channels = new MidiChannel[0];
voiceStatuses = new VoiceStatus[0];
} else {
// 16 channels
voiceStatuses = new VoiceStatus[16];
channels = new MidiChannel[16];
for (int channel = 0; channel < 16; channel++) {
voiceStatuses[channel] = new VoiceStatus();
channels[channel] = new MidiChannelImpl(channel, receiver, voiceStatuses[channel]);
}
}
setReceiver(receiver);
}

@NonNull
Expand Down Expand Up @@ -196,12 +184,18 @@ public List<Transmitter> getTransmitters() {
}

public void setReceiver(final Receiver receiver) {
// 16 channels
voiceStatuses = new VoiceStatus[16];
channels = new MidiChannel[16];
for (int channel = 0; channel < 16; channel++) {
voiceStatuses[channel] = new VoiceStatus();
channels[channel] = new MidiChannelImpl(channel, receiver, voiceStatuses[channel]);
if (receiver == null) {
// empty
channels = new MidiChannel[0];
voiceStatuses = new VoiceStatus[0];
} else {
// 16 channels
voiceStatuses = new VoiceStatus[16];
channels = new MidiChannel[16];
for (int channel = 0; channel < 16; channel++) {
voiceStatuses[channel] = new VoiceStatus();
channels[channel] = new MidiChannelImpl(channel, receiver, voiceStatuses[channel]);
}
}
}
}

0 comments on commit 3e954df

Please sign in to comment.