+ public static void play() {
+ sequencer.setTickPosition(editPosition);
+ resume();
+ }
+
+ /**
+ * Resumes playback of the current sequence.
+ */
+ public static void resume() {
+ gui.update(0);
+ try {
+ sequencer.setSequence(seq);
+ } catch (InvalidMidiDataException e) {}
+ Track[] tracks = seq.getTracks();
+
+ sequencer.start();
+
+ for (int i = 0; i < tracks.length; i++) {
+
+ Object ob = trackSolo.get(tracks[i]);
+ if(ob instanceof Boolean){
+ sequencer.setTrackSolo(i,((Boolean)ob).booleanValue());
+ }
+
+ ob = trackMute.get(tracks[i]);
+ if(ob instanceof Boolean){
+ sequencer.setTrackMute(i,((Boolean)ob).booleanValue());
+ }
+ }
+
+
+ // Disables input to volatile components
+ // gui.disable();
+
+ // Creates the visualisation thread and starts it.
+ player = new Thread () {
+ public void run() {
+ while(sequencer.isRunning()) {
+ // Updates the GUI with the current tick position.
+ gui.update(sequencer.getTickPosition());
+
+ // Puts the thread to sleep for as long as it takes
+ // the sequencer to reach the next sixteenth.
+ try {
+ //sleep((long)((15000 / getTempo()) * (tickDiff / ticksPerSixteenth)));
+ sleep (10);
+ } catch (InterruptedException e) {
+ Moosique.stop();
+ }
+ }
+ Moosique.stop();
+ }
+ };
+ player.start();
+ }
+
+ /**
+ * Pauses playback of the current sequence.
+ */
+ public static void pause() {
+ if (sequencer.isRunning()) {
+ sequencer.stop();
+ }
+ if (player != null) player.interrupt();
+ }
+
+ /**
+ * Stops playback of the current sequence.
+ */
+ public static void stop() {
+ if (sequencer.isRunning()) {
+ sequencer.stop();
+ }
+ sequencer.setTickPosition(editPosition);
+ if (player != null) player.interrupt();
+ gui.update((long)0);
+ }
+
+
+
+
+
+
+
+
+ /* ***
+ ** SYSTEM & IO METHODS **
+ *** */
+
+
+
+
+
+
+
+
+ /**
+ * Replaces the current sequence with a new one, holding three empty tracks.
+ */
+ public static void clearSequence() {
+ // Creates a new sequence and sends it to the sequencer.
+ try {
+ seq = new Sequence(Sequence.PPQ, DEFAULT_RESOLUTION, DEFAULT_TRACKS);
+ sequencer.setSequence(seq);
+ filename = null;
+ emptyTracks = new ArrayList();
+ trackSolo = new HashMap();
+ trackMute = new HashMap();
+ copyBuffer = new ArrayList();
+ } catch (InvalidMidiDataException e) {}
+ // Sends sequence to GUI.
+ if (gui != null) gui.setSequence(seq);
+ }
+
+ /**
+ * Wraps each NoteOn event in the track with its NoteOff event in a MooNote.
+ */
+ public static void convertTrack(Track track) {
+ // Searches the track for NoteOn and NoteOff events
+ ArrayList noteOns = new ArrayList(track.size() / 2);
+ ArrayList noteOffs = new ArrayList(track.size() / 2);
+ MidiEvent event;
+ for (int j = 0; j < track.size(); j++) {
+ event = track.get(j);
+ if (event.getMessage().getStatus() >= 144 &&
+ event.getMessage().getStatus() < 160) noteOns.add(event);
+ if (event.getMessage().getStatus() >= 128 &&
+ event.getMessage().getStatus() < 144) noteOffs.add(event);
+ }
+ noteOns.trimToSize();
+ noteOffs.trimToSize();
+ if (noteOns.size() == 0) emptyTracks.add(track);
+
+ // Sorts the note lists by tick position.
+ Comparator c = new MidiEventComparator();
+ Collections.sort(noteOns, c);
+ Collections.sort(noteOffs, c);
+
+ // Replaces each NoteOn event it with a MooNote containing a reference to the NoteOff event.
+ Iterator iOn = noteOns.iterator(), iOff;
+ MidiEvent on, off = null, nextOff;
+ ShortMessage onMsg, nextOffMsg;
+ while(iOn.hasNext()) {
+ on = (MidiEvent)iOn.next();
+ onMsg = (ShortMessage)on.getMessage();
+ iOff = noteOffs.iterator();
+ while(iOff.hasNext()) {
+ nextOff = (MidiEvent)iOff.next();
+ nextOffMsg = (ShortMessage)nextOff.getMessage();
+ if(onMsg.getChannel() == nextOffMsg.getChannel() &&
+ onMsg.getData1() == nextOffMsg.getData1() &&
+ c.compare(nextOff, on) > 0) {
+ off = nextOff;
+ iOff.remove();
+ break;
+ }
+
+ }
+ track.remove(on);
+ if (off != null) {
+ track.add(new MooNote(on, off));
+ } else {
+ track.add(new MooNote(on, new MidiEvent((ShortMessage)on.getMessage().clone(), on.getTick() + 48)));
+ }
+ iOn.remove();
+ }