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 protected static MooGUI gui;
16 protected static Sequence seq;
18 protected static Sequencer sequencer = null;
19 protected static Synthesizer synthesizer = null;
20 protected static MidiChannel[] channels;
21 protected static MidiChannel activeChannel;
23 protected static String filename;
24 protected static long position;
27 * Starts the application.
29 public static void main (String[] args) {
30 System.out.println("\nMoosique version 1.0\n");
31 // Acquires MIDI devices and connects them.
32 System.out.print("Initializing MIDI devices.");
34 sequencer = MidiSystem.getSequencer();
35 System.out.print(".");
37 synthesizer = MidiSystem.getSynthesizer();
38 System.out.print(".");
40 sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
41 channels = synthesizer.getChannels();
43 } catch (MidiUnavailableException e) {
44 System.out.println("Failed, quitting.");
47 System.out.println("Done");
49 //If a filename is given as the command-line argument, attempts to load a sequence from the file.
50 System.out.print("Loading MIDI sequence...");
51 if (args.length == 1) {
52 if (!load(args[0])) clearSequence();
54 // Otherwise creates a new empty one.
57 System.out.println("Done");
59 // Sets up channels and GUI.
60 System.out.print("Creating GUI...");
61 gui = new MooGUI(seq);
62 System.out.println("Done");
66 * Returns a pointer to the current sequence.
67 * @return the current sequence
69 public static Sequence getSequence() {
74 * Returns a pointer to the MidiChannels of the selected synthesizer.
75 * @return the available MidiChannels
77 public static MidiChannel[] getChannels() {
82 * Returns a pointer to the currently active MidiChannel.
83 * @return the active MidiChannel
85 public static MidiChannel getActiveChannel() {
90 * Sets the currently active MidiChannel.
91 * @param channel the number of the MidiChannel to activate
93 public static void setActiveChannel(int channel) {
94 activeChannel = channels[channel];
98 * Replaces the current sequence with a new one, holding three empty tracks.
100 public static void clearSequence() {
103 seq = new Sequence(Sequence.PPQ, 96, 3);
104 } catch (InvalidMidiDataException e) {}
105 // Sends sequence to GUI.
106 if (gui != null) gui.setSequence(seq);
111 * Starts playback of the current sequence.
113 public static void play() {
115 sequencer.setSequence(seq);
116 sequencer.setTickPosition(position);
117 } catch (InvalidMidiDataException e) {}
122 * Pauses playback of the current sequence.
124 public static void pause() {
129 * Resumes playback of the current sequence.
131 public static void resume() {
136 * Stops playback of the current sequence.
138 public static void stop() {
140 sequencer.setTickPosition(position);
144 * Rewinds the current sequence the given number of measures.
145 * @param measures the number of measures to rewind
147 public static long getPosition() {
152 * Rewinds the current sequence the given number of measures.
153 * @param measures the number of measures to rewind
155 public static void setPosition(long ticks) {
160 * Rewinds the current sequence the given number of measures.
161 * @param measures the number of measures to rewind
163 public static void rewind(long ticks) {
168 * Fast forwards the current sequence the given number of measures.
169 * @param measures the number of measures to fast forward
171 public static void forward(long ticks) {
176 * Loads the MooSequence in the given file.
177 * @param filename the filename to use
179 public static boolean load(String file) {
180 // Loads sequence from file
183 seq = MidiSystem.getSequence(new File(filename));
184 } catch (InvalidMidiDataException e) {
186 } catch (IOException e) {
187 JOptionPane.showMessageDialog(null, "File", "alert", JOptionPane.ERROR_MESSAGE);
191 // Sends sequence to GUI
192 if (gui != null) gui.setSequence(seq);
194 // Searches the sequence for NoteOn events
195 Track[] tracks = seq.getTracks();
196 MidiEvent noteOn, noteOff = null, nextEvent;
198 ShortMessage shortMsg;
199 for (int i = 0; i < tracks.length; i++) {
200 for (int j = 0; j < tracks[i].size(); j++) {
201 noteOn = tracks[i].get(j);
202 if (noteOn.getMessage() instanceof ShortMessage) {
203 if (((ShortMessage)noteOn.getMessage()).getCommand() == ShortMessage.NOTE_ON) {
204 // Finds the corresponding NoteOff event
205 for (int k = j + 1; k < tracks[i].size(); k++) {
206 nextEvent = tracks[i].get(k);
207 nextMsg = nextEvent.getMessage();
208 if (nextMsg instanceof ShortMessage) {
209 shortMsg = (ShortMessage) nextMsg;
210 if (shortMsg.getCommand() == ShortMessage.NOTE_OFF && shortMsg.getChannel() == ((ShortMessage)noteOn.getMessage()).getChannel() && shortMsg.getData1() == ((ShortMessage)noteOn.getMessage()).getData1()) {
216 // Replaces the NoteOn event with a MooNote, if possible with the corresponding NoteOff event
217 tracks[i].remove(noteOn);
218 if (noteOff != null) {
219 tracks[i].add(new MooNote(noteOn, noteOff));
221 tracks[i].add(new MooNote(noteOn));
231 * Saves the current sequence to the given filename
232 * @param filename the filename to use
234 public static void saveAs(String filename) throws IOException {
235 MidiSystem.write(seq, 1, new File(filename));
240 * Saves the current sequence to the previously given filename.
242 public static void save() throws IOException {
247 * Releases all reserved devices and exits the program.
249 public static void quit() {
250 if (sequencer.isOpen()) {
253 } catch (MidiUnavailableException e) {}