import javax.sound.midi.*;
import java.io.*;
-/**
- * Moosique - The trackers approach to MIDI
- * ugg
+/* 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
+ *
* Main class that handles initiation, IO and sound FX.
*
* @author Andersson, Andreen, Lanneskog, Pehrson
private static MooGUI gui;
private static MooSequence seq;
- private static String filename;
- /**
- * Runs the application.
+ private static Sequencer sequencer = null;
+ 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) {
+ seq = new MooSequence();
+ gui = new MooGUI(seq);
+
+ // 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) {
+ quit();
+ }
+ channels = synthesizer.getChannels();
}
- /**
+ /*
* Returns a pointer to the current sequence.
* @return the current sequence
*/
public static MooSequence getSequence() {
-
+ return seq;
}
- /**
+ /*
* Starts playback of the current sequence.
*/
public static void play() {
-
+ sequencer.setSequence(seq.getSequence());
+ sequencer.setTickPosition(position);
+ sequencer.start();
}
- /**
+ /*
* Pauses playback of the current sequence.
*/
public static void pause() {
-
+ sequencer.stop();
}
- /**
+ /*
+ * Resumes playback of the current sequence.
+ */
+ public static void resume() {
+ sequencer.start();
+ }
+
+ /*
* Stops playback of the current sequence.
*/
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 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(int measures) {
-
+ 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;
}
- /**
+ /*
* Loads the MooSequence in the given file.
* @param filename the filename to use
*/
public static void load(String filename) throws IOException {
-
+ file = filename;
+ seq = new MooSequence(MidiSystem.getSequence(new File(filename)));
}
- /**
+ /*
* Saves the current sequence to the given filename
* @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