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 editPosition;
25 private static boolean makeGUI = true, isEdited;
26 private static Thread player;
27 public static final int RESOLUTION = 96, DEFAULT_TRACKS = 4;
30 * Starts the application.
32 public static void main (String[] args) {
33 System.out.println("\nMoosique version 1.0\n");
35 // Parses command-line arguments.
36 for (int i = 0; i < args.length; i++) {
37 if (args[i].equals("-n")) {makeGUI = false;}
38 else if (fileArg == null) {fileArg = args[i];}
41 // Acquires MIDI devices and connects them.
42 System.out.print("Initializing MIDI devices.");
44 sequencer = MidiSystem.getSequencer();
45 System.out.print(".");
47 synthesizer = MidiSystem.getSynthesizer();
48 System.out.print(".");
50 sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
51 channels = synthesizer.getChannels();
53 } catch (MidiUnavailableException e) {
54 System.out.println("Failed, quitting.");
57 System.out.println("Done");
59 //If a filename is given as the command-line argument, attempts to load a sequence from the file.
60 if (fileArg != null) {
61 System.out.print("Loading MIDI sequence from " + fileArg + "...");
63 System.out.println("Failed");
66 System.out.println("Done");
69 // Otherwise creates a new empty one.
73 // Builds GUI, unless n-flag is set.
75 System.out.print("Building GUI...");
77 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
78 } catch (Exception e) {}
79 gui = new MooGUI(seq);
80 System.out.println("Done");
82 System.out.print("Playing...");
84 while (sequencer.isRunning()) {}
85 System.out.println("Done");
94 public static MooGUI getGUI() {
99 * Returns the current sequence.
100 * @return the current sequence
102 public static Sequence getSequence() {
107 * Returns the current sequencer.
108 * @return the current sequencer
110 public static Sequencer getSequencer() {
115 * Returns the MidiChannels of the selected synthesizer.
116 * @return the available MidiChannels
118 public static MidiChannel[] getChannels() {
123 * Returns the MidiChannels of the selected synthesizer.
124 * @return the available MidiChannels
126 public static MidiChannel getChannel(int i) {
131 * Returns the currently active MidiChannel.
132 * @return the active MidiChannel
134 public static MidiChannel getActiveChannel() {
135 return activeChannel;
139 * Sets the currently active MidiChannel.
140 * @param channel the number of the MidiChannel to activate
142 public static void setActiveChannel(int channel) {
143 activeChannel = channels[channel];
147 * Replaces the current sequence with a new one, holding three empty tracks.
149 public static void clearSequence() {
150 // Creates a new sequence and sends it to the sequencer.
152 seq = new Sequence(Sequence.PPQ, RESOLUTION, DEFAULT_TRACKS);
153 sequencer.setSequence(seq);
154 } catch (InvalidMidiDataException e) {}
155 // Sends sequence to GUI.
156 if (gui != null) gui.setSequence(seq);
160 * Starts playback of the current sequence.
162 public static void play() {
163 sequencer.setTickPosition(editPosition);
168 * Pauses playback of the current sequence.
170 public static void pause() {
176 * Resumes playback of the current sequence.
178 public static void resume() {
181 // Disables input to volatile components
184 // Creates the visualization thread and starts it.
185 player = new Thread () {
187 while(sequencer.isRunning()) gui.update(sequencer.getTickPosition());
195 * Stops playback of the current sequence.
197 public static void stop() {
198 if (sequencer.isRunning()) {
200 sequencer.setTickPosition(editPosition);
207 * Returns the current editing position of the sequencer.
208 * @return the tick position
210 public static long getPosition() {
215 * Sets the current editing position of the sequencer.
216 * @param ticks the tick position
218 public static void setPosition(long ticks) {
219 editPosition = ticks;
223 * Returns true if the current sequence has been edited.
224 * @return the tick position
226 public static boolean isEdited() {
231 * Sets the current sequence as edited, which implies prompts when loading a new sequence.
233 public static void setEdited() {
238 * Rewinds the current sequence the given number of measures.
239 * @param measures the number of measures to rewind
241 public static void rewind(long ticks) {
242 editPosition -= ticks;
246 * Fast forwards the current sequence the given number of measures.
247 * @param measures the number of measures to fast forward
249 public static void forward(long ticks) {
250 editPosition += ticks;
254 * Loads the MooSequence in the given file.
255 * @param filename the filename to use
257 public static boolean load(String file) {
258 // Loads sequence from file
261 seq = MidiSystem.getSequence(new File(filename));
262 } catch (InvalidMidiDataException e) {
264 } catch (IOException e) {
269 // Searches the sequence for NoteOn events
270 Track[] tracks = seq.getTracks();
271 MidiEvent noteOn, noteOff = null, nextEvent;
273 ShortMessage shortMsg;
274 for (int i = 0; i < tracks.length; i++) {
276 Collections.sort(track[i].events, new Comparator() {
277 public int compare(Object o1, Object o2) {
278 return ((MidiEvent)o2).getTick() - ((MidiEvent)o1).getTick();
282 for (int j = 0; j < tracks[i].size(); j++) {
283 noteOn = tracks[i].get(j);
284 if (noteOn.getMessage() instanceof ShortMessage) {
285 if (((ShortMessage)noteOn.getMessage()).getCommand() == ShortMessage.NOTE_ON) {
286 // Finds the corresponding NoteOff event
287 for (int k = j + 1; k < tracks[i].size(); k++) {
288 nextEvent = tracks[i].get(k);
289 nextMsg = nextEvent.getMessage();
290 if (nextMsg instanceof ShortMessage) {
291 shortMsg = (ShortMessage) nextMsg;
292 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
298 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
299 tracks[i].remove(noteOn);
300 if (noteOff != null) {
301 tracks[i].add(new MooNote(noteOn, noteOff));
303 tracks[i].add(new MooNote(noteOn));
309 // Sends sequence to GUI and sequencer, then returns
310 if (gui != null) gui.setSequence(seq);
312 sequencer.setSequence(seq);
313 } catch (InvalidMidiDataException e) {}
318 * Saves the current sequence to the given filename
319 * @param file the filename to use
321 public static void saveAs(String file) {
323 MidiSystem.write(seq, 1, new File(filename));
324 } catch (IOException e) {}
329 * Saves the current sequence to the previously given filename.
331 public static void save() {
336 * Releases all reserved devices and exits the program.
338 public static void quit() {
339 if (sequencer.isOpen()) sequencer.close();
340 if (synthesizer.isOpen()) synthesizer.close();