1 import javax.sound.midi.*;
7 * Moosique - The MIDI Tracker
9 * Main class that handles initiation, IO and sound.
11 * @author Einar Pehrson
14 public class Moosique {
16 private static MooGUI gui;
17 private static Sequence seq;
18 private static Sequencer sequencer;
19 private static Synthesizer synthesizer;
20 private static MidiChannel[] channels;
21 private static MidiChannel activeChannel;
23 private static String filename, fileArg;
24 private static long position;
25 private static boolean makeGUI = true, isEdited;
26 private static Thread player;
29 * Starts the application.
31 public static void main (String[] args) {
32 System.out.println("\nMoosique version 1.0\n");
34 // Parses command-line arguments.
35 for (int i = 0; i < args.length; i++) {
36 if (args[i].equals("-n")) {makeGUI = false;}
37 else if (fileArg == null) {fileArg = args[i];}
40 // Acquires MIDI devices and connects them.
41 System.out.print("Initializing MIDI devices.");
43 sequencer = MidiSystem.getSequencer();
44 System.out.print(".");
46 synthesizer = MidiSystem.getSynthesizer();
47 System.out.print(".");
49 sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
50 channels = synthesizer.getChannels();
52 } catch (MidiUnavailableException e) {
53 System.out.println("Failed, quitting.");
56 System.out.println("Done");
58 //If a filename is given as the command-line argument, attempts to load a sequence from the file.
59 if (fileArg != null) {
60 System.out.print("Loading MIDI sequence from " + fileArg + "...");
62 System.out.println("Failed");
65 System.out.println("Done");
68 // Otherwise creates a new empty one.
72 // Builds GUI, unless n-flag is set.
74 System.out.print("Building GUI...");
76 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
77 } catch (Exception e) {}
78 gui = new MooGUI(seq);
79 System.out.println("Done");
81 System.out.print("Playing...");
83 while (sequencer.isRunning()) {}
84 System.out.println("Done");
93 public static MooGUI getGUI() {
98 * Returns the current sequence.
99 * @return the current sequence
101 public static Sequence getSequence() {
106 * Returns the current sequencer.
107 * @return the current sequencer
109 public static Sequencer getSequencer() {
114 * Returns the MidiChannels of the selected synthesizer.
115 * @return the available MidiChannels
117 public static MidiChannel[] getChannels() {
122 * Returns the currently active MidiChannel.
123 * @return the active MidiChannel
125 public static MidiChannel getActiveChannel() {
126 return activeChannel;
130 * Sets the currently active MidiChannel.
131 * @param channel the number of the MidiChannel to activate
133 public static void setActiveChannel(int channel) {
134 activeChannel = channels[channel];
138 * Replaces the current sequence with a new one, holding three empty tracks.
140 public static void clearSequence() {
141 // Creates a new sequence and sends it to the sequencer.
143 seq = new Sequence(Sequence.PPQ, 96, 3);
144 sequencer.setSequence(seq);
145 } catch (InvalidMidiDataException e) {}
146 // Sends sequence to GUI.
147 if (gui != null) gui.setSequence(seq);
151 * Starts playback of the current sequence.
153 public static void play() {
154 sequencer.setTickPosition(position);
160 * Pauses playback of the current sequence.
162 public static void pause() {
168 * Resumes playback of the current sequence.
170 public static void resume() {
173 // Disables input to volatile components
176 // Creates the visualization thread and starts it.
177 player = new Thread () {
179 gui.update(sequencer.getTickPosition());
186 * Stops playback of the current sequence.
188 public static void stop() {
190 sequencer.setTickPosition(position);
196 * Returns the current tick position of the sequencer.
197 * @return the tick position
199 public static long getPosition() {
204 * Sets the current tick position of the sequencer.
205 * @param ticks the tick position
207 public static void setPosition(long ticks) {
212 * Returns true if the current sequence has been edited.
213 * @return the tick position
215 public static boolean isEdited() {
220 * Sets the current sequence as edited, which implies prompts when loading a new sequence.
222 public static void setEdited() {
227 * Rewinds the current sequence the given number of measures.
228 * @param measures the number of measures to rewind
230 public static void rewind(long ticks) {
231 setPosition(position - ticks);
235 * Fast forwards the current sequence the given number of measures.
236 * @param measures the number of measures to fast forward
238 public static void forward(long ticks) {
239 setPosition(position + ticks);
243 * Loads the MooSequence in the given file.
244 * @param filename the filename to use
246 public static boolean load(String file) {
247 // Loads sequence from file
250 seq = MidiSystem.getSequence(new File(filename));
251 } catch (InvalidMidiDataException e) {
253 } catch (IOException e) {
258 // Searches the sequence for NoteOn events
259 Track[] tracks = seq.getTracks();
260 MidiEvent noteOn, noteOff = null, nextEvent;
262 ShortMessage shortMsg;
263 for (int i = 0; i < tracks.length; i++) {
265 Collections.sort(track[i].events, new Comparator() {
266 public int compare(Object o1, Object o2) {
267 return ((MidiEvent)o2).getTick() - ((MidiEvent)o1).getTick();
271 for (int j = 0; j < tracks[i].size(); j++) {
272 noteOn = tracks[i].get(j);
273 if (noteOn.getMessage() instanceof ShortMessage) {
274 if (((ShortMessage)noteOn.getMessage()).getCommand() == ShortMessage.NOTE_ON) {
275 // Finds the corresponding NoteOff event
276 for (int k = j + 1; k < tracks[i].size(); k++) {
277 nextEvent = tracks[i].get(k);
278 nextMsg = nextEvent.getMessage();
279 if (nextMsg instanceof ShortMessage) {
280 shortMsg = (ShortMessage) nextMsg;
281 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
287 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
288 tracks[i].remove(noteOn);
289 if (noteOff != null) {
290 tracks[i].add(new MooNote(noteOn, noteOff));
292 tracks[i].add(new MooNote(noteOn));
298 // Sends sequence to GUI and sequencer, then returns
299 if (gui != null) gui.setSequence(seq);
301 sequencer.setSequence(seq);
302 } catch (InvalidMidiDataException e) {}
307 * Saves the current sequence to the given filename
308 * @param file the filename to use
310 public static void saveAs(String file) {
312 MidiSystem.write(seq, 1, new File(filename));
313 } catch (IOException e) {}
318 * Saves the current sequence to the previously given filename.
320 public static void save() {
325 * Releases all reserved devices and exits the program.
327 public static void quit() {
328 if (sequencer.isOpen()) sequencer.close();
329 if (synthesizer.isOpen()) synthesizer.close();