1 import javax.sound.midi.*;
6 * Moosique - The MIDI Tracker
8 * Main class that handles initiation, IO and sound.
10 * @author Einar Pehrson
13 public class Moosique {
15 private static MooGUI gui;
16 private static Sequence seq;
17 private static Sequencer sequencer;
18 private static Synthesizer synthesizer;
19 private static MidiChannel[] channels;
20 private static MidiChannel activeChannel;
22 private static String filename, fileArg;
23 private static long position;
24 private static boolean makeGUI = true;
27 * Starts the application.
29 public static void main (String[] args) {
30 System.out.println("\nMoosique version 1.0\n");
32 // Parses command-line arguments.
33 for (int i = 0; i < args.length; i++) {
34 if (args[i].equals("-n")) {makeGUI = false;}
35 else if (fileArg == null) {fileArg = args[i];}
38 // Acquires MIDI devices and connects them.
39 System.out.print("Initializing MIDI devices.");
41 sequencer = MidiSystem.getSequencer();
42 System.out.print(".");
44 synthesizer = MidiSystem.getSynthesizer();
45 System.out.print(".");
47 sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
48 channels = synthesizer.getChannels();
50 } catch (MidiUnavailableException e) {
51 System.out.println("Failed, quitting.");
54 System.out.println("Done");
56 //If a filename is given as the command-line argument, attempts to load a sequence from the file.
57 if (fileArg != null) {
58 System.out.print("Loading MIDI sequence from " + fileArg + "...");
60 System.out.println("Failed");
63 System.out.println("Done");
66 // Otherwise creates a new empty one.
70 // Builds GUI, unless n-flag is set.
72 System.out.print("Building GUI...");
74 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
75 } catch (Exception e) {}
76 gui = new MooGUI(seq);
77 System.out.println("Done");
79 System.out.print("Playing...");
81 while (sequencer.isRunning()) {}
82 System.out.println("Done");
91 public static MooGUI getGUI() {
96 * Returns the current sequence.
97 * @return the current sequence
99 public static Sequence getSequence() {
104 * Returns the current sequencer.
105 * @return the current sequencer
107 public static Sequencer getSequencer() {
112 * Returns the MidiChannels of the selected synthesizer.
113 * @return the available MidiChannels
115 public static MidiChannel[] getChannels() {
120 * Returns the currently active MidiChannel.
121 * @return the active MidiChannel
123 public static MidiChannel getActiveChannel() {
124 return activeChannel;
128 * Sets the currently active MidiChannel.
129 * @param channel the number of the MidiChannel to activate
131 public static void setActiveChannel(int channel) {
132 activeChannel = channels[channel];
136 * Replaces the current sequence with a new one, holding three empty tracks.
138 public static void clearSequence() {
139 // Creates a new sequence and sends it to the sequencer.
141 seq = new Sequence(Sequence.PPQ, 96, 3);
142 sequencer.setSequence(seq);
143 } catch (InvalidMidiDataException e) {}
144 // Sends sequence to GUI.
145 if (gui != null) gui.setSequence(seq);
149 * Starts playback of the current sequence.
151 public static void play() {
152 sequencer.setTickPosition(position);
157 * Pauses playback of the current sequence.
159 public static void pause() {
164 * Resumes playback of the current sequence.
166 public static void resume() {
171 * Stops playback of the current sequence.
173 public static void stop() {
175 sequencer.setTickPosition(position);
179 * Rewinds the current sequence the given number of measures.
180 * @param measures the number of measures to rewind
182 public static long getPosition() {
187 * Rewinds the current sequence the given number of measures.
188 * @param measures the number of measures to rewind
190 public static void setPosition(long ticks) {
195 * Rewinds the current sequence the given number of measures.
196 * @param measures the number of measures to rewind
198 public static void rewind(long ticks) {
199 setPosition(position - ticks);
203 * Fast forwards the current sequence the given number of measures.
204 * @param measures the number of measures to fast forward
206 public static void forward(long ticks) {
207 setPosition(position + ticks);
211 * Loads the MooSequence in the given file.
212 * @param filename the filename to use
214 public static boolean load(String file) {
215 // Loads sequence from file
218 seq = MidiSystem.getSequence(new File(filename));
219 } catch (InvalidMidiDataException e) {
221 } catch (IOException e) {
225 // Sends sequence to GUI and sequencer
226 if (gui != null) gui.setSequence(seq);
228 sequencer.setSequence(seq);
229 } catch (InvalidMidiDataException e) {}
231 // Searches the sequence for NoteOn events
232 Track[] tracks = seq.getTracks();
233 MidiEvent noteOn, noteOff = null, nextEvent;
235 ShortMessage shortMsg;
236 for (int i = 0; i < tracks.length; i++) {
237 for (int j = 0; j < tracks[i].size(); j++) {
238 noteOn = tracks[i].get(j);
239 if (noteOn.getMessage() instanceof ShortMessage) {
240 if (((ShortMessage)noteOn.getMessage()).getCommand() == ShortMessage.NOTE_ON) {
241 // Finds the corresponding NoteOff event
242 for (int k = j + 1; k < tracks[i].size(); k++) {
243 nextEvent = tracks[i].get(k);
244 nextMsg = nextEvent.getMessage();
245 if (nextMsg instanceof ShortMessage) {
246 shortMsg = (ShortMessage) nextMsg;
247 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
253 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
254 tracks[i].remove(noteOn);
255 if (noteOff != null) {
256 tracks[i].add(new MooNote(noteOn, noteOff));
258 tracks[i].add(new MooNote(noteOn));
268 * Saves the current sequence to the given filename
269 * @param file the filename to use
271 public static void saveAs(String file) {
273 MidiSystem.write(seq, 1, new File(filename));
274 } catch (IOException e) {}
279 * Saves the current sequence to the previously given filename.
281 public static void save() {
286 * Releases all reserved devices and exits the program.
288 public static void quit() {
289 if (sequencer.isOpen()) sequencer.close();
290 if (synthesizer.isOpen()) synthesizer.close();