]> ruin.nu Git - moosique.git/commitdiff
removed exportMIDI method; added quit, getPosition, setPosition and resume
authorEinar Pehrson <einarp@itstud.chalmers.se>
Wed, 23 Apr 2003 02:12:53 +0000 (02:12 +0000)
committerEinar Pehrson <einarp@itstud.chalmers.se>
Wed, 23 Apr 2003 02:12:53 +0000 (02:12 +0000)
methods; changed arguments in forward and rewind methods to (long ticks);

MooNote.java
MooSequence.java
MooTrack.java
Moosique.java

index fcf32e047c8af6d1819937c914cd57a7bd82f498..d57772b92be50251ce9a06bb2456344cff570772 100644 (file)
@@ -17,20 +17,19 @@ public class MooNote {
         * Creates a MooNote of the given pitch, velocity and length in the current track.
         */
        public MooNote (int pitch, int velocity, int length) {
-               // MidiEvent(MidiMessage message, long tick)
                noteOnMsg = new ShortMessage();
                noteOffMsg = new ShortMessage();
                noteOnMsg.setMessage(ShortMessage.NOTE_ON, pitch, velocity);
                noteOffMsg.setMessage(ShortMessage.NOTE_OFF, pitch, velocity);
 //             noteOnTime = ???;
                noteOffTime = noteOnTime + length;
-               noteOnEvent = new MidiEvent(noteOnMsg, noteOnTime)
-               noteOffEvent = new MidiEvent(noteOffMsg, noteOffTime)
+               noteOnEvent = new MidiEvent(noteOnMsg, noteOnTime);
+               noteOffEvent = new MidiEvent(noteOffMsg, noteOffTime);
        }
 
        /* 
         * Sets the pitch of the current note.
-        + @param pitch         the pitch of the note (0-127)
+        * @param pitch         the pitch of the note (0-127)
         */
        public void setPitch(int pitch) {
                noteOnMsg.setMessage(ShortMessage.NOTE_ON, pitch, noteOnMsg.getData2());
index 2390c4a01da6d8d28f6b7c0084a678fa64f4b3fc..96753790fcc2d83ef7c834fb3a37863b5171f0e9 100644 (file)
@@ -1,6 +1,10 @@
 import javax.sound.midi.*;
 import java.util.*;
 
+/* UPDATES
+   Added MooSequence(Sequence seq) constructor.
+*/
+
 /*
  * Functional representation of a MIDI sequence.
  *
@@ -12,10 +16,17 @@ public class MooSequence {
 
        private ArrayList tracks;
 
+       /* 
+        * Creates a MooSequence from the given Sequence.
+        */
+       public MooSequence(Sequence seq) {
+
+       }
+
        /* 
         * Creates a MooSequence with three tracks.
         */
-       public MooSequence () {
+       public MooSequence() {
                tracks = new ArrayList();
                addTrack(0);
        }
@@ -58,7 +69,15 @@ public class MooSequence {
         * @return a Sequence
         */
        public Sequence getSequence() {
-       
+               Sequence seq = new Sequence(Sequencer.PPQ, 96, tracks.size());
+               Track t;
+               for (int i = 0; i < tracks.size(); i++) {
+                       t = tracks.get(i);
+                       for (int j = 0; j < t.notes.size(); j++) {
+                               t.add(t.notes.get(j).getNoteOnEvent());
+                               t.add(t.notes.get(j).getNoteOffEvent());                        }
+                       }
+               }
        }
 
        /* 
index b14bcca97de709b8491b0368f11c67108572b41f..64e6da2af10947058ad2defb5c14b8ee36bb57ae 100644 (file)
@@ -65,11 +65,11 @@ public class MooTrack {
        }
 
        /* 
-        * Deletes the given note to the current track.
+        * Deletes the given note from the current track.
         * @param note          the MooNote to delete
         */
        public void deleteNote(MooNote note) {
-       
+               notes.remove(note);
        }
 
        /* 
index 42c5e2e03fea09b18c15c566d3fa044502107d01..4cf89659d86a2d572c706a14febe75aeebd85943 100644 (file)
@@ -1,6 +1,15 @@
 import javax.sound.midi.*;
 import java.io.*;
-// Import external MIDIFileReader and MIDIFileWriter
+
+/* UPDATES
+       Killed superfluous exportMIDI method.
+       Added public static void quit()
+       Removed Sequence seq property.
+       Changed forward(int measures) and rewind(int measures) to forward(long ticks) and rewind(long ticks).
+       Added public static long getPosition()
+       Added public static void setPosition(long ticks)
+       Added public static void resume()
+*/
 
 /*
  * Moosique - The MIDI Tracker
@@ -14,27 +23,35 @@ import java.io.*;
 public class Moosique {
 
        private static MooGUI gui;
-       private static MooSequence mooSeq;
-       private static Sequence seq;
+       private static MooSequence seq;
+
        private static Sequencer sequencer = null;
-       private static String filename;
-       private static int position;
+       private static Synthesizer synthesizer = null;
+       private static Receiver receiver = null;
+       private static MidiChannel[] channels;
+
+       private static String file;
+       private static long position;
 
        /* 
         * Starts the application.
         */
        public static void main (String[] args) {
-               // Creates song and GUI
                seq = new MooSequence();
-               gui = new MooGUI(mooSeq);
+               gui = new MooGUI(seq);
 
-               // Initializes MIDI sequencer
+               // Acquires MIDI devices and connects them.
                try {
                        sequencer = MidiSystem.getSequencer();
                        sequencer.open();
+                       synthesizer = MidiSystem.getSynthesizer();
+                       synthesizer.open();
+                       sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
                } catch (MidiUnavailableException e) {
-                       System.exit(0);
+                       quit();
                }
+
+               channels = synthesizer.getChannels();
        }
 
        /* 
@@ -42,15 +59,15 @@ public class Moosique {
         * @return the current sequence
         */
        public static MooSequence getSequence() {
-               return mooSeq;
+               return seq;
        }
 
        /* 
         * Starts playback of the current sequence.
         */
        public static void play() {
-               seq = mooSeq.getSequence();
-               sequencer.setSequence(seq);
+               sequencer.setSequence(seq.getSequence());
+               sequencer.setTickPosition(position);
                sequencer.start();
        }
 
@@ -58,7 +75,14 @@ public class Moosique {
         * Pauses playback of the current sequence.
         */
        public static void pause() {
-       
+               sequencer.stop();
+       }
+
+       /* 
+        * Resumes playback of the current sequence.
+        */
+       public static void resume() {
+               sequencer.start();
        }
 
        /* 
@@ -66,22 +90,39 @@ public class Moosique {
         */
        public static void stop() {
                sequencer.stop();
+               sequencer.setTickPosition(position);
        }
 
        /* 
         * Rewinds the current sequence the given number of measures.
         * @param measures      the number of measures to rewind
         */
-       public static void rewind(int measures) {
-       
+       public static long getPosition() {
+               return position;
+       }
+
+       /* 
+        * Rewinds the current sequence the given number of measures.
+        * @param measures      the number of measures to rewind
+        */
+       public static void setPosition(long ticks) {
+               position = ticks;
+       }
+
+       /* 
+        * Rewinds the current sequence the given number of measures.
+        * @param measures      the number of measures to rewind
+        */
+       public static void rewind(long ticks) {
+               position -= ticks;
        }
 
        /* 
         * Fast forwards the current sequence the given number of measures.
         * @param measures      the number of measures to fast forward
         */
-       public static void forward(int measures) {
-       
+       public static void forward(long ticks) {
+               position += ticks;
        }
 
        /* 
@@ -89,7 +130,8 @@ public class Moosique {
         * @param filename      the filename to use
         */
        public static void load(String filename) throws IOException {
-       
+               file = filename;
+               seq = new MooSequence(MidiSystem.getSequence(new File(filename)));
        }
 
        /* 
@@ -97,21 +139,26 @@ public class Moosique {
         * @param filename      the filename to use
         */
        public static void saveAs(String filename) throws IOException {
-               
+               MidiSystem.write(seq.getSequence(), 1, new File(filename));
+
        }
 
        /* 
         * Saves the current sequence to the previously given filename.
         */
        public static void save() throws IOException {
-       
+               saveAs(file);
        }
 
        /* 
-        * Exports the current sequence to a standard MIDI file.
-        * @param filename      the filename to use
+        * Releases all reserved devices and exits the program.
         */
-       public static void exportMIDI(String filename) throws IOException {
-       
+       public static void quit() {
+               if (sequencer.isOpen()) {
+                       try {
+                               sequencer.open();
+                       } catch (MidiUnavailableException e) {}
+               }
+               System.exit(0);
        }
 }
\ No newline at end of file