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;
22 private static MidiEvent[] timeSignatures, tempoChanges;
23 private static ArrayList emptyTracks;
25 private static String filename, fileArg;
26 private static long editPosition;
27 private static boolean makeGUI = true, isEdited = false, drawEmptyTracks = false;
28 private static Thread player;
29 public static final int DEFAULT_RESOLUTION = 96, DEFAULT_TRACKS = 4;
32 * Starts the application.
34 public static void main (String[] args) {
35 System.out.println("\nMoosique version 1.0\n");
37 // Parses command-line arguments.
38 for (int i = 0; i < args.length; i++) {
39 if (args[i].equals("-n")) {makeGUI = false;}
40 else if (fileArg == null) {fileArg = args[i];}
43 // Acquires MIDI devices and connects them.
44 System.out.print("Initializing MIDI devices.");
46 sequencer = MidiSystem.getSequencer();
47 System.out.print(".");
49 synthesizer = MidiSystem.getSynthesizer();
50 System.out.print(".");
52 sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
53 channels = synthesizer.getChannels();
55 } catch (MidiUnavailableException e) {
56 System.out.println("Failed, quitting.");
59 System.out.println("Done");
61 //If a filename is given as the command-line argument, attempts to load a sequence from the file.
62 if (fileArg != null) {
63 System.out.print("Loading MIDI sequence from " + fileArg + "...");
65 System.out.println("Failed");
68 System.out.println("Done");
71 // Otherwise creates a new empty one.
75 // Builds GUI, unless n-flag is set.
77 System.out.print("Building GUI...");
79 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
80 } catch (Exception e) {}
81 gui = new MooGUI(seq);
82 System.out.println("Done");
84 System.out.print("Playing...");
86 while (sequencer.isRunning()) {}
87 System.out.println("Done");
96 public static MooGUI getGUI() {
101 * Returns the current sequence.
102 * @return the current sequence
104 public static Sequence getSequence() {
109 * Returns the current sequencer.
110 * @return the current sequencer
112 public static Sequencer getSequencer() {
117 * Returns the MidiChannels of the selected synthesizer.
118 * @return the available MidiChannels
120 public static MidiChannel[] getChannels() {
125 * Returns the MidiChannels of the selected synthesizer.
126 * @return the available MidiChannels
128 public static MidiChannel getChannel(int i) {
133 * Returns the currently active MidiChannel.
134 * @return the active MidiChannel
136 public static MidiChannel getActiveChannel() {
137 return activeChannel;
141 * Sets the currently active MidiChannel.
142 * @param channel the number of the MidiChannel to activate
144 public static void setActiveChannel(int channel) {
145 activeChannel = channels[channel];
149 * Replaces the current sequence with a new one, holding three empty tracks.
151 public static void clearSequence() {
152 // Creates a new sequence and sends it to the sequencer.
154 seq = new Sequence(Sequence.PPQ, DEFAULT_RESOLUTION, DEFAULT_TRACKS);
155 sequencer.setSequence(seq);
156 emptyTracks = new ArrayList();
157 } catch (InvalidMidiDataException e) {}
158 // Sends sequence to GUI.
159 if (gui != null) gui.setSequence(seq);
163 * Starts playback of the current sequence.
165 public static void play() {
166 sequencer.setTickPosition(editPosition);
171 * Pauses playback of the current sequence.
173 public static void pause() {
174 if (sequencer.isRunning()) {
177 if (player != null) player.interrupt();
181 * Resumes playback of the current sequence.
183 public static void resume() {
187 // Disables input to volatile components
190 // Creates the visualisation thread and starts it.
191 player = new Thread () {
193 long ticksPerSixteenth = seq.getResolution()/4;
194 System.out.println("Ticks/16: " + ticksPerSixteenth);
195 long position = sequencer.getTickPosition();
196 while(sequencer.isRunning()) {
197 long pos = sequencer.getTickPosition();
198 long tickDiff = pos - position;
199 System.out.print(" ... " + tickDiff);
202 // Updates the GUI with the current tick position.
203 gui.update(sequencer.getTickPosition());
205 // Puts the thread to sleep for as long as it takes
206 // the sequencer to reach the next sixteenth.
208 //sleep((long)((15000 / getTempo()) * (tickDiff / ticksPerSixteenth)));
210 } catch (InterruptedException e) {
221 * Stops playback of the current sequence.
223 public static void stop() {
224 if (sequencer.isRunning()) {
227 sequencer.setTickPosition(editPosition);
228 if (player != null) player.interrupt();
233 * Returns the current editing position of the sequencer.
234 * @return the tick position
236 public static long getEditPosition() {
241 * Sets the current editing position of the sequencer.
242 * @param ticks the tick position
244 public static void setEditPosition(long ticks) {
245 editPosition = ticks;
249 * Returns the tempo of the current sequence.
250 * @return the tick position
252 public static int getTempo() {
254 // if (tempoMsg == null) return 0;
258 * Sets the current editing position of the sequencer.
259 * @param ticks the tick position
261 public static void setTempo(int bpm) {
266 * Returns the tempo of the current sequence.
267 * @return the tick position
269 public static int[] getTimeSig() {
272 // if (timeSigMsg == null) return 0;
276 * Sets the current editing position of the sequencer.
277 * @param ticks the tick position
279 public static void setTimeSig(int bpm) {
284 * Returns true if the current sequence has been edited.
285 * @return the tick position
287 public static boolean isEdited() {
292 * Sets the current sequence as edited, which implies prompts when loading a new sequence.
294 public static void setEdited() {
299 * Rewinds the current sequence the given number of measures.
300 * @param measures the number of measures to rewind
302 public static void rewind(long ticks) {
303 editPosition -= ticks;
307 * Fast forwards the current sequence the given number of measures.
308 * @param measures the number of measures to fast forward
310 public static void forward(long ticks) {
311 editPosition += ticks;
315 * Returns whether the given track should be drawn
316 * @return true if the given track should be drawn
318 public static boolean shouldBeDrawn(Track track) {
319 if (drawEmptyTracks) return true;
320 else return (!emptyTracks.contains(track));
325 * Sets whether empty tracks should be drawn
326 * @param state true if empty tracks should be drawn
328 public static void setDrawEmptyTracks(boolean state) {
329 drawEmptyTracks = state;
333 * Loads the MooSequence in the given file.
334 * @param filename the filename to use
336 public static boolean load(String file) {
337 // Loads sequence from file
340 seq = MidiSystem.getSequence(new File(filename));
341 } catch (InvalidMidiDataException e) {
343 } catch (IOException e) {
348 Track[] tracks = seq.getTracks();
349 emptyTracks = new ArrayList();
351 // Searches track 0 for changes in tempo and time signature.
354 ArrayList ts = new ArrayList(), tc = new ArrayList();
355 for (int i = 0; i < tracks[0].size(); i++) {
356 event = tracks[0].get(i);
357 if (event.getMessage().getStatus() == MetaMessage.META) {
358 metaMsg = (MetaMessage)event.getMessage();
359 switch(metaMsg.getType()) {
360 case 81: tc.add(event); break;
361 case 88: ts.add(event);
365 // timeSignatures = ts.toArray(timeSignatures);
366 // tempoChanges = tc.toArray(tempoChanges);
368 // Wraps each NoteOn event with its NoteOff event in a MooNote
369 ArrayList noteOns, noteOffs;
370 for (int i = 0; i < tracks.length; i++) {
371 // Searches the sequence for NoteOn and NoteOff events
372 noteOns = new ArrayList(tracks[i].size() / 2);
373 noteOffs = new ArrayList(tracks[i].size() / 2);
374 for (int j = 0; j < tracks[i].size(); j++) {
375 event = tracks[i].get(j);
376 if (event.getMessage().getStatus() >= 144 &&
377 event.getMessage().getStatus() < 160) noteOns.add(event);
378 if (event.getMessage().getStatus() >= 128 &&
379 event.getMessage().getStatus() < 144) noteOffs.add(event);
381 noteOns.trimToSize();
382 noteOffs.trimToSize();
383 if (noteOns.size() == 0) emptyTracks.add(tracks[i]);
385 // Sorts the note lists by tick position.
386 Comparator c = new Comparator() {
387 public int compare(Object o1, Object o2) {
388 return (int)(((MidiEvent)o1).getTick() - ((MidiEvent)o2).getTick());
391 Collections.sort(noteOns, c);
392 Collections.sort(noteOffs, c);
394 // For each NoteOn event, finds its NoteOff event and replaces it with a MooNote.
395 Iterator iOn = noteOns.iterator(), iOff;
396 MidiEvent on, off = null, nextOff;
397 ShortMessage onMsg, nextOffMsg;
398 while(iOn.hasNext()) {
399 on = (MidiEvent)iOn.next();
400 onMsg = (ShortMessage)on.getMessage();
401 iOff = noteOffs.iterator();
402 while(iOff.hasNext()) {
403 nextOff = (MidiEvent)iOff.next();
404 nextOffMsg = (ShortMessage)nextOff.getMessage();
405 if(onMsg.getChannel() == nextOffMsg.getChannel() &&
406 onMsg.getData1() == nextOffMsg.getData1() &&
407 c.compare(nextOff, on) > 0) {
414 tracks[i].remove(on);
416 tracks[i].add(new MooNote(on, off));
418 tracks[i].add(new MooNote(on));
424 // Sends sequence to GUI and sequencer, then returns
425 if (gui != null) gui.setSequence(seq);
427 sequencer.setSequence(seq);
428 } catch (InvalidMidiDataException e) {}
433 * Saves the current sequence to the given filename
434 * @param file the filename to use
436 public static void saveAs(String file) {
438 MidiSystem.write(seq, 1, new File(filename));
439 } catch (IOException e) {}
441 gui.setStatus("Saved " + file);
445 * Saves the current sequence to the previously given filename.
447 public static void save() {
452 * Releases all reserved devices and exits the program.
454 public static void quit() {
455 if (sequencer.isOpen()) sequencer.close();
456 if (synthesizer.isOpen()) synthesizer.close();