Skip to content

Commit

Permalink
Partially cleanup MIDI synchronisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Nov 14, 2017
1 parent 53400dc commit 14906e4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
Expand Up @@ -27,6 +27,8 @@ public interface JingleSequencer {

void play(JingleNotePlayer player);

int getPlayerCount();

Set<JingleNotePlayer> getPlayers();

class Note {
Expand Down
Expand Up @@ -9,14 +9,22 @@
import com.sk89q.craftbook.bukkit.util.BukkitUtil;
import com.sk89q.craftbook.mechanics.ic.ICMechanic;

import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;

/**
* A sequencer that reads MIDI files.
*
Expand Down Expand Up @@ -93,11 +101,9 @@ public void run() {

@Override
public void send(MidiMessage message, long timeStamp) {
synchronized(PLAYER_LOCK) {
if (players.isEmpty()) {
running = false;
return;
}
if (getPlayerCount() == 0) {
running = false;
return;
}

if ((message.getStatus() & 0xF0) == ShortMessage.PROGRAM_CHANGE) {
Expand Down Expand Up @@ -143,9 +149,11 @@ public void close() {
sequencer.start();
running = true;
playedBefore = true;
synchronized(PLAYER_LOCK) {
for(JingleNotePlayer player : players)
CraftBookPlugin.logDebugMessage("Opening midi sequencer: " + player.player, "midi");
if (CraftBookPlugin.inst().getConfiguration().debugMode) {
synchronized (PLAYER_LOCK) {
for (JingleNotePlayer player : players)
CraftBookPlugin.logDebugMessage("Opening midi sequencer: " + player.player, "midi");
}
}
} else
throw new IllegalArgumentException("Sequencer is not open!");
Expand Down Expand Up @@ -224,9 +232,10 @@ public boolean hasPlayedBefore () {
public void stop (JingleNotePlayer player) {
synchronized(PLAYER_LOCK) {
players.remove(player);
if (players.isEmpty()) {
stop();
}
}

if (this.getPlayerCount() == 0) {
stop();
}
}

Expand All @@ -240,6 +249,11 @@ public void play (JingleNotePlayer player) {
}
}

@Override
public int getPlayerCount() {
return this.players.size();
}

@Override
public Set<JingleNotePlayer> getPlayers () {
Set<JingleNotePlayer> copy;
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/sk89q/craftbook/util/jinglenote/Playlist.java
Expand Up @@ -9,11 +9,19 @@
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class Playlist {

Expand Down Expand Up @@ -138,7 +146,7 @@ public void run () {

while (position < lines.size() && isPlaying()) {
if(sequencer != null) {
while(sequencer != null && sequencer.isPlaying() && !sequencer.getPlayers().isEmpty()) {
while(sequencer != null && sequencer.isPlaying() && sequencer.getPlayerCount() > 0) {
for(Entry<String, SearchArea> p : lastPlayers.entrySet()) {

if(players.containsKey(p.getKey()) && jNote.isPlaying(p.getKey()))
Expand Down
Expand Up @@ -209,6 +209,11 @@ public void play (JingleNotePlayer player) {
}
}

@Override
public int getPlayerCount() {
return players.size();
}

@Override
public Set<JingleNotePlayer> getPlayers () {
return players;
Expand Down

0 comments on commit 14906e4

Please sign in to comment.