]> ruin.nu Git - moosique.git/blob - MooNote.java
inte helt klar
[moosique.git] / MooNote.java
1 import javax.sound.midi.*;
2
3 /**
4  * Functional representation of a MIDI note, which adds functionality to the existent MidiEvent class.
5  * Also provides a reference to the corresponding NoteOff event.
6  * 
7  * @author  Einar Pehrson
8  */
9  
10 public class MooNote extends MidiEvent {
11
12         protected MidiEvent noteOffEvent;
13         protected ShortMessage noteOnMsg, noteOffMsg;
14
15         /** 
16          * Creates a MooNote from the given NoteOn event in the current track.
17          * @param noteOnEvent   the NoteOn event of the note
18          */
19         public MooNote (MidiEvent noteOnEvent) {
20                 super(noteOnEvent.getMessage(), noteOnEvent.getTick());
21                 noteOnMsg = (ShortMessage)getMessage();
22         }
23
24         /** 
25          * Creates a MooNote from the given NoteOn event in the current track and creates a reference to
26          * the corresponding NoteOff event.
27          * @param noteOnEvent   the NoteOn event of the note
28          * @param noteOffEvent  the NoteOff event of the note
29          */
30         public MooNote (MidiEvent noteOnEvent, MidiEvent noteOffEvent) {
31                 super(noteOnEvent.getMessage(), noteOnEvent.getTick());
32                 this.noteOffEvent = noteOffEvent;
33                 noteOnMsg = (ShortMessage)getMessage();
34                 noteOffMsg = (ShortMessage)noteOffEvent.getMessage();
35         }
36
37         /** 
38          * Creates a MooNote of the given pitch, velocity and duration in the current track.
39          * @param track         the track to which the MooNote was added
40          * @param channel       the channel of the note (1-16)
41          * @param pitch         the pitch of the note (0-127)
42          * @param velocity      the velocity of the note (0-127)
43          * @param timestamp     the timestamp of the note in ticks (96 per beat)
44          * @param duration      the duration of the note in ticks (96 per beat)
45          */
46         public MooNote (int track, int channel, int pitch, int velocity, long timestamp, int duration) {
47                 super(new ShortMessage(), timestamp);
48                 noteOffEvent = new MidiEvent(new ShortMessage(), timestamp + duration);
49                 noteOnMsg = (ShortMessage)getMessage();
50                 noteOffMsg = (ShortMessage)noteOffEvent.getMessage();
51                 try {
52                         noteOnMsg.setMessage(ShortMessage.NOTE_ON, channel, pitch, velocity);
53                         noteOffMsg.setMessage(ShortMessage.NOTE_OFF, channel, pitch, 0);
54                 } catch (InvalidMidiDataException e) {}
55         }
56
57         /** 
58          * Sets the channel of the current note.
59          * @param channel       the channel of the note (1-16)
60          */
61         public void setChannel(int channel) {
62                 try {
63                         noteOnMsg.setMessage(noteOnMsg.getCommand(), (byte)channel, noteOnMsg.getData1(), noteOnMsg.getData2());
64                         if(hasNoteOffEvent()) noteOffMsg.setMessage(noteOffMsg.getCommand(), (byte)channel, noteOffMsg.getData1(), noteOffMsg.getData2());
65                 } catch (InvalidMidiDataException e) {}
66         }
67
68         /** 
69          * Sets the pitch of the current note.
70          * @param pitch         the pitch of the note (0-127)
71          */
72         public void setPitch(int pitch) {
73                 try {
74                         noteOnMsg.setMessage(noteOnMsg.getCommand(), noteOnMsg.getChannel(), (byte)pitch, noteOnMsg.getData2());
75                         if(hasNoteOffEvent()) noteOffMsg.setMessage(noteOffMsg.getCommand(), noteOffMsg.getChannel(), (byte)pitch, noteOffMsg.getData2());
76                 } catch (InvalidMidiDataException e) {}
77         }
78
79         /** 
80          * Sets the velocity of the current note.
81          * @param vel   the velocity of the note (0-127)
82          */
83         public void setVelocity(int vel) {
84                 try {
85                         noteOnMsg.setMessage(noteOnMsg.getCommand(), noteOnMsg.getChannel(), noteOnMsg.getData1(), (byte)vel);
86                         if(hasNoteOffEvent()) noteOffMsg.setMessage(noteOffMsg.getCommand(), noteOffMsg.getChannel(), noteOffMsg.getData1(), noteOffMsg.getData2());
87                 } catch (InvalidMidiDataException e) {}
88         }
89
90         /** 
91          * Sets the duration of the current note (or rather moves the note off event).
92          * @param n     the duration of the note in ticks (96 per beat)
93          */
94         public void setDuration(int ticks) {
95                 if (hasNoteOffEvent()) noteOffEvent.setTick(getTick() + ticks);
96         }
97
98         /** 
99          * Sets the timestamp of the current note.
100          * @param tick  the timestamp of the note in ticks (96 per beat)
101          */
102         public void setTick(long tick) {
103                 if (hasNoteOffEvent()) noteOffEvent.setTick(tick +  getDuration());
104                 super.setTick(tick);
105         }
106
107         /** 
108          * Returns the channel of the current note.
109          * @return the channel of the note (1-16)
110          */
111         public int getChannel() {
112                 return noteOnMsg.getChannel();
113         }
114
115         /** 
116          * Returns the pitch of the current note.
117          * @return the pitch of the note (0-127)
118          */
119         public int getPitch() {
120                 return noteOnMsg.getData1();
121         }
122
123         /** 
124          * Returns the velocity of the current note.
125          * @return the velocity of the note (0-127)
126          */
127         public int getVelocity() {
128                 return noteOnMsg.getData2();
129         }
130
131         /** 
132          * Returns the duration of the current note.
133          * @return the duration of the note (in ticks)
134          */
135         public int getDuration() {
136                 if (!hasNoteOffEvent()) return 0;
137                 return (int)(noteOffEvent.getTick() - getTick());
138         }
139
140         /** 
141          * Returns whether the NoteOff event was found.
142          * @return      the note off MidiEvent
143          */
144         public boolean hasNoteOffEvent() {
145                 return noteOffEvent != null;
146         }
147
148         public void addTo(Track track){
149                 track.add(this);
150                 if (hasNoteOffEvent()) track.add(noteOffEvent);
151         }
152         
153         public void removeFrom(Track track){
154                 track.remove(this);
155                 if (hasNoteOffEvent()) track.remove(noteOffEvent);
156         }
157 }