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;
25 private static boolean playing = false;
28 * Starts the application.
30 public static void main (String[] args) {
31 System.out.println("\nMoosique version 1.0\n");
33 // Parses command-line arguments.
34 for (int i = 0; i < args.length; i++) {
35 if (args[i].equals("-n")) {makeGUI = false;}
36 else if (fileArg == null) {fileArg = args[i];}
39 // Acquires MIDI devices and connects them.
40 System.out.print("Initializing MIDI devices.");
42 sequencer = MidiSystem.getSequencer();
43 System.out.print(".");
45 synthesizer = MidiSystem.getSynthesizer();
46 System.out.print(".");
48 sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
49 channels = synthesizer.getChannels();
51 } catch (MidiUnavailableException e) {
52 System.out.println("Failed, quitting.");
55 System.out.println("Done");
57 //If a filename is given as the command-line argument, attempts to load a sequence from the file.
58 if (fileArg != null) {
59 System.out.print("Loading MIDI sequence from " + fileArg + "...");
61 System.out.println("Failed");
64 System.out.println("Done");
67 // Otherwise creates a new empty one.
71 // If n-flag is set, plays song and then exits. Otherwise builds GUI.
73 System.out.print("Building GUI...");
74 gui = new MooGUI(seq);
75 System.out.println("Done");
77 System.out.print("Playing...");
79 while (sequencer.isRunning()) {}
80 System.out.println("Done");
89 public static MooGUI getGUI() {
94 * Returns the current sequence.
95 * @return the current sequence
97 public static Sequence getSequence() {
102 * Returns the current sequencer.
103 * @return the current sequencer
105 public static Sequencer getSequencer() {
110 * Returns the MidiChannels of the selected synthesizer.
111 * @return the available MidiChannels
113 public static MidiChannel[] getChannels() {
118 * Returns the currently active MidiChannel.
119 * @return the active MidiChannel
121 public static MidiChannel getActiveChannel() {
122 return activeChannel;
126 * Sets the currently active MidiChannel.
127 * @param channel the number of the MidiChannel to activate
129 public static void setActiveChannel(int channel) {
130 activeChannel = channels[channel];
134 * Replaces the current sequence with a new one, holding three empty tracks.
136 public static void clearSequence() {
137 // Creates a new sequence and sends it to the sequencer.
139 seq = new Sequence(Sequence.PPQ, 96, 3);
140 sequencer.setSequence(seq);
141 } catch (InvalidMidiDataException e) {}
142 // Sends sequence to GUI.
143 if (gui != null) gui.setSequence(seq);
147 * Starts playback of the current sequence.
149 public static void play() {
151 sequencer.setTickPosition(position);
156 * Pauses playback of the current sequence.
158 public static void pause() {
164 * Resumes playback of the current sequence.
166 public static void resume() {
172 * Stops playback of the current sequence.
174 public static void stop() {
177 sequencer.setTickPosition(position);
181 * Pauses if playing and resumes if stopped.
183 public static void resumepause()
192 * Rewinds the current sequence the given number of measures.
193 * @param measures the number of measures to rewind
195 public static long getPosition() {
200 * Rewinds the current sequence the given number of measures.
201 * @param measures the number of measures to rewind
203 public static void setPosition(long ticks) {
208 * Rewinds the current sequence the given number of measures.
209 * @param measures the number of measures to rewind
211 public static void rewind(long ticks) {
212 setPosition(position - ticks);
216 * Fast forwards the current sequence the given number of measures.
217 * @param measures the number of measures to fast forward
219 public static void forward(long ticks) {
220 setPosition(position + ticks);
224 * Loads the MooSequence in the given file.
225 * @param filename the filename to use
227 public static boolean load(String file) {
228 // Loads sequence from file
231 seq = MidiSystem.getSequence(new File(filename));
232 } catch (InvalidMidiDataException e) {
234 } catch (IOException e) {
235 JOptionPane.showMessageDialog(null, "Error 404", "File Not Found", JOptionPane.ERROR_MESSAGE);
239 // Sends sequence to GUI and sequencer
240 if (gui != null) gui.setSequence(seq);
242 sequencer.setSequence(seq);
243 } catch (InvalidMidiDataException e) {}
245 // Searches the sequence for NoteOn events
246 Track[] tracks = seq.getTracks();
247 MidiEvent noteOn, noteOff = null, nextEvent;
249 ShortMessage shortMsg;
250 for (int i = 0; i < tracks.length; i++) {
251 for (int j = 0; j < tracks[i].size(); j++) {
252 noteOn = tracks[i].get(j);
253 if (noteOn.getMessage() instanceof ShortMessage) {
254 if (((ShortMessage)noteOn.getMessage()).getCommand() == ShortMessage.NOTE_ON) {
255 // Finds the corresponding NoteOff event
256 for (int k = j + 1; k < tracks[i].size(); k++) {
257 nextEvent = tracks[i].get(k);
258 nextMsg = nextEvent.getMessage();
259 if (nextMsg instanceof ShortMessage) {
260 shortMsg = (ShortMessage) nextMsg;
261 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
267 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
268 tracks[i].remove(noteOn);
269 if (noteOff != null) {
270 tracks[i].add(new MooNote(noteOn, noteOff));
272 tracks[i].add(new MooNote(noteOn));
282 * Saves the current sequence to the given filename
283 * @param file the filename to use
285 public static void saveAs(String file) {
287 MidiSystem.write(seq, 1, new File(filename));
288 } catch (IOException e) {}
293 * Saves the current sequence to the previously given filename.
295 public static void save() {
300 * Releases all reserved devices and exits the program.
302 public static void quit() {
303 if (sequencer.isOpen()) sequencer.close();
304 if (synthesizer.isOpen()) synthesizer.close();