Skip to content

Commit

Permalink
Cleanup for previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
wizjany committed Jan 3, 2014
1 parent 62364f2 commit 9b54d20
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 131 deletions.
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.jinglenote;
package com.sk89q.commandbook;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -34,8 +34,9 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.util.PlayerUtil;
import com.sk89q.jinglenote.JingleNoteManager;
import com.sk89q.jinglenote.MidiJingleSequencer;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/com/sk89q/jinglenote/Instrument.java
@@ -1,6 +1,29 @@
/*
* CommandBook
* Copyright (C) 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.jinglenote;

public enum Instrument {

GUITAR, PIANO, BASS, BASS_GUITAR, STICKS, BASS_DRUM, SNARE_DRUM;
GUITAR,
PIANO,
BASS,
BASS_GUITAR,
STICKS,
BASS_DRUM,
SNARE_DRUM;
}
18 changes: 16 additions & 2 deletions src/main/java/com/sk89q/jinglenote/JingleNoteManager.java
@@ -1,8 +1,22 @@
// $Id$
/*
* Tetsuuuu plugin for SK's Minecraft Server Copyright (C) 2010 sk89q <http://www.sk89q.com> All rights reserved.
* CommandBook
* Copyright (C) 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


package com.sk89q.jinglenote;

import java.util.HashMap;
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/sk89q/jinglenote/JingleNotePlayer.java
@@ -1,6 +1,19 @@
// $Id$
/*
* Tetsuuuu plugin for SK's Minecraft Server Copyright (C) 2010 sk89q <http://www.sk89q.com> All rights reserved.
* CommandBook
* Copyright (C) 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.jinglenote;
Expand All @@ -20,7 +33,6 @@ public abstract class JingleNotePlayer implements Runnable {
* @param area The SearchArea for this player. (optional)
*/
public JingleNotePlayer(String player, JingleSequencer seq) {

this.player = player;
sequencer = seq;
}
Expand All @@ -47,12 +59,10 @@ public void run() {
}

public String getPlayer() {

return player;
}

public void stop() {

if (sequencer != null) {
sequencer.stop();
}
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/sk89q/jinglenote/JingleSequencer.java
@@ -1,6 +1,19 @@
// $Id$
/*
* Tetsuuuu plugin for SK's Minecraft Server Copyright (C) 2010 sk89q <http://www.sk89q.com> All rights reserved.
* CommandBook
* Copyright (C) 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.jinglenote;
Expand All @@ -24,24 +37,20 @@ public class Note {
float velocity;

public Note(Instrument instrument, byte note, float velocity) {

this.instrument = instrument;
this.note = note;
this.velocity = velocity;
}

public Instrument getInstrument() {

return instrument;
}

public float getNote() {

return (float) Math.pow(2.0D, (note - 12) / 12.0D);
}

public float getVelocity() {

return velocity;
}
}
Expand Down
68 changes: 41 additions & 27 deletions src/main/java/com/sk89q/jinglenote/MidiJingleSequencer.java
@@ -1,6 +1,19 @@
// $Id$
/*
* Tetsuuuu plugin for SK's Minecraft Server Copyright (C) 2010 sk89q <http://www.sk89q.com> All rights reserved.
* CommandBook
* Copyright (C) 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.jinglenote;
Expand Down Expand Up @@ -45,7 +58,6 @@ public class MidiJingleSequencer implements JingleSequencer {
1, 1, 1, 1, 1, 2, 4, 3, // 128
};


private static final int[] percussion = {
3, 3, 4, 4, 3, 2, 3, 2, //8 - Electric Snare
2, 2, 2, 2, 2, 2, 2, 2, //16 - Hi Mid Tom
Expand All @@ -56,7 +68,6 @@ public class MidiJingleSequencer implements JingleSequencer {
3, 3, //50 - Open Triangle
};


protected final File midiFile;
private Sequencer sequencer = null;

Expand All @@ -69,19 +80,23 @@ public MidiJingleSequencer(File midiFile, boolean loop) throws MidiUnavailableEx
sequencer.open();
Sequence seq = MidiSystem.getSequence(midiFile);
sequencer.setSequence(seq);
if(loop)
if (loop) {
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
}
} catch (MidiUnavailableException e) {
if (sequencer != null && sequencer.isOpen())
if (sequencer != null && sequencer.isOpen()) {
sequencer.close();
}
throw e;
} catch (InvalidMidiDataException e) {
if (sequencer != null && sequencer.isOpen())
if (sequencer != null && sequencer.isOpen()) {
sequencer.close();
}
throw e;
} catch (IOException e) {
if (sequencer != null && sequencer.isOpen())
if (sequencer != null && sequencer.isOpen()) {
sequencer.close();
}
throw e;
}
}
Expand All @@ -92,24 +107,22 @@ public void run(final JingleNotePlayer notePlayer) throws InterruptedException {
final Map<Integer, Integer> patches = new HashMap<Integer, Integer>();

try {
if(sequencer.getSequence() == null)
if (sequencer.getSequence() == null) {
return;
}
if (!sequencer.isOpen()) {
sequencer.open();
}
sequencer.getTransmitter().setReceiver(new Receiver() {

@Override
public void send(MidiMessage message, long timeStamp) {

if ((message.getStatus() & 0xF0) == ShortMessage.PROGRAM_CHANGE) {

ShortMessage msg = (ShortMessage) message;
int chan = msg.getChannel();
int patch = msg.getData1();
patches.put(chan, patch);
} else if ((message.getStatus() & 0xF0) == ShortMessage.NOTE_ON) {

ShortMessage msg = (ShortMessage) message;
int chan = msg.getChannel();
int n = msg.getData1();
Expand All @@ -124,7 +137,6 @@ public void send(MidiMessage message, long timeStamp) {

@Override
public void close() {

}
});

Expand All @@ -133,7 +145,7 @@ public void close() {
sequencer.start();
}
}
catch(Exception ignored){}
catch(Exception ignored) {}

while (sequencer.isRunning()) {
Thread.sleep(1000);
Expand All @@ -153,30 +165,33 @@ public void close() {

@Override
public void stop() {

if (sequencer != null && sequencer.isOpen()) {
sequencer.close();
}
}

protected static byte toMCNote(int n) {

if (n < 54) return (byte) ((n - 6) % (18 - 6));
else if (n > 78) return (byte) ((n - 6) % (18 - 6) + 12);
else return (byte) (n - 54);
if (n < 54) {
return (byte) ((n - 6) % (18 - 6));
} else if (n > 78) {
return (byte) ((n - 6) % (18 - 6) + 12);
} else {
return (byte) (n - 54);
}
}

protected static byte toMCInstrument(Integer patch) {

if (patch == null) return 0;

if (patch < 0 || patch >= instruments.length) return 0;
if (patch == null) {
return 0;
}
if (patch < 0 || patch >= instruments.length) {
return 0;
}

return (byte) instruments[patch];
}

protected Instrument toMCSound(byte instrument) {

switch (instrument) {
case 1:
return Instrument.BASS_GUITAR;
Expand All @@ -196,9 +211,9 @@ protected Instrument toMCSound(byte instrument) {
}

protected static byte toMCPercussion(Integer patch) {

if(patch == null)
if (patch == null) {
return 0;
}

int i = patch - 33;
if (i < 0 || i >= percussion.length) {
Expand All @@ -210,7 +225,6 @@ protected static byte toMCPercussion(Integer patch) {


public boolean isSongPlaying() {

return sequencer.isRunning();
}

Expand Down

0 comments on commit 9b54d20

Please sign in to comment.