Skip to content

Commit

Permalink
Add Midi command to let you play midi files. Big thanks to authorblue…
Browse files Browse the repository at this point in the history
…s for letting us use his great midi code. Usage: - midi file:pacman
  • Loading branch information
davidcernat committed Jun 15, 2013
1 parent e11de6d commit c1b3c34
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 4 deletions.
Expand Up @@ -172,8 +172,11 @@ public void registerCoreMembers() {
registerCoreMember(LookcloseCommand.class,
"LOOKCLOSE", "lookclose [toggle:true|false]", 1);

registerCoreMember(MidiCommand.class,
"MIDI", "midi [file:<name>] (listener(s):[p@<name>|...])|(location:<x,y,z,world>) (tempo:<#.#>)", 1);

registerCoreMember(MountCommand.class,
"MOUNT", "mount (cancel) (location:x,y,z,world) (target(s):[n@#]|[p@name]|[e@name])", 0);
"MOUNT", "mount (cancel) (location:<x,y,z,world>) (target(s):[n@#]|[p@name]|[e@name])", 0);

registerCoreMember(ModifyBlockCommand.class,
"MODIFYBLOCK", "modifyblock [location:<x,y,z,world>] [<material>(:<data>)] (radius:<#>) (height:<#>) (depth:<#>)", 2);
Expand Down
@@ -0,0 +1,140 @@
package net.aufdemrand.denizen.scripts.commands.core;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.midi.MidiUtil;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.dEntity;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;

/* midi [file:<name>] (listener(s):[p@<name>|...])|(location:<x,y,z,world>) (tempo:<#.#>) */

/**
* Arguments: [] - Required, () - Optional
* [file:<name>] specifies the name of the file under plugins/Denizen/midi/
* (listener(s):[p@<name>|...]) specifies the players who will listen to the midi
* (location:<x,y,z,world>) specifies the location where the midi will be played
* [tempo:<#.#>] sets the tempo of the midi
*
* The listeners and location arguments cannot be used at the same time, but
* the location has a higher priority if both are included.
*
* Example Usage:
* midi "file:stillalive" "tempo:1.0"
* midi "file:mariotheme" listeners:p@aufdemrand|p@Jeebiss
* midi "file:clairdelune" location:200,63,200,world
*
* @author authorblues, David Cernat
*/

public class MidiCommand extends AbstractCommand {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

// Initialize fields
File file = null;
float tempo = 1.0F;
Location location = null;
Set<Player> listeners = new HashSet<Player>();

// Iterate through arguments
for (String arg : scriptEntry.getArguments()){
if (aH.matchesLocation(arg))
location = aH.getLocationFrom(arg);

else if (aH.matchesValueArg("file, f", arg, ArgumentType.Custom)) {
try {
String path = "plugins/Denizen/midi/" + aH.getStringFrom(arg);

if (!path.endsWith(".mid")) {

path = path + ".mid";
}

file = new File(path);
} catch (Exception e) {
dB.echoError("Invalid file!");
}
}

else if (aH.matchesValueArg("listeners, l", arg, ArgumentType.Custom)) {

Entity entity = null;

for (String listener : aH.getListFrom(arg)) {

entity = dEntity.valueOf(listener).getBukkitEntity();

if (entity != null && entity instanceof Player) {

listeners.add((Player) entity);
}
else {
dB.echoError("Invalid listener '%s'!", listener);
}
}
}

else if (aH.matchesValueArg("tempo, t", arg, ArgumentType.Float))
tempo = aH.getFloatFrom(arg);

else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

// Check required args
if (file == null)
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "FILE");

// If there are no listeners, and the location is null,
// add this player to the listeners
if (location == null && listeners.size() == 0) {

listeners.add(scriptEntry.getPlayer());
}

// Stash args in ScriptEntry for use in execute()
scriptEntry.addObject("file", file);
scriptEntry.addObject("listeners", listeners);
scriptEntry.addObject("location", location);
scriptEntry.addObject("tempo", tempo);
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

// Extract objects from ScriptEntry
File file = (File) scriptEntry.getObject("file");
@SuppressWarnings("unchecked")
Set<Player> listeners = (Set<Player>) scriptEntry.getObject("listeners");
Location location = (Location) scriptEntry.getObject("location");
Float tempo = (Float) scriptEntry.getObject("tempo");

// Report to dB
dB.report(getName(),
aH.debugObj("Playing midi file", file.getPath()
+ (listeners != null ? aH.debugObj("Listeners", listeners) : "")
+ (location != null ? aH.debugObj("Location", location) : ""))
+ aH.debugObj("Tempo", tempo));

// Play the sound
if (location != null) {
MidiUtil.playMidiQuietly(file, tempo, location);
}
else {
MidiUtil.playMidiQuietly(file, tempo, listeners);
}
}
}
Expand Up @@ -14,7 +14,7 @@

/* playeffect [location:<x,y,z,world>] [effect:<name>] (data:<#>) (radius:<#>)*/

