]> ruin.nu Git - moosique.git/blob - Moosique.java
removed exportMIDI method; added quit, getPosition, setPosition and resume
[moosique.git] / Moosique.java
1 import javax.sound.midi.*;
2 import java.io.*;
3
4 /* UPDATES
5         Killed superfluous exportMIDI method.
6         Added public static void quit()
7         Removed Sequence seq property.
8         Changed forward(int measures) and rewind(int measures) to forward(long ticks) and rewind(long ticks).
9         Added public static long getPosition()
10         Added public static void setPosition(long ticks)
11         Added public static void resume()
12 */
13
14 /*
15  * Moosique - The MIDI Tracker
16  * 
17  * Main class that handles initiation, IO and sound FX.
18  * 
19  * @author  Andersson, Andreen, Lanneskog, Pehrson
20  * @version 1
21  */
22  
23 public class Moosique {
24
25         private static MooGUI gui;
26         private static MooSequence seq;
27
28         private static Sequencer sequencer = null;
29         private static Synthesizer synthesizer = null;
30         private static Receiver receiver = null;
31         private static MidiChannel[] channels;
32
33         private static String file;
34         private static long position;
35
36         /* 
37          * Starts the application.
38          */
39         public static void main (String[] args) {
40                 seq = new MooSequence();
41                 gui = new MooGUI(seq);
42
43                 // Acquires MIDI devices and connects them.
44                 try {
45                         sequencer = MidiSystem.getSequencer();
46                         sequencer.open();
47                         synthesizer = MidiSystem.getSynthesizer();
48                         synthesizer.open();
49                         sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
50                 } catch (MidiUnavailableException e) {
51                         quit();
52                 }
53
54                 channels = synthesizer.getChannels();
55         }
56
57         /* 
58          * Returns a pointer to the current sequence.
59          * @return the current sequence
60          */
61         public static MooSequence getSequence() {
62                 return seq;
63         }
64
65         /* 
66          * Starts playback of the current sequence.
67          */
68         public static void play() {
69                 sequencer.setSequence(seq.getSequence());
70                 sequencer.setTickPosition(position);
71                 sequencer.start();
72         }
73
74         /* 
75          * Pauses playback of the current sequence.
76          */
77         public static void pause() {
78                 sequencer.stop();
79         }
80
81         /* 
82          * Resumes playback of the current sequence.
83          */
84         public static void resume() {
85                 sequencer.start();
86         }
87
88         /* 
89          * Stops playback of the current sequence.
90          */
91         public static void stop() {
92                 sequencer.stop();
93                 sequencer.setTickPosition(position);
94         }
95
96         /* 
97          * Rewinds the current sequence the given number of measures.
98          * @param measures      the number of measures to rewind
99          */
100         public static long getPosition() {
101                 return position;
102         }
103
104         /* 
105          * Rewinds the current sequence the given number of measures.
106          * @param measures      the number of measures to rewind
107          */
108         public static void setPosition(long ticks) {
109                 position = ticks;
110         }
111
112         /* 
113          * Rewinds the current sequence the given number of measures.
114          * @param measures      the number of measures to rewind
115          */
116         public static void rewind(long ticks) {
117                 position -= ticks;
118         }
119
120         /* 
121          * Fast forwards the current sequence the given number of measures.
122          * @param measures      the number of measures to fast forward
123          */
124         public static void forward(long ticks) {
125                 position += ticks;
126         }
127
128         /* 
129          * Loads the MooSequence in the given file.
130          * @param filename      the filename to use
131          */
132         public static void load(String filename) throws IOException {
133                 file = filename;
134                 seq = new MooSequence(MidiSystem.getSequence(new File(filename)));
135         }
136
137         /* 
138          * Saves the current sequence to the given filename
139          * @param filename      the filename to use
140          */
141         public static void saveAs(String filename) throws IOException {
142                 MidiSystem.write(seq.getSequence(), 1, new File(filename));
143
144         }
145
146         /* 
147          * Saves the current sequence to the previously given filename.
148          */
149         public static void save() throws IOException {
150                 saveAs(file);
151         }
152
153         /* 
154          * Releases all reserved devices and exits the program.
155          */
156         public static void quit() {
157                 if (sequencer.isOpen()) {
158                         try {
159                                 sequencer.open();
160                         } catch (MidiUnavailableException e) {}
161                 }
162                 System.exit(0);
163         }
164 }