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 MetaMessage timeSigMsg;
23 private static MetaMessage[] tempoChgMsgs;
25 private static String filename, fileArg;
26 private static long editPosition;
27 private static boolean makeGUI = true, isEdited;
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 } catch (InvalidMidiDataException e) {}
157 // Sends sequence to GUI.
158 if (gui != null) gui.setSequence(seq);
162 * Starts playback of the current sequence.
164 public static void play() {
165 sequencer.setTickPosition(editPosition);
170 * Pauses playback of the current sequence.
172 public static void pause() {
173 if (sequencer.isRunning()) {
176 if (player != null) player.interrupt();
180 * Resumes playback of the current sequence.
182 public static void resume() {
186 // Disables input to volatile components
189 // Creates the visualization thread and starts it.
190 player = new Thread () {
192 while(sequencer.isRunning()) {
193 // Updates the GUI with the current tick position.
194 gui.update(sequencer.getTickPosition());
196 // Puts the thread to sleep for as long as it takes
197 // the sequencer to reach the next sixteenth.
199 sleep((long)((1000 * 60 * 1) / (getTempo() * 4)));
200 } catch (InterruptedException e) {
211 * Stops playback of the current sequence.
213 public static void stop() {
214 if (sequencer.isRunning()) {
217 sequencer.setTickPosition(editPosition);
218 if (player != null) player.interrupt();
223 * Returns the current editing position of the sequencer.
224 * @return the tick position
226 public static long getPosition() {
231 * Sets the current editing position of the sequencer.
232 * @param ticks the tick position
234 public static void setPosition(long ticks) {
235 editPosition = ticks;
239 * Returns the tempo of the current sequence.
240 * @return the tick position
242 public static int getTempo() {
244 // if (tempoMsg == null) return 0;
248 * Sets the current editing position of the sequencer.
249 * @param ticks the tick position
251 public static void setTempo(int bpm) {
256 * Returns the tempo of the current sequence.
257 * @return the tick position
259 public static int[] getTimeSig() {
262 // if (timeSigMsg == null) return 0;
266 * Sets the current editing position of the sequencer.
267 * @param ticks the tick position
269 public static void setTimeSig(int bpm) {
274 * Returns true if the current sequence has been edited.
275 * @return the tick position
277 public static boolean isEdited() {
282 * Sets the current sequence as edited, which implies prompts when loading a new sequence.
284 public static void setEdited() {
289 * Rewinds the current sequence the given number of measures.
290 * @param measures the number of measures to rewind
292 public static void rewind(long ticks) {
293 editPosition -= ticks;
297 * Fast forwards the current sequence the given number of measures.
298 * @param measures the number of measures to fast forward
300 public static void forward(long ticks) {
301 editPosition += ticks;
305 * Loads the MooSequence in the given file.
306 * @param filename the filename to use
308 public static boolean load(String file) {
309 // Loads sequence from file
312 seq = MidiSystem.getSequence(new File(filename));
313 } catch (InvalidMidiDataException e) {
315 } catch (IOException e) {
320 Track[] tracks = seq.getTracks();
322 // Stores tempo and time signature.
325 for (int i = 0; i < tracks[0].size(); i++) {
326 msg = tracks[0].get(i).getMessage();
327 if (msg.getStatus() == MetaMessage.META) {
328 metaMsg = (MetaMessage)msg;
329 if (metaMsg.getType() == 81) {
330 // FLERA!!! tempoMsg = metaMsg;
331 } else if (metaMsg.getType() == 88) {
332 timeSigMsg = (MetaMessage)msg;
338 // Searches the sequence for NoteOn events
339 MidiEvent noteOn, noteOff = null, nextEvent;
341 ShortMessage shortMsg;
342 ArrayList noteOns, noteOffs;
343 for (int i = 0; i < tracks.length; i++) {
344 for (int j = 0; j < tracks[i].size(); j++) {
345 noteOn = tracks[i].get(j);
346 if (noteOn.getMessage().getStatus() == ShortMessage.NOTE_ON) {
347 // Finds the corresponding NoteOff event
348 for (int k = j + 1; k < tracks[i].size(); k++) {
349 nextEvent = tracks[i].get(k);
350 nextMsg = nextEvent.getMessage();
351 if (nextMsg instanceof ShortMessage) {
352 shortMsg = (ShortMessage) nextMsg;
353 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
359 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
360 tracks[i].remove(noteOn);
361 if (noteOff != null) {
362 tracks[i].add(new MooNote(noteOn, noteOff));
364 tracks[i].add(new MooNote(noteOn));
370 // Sends sequence to GUI and sequencer, then returns
371 if (gui != null) gui.setSequence(seq);
373 sequencer.setSequence(seq);
374 } catch (InvalidMidiDataException e) {}
379 * Saves the current sequence to the given filename
380 * @param file the filename to use
382 public static void saveAs(String file) {
384 MidiSystem.write(seq, 1, new File(filename));
385 } catch (IOException e) {}
387 gui.setStatus("Saved " + file);
391 * Saves the current sequence to the previously given filename.
393 public static void save() {
398 * Releases all reserved devices and exits the program.
400 public static void quit() {
401 if (sequencer.isOpen()) sequencer.close();
402 if (synthesizer.isOpen()) synthesizer.close();