]> ruin.nu Git - moosique.git/blobdiff - Moosique.java
Fixed drag select in track view.
[moosique.git] / Moosique.java
index 0fc6fd74aba1f76cb11b17d1ec75baf8a79657ed..09e8395707b175f41a9ab18e94b4a0858c8ef333 100644 (file)
@@ -21,15 +21,15 @@ public class Moosique {
        private static MidiChannel[] channels;
        private static MidiChannel activeChannel;
        private static MidiEvent[] timeSignatures, tempoChanges;
+
        private static ArrayList emptyTracks;
        private static Map trackMute = new HashMap();
        private static Map trackSolo = new HashMap();
-
+       private static Thread player;
 
        private static String filename;
        private static long editPosition;
-       private static boolean makeGUI = true, isEdited = false, drawEmptyTracks = false;
-       private static Thread player;
+       private static boolean makeGUI = true, initSound = true, edited = false, drawEmptyTracks = false;
        public static final int DEFAULT_RESOLUTION = 96, DEFAULT_TRACKS = 4;
 
        /** 
@@ -44,8 +44,9 @@ public class Moosique {
                // Parses command-line arguments.
                String fileArg = null;
                for (int i = 0; i < args.length; i++) {
-                       if (args[i].equals("-n")) {makeGUI = false;}
-                       else if (fileArg == null) {fileArg = args[i];}
+                       if (args[i].equals("-n")) makeGUI = false;
+                       else if (args[i].equals("-m")) initSound = false;
+                       else if (fileArg == null) fileArg = args[i];
                }
 
                // Acquires MIDI devices and connects them.
@@ -63,7 +64,7 @@ public class Moosique {
                        setActiveChannel(0);
                } catch (MidiUnavailableException e) {
                        System.out.println("Failed, quitting.");
-                       // System.exit(1);
+                       System.exit(1);
                }
                System.out.println("Done");
 
@@ -106,8 +107,8 @@ public class Moosique {
 
 
        /*                              ***
-        **       ACCESSOR METHODS      **
-        ***                            */
+        **       ACCESSOR METHODS       **
+        ***                              */
 
 
 
@@ -156,6 +157,22 @@ public class Moosique {
                return gui;
        }
 
+       /** 
+        * Calculates the position (measures, beats, ticks) in the current sequence for the given tick position.
+        * @return an array of integers where index 0 is measures, 1 is beats and 2 is ticks.
+        */
+       public static int[] getPositionForTick(long ticks) {
+               /*
+               int measures, beats, ticks;
+               for (int i = 0; i < timeSignatures.length; i++) {
+                       long tick = timeSignatures[i].getTick();
+                       // Split the ticks in the interval into measures, beats and ticks.
+               }
+               */
+               int[] pos = {1, 1, 1};
+               return pos;
+       }
+
        /** 
         * Returns the receiver of the current sequencer.
         * @return the receiver
@@ -189,6 +206,21 @@ public class Moosique {
                // if (tempoMsg == null) return 0;
        }
 
+       /** 
+        * Calculates the tick position in the current sequence for the given position (measures, beats, ticks).
+        * @return the tick position.
+        */
+       public static long getTicksForPosition(int measures, int beats, int ticks) {
+               long tickPos = 0;
+               /*
+               for (int i = 0; i < timeSignatures.length; i++) {
+                       long tick = timeSignatures[i].getTick();
+                       // Add the measures, beats and ticks in the interval.
+               }
+               */
+               return tickPos;
+       }
+
        /** 
         * Returns the tempo of the current sequence.
         * @return the tick position
@@ -204,7 +236,7 @@ public class Moosique {
         * @return the tick position
         */
        public static boolean isEdited() {
-               return isEdited;
+               return edited;
        }
 
        /** 
@@ -224,8 +256,8 @@ public class Moosique {
 
 
        /*                              ***
-        **       MUTATOR METHODS       **
-        ***                            */
+        **       MUTATOR METHODS        **
+        ***                              */
 
 
 
@@ -270,7 +302,7 @@ public class Moosique {
         * Sets the current sequence as edited, which implies prompts when loading a new sequence.
         */
        public static void setEdited() {
-               isEdited = true;
+               edited = true;
        }
 
        /** 
@@ -296,6 +328,14 @@ public class Moosique {
        public static void setTimeSig(int bpm) {
                // timeSigMsg
        }
+       
+       public static void setTrackSolo(Track track, boolean on){
+               trackSolo.put(track, new Boolean(on));  
+       }
+
+       public static void setTrackMute(Track track, boolean on){
+               trackMute.put(track, new Boolean(on));  
+       }
 
 
 
@@ -305,8 +345,8 @@ public class Moosique {
 
 
        /*                              ***
-        **       PLAYBACK METHODS      **
-        ***                            */
+        **       PLAYBACK METHODS       **
+        ***                              */
 
 
 
@@ -404,8 +444,8 @@ public class Moosique {
 
 
        /*                              ***
-        **       SYSTEM & IO METHODS   **
-        ***                            */
+        **       SYSTEM & IO METHODS    **
+        ***                              */
 
 
 
@@ -445,7 +485,7 @@ public class Moosique {
                } catch (IOException e) {
                        return false;
                }
-               isEdited = false;
+               edited = false;
 
                Track[] tracks = seq.getTracks();
                emptyTracks = new ArrayList();
@@ -487,11 +527,7 @@ public class Moosique {
                        if (noteOns.size() == 0) emptyTracks.add(tracks[i]);
                        
                        // Sorts the note lists by tick position.
-                       Comparator c = new Comparator() {
-                               public int compare(Object o1, Object o2) {
-                                       return (int)(((MidiEvent)o1).getTick() - ((MidiEvent)o2).getTick());
-                               }
-                       };
+                       Comparator c = new NoteComparator();
                        Collections.sort(noteOns, c);
                        Collections.sort(noteOffs, c);
 
@@ -519,7 +555,7 @@ public class Moosique {
                                if (off != null) {
                                        tracks[i].add(new MooNote(on, off));
                                } else {
-                                       tracks[i].add(new MooNote(on));
+                                       tracks[i].add(new MooNote(on, new MidiEvent((ShortMessage)on.getMessage().clone(), on.getTick() + 48)));
                                }
                                iOn.remove();
                        }
@@ -532,6 +568,20 @@ public class Moosique {
                return true;
        }
 
+       /** 
+        * Prompts the user .
+        */
+       public static boolean promptOnUnsavedChanges() {
+               if (!edited) return false;
+               int exitOption = JOptionPane.showConfirmDialog(gui,
+                       "The current sequence has been edited, but not saved.\nDo you wish to continue anyway?",
+                       "File not saved - continue?", 
+                       JOptionPane.OK_CANCEL_OPTION, 
+                       JOptionPane.WARNING_MESSAGE);
+               if (exitOption == JOptionPane.CANCEL_OPTION || exitOption == JOptionPane.CLOSED_OPTION) return true;
+               return false;
+       }
+
        /** 
         * Saves the current sequence to the previously given filename.
         */
@@ -551,6 +601,7 @@ public class Moosique {
                try {
                        MidiSystem.write(seq, 1, new File(file));
                        filename = file;
+                       edited = false;
                        gui.setStatus("Saved " + file);
                        return true;
                } catch (IOException e) {
@@ -563,24 +614,20 @@ public class Moosique {
         * Releases all reserved devices and exits the program.
         */
        public static void quit() {
-               if (isEdited && gui != null) {
-                       int exitOption = JOptionPane.showConfirmDialog(gui,
-                               "The current sequence has been edited, but not saved.\nDo you wish to quit anyway?",
-                               "File not saved - really quit?", 
-                               JOptionPane.OK_CANCEL_OPTION, 
-                               JOptionPane.WARNING_MESSAGE);
-                       if (exitOption == JOptionPane.CANCEL_OPTION || exitOption == JOptionPane.CLOSED_OPTION) return;
+               if (gui != null) {
+                       if (promptOnUnsavedChanges()) return;
                }
                if (sequencer.isOpen()) sequencer.close();
                if (synthesizer.isOpen()) synthesizer.close();
                System.exit(0);
        }
        
-       public static void setTrackSolo(Track track, boolean on){
-               trackSolo.put(track, new Boolean(on));  
-       }
-
-       public static void setTrackMute(Track track, boolean on){
-               trackMute.put(track, new Boolean(on));  
+       /** 
+        * A Comparator for sorting lists of MidiEvents.
+        */
+       public static class NoteComparator implements Comparator {
+               public int compare(Object o1, Object o2) {
+                       return (int)(((MidiEvent)o1).getTick() - ((MidiEvent)o2).getTick());
+               }
        }
 }