1 import javax.sound.midi.*;
6 * Moosique - The MIDI Tracker
8 * Main class that handles initiation, IO and sound.
10 * @author Einar Pehrson
13 public class Moosique {
15 private static MooGUI gui;
16 private static Sequence seq;
17 private static Sequencer sequencer;
18 private static Synthesizer synthesizer;
19 private static MidiChannel[] channels;
20 private static MidiChannel activeChannel;
22 private static String filename, fileArg;
23 private static long position;
24 private static boolean makeGUI = true;
27 * Starts the application.
29 public static void main (String[] args) {
30 System.out.println("\nMoosique version 1.0\n");
32 // Parses command-line arguments.
33 for (int i = 0; i < args.length; i++) {
34 if (args[i].equals("-n")) {makeGUI = false;}
35 else if (fileArg == null) {fileArg = args[i];}
38 // Acquires MIDI devices and connects them.
39 System.out.print("Initializing MIDI devices.");
41 sequencer = MidiSystem.getSequencer();
42 System.out.print(".");
44 synthesizer = MidiSystem.getSynthesizer();
45 System.out.print(".");
47 sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
48 channels = synthesizer.getChannels();
50 } catch (MidiUnavailableException e) {
51 System.out.println("Failed, quitting.");
54 System.out.println("Done");
56 //If a filename is given as the command-line argument, attempts to load a sequence from the file.
57 if (fileArg != null) {
58 System.out.print("Loading MIDI sequence from " + fileArg + "...");
59 if (!load(fileArg)) clearSequence();
60 System.out.println("Done");
62 // Otherwise creates a new empty one.
66 // If n-flag is set, plays song and then exits. Otherwise builds GUI.
68 System.out.print("Building GUI...");
69 gui = new MooGUI(seq);
70 System.out.println("Done");
72 System.out.print("Playing...");
74 while (sequencer.isRunning()) {}
75 System.out.println("Done");
81 * Returns the current sequence.
82 * @return the current sequence
84 public static Sequence getSequence() {
89 * Returns the current sequencer.
90 * @return the current sequencer
92 public static Sequencer getSequencer() {
97 * Returns the MidiChannels of the selected synthesizer.
98 * @return the available MidiChannels
100 public static MidiChannel[] getChannels() {
105 * Returns the currently active MidiChannel.
106 * @return the active MidiChannel
108 public static MidiChannel getActiveChannel() {
109 return activeChannel;
113 * Sets the currently active MidiChannel.
114 * @param channel the number of the MidiChannel to activate
116 public static void setActiveChannel(int channel) {
117 activeChannel = channels[channel];
121 * Replaces the current sequence with a new one, holding three empty tracks.
123 public static void clearSequence() {
124 // Creates a new sequence and sends it to the sequencer.
126 seq = new Sequence(Sequence.PPQ, 96, 3);
127 sequencer.setSequence(seq);
128 } catch (InvalidMidiDataException e) {}
129 // Sends sequence to GUI.
130 if (gui != null) gui.setSequence(seq);
134 * Starts playback of the current sequence.
136 public static void play() {
137 sequencer.setTickPosition(position);
142 * Pauses playback of the current sequence.
144 public static void pause() {
149 * Resumes playback of the current sequence.
151 public static void resume() {
156 * Stops playback of the current sequence.
158 public static void stop() {
160 sequencer.setTickPosition(position);
164 * Rewinds the current sequence the given number of measures.
165 * @param measures the number of measures to rewind
167 public static long getPosition() {
172 * Rewinds the current sequence the given number of measures.
173 * @param measures the number of measures to rewind
175 public static void setPosition(long ticks) {
180 * Rewinds the current sequence the given number of measures.
181 * @param measures the number of measures to rewind
183 public static void rewind(long ticks) {
184 setPosition(position - ticks);
188 * Fast forwards the current sequence the given number of measures.
189 * @param measures the number of measures to fast forward
191 public static void forward(long ticks) {
192 setPosition(position + ticks);
196 * Loads the MooSequence in the given file.
197 * @param filename the filename to use
199 public static boolean load(String file) {
200 // Loads sequence from file
203 seq = MidiSystem.getSequence(new File(filename));
204 } catch (InvalidMidiDataException e) {
206 } catch (IOException e) {
207 JOptionPane.showMessageDialog(null, "Error 404", "File Not Found", JOptionPane.ERROR_MESSAGE);
211 // Sends sequence to GUI and sequencer
212 if (gui != null) gui.setSequence(seq);
214 sequencer.setSequence(seq);
215 } catch (InvalidMidiDataException e) {}
217 // Searches the sequence for NoteOn events
218 Track[] tracks = seq.getTracks();
219 MidiEvent noteOn, noteOff = null, nextEvent;
221 ShortMessage shortMsg;
222 for (int i = 0; i < tracks.length; i++) {
223 for (int j = 0; j < tracks[i].size(); j++) {
224 noteOn = tracks[i].get(j);
225 if (noteOn.getMessage() instanceof ShortMessage) {
226 if (((ShortMessage)noteOn.getMessage()).getCommand() == ShortMessage.NOTE_ON) {
227 // Finds the corresponding NoteOff event
228 for (int k = j + 1; k < tracks[i].size(); k++) {
229 nextEvent = tracks[i].get(k);
230 nextMsg = nextEvent.getMessage();
231 if (nextMsg instanceof ShortMessage) {
232 shortMsg = (ShortMessage) nextMsg;
233 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
239 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
240 tracks[i].remove(noteOn);
241 if (noteOff != null) {
242 tracks[i].add(new MooNote(noteOn, noteOff));
244 tracks[i].add(new MooNote(noteOn));
254 * Saves the current sequence to the given filename
255 * @param file the filename to use
257 public static void saveAs(String file) {
259 MidiSystem.write(seq, 1, new File(filename));
260 } catch (IOException e) {}
265 * Saves the current sequence to the previously given filename.
267 public static void save() {
272 * Releases all reserved devices and exits the program.
274 public static void quit() {
275 if (sequencer.isOpen()) sequencer.close();
276 if (synthesizer.isOpen()) synthesizer.close();