X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Moosique.java;h=531e4f511886dcc96c10c7ed9d614f9da83d6bf0;hb=fe67e0acf0d44c09dcfbbfd1a02a91f43d2cf60e;hp=c7ea53fcc01703130d09cf33c4a722c57ab40ac6;hpb=c2644cb200b06d71a3e6c4548c37272228130c3d;p=moosique.git diff --git a/Moosique.java b/Moosique.java index c7ea53f..531e4f5 100644 --- a/Moosique.java +++ b/Moosique.java @@ -22,7 +22,9 @@ public class Moosique { private static String filename, fileArg; private static long position; - private static boolean makeGUI = true; + 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."); - System.exit(1); +// System.exit(1); } System.out.println("Done"); @@ -139,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. @@ -151,7 +153,7 @@ public class Moosique { */ public static void play() { sequencer.setTickPosition(position); - sequencer.start(); + resume(); } /** @@ -159,6 +161,7 @@ public class Moosique { */ public static void pause() { sequencer.stop(); + player.destroy(); } /** @@ -166,6 +169,18 @@ public class Moosique { */ public static void resume() { 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(); } /** @@ -174,24 +189,41 @@ public class Moosique { public static void stop() { sequencer.stop(); sequencer.setTickPosition(position); + 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 @@ -222,13 +254,8 @@ public class Moosique { } catch (IOException e) { 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; @@ -269,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; }