Permalink
Browse files

Add some more locking around the Midi players list.

  • Loading branch information...
me4502 committed Sep 23, 2017
1 parent 05e574b commit dab5693217b7c2f06abc3f2cb0fe550c5594a4d0
Showing with 23 additions and 13 deletions.
  1. +23 −13 src/main/java/com/sk89q/craftbook/util/jinglenote/MidiJingleSequencer.java
@@ -55,13 +55,13 @@
10, 10, //82 - Open Triangle
};
private Sequencer sequencer = null;
private Sequencer sequencer;
private boolean running = false;
private boolean playedBefore = false;
private static final Object PLAYER_LOCK = new Object();
private Set<JingleNotePlayer> players = new HashSet<>();
private volatile Set<JingleNotePlayer> players = new HashSet<>();
public MidiJingleSequencer(File midiFile, boolean loop) throws MidiUnavailableException, InvalidMidiDataException, IOException {
try {
@@ -93,10 +93,11 @@ public void run() {
@Override
public void send(MidiMessage message, long timeStamp) {
if(players.isEmpty()) {
running = false;
return;
synchronized(PLAYER_LOCK) {
if (players.isEmpty()) {
running = false;
return;
}
}
if ((message.getStatus() & 0xF0) == ShortMessage.PROGRAM_CHANGE) {
@@ -160,7 +161,9 @@ public void close() {
public void stop() {
if(!running) return;
players.clear();
synchronized(PLAYER_LOCK) {
players.clear();
}
CraftBookPlugin.logDebugMessage("Stopping MIDI sequencer. (Stop called)", "midi");
if (sequencer != null) {
try {
@@ -209,7 +212,6 @@ Sequencer getSequencer() {
@Override
public boolean isPlaying () {
return running && sequencer != null;
}
@@ -220,22 +222,30 @@ public boolean hasPlayedBefore () {
@Override
public void stop (JingleNotePlayer player) {
players.remove(player);
if(players.isEmpty()) {
stop();
synchronized(PLAYER_LOCK) {
players.remove(player);
if (players.isEmpty()) {
stop();
}
}
}
@Override
public void play (JingleNotePlayer player) {
players.add(player);
synchronized(PLAYER_LOCK) {
players.add(player);
}
if(!playedBefore) {
run();
}
}
@Override
public Set<JingleNotePlayer> getPlayers () {
return players;
Set<JingleNotePlayer> copy;
synchronized(PLAYER_LOCK) {
copy = new HashSet<>(players);
}
return copy;
}
}

0 comments on commit dab5693

Please sign in to comment.