X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooNote.java;h=2a82b6d21b5ac48489aa1a5548f49f96152bb21e;hp=023de207da3aa053ffafff81f2febabe076ea996;hb=HEAD;hpb=570c4561b55541309efb977d0930777b8d214336 diff --git a/MooNote.java b/MooNote.java index 023de20..2a82b6d 100644 --- a/MooNote.java +++ b/MooNote.java @@ -1,13 +1,13 @@ import javax.sound.midi.*; /** - * Functional representation of a MIDI note, which adds functionality to the existent MidiEvent class. + * Functional representation of a MIDI note, which adds functionality to the existing MidiEvent class. * Also provides a reference to the corresponding NoteOff event. * * @author Einar Pehrson */ -public class MooNote extends MidiEvent implements Cloneable { +public class MooNote extends MidiEvent implements Cloneable, Comparable { private MidiEvent noteOffEvent; private ShortMessage noteOnMsg, noteOffMsg; @@ -40,10 +40,50 @@ public class MooNote extends MidiEvent implements Cloneable { noteOffMsg = (ShortMessage)noteOffEvent.getMessage(); try { noteOnMsg.setMessage(ShortMessage.NOTE_ON, channel, pitch, velocity); - noteOffMsg.setMessage(ShortMessage.NOTE_OFF, channel, pitch, 0); + noteOffMsg.setMessage(ShortMessage.NOTE_OFF, channel, pitch, 64); } catch (InvalidMidiDataException e) {System.out.println("Invalid data!");} } + /** + * Returns the note off event of this note. + * @return the note off event + */ + public MidiEvent getNoteOffEvent() { + return noteOffEvent; + } + + /** + * Returns the channel of the current note. + * @return the channel of the note (1-16) + */ + public int getChannel() { + return noteOnMsg.getChannel(); + } + + /** + * Returns the pitch of the current note. + * @return the pitch of the note (0-127) + */ + public int getPitch() { + return noteOnMsg.getData1(); + } + + /** + * Returns the velocity of the current note. + * @return the velocity of the note (0-127) + */ + public int getVelocity() { + return noteOnMsg.getData2(); + } + + /** + * Returns the duration of the current note. + * @return the duration of the note (in ticks) + */ + public int getDuration() { + return (int)(noteOffEvent.getTick() - getTick()); + } + /** * Sets the channel of the current note. * @param channel the channel of the note (1-16) @@ -102,38 +142,6 @@ public class MooNote extends MidiEvent implements Cloneable { setPitch(getPitch() + halftones); } - /** - * Returns the channel of the current note. - * @return the channel of the note (1-16) - */ - public int getChannel() { - return noteOnMsg.getChannel(); - } - - /** - * Returns the pitch of the current note. - * @return the pitch of the note (0-127) - */ - public int getPitch() { - return noteOnMsg.getData1(); - } - - /** - * Returns the velocity of the current note. - * @return the velocity of the note (0-127) - */ - public int getVelocity() { - return noteOnMsg.getData2(); - } - - /** - * Returns the duration of the current note. - * @return the duration of the note (in ticks) - */ - public int getDuration() { - return (int)(noteOffEvent.getTick() - getTick()); - } - /** * Adds this note (both noteOn and noteOffEvents) to a track. * @param track the track it'll be added to. @@ -161,4 +169,14 @@ public class MooNote extends MidiEvent implements Cloneable { new MidiEvent((ShortMessage)noteOffEvent.getMessage().clone(), noteOffEvent.getTick()) ); } + + /** + * Compares this note to another note. + * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object + */ + public int compareTo(Object o) { + int diff = (int)(getTick() - ((MidiEvent)o).getTick()); + if (diff != 0) return diff; + return (noteOnMsg.getData1() - ((ShortMessage)((MidiEvent)o).getMessage()).getData1()); + } }