+ public static boolean load(File loadFile) {
+ // Loads sequence from file
+ try {seq = MidiSystem.getSequence(loadFile);}
+ catch (Exception e) {return false;}
+ file = loadFile;
+ edited = false;
+
+ Track[] tracks = seq.getTracks();
+ reinitializeLists();
+
+ // Searches track 0 for changes in tempo and time signature.
+ MidiEvent event;
+ MetaMessage metaMsg;
+ for (int i = 0; i < tracks[0].size(); i++) {
+ event = tracks[0].get(i);
+ if (event.getMessage().getStatus() == MetaMessage.META) {
+ metaMsg = (MetaMessage)event.getMessage();
+ switch(metaMsg.getType()) {
+ case 81: tempoChanges.add(event); break;
+ case 88: timeSignatures.add(event);
+ }
+ }
+ }
+ Comparator c = new MidiEventComparator();
+ Collections.sort(tempoChanges, c);
+ Collections.sort(timeSignatures, c);
+
+ try {
+ // If no time signature specified at tick 0, adds the standard one.
+ if (timeSignatures.size() == 0 || ((MidiEvent)timeSignatures.get(0)).getTick() != 0) {
+ MetaMessage timeSigMsg = new MetaMessage();
+ timeSigMsg.setMessage(88, encodeTimeSig(4, 4), 4);
+ timeSignatures.add(0, new MidiEvent(timeSigMsg, (long)0));
+ }
+
+ // If no tempo specified at tick 0, adds the standard one.
+ if (tempoChanges.size() == 0 || ((MidiEvent)tempoChanges.get(0)).getTick() != 0) {
+ MetaMessage tempoMsg = new MetaMessage();
+ tempoMsg.setMessage(81, encodeTempo(120), 3);
+ tempoChanges.add(0, new MidiEvent(tempoMsg, (long)0));
+ }
+ } catch (Exception e) {}
+
+ // Converts tracks.
+ for (int i = 0; i < tracks.length; i++) {
+ convertTrack(tracks[i]);
+ }
+
+ // Sends sequence to GUI and sequencer, then returns
+ if (gui != null) gui.setSequence(seq, file);
+ try {
+ sequencer.setSequence(seq);
+ } catch (InvalidMidiDataException e) {}
+ return true;
+ }
+
+ /**
+ * Quantizes the given list of MIDI events
+ * @param notes a list of the notes to quantize
+ * @param resolution the note size to round each note to
+ * @param location whether the quantize should affect the location of the note
+ * @param duration whether the quantize should affect the duration of the note
+ */
+ public static List quantize(List notes, int resolution, boolean location, boolean duration) {
+ Iterator it = notes.iterator();
+ int noteSize = resolution * seq.getResolution() / 4;
+ while(it.hasNext()) {
+ MidiEvent note = (MidiEvent)it.next();
+ if (note instanceof MooNote) {
+ MooNote mn = (MooNote)note;
+ if (location) mn.setTick(Math.round(mn.getTick() / noteSize) * noteSize);
+ if (duration) {
+ int length = Math.round(mn.getDuration() / noteSize) * noteSize;
+ if (length < noteSize) length = noteSize;
+ mn.setDuration(length);
+ }
+ }
+ }
+ return notes;
+ }
+
+ /**
+ * Reinitializes sequence-specific collections.
+ */
+ private static void reinitializeLists() {
+ emptyTracks = new ArrayList();
+ timeSignatures = new ArrayList();
+ tempoChanges = new ArrayList();
+ copyBuffer = new ArrayList();
+ trackSolo = new HashMap();
+ trackMute = new HashMap();
+ selection = new TreeSet();
+ }
+
+ /**
+ * Loads the user preferences.
+ */
+ public static void loadPreferences() {
+
+ }
+
+ /**
+ * Saves the user preferences.
+ */
+ public static void savePreferences() {