/*
/**
* Arguments: [] - Required, () - Optional
* [location:<x,y,z,world>] specifies location of the effect
* [effect:<name>] sets the name of effect to be played
Expand All @@ -24,6 +24,8 @@
* Example Usage:
* playeffect location:123,65,765,world effect:record_play data:2259 radius:7
* playeffect location:<npc.location> e:smoke r:3
*
* @author David Cernat
*/

public class PlayEffectCommand extends AbstractCommand {
Expand All @@ -50,10 +52,10 @@ else if (aH.matchesValueArg("effect, e", arg, ArgumentType.Custom)) {
}
}

else if (aH.matchesValueArg("radius, r", arg, ArgumentType.Float))
else if (aH.matchesValueArg("radius, r", arg, ArgumentType.Integer))
radius = aH.getIntegerFrom(arg);

else if (aH.matchesValueArg("data, d", arg, ArgumentType.Float))
else if (aH.matchesValueArg("data, d", arg, ArgumentType.Integer))
data = aH.getIntegerFrom(arg);

else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/net/aufdemrand/denizen/utilities/midi/MidiUtil.java
@@ -0,0 +1,120 @@
package net.aufdemrand.denizen.utilities.midi;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequencer;

import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;

/**
* Utility for playing midi files for players to hear.
*
* @author authorblues
*/
public class MidiUtil
{
public static void startSequencer(File file, float tempo, Receiver receiver)
throws InvalidMidiDataException, IOException, MidiUnavailableException
{

Sequencer sequencer = MidiSystem.getSequencer(false);
sequencer.setSequence(MidiSystem.getSequence(file));
sequencer.open();

// Set desired tempo
sequencer.setTempoFactor(tempo);

sequencer.getTransmitter().setReceiver(receiver);
sequencer.start();
}

public static void playMidi(File file, float tempo, Set<Player> listeners)
throws InvalidMidiDataException, IOException, MidiUnavailableException
{
NoteBlockReceiver noteblockRecv = new NoteBlockReceiver(listeners);
startSequencer(file, tempo, noteblockRecv);
}

public static void playMidi(File file, float tempo, Location location)
throws InvalidMidiDataException, IOException, MidiUnavailableException
{
NoteBlockReceiver noteblockRecv = new NoteBlockReceiver(location);
startSequencer(file, tempo, noteblockRecv);
}

public static boolean playMidiQuietly(File file, float tempo, Set<Player> listeners)
{
try { MidiUtil.playMidi(file, tempo, listeners); }
catch (MidiUnavailableException e) { e.printStackTrace(); return false; }
catch (InvalidMidiDataException e) { e.printStackTrace(); return false; }
catch (IOException e) { e.printStackTrace(); return false; }

return true;
}

public static boolean playMidiQuietly(File file, float tempo, Location location)
{
try { MidiUtil.playMidi(file, tempo, location); }
catch (MidiUnavailableException e) { e.printStackTrace(); return false; }
catch (InvalidMidiDataException e) { e.printStackTrace(); return false; }
catch (IOException e) { e.printStackTrace(); return false; }

return true;
}

public static boolean playMidiQuietly(File file, Set<Player> listeners)
{
return playMidiQuietly(file, 1.0f, listeners);
}

public static boolean playMidiQuietly(File file, Location location)
{
return playMidiQuietly(file, 1.0f, location);
}

// provided by github.com/sk89q/craftbook
private static final int[] instruments = {
0, 0, 0, 0, 0, 0, 0, 5, // 8
6, 0, 0, 0, 0, 0, 0, 0, // 16
0, 0, 0, 0, 0, 0, 0, 5, // 24
5, 5, 5, 5, 5, 5, 5, 5, // 32
6, 6, 6, 6, 6, 6, 6, 6, // 40
5, 5, 5, 5, 5, 5, 5, 2, // 48
5, 5, 5, 5, 0, 0, 0, 0, // 56
0, 0, 0, 0, 0, 0, 0, 0, // 64
0, 0, 0, 0, 0, 0, 0, 0, // 72
0, 0, 0, 0, 0, 0, 0, 0, // 80
0, 0, 0, 0, 0, 0, 0, 0, // 88
0, 0, 0, 0, 0, 0, 0, 0, // 96
0, 0, 0, 0, 0, 0, 0, 0, // 104
0, 0, 0, 0, 0, 0, 0, 0, // 112
1, 1, 1, 3, 1, 1, 1, 5, // 120
1, 1, 1, 1, 1, 2, 4, 3, // 128
};

public static Sound patchToInstrument(int patch)
{
// look up the instrument matching the patch
switch (instruments[patch])
{
case 1: return Sound.NOTE_BASS_GUITAR;
case 2: return Sound.NOTE_SNARE_DRUM;
case 3: return Sound.NOTE_STICKS;
case 4: return Sound.NOTE_BASS_DRUM;
case 5: return Sound.NOTE_PLING;
case 6: return Sound.NOTE_BASS;
}

// if no instrument match is found, use piano
return Sound.NOTE_PIANO;
}
}

0 comments on commit c1b3c34

Please sign in to comment.