X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Moosique.java;h=531e4f511886dcc96c10c7ed9d614f9da83d6bf0;hb=fe67e0acf0d44c09dcfbbfd1a02a91f43d2cf60e;hp=5ee3bccea394dd8819cba008c3627d8e7139972e;hpb=ba84468acd609e00f4c2df575712b4ffd1399e58;p=moosique.git diff --git a/Moosique.java b/Moosique.java index 5ee3bcc..531e4f5 100644 --- a/Moosique.java +++ b/Moosique.java @@ -1,6 +1,7 @@ import javax.sound.midi.*; -import javax.swing.*; import java.io.*; +import javax.swing.*; +import java.util.*; /** * Moosique - The MIDI Tracker @@ -21,8 +22,9 @@ public class Moosique { private static String filename, fileArg; private static long position; - private static boolean makeGUI = true; - private static boolean playing = false; + private static boolean makeGUI = true, isEdited; + private static Thread player; + public static final int RESOLUTION = 96, DEFAULT_TRACKS = 4; /** * Starts the application. @@ -50,7 +52,7 @@ public class Moosique { setActiveChannel(0); } catch (MidiUnavailableException e) { System.out.println("Failed, quitting."); - quit(); +// System.exit(1); } System.out.println("Done"); @@ -68,9 +70,12 @@ public class Moosique { clearSequence(); } - // If n-flag is set, plays song and then exits. Otherwise builds GUI. + // Builds GUI, unless n-flag is set. if (makeGUI) { System.out.print("Building GUI..."); + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) {} gui = new MooGUI(seq); System.out.println("Done"); } else { @@ -136,7 +141,7 @@ public class Moosique { public static void clearSequence() { // Creates a new sequence and sends it to the sequencer. try { - seq = new Sequence(Sequence.PPQ, 96, 3); + seq = new Sequence(Sequence.PPQ, RESOLUTION, DEFAULT_TRACKS); sequencer.setSequence(seq); } catch (InvalidMidiDataException e) {} // Sends sequence to GUI. @@ -147,63 +152,78 @@ public class Moosique { * Starts playback of the current sequence. */ public static void play() { - playing = true; sequencer.setTickPosition(position); - sequencer.start(); + resume(); } /** * Pauses playback of the current sequence. */ public static void pause() { - playing = false; sequencer.stop(); + player.destroy(); } /** * Resumes playback of the current sequence. */ public static void resume() { - playing = true; sequencer.start(); + + // Disables input to volatile components + // gui.disable(); + + // Creates the visualization thread and starts it. + player = new Thread () { + public void run() { + while(sequencer.isRunning()) gui.update(sequencer.getTickPosition()); + Moosique.stop(); + } + }; + player.start(); } /** * Stops playback of the current sequence. */ public static void stop() { - playing = false; sequencer.stop(); sequencer.setTickPosition(position); - } - - /** - * Pauses if playing and resumes if stopped. - */ - public static void resumepause() - { - if (playing) - pause(); - else - resume(); + player.interrupt(); + gui.update((long)0); } /** - * Rewinds the current sequence the given number of measures. - * @param measures the number of measures to rewind + * Returns the current tick position of the sequencer. + * @return the tick position */ public static long getPosition() { return position; } /** - * Rewinds the current sequence the given number of measures. - * @param measures the number of measures to rewind + * Sets the current tick position of the sequencer. + * @param ticks the tick position */ public static void setPosition(long ticks) { position = ticks; } + /** + * Returns true if the current sequence has been edited. + * @return the tick position + */ + public static boolean isEdited() { + return isEdited; + } + + /** + * Sets the current sequence as edited, which implies prompts when loading a new sequence. + */ + public static void setEdited() { + isEdited = true; + } + /** * Rewinds the current sequence the given number of measures. * @param measures the number of measures to rewind @@ -232,22 +252,23 @@ public class Moosique { } catch (InvalidMidiDataException e) { return false; } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Error 404", "File Not Found", JOptionPane.ERROR_MESSAGE); return false; } - - // Sends sequence to GUI and sequencer - if (gui != null) gui.setSequence(seq); - try { - sequencer.setSequence(seq); - } catch (InvalidMidiDataException e) {} - + isEdited = false; + // Searches the sequence for NoteOn events Track[] tracks = seq.getTracks(); MidiEvent noteOn, noteOff = null, nextEvent; MidiMessage nextMsg; ShortMessage shortMsg; for (int i = 0; i < tracks.length; i++) { + /* + Collections.sort(track[i].events, new Comparator() { + public int compare(Object o1, Object o2) { + return ((MidiEvent)o2).getTick() - ((MidiEvent)o1).getTick(); + } + }); + */ for (int j = 0; j < tracks[i].size(); j++) { noteOn = tracks[i].get(j); if (noteOn.getMessage() instanceof ShortMessage) { @@ -275,6 +296,11 @@ public class Moosique { } } } + // Sends sequence to GUI and sequencer, then returns + if (gui != null) gui.setSequence(seq); + try { + sequencer.setSequence(seq); + } catch (InvalidMidiDataException e) {} return true; }