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;
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 // Builds GUI, unless n-flag is set.
73 System.out.print("Building GUI...");
75 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
76 } catch (Exception e) {}
77 gui = new MooGUI(seq);
78 System.out.println("Done");
80 System.out.print("Playing...");
82 while (sequencer.isRunning()) {}
83 System.out.println("Done");
92 public static MooGUI getGUI() {
97 * Returns the current sequence.
98 * @return the current sequence
100 public static Sequence getSequence() {
105 * Returns the current sequencer.
106 * @return the current sequencer
108 public static Sequencer getSequencer() {
113 * Returns the MidiChannels of the selected synthesizer.
114 * @return the available MidiChannels
116 public static MidiChannel[] getChannels() {
121 * Returns the currently active MidiChannel.
122 * @return the active MidiChannel
124 public static MidiChannel getActiveChannel() {
125 return activeChannel;
129 * Sets the currently active MidiChannel.
130 * @param channel the number of the MidiChannel to activate
132 public static void setActiveChannel(int channel) {
133 activeChannel = channels[channel];
137 * Replaces the current sequence with a new one, holding three empty tracks.
139 public static void clearSequence() {
140 // Creates a new sequence and sends it to the sequencer.
142 seq = new Sequence(Sequence.PPQ, 96, 3);
143 sequencer.setSequence(seq);
144 } catch (InvalidMidiDataException e) {}
145 // Sends sequence to GUI.
146 if (gui != null) gui.setSequence(seq);
150 * Starts playback of the current sequence.
152 public static void play() {
153 sequencer.setTickPosition(position);
158 * Pauses playback of the current sequence.
160 public static void pause() {
165 * Resumes playback of the current sequence.
167 public static void resume() {
172 * Stops playback of the current sequence.
174 public static void stop() {
176 sequencer.setTickPosition(position);
180 * Returns the current tick position of the sequencer.
181 * @return the tick position
183 public static long getPosition() {
188 * Sets the current tick position of the sequencer.
189 * @param ticks the tick position
191 public static void setPosition(long ticks) {
196 * Returns true if the current sequence has been edited.
197 * @return the tick position
199 public static boolean isEdited() {
204 * Sets the current sequence as edited, which implies prompts when loading a new sequence.
206 public static void setEdited() {
211 * Rewinds the current sequence the given number of measures.
212 * @param measures the number of measures to rewind
214 public static void rewind(long ticks) {
215 setPosition(position - ticks);
219 * Fast forwards the current sequence the given number of measures.
220 * @param measures the number of measures to fast forward
222 public static void forward(long ticks) {
223 setPosition(position + ticks);
227 * Loads the MooSequence in the given file.
228 * @param filename the filename to use
230 public static boolean load(String file) {
231 // Loads sequence from file
234 seq = MidiSystem.getSequence(new File(filename));
235 } catch (InvalidMidiDataException e) {
237 } catch (IOException e) {
242 // Searches the sequence for NoteOn events
243 Track[] tracks = seq.getTracks();
244 MidiEvent noteOn, noteOff = null, nextEvent;
246 ShortMessage shortMsg;
247 for (int i = 0; i < tracks.length; i++) {
249 Collections.sort(track[i].events, new Comparator() {
250 public int compare(Object o1, Object o2) {
251 return ((MidiEvent)o2).getTick() - ((MidiEvent)o1).getTick();
255 for (int j = 0; j < tracks[i].size(); j++) {
256 noteOn = tracks[i].get(j);
257 if (noteOn.getMessage() instanceof ShortMessage) {
258 if (((ShortMessage)noteOn.getMessage()).getCommand() == ShortMessage.NOTE_ON) {
259 // Finds the corresponding NoteOff event
260 for (int k = j + 1; k < tracks[i].size(); k++) {
261 nextEvent = tracks[i].get(k);
262 nextMsg = nextEvent.getMessage();
263 if (nextMsg instanceof ShortMessage) {
264 shortMsg = (ShortMessage) nextMsg;
265 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
271 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
272 tracks[i].remove(noteOn);
273 if (noteOff != null) {
274 tracks[i].add(new MooNote(noteOn, noteOff));
276 tracks[i].add(new MooNote(noteOn));
282 // Sends sequence to GUI and sequencer, then returns
283 if (gui != null) gui.setSequence(seq);
285 sequencer.setSequence(seq);
286 } catch (InvalidMidiDataException e) {}
291 * Saves the current sequence to the given filename
292 * @param file the filename to use
294 public static void saveAs(String file) {
296 MidiSystem.write(seq, 1, new File(filename));
297 } catch (IOException e) {}
302 * Saves the current sequence to the previously given filename.
304 public static void save() {
309 * Releases all reserved devices and exits the program.
311 public static void quit() {
312 if (sequencer.isOpen()) sequencer.close();
313 if (synthesizer.isOpen()) synthesizer.close();