-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
executable file
·313 lines (268 loc) · 9.13 KB
/
Player.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* SimpleMidiPlayer.java
*
* This file is part of jsresources.org
*/
/*
* Copyright (c) 1999 - 2001 by Matthias Pfisterer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
|<--- this code is formatted to fit into 80 columns --->|
*/
import java.lang.Thread;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.MetaEventListener;
import javax.sound.midi.MetaMessage;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Synthesizer;
import javax.sound.midi.Receiver;
import javax.sound.midi.Transmitter;
import javax.sound.midi.Instrument;
public class Player extends Thread
{
/*
These variables are not really intended to be static in a
meaning of (good) design. They are used by inner classes, so they
can't just be automatic variables. There were three possibilities:
a) make them instance variables and instantiate the object they
belong to. This is clean (and is how you should do it in a real
application), but would have made the example more complex.
b) make them automatic final variables inside main(). Design-wise,
this is better than static, but automatic final is something that
is still like some black magic to me.
c) make them static variables, as it is done here. This is quite bad
design, because if you have global variables, you can't easily do
the thing they are used for two times in concurrency without risking
indeterministic behaviour. However, it makes the example easy to
read.
*/
private Sequencer sm_sequencer;
private Synthesizer sm_synthesizer;
private boolean DEBUG;
public Player(boolean debug) {
this();
this.DEBUG = debug;
}
public Player(){
DEBUG = false;
sm_sequencer = null;
sm_synthesizer = null;
/*
* Now, we need a Sequencer to play the sequence.
* Here, we simply request the default sequencer.
*/
try
{
sm_sequencer = MidiSystem.getSequencer();
}
catch (MidiUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
if (sm_sequencer == null)
{
out("SimpleMidiPlayer.main(): can't get a Sequencer");
System.exit(1);
}
/*
* The Sequencer is still a dead object.
* We have to open() it to become live.
* This is necessary to allocate some ressources in
* the native part.
*/
try
{
sm_sequencer.open();
}
catch (MidiUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
}
public void run(Sequence playMe)
{
if (DEBUG) {
System.out.println("In Player");
}
/*
* There is a bug in the Sun jdk1.3/1.4.
* It prevents correct termination of the VM.
* So we have to exit ourselves.
* To accomplish this, we register a Listener to the Sequencer.
* It is called when there are "meta" events. Meta event
* 47 is end of track.
*
* Thanks to Espen Riskedal for finding this trick.
*/
/* sm_sequencer.addMetaEventListener(new MetaEventListener()
{
public void meta(MetaMessage event)
{
if (event.getType() == 47)
{
sm_sequencer.close();
if (sm_synthesizer != null)
{
sm_synthesizer.close();
}
System.out.println("exiting");
System.exit(0);
}
}
});
*/
/*
* Next step is to tell the Sequencer which
* Sequence it has to play. In this case, we
* set it as the Sequence object created above.
*/
try
{
sm_sequencer.setSequence(playMe);
}
catch (InvalidMidiDataException e)
{
System.out.println("Error in player.java");
e.printStackTrace();
System.exit(1);
}
/*
* Now, we set up the destinations the Sequence should be
* played on. Here, we try to use the default
* synthesizer. With some Java Sound implementations
* (Sun jdk1.3/1.4 and others derived from this codebase),
* the default sequencer and the default synthesizer
* are combined in one object. We test for this
* condition, and if it's true, nothing more has to
* be done. With other implementations (namely Tritonus),
* sequencers and synthesizers are always seperate
* objects. In this case, we have to set up a link
* between the two objects manually.
*
* By the way, you should never rely on sequencers
* being synthesizers, too; this is a highly non-
* portable programming style. You should be able to
* rely on the other case working. Alas, it is only
* partly true for the Sun jdk1.3/1.4.
*/
if (! (sm_sequencer instanceof Synthesizer))
{
/*
* We try to get the default synthesizer, open()
* it and chain it to the sequencer with a
* Transmitter-Receiver pair.
*/
try
{
//Testing to see what midi devices are available
for(MidiDevice.Info x : MidiSystem.getMidiDeviceInfo()) {
MidiDevice device = MidiSystem.getMidiDevice(x);
if (DEBUG) {
System.out.println(x);
System.out.println(device.getDeviceInfo().getName());
System.out.println("Max Transmitters: " + device.getMaxTransmitters());
System.out.println("Max Reciever " + device.getMaxReceivers());
}
if(device.getMaxReceivers() != 0 && device.getDeviceInfo().getName().contains("USB")){
//sm_synthesizer = MidiSystem.getSynthesizer();
// Instrument[] availInstruments = sm_synthesizer.getAvailableInstruments();
// System.out.println(availInstruments.length);
// for(int i=0; i<availInstruments.length; i++)
// System.out.println("instruments" + availInstruments[i].toString());
//sm_synthesizer.open();
device.open();
Receiver usbReceiver = device.getReceiver();
Transmitter seqTransmitter = sm_sequencer.getTransmitter();
seqTransmitter.setReceiver(usbReceiver);
break;
}
}
// sm_synthesizer = MidiSystem.getSynthesizer();
// // Instrument[] availInstruments = sm_synthesizer.getAvailableInstruments();
// // System.out.println(availInstruments.length);
// // for(int i=0; i<availInstruments.length; i++)
// // System.out.println("instruments" + availInstruments[i].toString());
// sm_synthesizer.open();
// Receiver synthReceiver = sm_synthesizer.getReceiver();
// Transmitter seqTransmitter = sm_sequencer.getTransmitter();
// seqTransmitter.setReceiver(synthReceiver);
}
catch (MidiUnavailableException e)
{
System.out.println("Unable to open synthesizer");
e.printStackTrace();
}
}
//try opening the reciever
//
/*
* Now, we can start over.
*/
if (DEBUG) {
System.out.println("Starting Sequencer");
}
sm_sequencer.start();
try{
long lastTick = 0;
while(sm_sequencer.isRunning()) {
if (DEBUG) {
if (lastTick != sm_sequencer.getTickPosition()) {
lastTick = sm_sequencer.getTickPosition();
System.out.print(lastTick + " ");
}
}
}
}
catch(Exception e){
System.out.println("Thread excption in Player.java:\n" + e.toString());
}
finally {
if (DEBUG) {
System.out.println("Closing Sequencer");
}
// If we don't close the sequence we can keep looping
// sm_sequencer.close();
}
}
private static void printUsageAndExit()
{
out("SimpleMidiPlayer: usage:");
out("\tjava SimpleMidiPlayer <midifile>");
System.exit(1);
}
private static void out(String strMessage)
{
System.out.println(strMessage);
}
}
/*** SimpleMidiPlayer.java ***/