Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix javax.sound.midi issues #33

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import jp.kshoji.javax.sound.midi.ble.BleMidiSynthesizer;

/**
* {@link jp.kshoji.javax.sound.midi.MidiSystem} initializer for BLE MIDI
* {@link jp.kshoji.javax.sound.midi.MidiSystem} for BLE MIDI
*
* @author K.Shoji
*/
Expand All @@ -35,7 +35,7 @@ public final class BleMidiSystem implements OnMidiDeviceAttachedListener, OnMidi
* @param context the context
*/
public BleMidiSystem(@NonNull final Context context) {
this.context = context.getApplicationContext();
this.context = context;
}

/**
Expand Down Expand Up @@ -79,10 +79,19 @@ public void terminate() {
synchronized (midiDeviceMap) {
for (final BleMidiDevice bleMidiDevice : midiDeviceMap.values()) {
bleMidiDevice.close();
MidiSystem.removeMidiDevice(bleMidiDevice);
}

midiDeviceMap.clear();
}

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

/**
Expand Down Expand Up @@ -175,6 +184,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 BleMidiSynthesizer(final BleMidiDevice bleMidiDevice) {
} 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]);
}
}
}
}