2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.media.sound;
|
|
27 |
|
|
28 |
import java.io.ByteArrayOutputStream;
|
|
29 |
import java.io.ByteArrayInputStream;
|
|
30 |
import java.io.DataOutputStream;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.InputStream;
|
|
33 |
|
|
34 |
import java.util.ArrayList;
|
|
35 |
import java.util.List;
|
|
36 |
|
|
37 |
import javax.sound.midi.*;
|
|
38 |
|
|
39 |
|
|
40 |
/**
|
|
41 |
* A Real Time Sequencer
|
|
42 |
*
|
|
43 |
* @author Florian Bomers
|
|
44 |
*/
|
|
45 |
|
|
46 |
/* TODO:
|
|
47 |
* - rename PlayThread to PlayEngine (because isn't a thread)
|
|
48 |
*/
|
|
49 |
class RealTimeSequencer extends AbstractMidiDevice implements Sequencer, AutoConnectSequencer {
|
|
50 |
|
|
51 |
// STATIC VARIABLES
|
|
52 |
|
|
53 |
/** debugging flags */
|
|
54 |
private final static boolean DEBUG_PUMP = false;
|
|
55 |
private final static boolean DEBUG_PUMP_ALL = false;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Event Dispatcher thread. Should be using a shared event
|
|
59 |
* dispatcher instance with a factory in EventDispatcher
|
|
60 |
*/
|
|
61 |
private static final EventDispatcher eventDispatcher;
|
|
62 |
|
|
63 |
/**
|
|
64 |
* All RealTimeSequencers share this info object.
|
|
65 |
*/
|
|
66 |
static final RealTimeSequencerInfo info = new RealTimeSequencerInfo();
|
|
67 |
|
|
68 |
|
|
69 |
private static Sequencer.SyncMode[] masterSyncModes = { Sequencer.SyncMode.INTERNAL_CLOCK };
|
|
70 |
private static Sequencer.SyncMode[] slaveSyncModes = { Sequencer.SyncMode.NO_SYNC };
|
|
71 |
|
|
72 |
private static Sequencer.SyncMode masterSyncMode = Sequencer.SyncMode.INTERNAL_CLOCK;
|
|
73 |
private static Sequencer.SyncMode slaveSyncMode = Sequencer.SyncMode.NO_SYNC;
|
|
74 |
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Sequence on which this sequencer is operating.
|
|
78 |
*/
|
|
79 |
private Sequence sequence = null;
|
|
80 |
|
|
81 |
// caches
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Same for setTempoInMPQ...
|
|
85 |
* -1 means not set.
|
|
86 |
*/
|
|
87 |
private double cacheTempoMPQ = -1;
|
|
88 |
|
|
89 |
|
|
90 |
/**
|
|
91 |
* cache value for tempo factor until sequence is set
|
|
92 |
* -1 means not set.
|
|
93 |
*/
|
|
94 |
private float cacheTempoFactor = -1;
|
|
95 |
|
|
96 |
|
|
97 |
/** if a particular track is muted */
|
|
98 |
private boolean[] trackMuted = null;
|
|
99 |
/** if a particular track is solo */
|
|
100 |
private boolean[] trackSolo = null;
|
|
101 |
|
|
102 |
/** tempo cache for getMicrosecondPosition */
|
|
103 |
private MidiUtils.TempoCache tempoCache = new MidiUtils.TempoCache();
|
|
104 |
|
|
105 |
/**
|
|
106 |
* True if the sequence is running.
|
|
107 |
*/
|
|
108 |
private boolean running = false;
|
|
109 |
|
|
110 |
|
|
111 |
/** the thread for pushing out the MIDI messages */
|
|
112 |
private PlayThread playThread;
|
|
113 |
|
|
114 |
|
|
115 |
/**
|
|
116 |
* True if we are recording
|
|
117 |
*/
|
|
118 |
private boolean recording = false;
|
|
119 |
|
|
120 |
|
|
121 |
/**
|
|
122 |
* List of tracks to which we're recording
|
|
123 |
*/
|
|
124 |
private List recordingTracks = new ArrayList();
|
|
125 |
|
|
126 |
|
|
127 |
private long loopStart = 0;
|
|
128 |
private long loopEnd = -1;
|
|
129 |
private int loopCount = 0;
|
|
130 |
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Meta event listeners
|
|
134 |
*/
|
|
135 |
private ArrayList metaEventListeners = new ArrayList();
|
|
136 |
|
|
137 |
|
|
138 |
/**
|
|
139 |
* Control change listeners
|
|
140 |
*/
|
|
141 |
private ArrayList controllerEventListeners = new ArrayList();
|
|
142 |
|
|
143 |
|
|
144 |
/** automatic connection support */
|
|
145 |
private boolean autoConnect = false;
|
|
146 |
|
|
147 |
/** if we need to autoconnect at next open */
|
|
148 |
private boolean doAutoConnectAtNextOpen = false;
|
|
149 |
|
|
150 |
/** the receiver that this device is auto-connected to */
|
|
151 |
Receiver autoConnectedReceiver = null;
|
|
152 |
|
|
153 |
|
|
154 |
static {
|
|
155 |
// create and start the global event thread
|
|
156 |
eventDispatcher = new EventDispatcher();
|
|
157 |
eventDispatcher.start();
|
|
158 |
}
|
|
159 |
|
|
160 |
|
|
161 |
/* ****************************** CONSTRUCTOR ****************************** */
|
|
162 |
|
|
163 |
protected RealTimeSequencer() throws MidiUnavailableException {
|
|
164 |
super(info);
|
|
165 |
|
|
166 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer CONSTRUCTOR");
|
|
167 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer CONSTRUCTOR completed");
|
|
168 |
}
|
|
169 |
|
|
170 |
|
|
171 |
/* ****************************** SEQUENCER METHODS ******************** */
|
|
172 |
|
|
173 |
public synchronized void setSequence(Sequence sequence)
|
|
174 |
throws InvalidMidiDataException {
|
|
175 |
|
|
176 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: setSequence(" + sequence +")");
|
|
177 |
|
|
178 |
if (sequence != this.sequence) {
|
|
179 |
if (this.sequence != null && sequence == null) {
|
|
180 |
setCaches();
|
|
181 |
stop();
|
|
182 |
// initialize some non-cached values
|
|
183 |
trackMuted = null;
|
|
184 |
trackSolo = null;
|
|
185 |
loopStart = 0;
|
|
186 |
loopEnd = -1;
|
|
187 |
loopCount = 0;
|
|
188 |
if (getDataPump() != null) {
|
|
189 |
getDataPump().setTickPos(0);
|
|
190 |
getDataPump().resetLoopCount();
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
if (playThread != null) {
|
|
195 |
playThread.setSequence(sequence);
|
|
196 |
}
|
|
197 |
|
|
198 |
// store this sequence (do not copy - we want to give the possibility
|
|
199 |
// of modifying the sequence at runtime)
|
|
200 |
this.sequence = sequence;
|
|
201 |
|
|
202 |
if (sequence != null) {
|
|
203 |
tempoCache.refresh(sequence);
|
|
204 |
// rewind to the beginning
|
|
205 |
setTickPosition(0);
|
|
206 |
// propagate caches
|
|
207 |
propagateCaches();
|
|
208 |
}
|
|
209 |
}
|
|
210 |
else if (sequence != null) {
|
|
211 |
tempoCache.refresh(sequence);
|
|
212 |
if (playThread != null) {
|
|
213 |
playThread.setSequence(sequence);
|
|
214 |
}
|
|
215 |
}
|
|
216 |
|
|
217 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: setSequence(" + sequence +") completed");
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
public synchronized void setSequence(InputStream stream) throws IOException, InvalidMidiDataException {
|
|
222 |
|
|
223 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: setSequence(" + stream +")");
|
|
224 |
|
|
225 |
if (stream == null) {
|
|
226 |
setSequence((Sequence) null);
|
|
227 |
return;
|
|
228 |
}
|
|
229 |
|
|
230 |
Sequence seq = MidiSystem.getSequence(stream); // can throw IOException, InvalidMidiDataException
|
|
231 |
|
|
232 |
setSequence(seq);
|
|
233 |
|
|
234 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: setSequence(" + stream +") completed");
|
|
235 |
|
|
236 |
}
|
|
237 |
|
|
238 |
|
|
239 |
public Sequence getSequence() {
|
|
240 |
return sequence;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
|
244 |
public synchronized void start() {
|
|
245 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: start()");
|
|
246 |
|
|
247 |
// sequencer not open: throw an exception
|
|
248 |
if (!isOpen()) {
|
|
249 |
throw new IllegalStateException("sequencer not open");
|
|
250 |
}
|
|
251 |
|
|
252 |
// sequence not available: throw an exception
|
|
253 |
if (sequence == null) {
|
|
254 |
throw new IllegalStateException("sequence not set");
|
|
255 |
}
|
|
256 |
|
|
257 |
// already running: return quietly
|
|
258 |
if (running == true) {
|
|
259 |
return;
|
|
260 |
}
|
|
261 |
|
|
262 |
// start playback
|
|
263 |
implStart();
|
|
264 |
|
|
265 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: start() completed");
|
|
266 |
}
|
|
267 |
|
|
268 |
|
|
269 |
public synchronized void stop() {
|
|
270 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: stop()");
|
|
271 |
|
|
272 |
if (!isOpen()) {
|
|
273 |
throw new IllegalStateException("sequencer not open");
|
|
274 |
}
|
|
275 |
stopRecording();
|
|
276 |
|
|
277 |
// not running; just return
|
|
278 |
if (running == false) {
|
|
279 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: stop() not running!");
|
|
280 |
return;
|
|
281 |
}
|
|
282 |
|
|
283 |
// stop playback
|
|
284 |
implStop();
|
|
285 |
|
|
286 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: stop() completed");
|
|
287 |
}
|
|
288 |
|
|
289 |
|
|
290 |
public boolean isRunning() {
|
|
291 |
return running;
|
|
292 |
}
|
|
293 |
|
|
294 |
|
|
295 |
public void startRecording() {
|
|
296 |
if (!isOpen()) {
|
|
297 |
throw new IllegalStateException("Sequencer not open");
|
|
298 |
}
|
|
299 |
|
|
300 |
start();
|
|
301 |
recording = true;
|
|
302 |
}
|
|
303 |
|
|
304 |
|
|
305 |
public void stopRecording() {
|
|
306 |
if (!isOpen()) {
|
|
307 |
throw new IllegalStateException("Sequencer not open");
|
|
308 |
}
|
|
309 |
recording = false;
|
|
310 |
}
|
|
311 |
|
|
312 |
|
|
313 |
public boolean isRecording() {
|
|
314 |
return recording;
|
|
315 |
}
|
|
316 |
|
|
317 |
|
|
318 |
public void recordEnable(Track track, int channel) {
|
|
319 |
if (!findTrack(track)) {
|
|
320 |
throw new IllegalArgumentException("Track does not exist in the current sequence");
|
|
321 |
}
|
|
322 |
|
|
323 |
synchronized(recordingTracks) {
|
|
324 |
RecordingTrack rc = RecordingTrack.get(recordingTracks, track);
|
|
325 |
if (rc != null) {
|
|
326 |
rc.channel = channel;
|
|
327 |
} else {
|
|
328 |
recordingTracks.add(new RecordingTrack(track, channel));
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
}
|
|
333 |
|
|
334 |
|
|
335 |
public void recordDisable(Track track) {
|
|
336 |
synchronized(recordingTracks) {
|
|
337 |
RecordingTrack rc = RecordingTrack.get(recordingTracks, track);
|
|
338 |
if (rc != null) {
|
|
339 |
recordingTracks.remove(rc);
|
|
340 |
}
|
|
341 |
}
|
|
342 |
|
|
343 |
}
|
|
344 |
|
|
345 |
|
|
346 |
private boolean findTrack(Track track) {
|
|
347 |
boolean found = false;
|
|
348 |
if (sequence != null) {
|
|
349 |
Track[] tracks = sequence.getTracks();
|
|
350 |
for (int i = 0; i < tracks.length; i++) {
|
|
351 |
if (track == tracks[i]) {
|
|
352 |
found = true;
|
|
353 |
break;
|
|
354 |
}
|
|
355 |
}
|
|
356 |
}
|
|
357 |
return found;
|
|
358 |
}
|
|
359 |
|
|
360 |
|
|
361 |
public float getTempoInBPM() {
|
|
362 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: getTempoInBPM() ");
|
|
363 |
|
|
364 |
return (float) MidiUtils.convertTempo(getTempoInMPQ());
|
|
365 |
}
|
|
366 |
|
|
367 |
|
|
368 |
public void setTempoInBPM(float bpm) {
|
|
369 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: setTempoInBPM() ");
|
|
370 |
if (bpm <= 0) {
|
|
371 |
// should throw IllegalArgumentException
|
|
372 |
bpm = 1.0f;
|
|
373 |
}
|
|
374 |
|
|
375 |
setTempoInMPQ((float) MidiUtils.convertTempo((double) bpm));
|
|
376 |
}
|
|
377 |
|
|
378 |
|
|
379 |
public float getTempoInMPQ() {
|
|
380 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: getTempoInMPQ() ");
|
|
381 |
|
|
382 |
if (needCaching()) {
|
|
383 |
// if the sequencer is closed, return cached value
|
|
384 |
if (cacheTempoMPQ != -1) {
|
|
385 |
return (float) cacheTempoMPQ;
|
|
386 |
}
|
|
387 |
// if sequence is set, return current tempo
|
|
388 |
if (sequence != null) {
|
|
389 |
return tempoCache.getTempoMPQAt(getTickPosition());
|
|
390 |
}
|
|
391 |
|
|
392 |
// last resort: return a standard tempo: 120bpm
|
|
393 |
return (float) MidiUtils.DEFAULT_TEMPO_MPQ;
|
|
394 |
}
|
|
395 |
return (float)getDataPump().getTempoMPQ();
|
|
396 |
}
|
|
397 |
|
|
398 |
|
|
399 |
public void setTempoInMPQ(float mpq) {
|
|
400 |
if (mpq <= 0) {
|
|
401 |
// should throw IllegalArgumentException
|
|
402 |
mpq = 1.0f;
|
|
403 |
}
|
|
404 |
|
|
405 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: setTempoInMPQ() ");
|
|
406 |
|
|
407 |
if (needCaching()) {
|
|
408 |
// cache the value
|
|
409 |
cacheTempoMPQ = mpq;
|
|
410 |
} else {
|
|
411 |
// set the native tempo in MPQ
|
|
412 |
getDataPump().setTempoMPQ(mpq);
|
|
413 |
|
|
414 |
// reset the tempoInBPM and tempoInMPQ values so we won't use them again
|
|
415 |
cacheTempoMPQ = -1;
|
|
416 |
}
|
|
417 |
}
|
|
418 |
|
|
419 |
|
|
420 |
public void setTempoFactor(float factor) {
|
|
421 |
if (factor <= 0) {
|
|
422 |
// should throw IllegalArgumentException
|
|
423 |
return;
|
|
424 |
}
|
|
425 |
|
|
426 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: setTempoFactor() ");
|
|
427 |
|
|
428 |
if (needCaching()) {
|
|
429 |
cacheTempoFactor = factor;
|
|
430 |
} else {
|
|
431 |
getDataPump().setTempoFactor(factor);
|
|
432 |
// don't need cache anymore
|
|
433 |
cacheTempoFactor = -1;
|
|
434 |
}
|
|
435 |
}
|
|
436 |
|
|
437 |
|
|
438 |
public float getTempoFactor() {
|
|
439 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: getTempoFactor() ");
|
|
440 |
|
|
441 |
if (needCaching()) {
|
|
442 |
if (cacheTempoFactor != -1) {
|
|
443 |
return cacheTempoFactor;
|
|
444 |
}
|
|
445 |
return 1.0f;
|
|
446 |
}
|
|
447 |
return getDataPump().getTempoFactor();
|
|
448 |
}
|
|
449 |
|
|
450 |
|
|
451 |
public long getTickLength() {
|
|
452 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: getTickLength() ");
|
|
453 |
|
|
454 |
if (sequence == null) {
|
|
455 |
return 0;
|
|
456 |
}
|
|
457 |
|
|
458 |
return sequence.getTickLength();
|
|
459 |
}
|
|
460 |
|
|
461 |
|
|
462 |
public synchronized long getTickPosition() {
|
|
463 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: getTickPosition() ");
|
|
464 |
|
|
465 |
if (getDataPump() == null || sequence == null) {
|
|
466 |
return 0;
|
|
467 |
}
|
|
468 |
|
|
469 |
return getDataPump().getTickPos();
|
|
470 |
}
|
|
471 |
|
|
472 |
|
|
473 |
public synchronized void setTickPosition(long tick) {
|
|
474 |
if (tick < 0) {
|
|
475 |
// should throw IllegalArgumentException
|
|
476 |
return;
|
|
477 |
}
|
|
478 |
|
|
479 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: setTickPosition("+tick+") ");
|
|
480 |
|
|
481 |
if (getDataPump() == null) {
|
|
482 |
if (tick != 0) {
|
|
483 |
// throw new InvalidStateException("cannot set position in closed state");
|
|
484 |
}
|
|
485 |
}
|
|
486 |
else if (sequence == null) {
|
|
487 |
if (tick != 0) {
|
|
488 |
// throw new InvalidStateException("cannot set position if sequence is not set");
|
|
489 |
}
|
|
490 |
} else {
|
|
491 |
getDataPump().setTickPos(tick);
|
|
492 |
}
|
|
493 |
}
|
|
494 |
|
|
495 |
|
|
496 |
public long getMicrosecondLength() {
|
|
497 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: getMicrosecondLength() ");
|
|
498 |
|
|
499 |
if (sequence == null) {
|
|
500 |
return 0;
|
|
501 |
}
|
|
502 |
|
|
503 |
return sequence.getMicrosecondLength();
|
|
504 |
}
|
|
505 |
|
|
506 |
|
|
507 |
public long getMicrosecondPosition() {
|
|
508 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: getMicrosecondPosition() ");
|
|
509 |
|
|
510 |
if (getDataPump() == null || sequence == null) {
|
|
511 |
return 0;
|
|
512 |
}
|
|
513 |
synchronized (tempoCache) {
|
|
514 |
return MidiUtils.tick2microsecond(sequence, getDataPump().getTickPos(), tempoCache);
|
|
515 |
}
|
|
516 |
}
|
|
517 |
|
|
518 |
|
|
519 |
public void setMicrosecondPosition(long microseconds) {
|
|
520 |
if (microseconds < 0) {
|
|
521 |
// should throw IllegalArgumentException
|
|
522 |
return;
|
|
523 |
}
|
|
524 |
|
|
525 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: setMicrosecondPosition("+microseconds+") ");
|
|
526 |
|
|
527 |
if (getDataPump() == null) {
|
|
528 |
if (microseconds != 0) {
|
|
529 |
// throw new InvalidStateException("cannot set position in closed state");
|
|
530 |
}
|
|
531 |
}
|
|
532 |
else if (sequence == null) {
|
|
533 |
if (microseconds != 0) {
|
|
534 |
// throw new InvalidStateException("cannot set position if sequence is not set");
|
|
535 |
}
|
|
536 |
} else {
|
|
537 |
synchronized(tempoCache) {
|
|
538 |
setTickPosition(MidiUtils.microsecond2tick(sequence, microseconds, tempoCache));
|
|
539 |
}
|
|
540 |
}
|
|
541 |
}
|
|
542 |
|
|
543 |
|
|
544 |
public void setMasterSyncMode(Sequencer.SyncMode sync) {
|
|
545 |
// not supported
|
|
546 |
}
|
|
547 |
|
|
548 |
|
|
549 |
public Sequencer.SyncMode getMasterSyncMode() {
|
|
550 |
return masterSyncMode;
|
|
551 |
}
|
|
552 |
|
|
553 |
|
|
554 |
public Sequencer.SyncMode[] getMasterSyncModes() {
|
|
555 |
Sequencer.SyncMode[] returnedModes = new Sequencer.SyncMode[masterSyncModes.length];
|
|
556 |
System.arraycopy(masterSyncModes, 0, returnedModes, 0, masterSyncModes.length);
|
|
557 |
return returnedModes;
|
|
558 |
}
|
|
559 |
|
|
560 |
|
|
561 |
public void setSlaveSyncMode(Sequencer.SyncMode sync) {
|
|
562 |
// not supported
|
|
563 |
}
|
|
564 |
|
|
565 |
|
|
566 |
public Sequencer.SyncMode getSlaveSyncMode() {
|
|
567 |
return slaveSyncMode;
|
|
568 |
}
|
|
569 |
|
|
570 |
|
|
571 |
public Sequencer.SyncMode[] getSlaveSyncModes() {
|
|
572 |
Sequencer.SyncMode[] returnedModes = new Sequencer.SyncMode[slaveSyncModes.length];
|
|
573 |
System.arraycopy(slaveSyncModes, 0, returnedModes, 0, slaveSyncModes.length);
|
|
574 |
return returnedModes;
|
|
575 |
}
|
|
576 |
|
|
577 |
protected int getTrackCount() {
|
|
578 |
Sequence seq = getSequence();
|
|
579 |
if (seq != null) {
|
|
580 |
// $$fb wish there was a nicer way to get the number of tracks...
|
|
581 |
return sequence.getTracks().length;
|
|
582 |
}
|
|
583 |
return 0;
|
|
584 |
}
|
|
585 |
|
|
586 |
|
|
587 |
|
|
588 |
public synchronized void setTrackMute(int track, boolean mute) {
|
|
589 |
int trackCount = getTrackCount();
|
|
590 |
if (track < 0 || track >= getTrackCount()) return;
|
|
591 |
trackMuted = ensureBoolArraySize(trackMuted, trackCount);
|
|
592 |
trackMuted[track] = mute;
|
|
593 |
if (getDataPump() != null) {
|
|
594 |
getDataPump().muteSoloChanged();
|
|
595 |
}
|
|
596 |
}
|
|
597 |
|
|
598 |
|
|
599 |
public synchronized boolean getTrackMute(int track) {
|
|
600 |
if (track < 0 || track >= getTrackCount()) return false;
|
|
601 |
if (trackMuted == null || trackMuted.length <= track) return false;
|
|
602 |
return trackMuted[track];
|
|
603 |
}
|
|
604 |
|
|
605 |
|
|
606 |
public synchronized void setTrackSolo(int track, boolean solo) {
|
|
607 |
int trackCount = getTrackCount();
|
|
608 |
if (track < 0 || track >= getTrackCount()) return;
|
|
609 |
trackSolo = ensureBoolArraySize(trackSolo, trackCount);
|
|
610 |
trackSolo[track] = solo;
|
|
611 |
if (getDataPump() != null) {
|
|
612 |
getDataPump().muteSoloChanged();
|
|
613 |
}
|
|
614 |
}
|
|
615 |
|
|
616 |
|
|
617 |
public synchronized boolean getTrackSolo(int track) {
|
|
618 |
if (track < 0 || track >= getTrackCount()) return false;
|
|
619 |
if (trackSolo == null || trackSolo.length <= track) return false;
|
|
620 |
return trackSolo[track];
|
|
621 |
}
|
|
622 |
|
|
623 |
|
|
624 |
public boolean addMetaEventListener(MetaEventListener listener) {
|
|
625 |
synchronized(metaEventListeners) {
|
|
626 |
if (! metaEventListeners.contains(listener)) {
|
|
627 |
|
|
628 |
metaEventListeners.add(listener);
|
|
629 |
}
|
|
630 |
return true;
|
|
631 |
}
|
|
632 |
}
|
|
633 |
|
|
634 |
|
|
635 |
public void removeMetaEventListener(MetaEventListener listener) {
|
|
636 |
synchronized(metaEventListeners) {
|
|
637 |
int index = metaEventListeners.indexOf(listener);
|
|
638 |
if (index >= 0) {
|
|
639 |
metaEventListeners.remove(index);
|
|
640 |
}
|
|
641 |
}
|
|
642 |
}
|
|
643 |
|
|
644 |
|
|
645 |
public int[] addControllerEventListener(ControllerEventListener listener, int[] controllers) {
|
|
646 |
synchronized(controllerEventListeners) {
|
|
647 |
|
|
648 |
// first find the listener. if we have one, add the controllers
|
|
649 |
// if not, create a new element for it.
|
|
650 |
ControllerListElement cve = null;
|
|
651 |
boolean flag = false;
|
|
652 |
for(int i=0; i < controllerEventListeners.size(); i++) {
|
|
653 |
|
|
654 |
cve = (ControllerListElement) controllerEventListeners.get(i);
|
|
655 |
|
|
656 |
if (cve.listener.equals(listener)) {
|
|
657 |
cve.addControllers(controllers);
|
|
658 |
flag = true;
|
|
659 |
break;
|
|
660 |
}
|
|
661 |
}
|
|
662 |
if (!flag) {
|
|
663 |
cve = new ControllerListElement(listener, controllers);
|
|
664 |
controllerEventListeners.add(cve);
|
|
665 |
}
|
|
666 |
|
|
667 |
// and return all the controllers this listener is interested in
|
|
668 |
return cve.getControllers();
|
|
669 |
}
|
|
670 |
}
|
|
671 |
|
|
672 |
|
|
673 |
public int[] removeControllerEventListener(ControllerEventListener listener, int[] controllers) {
|
|
674 |
synchronized(controllerEventListeners) {
|
|
675 |
ControllerListElement cve = null;
|
|
676 |
boolean flag = false;
|
|
677 |
for (int i=0; i < controllerEventListeners.size(); i++) {
|
|
678 |
cve = (ControllerListElement) controllerEventListeners.get(i);
|
|
679 |
if (cve.listener.equals(listener)) {
|
|
680 |
cve.removeControllers(controllers);
|
|
681 |
flag = true;
|
|
682 |
break;
|
|
683 |
}
|
|
684 |
}
|
|
685 |
if (!flag) {
|
|
686 |
return new int[0];
|
|
687 |
}
|
|
688 |
if (controllers == null) {
|
|
689 |
int index = controllerEventListeners.indexOf(cve);
|
|
690 |
if (index >= 0) {
|
|
691 |
controllerEventListeners.remove(index);
|
|
692 |
}
|
|
693 |
return new int[0];
|
|
694 |
}
|
|
695 |
return cve.getControllers();
|
|
696 |
}
|
|
697 |
}
|
|
698 |
|
|
699 |
|
|
700 |
////////////////// LOOPING (added in 1.5) ///////////////////////
|
|
701 |
|
|
702 |
public void setLoopStartPoint(long tick) {
|
|
703 |
if ((tick > getTickLength())
|
|
704 |
|| ((loopEnd != -1) && (tick > loopEnd))
|
|
705 |
|| (tick < 0)) {
|
|
706 |
throw new IllegalArgumentException("invalid loop start point: "+tick);
|
|
707 |
}
|
|
708 |
loopStart = tick;
|
|
709 |
}
|
|
710 |
|
|
711 |
public long getLoopStartPoint() {
|
|
712 |
return loopStart;
|
|
713 |
}
|
|
714 |
|
|
715 |
public void setLoopEndPoint(long tick) {
|
|
716 |
if ((tick > getTickLength())
|
|
717 |
|| ((loopStart > tick) && (tick != -1))
|
|
718 |
|| (tick < -1)) {
|
|
719 |
throw new IllegalArgumentException("invalid loop end point: "+tick);
|
|
720 |
}
|
|
721 |
loopEnd = tick;
|
|
722 |
}
|
|
723 |
|
|
724 |
public long getLoopEndPoint() {
|
|
725 |
return loopEnd;
|
|
726 |
}
|
|
727 |
|
|
728 |
public void setLoopCount(int count) {
|
|
729 |
if (count != LOOP_CONTINUOUSLY
|
|
730 |
&& count < 0) {
|
|
731 |
throw new IllegalArgumentException("illegal value for loop count: "+count);
|
|
732 |
}
|
|
733 |
loopCount = count;
|
|
734 |
if (getDataPump() != null) {
|
|
735 |
getDataPump().resetLoopCount();
|
|
736 |
}
|
|
737 |
}
|
|
738 |
|
|
739 |
public int getLoopCount() {
|
|
740 |
return loopCount;
|
|
741 |
}
|
|
742 |
|
|
743 |
|
|
744 |
/* *********************************** play control ************************* */
|
|
745 |
|
|
746 |
/*
|
|
747 |
*/
|
|
748 |
protected void implOpen() throws MidiUnavailableException {
|
|
749 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: implOpen()");
|
|
750 |
|
|
751 |
//openInternalSynth();
|
|
752 |
|
|
753 |
// create PlayThread
|
|
754 |
playThread = new PlayThread();
|
|
755 |
|
|
756 |
//id = nOpen();
|
|
757 |
//if (id == 0) {
|
|
758 |
// throw new MidiUnavailableException("unable to open sequencer");
|
|
759 |
//}
|
|
760 |
if (sequence != null) {
|
|
761 |
playThread.setSequence(sequence);
|
|
762 |
}
|
|
763 |
|
|
764 |
// propagate caches
|
|
765 |
propagateCaches();
|
|
766 |
|
|
767 |
if (doAutoConnectAtNextOpen) {
|
|
768 |
doAutoConnect();
|
|
769 |
}
|
|
770 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: implOpen() succeeded");
|
|
771 |
}
|
|
772 |
|
|
773 |
private void doAutoConnect() {
|
|
774 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: doAutoConnect()");
|
|
775 |
Receiver rec = null;
|
|
776 |
// first try to connect to the default synthesizer
|
|
777 |
// IMPORTANT: this code needs to be synch'ed with
|
|
778 |
// MidiSystem.getReceiver(boolean), because the same
|
|
779 |
// algorithm needs to be used!
|
|
780 |
try {
|
|
781 |
Synthesizer synth = MidiSystem.getSynthesizer();
|
|
782 |
synth.open();
|
|
783 |
if (synth instanceof ReferenceCountingDevice) {
|
|
784 |
rec = ((ReferenceCountingDevice) synth).getReceiverReferenceCounting();
|
|
785 |
} else {
|
|
786 |
rec = synth.getReceiver();
|
|
787 |
}
|
|
788 |
} catch (Exception e) {
|
|
789 |
// something went wrong with synth
|
|
790 |
}
|
|
791 |
if (rec == null) {
|
|
792 |
// then try to connect to the default Receiver
|
|
793 |
try {
|
|
794 |
rec = MidiSystem.getReceiver();
|
|
795 |
} catch (Exception e) {
|
|
796 |
// something went wrong. Nothing to do then!
|
|
797 |
}
|
|
798 |
}
|
|
799 |
if (rec != null) {
|
|
800 |
autoConnectedReceiver = rec;
|
|
801 |
try {
|
|
802 |
getTransmitter().setReceiver(rec);
|
|
803 |
} catch (Exception e) {}
|
|
804 |
}
|
|
805 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: doAutoConnect() succeeded");
|
|
806 |
}
|
|
807 |
|
|
808 |
private synchronized void propagateCaches() {
|
|
809 |
// only set caches if open and sequence is set
|
|
810 |
if (sequence != null && isOpen()) {
|
|
811 |
if (cacheTempoFactor != -1) {
|
|
812 |
setTempoFactor(cacheTempoFactor);
|
|
813 |
}
|
|
814 |
if (cacheTempoMPQ == -1) {
|
|
815 |
setTempoInMPQ((new MidiUtils.TempoCache(sequence)).getTempoMPQAt(getTickPosition()));
|
|
816 |
} else {
|
|
817 |
setTempoInMPQ((float) cacheTempoMPQ);
|
|
818 |
}
|
|
819 |
}
|
|
820 |
}
|
|
821 |
|
|
822 |
/** populate the caches with the current values */
|
|
823 |
private synchronized void setCaches() {
|
|
824 |
cacheTempoFactor = getTempoFactor();
|
|
825 |
cacheTempoMPQ = getTempoInMPQ();
|
|
826 |
}
|
|
827 |
|
|
828 |
|
|
829 |
|
|
830 |
protected synchronized void implClose() {
|
|
831 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: implClose() ");
|
|
832 |
|
|
833 |
if (playThread == null) {
|
|
834 |
if (Printer.err) Printer.err("RealTimeSequencer.implClose() called, but playThread not instanciated!");
|
|
835 |
} else {
|
|
836 |
// Interrupt playback loop.
|
|
837 |
playThread.close();
|
|
838 |
playThread = null;
|
|
839 |
}
|
|
840 |
|
|
841 |
super.implClose();
|
|
842 |
|
|
843 |
sequence = null;
|
|
844 |
running = false;
|
|
845 |
cacheTempoMPQ = -1;
|
|
846 |
cacheTempoFactor = -1;
|
|
847 |
trackMuted = null;
|
|
848 |
trackSolo = null;
|
|
849 |
loopStart = 0;
|
|
850 |
loopEnd = -1;
|
|
851 |
loopCount = 0;
|
|
852 |
|
|
853 |
/** if this sequencer is set to autoconnect, need to
|
|
854 |
* re-establish the connection at next open!
|
|
855 |
*/
|
|
856 |
doAutoConnectAtNextOpen = autoConnect;
|
|
857 |
|
|
858 |
if (autoConnectedReceiver != null) {
|
|
859 |
try {
|
|
860 |
autoConnectedReceiver.close();
|
|
861 |
} catch (Exception e) {}
|
|
862 |
autoConnectedReceiver = null;
|
|
863 |
}
|
|
864 |
|
|
865 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: implClose() completed");
|
|
866 |
}
|
|
867 |
|
|
868 |
protected void implStart() {
|
|
869 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: implStart()");
|
|
870 |
|
|
871 |
if (playThread == null) {
|
|
872 |
if (Printer.err) Printer.err("RealTimeSequencer.implStart() called, but playThread not instanciated!");
|
|
873 |
return;
|
|
874 |
}
|
|
875 |
|
|
876 |
tempoCache.refresh(sequence);
|
|
877 |
if (!running) {
|
|
878 |
running = true;
|
|
879 |
playThread.start();
|
|
880 |
}
|
|
881 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: implStart() completed");
|
|
882 |
}
|
|
883 |
|
|
884 |
|
|
885 |
protected void implStop() {
|
|
886 |
if (Printer.trace) Printer.trace(">> RealTimeSequencer: implStop()");
|
|
887 |
|
|
888 |
if (playThread == null) {
|
|
889 |
if (Printer.err) Printer.err("RealTimeSequencer.implStop() called, but playThread not instanciated!");
|
|
890 |
return;
|
|
891 |
}
|
|
892 |
|
|
893 |
recording = false;
|
|
894 |
if (running) {
|
|
895 |
running = false;
|
|
896 |
playThread.stop();
|
|
897 |
}
|
|
898 |
if (Printer.trace) Printer.trace("<< RealTimeSequencer: implStop() completed");
|
|
899 |
}
|
|
900 |
|
|
901 |
|
|
902 |
/**
|
|
903 |
* Send midi player events.
|
|
904 |
* must not be synchronized on "this"
|
|
905 |
*/
|
|
906 |
protected void sendMetaEvents(MidiMessage message) {
|
|
907 |
if (metaEventListeners.size() == 0) return;
|
|
908 |
|
|
909 |
//if (Printer.debug) Printer.debug("sending a meta event");
|
|
910 |
eventDispatcher.sendAudioEvents(message, metaEventListeners);
|
|
911 |
}
|
|
912 |
|
|
913 |
/**
|
|
914 |
* Send midi player events.
|
|
915 |
*/
|
|
916 |
protected void sendControllerEvents(MidiMessage message) {
|
|
917 |
int size = controllerEventListeners.size();
|
|
918 |
if (size == 0) return;
|
|
919 |
|
|
920 |
//if (Printer.debug) Printer.debug("sending a controller event");
|
|
921 |
|
|
922 |
if (! (message instanceof ShortMessage)) {
|
|
923 |
if (Printer.debug) Printer.debug("sendControllerEvents: message is NOT instanceof ShortMessage!");
|
|
924 |
return;
|
|
925 |
}
|
|
926 |
ShortMessage msg = (ShortMessage) message;
|
|
927 |
int controller = msg.getData1();
|
|
928 |
List sendToListeners = new ArrayList();
|
|
929 |
for (int i = 0; i < size; i++) {
|
|
930 |
ControllerListElement cve = (ControllerListElement) controllerEventListeners.get(i);
|
|
931 |
for(int j = 0; j < cve.controllers.length; j++) {
|
|
932 |
if (cve.controllers[j] == controller) {
|
|
933 |
sendToListeners.add(cve.listener);
|
|
934 |
break;
|
|
935 |
}
|
|
936 |
}
|
|
937 |
}
|
|
938 |
eventDispatcher.sendAudioEvents(message, sendToListeners);
|
|
939 |
}
|
|
940 |
|
|
941 |
|
|
942 |
|
|
943 |
private boolean needCaching() {
|
|
944 |
return !isOpen() || (sequence == null) || (playThread == null);
|
|
945 |
}
|
|
946 |
|
|
947 |
/**
|
|
948 |
* return the data pump instance, owned by play thread
|
|
949 |
* if playthread is null, return null.
|
|
950 |
* This method is guaranteed to return non-null if
|
|
951 |
* needCaching returns false
|
|
952 |
*/
|
|
953 |
private DataPump getDataPump() {
|
|
954 |
if (playThread != null) {
|
|
955 |
return playThread.getDataPump();
|
|
956 |
}
|
|
957 |
return null;
|
|
958 |
}
|
|
959 |
|
|
960 |
private MidiUtils.TempoCache getTempoCache() {
|
|
961 |
return tempoCache;
|
|
962 |
}
|
|
963 |
|
|
964 |
private static boolean[] ensureBoolArraySize(boolean[] array, int desiredSize) {
|
|
965 |
if (array == null) {
|
|
966 |
return new boolean[desiredSize];
|
|
967 |
}
|
|
968 |
if (array.length < desiredSize) {
|
|
969 |
boolean[] newArray = new boolean[desiredSize];
|
|
970 |
System.arraycopy(array, 0, newArray, 0, array.length);
|
|
971 |
return newArray;
|
|
972 |
}
|
|
973 |
return array;
|
|
974 |
}
|
|
975 |
|
|
976 |
|
|
977 |
// OVERRIDES OF ABSTRACT MIDI DEVICE METHODS
|
|
978 |
|
|
979 |
protected boolean hasReceivers() {
|
|
980 |
return true;
|
|
981 |
}
|
|
982 |
|
|
983 |
// for recording
|
|
984 |
protected Receiver createReceiver() throws MidiUnavailableException {
|
|
985 |
return new SequencerReceiver();
|
|
986 |
}
|
|
987 |
|
|
988 |
|
|
989 |
protected boolean hasTransmitters() {
|
|
990 |
return true;
|
|
991 |
}
|
|
992 |
|
|
993 |
|
|
994 |
protected Transmitter createTransmitter() throws MidiUnavailableException {
|
|
995 |
return new SequencerTransmitter();
|
|
996 |
}
|
|
997 |
|
|
998 |
|
|
999 |
// interface AutoConnectSequencer
|
|
1000 |
public void setAutoConnect(Receiver autoConnectedReceiver) {
|
|
1001 |
this.autoConnect = (autoConnectedReceiver != null);
|
|
1002 |
this.autoConnectedReceiver = autoConnectedReceiver;
|
|
1003 |
}
|
|
1004 |
|
|
1005 |
|
|
1006 |
|
|
1007 |
// INNER CLASSES
|
|
1008 |
|
|
1009 |
/**
|
|
1010 |
* An own class to distinguish the class name from
|
|
1011 |
* the transmitter of other devices
|
|
1012 |
*/
|
|
1013 |
private class SequencerTransmitter extends BasicTransmitter {
|
|
1014 |
private SequencerTransmitter() {
|
|
1015 |
super();
|
|
1016 |
}
|
|
1017 |
}
|
|
1018 |
|
|
1019 |
|
|
1020 |
class SequencerReceiver extends AbstractReceiver {
|
|
1021 |
|
|
1022 |
protected void implSend(MidiMessage message, long timeStamp) {
|
|
1023 |
if (recording) {
|
|
1024 |
long tickPos = 0;
|
|
1025 |
|
|
1026 |
// convert timeStamp to ticks
|
|
1027 |
if (timeStamp < 0) {
|
|
1028 |
tickPos = getTickPosition();
|
|
1029 |
} else {
|
|
1030 |
synchronized(tempoCache) {
|
|
1031 |
tickPos = MidiUtils.microsecond2tick(sequence, timeStamp, tempoCache);
|
|
1032 |
}
|
|
1033 |
}
|
|
1034 |
|
|
1035 |
// and record to the first matching Track
|
|
1036 |
Track track = null;
|
|
1037 |
// do not record real-time events
|
|
1038 |
// see 5048381: NullPointerException when saving a MIDI sequence
|
|
1039 |
if (message.getLength() > 1) {
|
|
1040 |
if (message instanceof ShortMessage) {
|
|
1041 |
ShortMessage sm = (ShortMessage) message;
|
|
1042 |
// all real-time messages have 0xF in the high nibble of the status byte
|
|
1043 |
if ((sm.getStatus() & 0xF0) != 0xF0) {
|
|
1044 |
track = RecordingTrack.get(recordingTracks, sm.getChannel());
|
|
1045 |
}
|
|
1046 |
} else {
|
|
1047 |
// $$jb: where to record meta, sysex events?
|
|
1048 |
// $$fb: the first recording track
|
|
1049 |
track = RecordingTrack.get(recordingTracks, -1);
|
|
1050 |
}
|
|
1051 |
if (track != null) {
|
|
1052 |
// create a copy of this message
|
|
1053 |
if (message instanceof ShortMessage) {
|
|
1054 |
message = new FastShortMessage((ShortMessage) message);
|
|
1055 |
} else {
|
|
1056 |
message = (MidiMessage) message.clone();
|
|
1057 |
}
|
|
1058 |
|
|
1059 |
// create new MidiEvent
|
|
1060 |
MidiEvent me = new MidiEvent(message, tickPos);
|
|
1061 |
track.add(me);
|
|
1062 |
}
|
|
1063 |
}
|
|
1064 |
}
|
|
1065 |
}
|
|
1066 |
}
|
|
1067 |
|
|
1068 |
|
|
1069 |
private static class RealTimeSequencerInfo extends MidiDevice.Info {
|
|
1070 |
|
|
1071 |
private static final String name = "Real Time Sequencer";
|
|
1072 |
private static final String vendor = "Sun Microsystems";
|
|
1073 |
private static final String description = "Software sequencer";
|
|
1074 |
private static final String version = "Version 1.0";
|
|
1075 |
|
|
1076 |
private RealTimeSequencerInfo() {
|
|
1077 |
super(name, vendor, description, version);
|
|
1078 |
}
|
|
1079 |
} // class Info
|
|
1080 |
|
|
1081 |
|
|
1082 |
private class ControllerListElement {
|
|
1083 |
|
|
1084 |
// $$jb: using an array for controllers b/c its
|
|
1085 |
// easier to deal with than turning all the
|
|
1086 |
// ints into objects to use a Vector
|
|
1087 |
int [] controllers;
|
|
1088 |
ControllerEventListener listener;
|
|
1089 |
|
|
1090 |
private ControllerListElement(ControllerEventListener listener, int[] controllers) {
|
|
1091 |
|
|
1092 |
this.listener = listener;
|
|
1093 |
if (controllers == null) {
|
|
1094 |
controllers = new int[128];
|
|
1095 |
for (int i = 0; i < 128; i++) {
|
|
1096 |
controllers[i] = i;
|
|
1097 |
}
|
|
1098 |
}
|
|
1099 |
this.controllers = controllers;
|
|
1100 |
}
|
|
1101 |
|
|
1102 |
private void addControllers(int[] c) {
|
|
1103 |
|
|
1104 |
if (c==null) {
|
|
1105 |
controllers = new int[128];
|
|
1106 |
for (int i = 0; i < 128; i++) {
|
|
1107 |
controllers[i] = i;
|
|
1108 |
}
|
|
1109 |
return;
|
|
1110 |
}
|
|
1111 |
int temp[] = new int[ controllers.length + c.length ];
|
|
1112 |
int elements;
|
|
1113 |
|
|
1114 |
// first add what we have
|
|
1115 |
for(int i=0; i<controllers.length; i++) {
|
|
1116 |
temp[i] = controllers[i];
|
|
1117 |
}
|
|
1118 |
elements = controllers.length;
|
|
1119 |
// now add the new controllers only if we don't already have them
|
|
1120 |
for(int i=0; i<c.length; i++) {
|
|
1121 |
boolean flag = false;
|
|
1122 |
|
|
1123 |
for(int j=0; j<controllers.length; j++) {
|
|
1124 |
if (c[i] == controllers[j]) {
|
|
1125 |
flag = true;
|
|
1126 |
break;
|
|
1127 |
}
|
|
1128 |
}
|
|
1129 |
if (!flag) {
|
|
1130 |
temp[elements++] = c[i];
|
|
1131 |
}
|
|
1132 |
}
|
|
1133 |
// now keep only the elements we need
|
|
1134 |
int newc[] = new int[ elements ];
|
|
1135 |
for(int i=0; i<elements; i++){
|
|
1136 |
newc[i] = temp[i];
|
|
1137 |
}
|
|
1138 |
controllers = newc;
|
|
1139 |
}
|
|
1140 |
|
|
1141 |
private void removeControllers(int[] c) {
|
|
1142 |
|
|
1143 |
if (c==null) {
|
|
1144 |
controllers = new int[0];
|
|
1145 |
} else {
|
|
1146 |
int temp[] = new int[ controllers.length ];
|
|
1147 |
int elements = 0;
|
|
1148 |
|
|
1149 |
|
|
1150 |
for(int i=0; i<controllers.length; i++){
|
|
1151 |
boolean flag = false;
|
|
1152 |
for(int j=0; j<c.length; j++) {
|
|
1153 |
if (controllers[i] == c[j]) {
|
|
1154 |
flag = true;
|
|
1155 |
break;
|
|
1156 |
}
|
|
1157 |
}
|
|
1158 |
if (!flag){
|
|
1159 |
temp[elements++] = controllers[i];
|
|
1160 |
}
|
|
1161 |
}
|
|
1162 |
// now keep only the elements remaining
|
|
1163 |
int newc[] = new int[ elements ];
|
|
1164 |
for(int i=0; i<elements; i++) {
|
|
1165 |
newc[i] = temp[i];
|
|
1166 |
}
|
|
1167 |
controllers = newc;
|
|
1168 |
|
|
1169 |
}
|
|
1170 |
}
|
|
1171 |
|
|
1172 |
private int[] getControllers() {
|
|
1173 |
|
|
1174 |
// return a copy of our array of controllers,
|
|
1175 |
// so others can't mess with it
|
|
1176 |
if (controllers == null) {
|
|
1177 |
return null;
|
|
1178 |
}
|
|
1179 |
|
|
1180 |
int c[] = new int[controllers.length];
|
|
1181 |
|
|
1182 |
for(int i=0; i<controllers.length; i++){
|
|
1183 |
c[i] = controllers[i];
|
|
1184 |
}
|
|
1185 |
return c;
|
|
1186 |
}
|
|
1187 |
|
|
1188 |
} // class ControllerListElement
|
|
1189 |
|
|
1190 |
|
|
1191 |
static class RecordingTrack {
|
|
1192 |
|
|
1193 |
private Track track;
|
|
1194 |
private int channel;
|
|
1195 |
|
|
1196 |
RecordingTrack(Track track, int channel) {
|
|
1197 |
this.track = track;
|
|
1198 |
this.channel = channel;
|
|
1199 |
}
|
|
1200 |
|
|
1201 |
static RecordingTrack get(List recordingTracks, Track track) {
|
|
1202 |
|
|
1203 |
synchronized(recordingTracks) {
|
|
1204 |
int size = recordingTracks.size();
|
|
1205 |
|
|
1206 |
for (int i = 0; i < size; i++) {
|
|
1207 |
RecordingTrack current = (RecordingTrack)recordingTracks.get(i);
|
|
1208 |
if (current.track == track) {
|
|
1209 |
return current;
|
|
1210 |
}
|
|
1211 |
}
|
|
1212 |
}
|
|
1213 |
return null;
|
|
1214 |
}
|
|
1215 |
|
|
1216 |
static Track get(List recordingTracks, int channel) {
|
|
1217 |
|
|
1218 |
synchronized(recordingTracks) {
|
|
1219 |
int size = recordingTracks.size();
|
|
1220 |
for (int i = 0; i < size; i++) {
|
|
1221 |
RecordingTrack current = (RecordingTrack)recordingTracks.get(i);
|
|
1222 |
if ((current.channel == channel) || (current.channel == -1)) {
|
|
1223 |
return current.track;
|
|
1224 |
}
|
|
1225 |
}
|
|
1226 |
}
|
|
1227 |
return null;
|
|
1228 |
|
|
1229 |
}
|
|
1230 |
}
|
|
1231 |
|
|
1232 |
|
|
1233 |
class PlayThread implements Runnable {
|
|
1234 |
private Thread thread;
|
|
1235 |
private Object lock = new Object();
|
|
1236 |
|
|
1237 |
/** true if playback is interrupted (in close) */
|
|
1238 |
boolean interrupted = false;
|
|
1239 |
boolean isPumping = false;
|
|
1240 |
|
|
1241 |
private DataPump dataPump = new DataPump();
|
|
1242 |
|
|
1243 |
|
|
1244 |
PlayThread() {
|
|
1245 |
// nearly MAX_PRIORITY
|
|
1246 |
int priority = Thread.NORM_PRIORITY
|
|
1247 |
+ ((Thread.MAX_PRIORITY - Thread.NORM_PRIORITY) * 3) / 4;
|
|
1248 |
thread = JSSecurityManager.createThread(this,
|
|
1249 |
"Java Sound Sequencer", // name
|
|
1250 |
false, // daemon
|
|
1251 |
priority, // priority
|
|
1252 |
true); // doStart
|
|
1253 |
}
|
|
1254 |
|
|
1255 |
DataPump getDataPump() {
|
|
1256 |
return dataPump;
|
|
1257 |
}
|
|
1258 |
|
|
1259 |
synchronized void setSequence(Sequence seq) {
|
|
1260 |
dataPump.setSequence(seq);
|
|
1261 |
}
|
|
1262 |
|
|
1263 |
|
|
1264 |
/** start thread and pump. Requires up-to-date tempoCache */
|
|
1265 |
synchronized void start() {
|
|
1266 |
// mark the sequencer running
|
|
1267 |
running = true;
|
|
1268 |
|
|
1269 |
if (!dataPump.hasCachedTempo()) {
|
|
1270 |
long tickPos = getTickPosition();
|
|
1271 |
dataPump.setTempoMPQ(tempoCache.getTempoMPQAt(tickPos));
|
|
1272 |
}
|
|
1273 |
dataPump.checkPointMillis = 0; // means restarted
|
|
1274 |
dataPump.clearNoteOnCache();
|
|
1275 |
dataPump.needReindex = true;
|
|
1276 |
|
|
1277 |
dataPump.resetLoopCount();
|
|
1278 |
|
|
1279 |
// notify the thread
|
|
1280 |
synchronized(lock) {
|
|
1281 |
lock.notifyAll();
|
|
1282 |
}
|
|
1283 |
|
|
1284 |
if (Printer.debug) Printer.debug(" ->Started MIDI play thread");
|
|
1285 |
|
|
1286 |
}
|
|
1287 |
|
|
1288 |
// waits until stopped
|
|
1289 |
synchronized void stop() {
|
|
1290 |
playThreadImplStop();
|
|
1291 |
long t = System.nanoTime() / 1000000l;
|
|
1292 |
while (isPumping) {
|
|
1293 |
synchronized(lock) {
|
|
1294 |
try {
|
|
1295 |
lock.wait(2000);
|
|
1296 |
} catch (InterruptedException ie) {
|
|
1297 |
// ignore
|
|
1298 |
}
|
|
1299 |
}
|
|
1300 |
// don't wait for more than 2 seconds
|
|
1301 |
if ((System.nanoTime()/1000000l) - t > 1900) {
|
|
1302 |
if (Printer.err) Printer.err("Waited more than 2 seconds in RealTimeSequencer.PlayThread.stop()!");
|
|
1303 |
//break;
|
|
1304 |
}
|
|
1305 |
}
|
|
1306 |
}
|
|
1307 |
|
|
1308 |
void playThreadImplStop() {
|
|
1309 |
// mark the sequencer running
|
|
1310 |
running = false;
|
|
1311 |
synchronized(lock) {
|
|
1312 |
lock.notifyAll();
|
|
1313 |
}
|
|
1314 |
}
|
|
1315 |
|
|
1316 |
void close() {
|
|
1317 |
Thread oldThread = null;
|
|
1318 |
synchronized (this) {
|
|
1319 |
// dispose of thread
|
|
1320 |
interrupted = true;
|
|
1321 |
oldThread = thread;
|
|
1322 |
thread = null;
|
|
1323 |
}
|
|
1324 |
if (oldThread != null) {
|
|
1325 |
// wake up the thread if it's in wait()
|
|
1326 |
synchronized(lock) {
|
|
1327 |
lock.notifyAll();
|
|
1328 |
}
|
|
1329 |
}
|
|
1330 |
// wait for the thread to terminate itself,
|
|
1331 |
// but max. 2 seconds. Must not be synchronized!
|
|
1332 |
if (oldThread != null) {
|
|
1333 |
try {
|
|
1334 |
oldThread.join(2000);
|
|
1335 |
} catch (InterruptedException ie) {}
|
|
1336 |
}
|
|
1337 |
}
|
|
1338 |
|
|
1339 |
|
|
1340 |
/**
|
|
1341 |
* Main process loop driving the media flow.
|
|
1342 |
*
|
|
1343 |
* Make sure to NOT synchronize on RealTimeSequencer
|
|
1344 |
* anywhere here (even implicit). That is a sure deadlock!
|
|
1345 |
*/
|
|
1346 |
public void run() {
|
|
1347 |
|
|
1348 |
while (!interrupted) {
|
|
1349 |
boolean EOM = false;
|
|
1350 |
boolean wasRunning = running;
|
|
1351 |
isPumping = !interrupted && running;
|
|
1352 |
while (!EOM && !interrupted && running) {
|
|
1353 |
EOM = dataPump.pump();
|
|
1354 |
|
|
1355 |
try {
|
|
1356 |
Thread.sleep(1);
|
|
1357 |
} catch (InterruptedException ie) {
|
|
1358 |
// ignore
|
|
1359 |
}
|
|
1360 |
}
|
|
1361 |
if (Printer.debug) {
|
|
1362 |
Printer.debug("Exited main pump loop because: ");
|
|
1363 |
if (EOM) Printer.debug(" -> EOM is reached");
|
|
1364 |
if (!running) Printer.debug(" -> running was set to false");
|
|
1365 |
if (interrupted) Printer.debug(" -> interrupted was set to true");
|
|
1366 |
}
|
|
1367 |
|
|
1368 |
playThreadImplStop();
|
|
1369 |
if (wasRunning) {
|
|
1370 |
dataPump.notesOff(true);
|
|
1371 |
}
|
|
1372 |
if (EOM) {
|
|
1373 |
dataPump.setTickPos(sequence.getTickLength());
|
|
1374 |
|
|
1375 |
// send EOT event (mis-used for end of media)
|
|
1376 |
MetaMessage message = new MetaMessage();
|
|
1377 |
try{
|
|
1378 |
message.setMessage(MidiUtils.META_END_OF_TRACK_TYPE, new byte[0], 0);
|
|
1379 |
} catch(InvalidMidiDataException e1) {}
|
|
1380 |
sendMetaEvents(message);
|
|
1381 |
}
|
|
1382 |
synchronized (lock) {
|
|
1383 |
isPumping = false;
|
|
1384 |
// wake up a waiting stop() method
|
|
1385 |
lock.notifyAll();
|
|
1386 |
while (!running && !interrupted) {
|
|
1387 |
try {
|
|
1388 |
lock.wait();
|
|
1389 |
} catch (Exception ex) {}
|
|
1390 |
}
|
|
1391 |
}
|
|
1392 |
} // end of while(!EOM && !interrupted && running)
|
|
1393 |
if (Printer.debug) Printer.debug("end of play thread");
|
|
1394 |
}
|
|
1395 |
}
|
|
1396 |
|
|
1397 |
|
|
1398 |
/**
|
|
1399 |
* class that does the actual dispatching of events,
|
|
1400 |
* used to be in native in MMAPI
|
|
1401 |
*/
|
|
1402 |
private class DataPump {
|
|
1403 |
private float currTempo; // MPQ tempo
|
|
1404 |
private float tempoFactor; // 1.0 is default
|
|
1405 |
private float inverseTempoFactor;// = 1.0 / tempoFactor
|
|
1406 |
private long ignoreTempoEventAt; // ignore next META tempo during playback at this tick pos only
|
|
1407 |
private int resolution;
|
|
1408 |
private float divisionType;
|
|
1409 |
private long checkPointMillis; // microseconds at checkoint
|
|
1410 |
private long checkPointTick; // ticks at checkpoint
|
|
1411 |
private int[] noteOnCache; // bit-mask of notes that are currently on
|
|
1412 |
private Track[] tracks;
|
|
1413 |
private boolean[] trackDisabled; // if true, do not play this track
|
|
1414 |
private int[] trackReadPos; // read index per track
|
|
1415 |
private long lastTick;
|
|
1416 |
private boolean needReindex = false;
|
|
1417 |
private int currLoopCounter = 0;
|
|
1418 |
|
|
1419 |
//private sun.misc.Perf perf = sun.misc.Perf.getPerf();
|
|
1420 |
//private long perfFreq = perf.highResFrequency();
|
|
1421 |
|
|
1422 |
|
|
1423 |
DataPump() {
|
|
1424 |
init();
|
|
1425 |
}
|
|
1426 |
|
|
1427 |
synchronized void init() {
|
|
1428 |
ignoreTempoEventAt = -1;
|
|
1429 |
tempoFactor = 1.0f;
|
|
1430 |
inverseTempoFactor = 1.0f;
|
|
1431 |
noteOnCache = new int[128];
|
|
1432 |
tracks = null;
|
|
1433 |
trackDisabled = null;
|
|
1434 |
}
|
|
1435 |
|
|
1436 |
synchronized void setTickPos(long tickPos) {
|
|
1437 |
long oldLastTick = tickPos;
|
|
1438 |
lastTick = tickPos;
|
|
1439 |
if (running) {
|
|
1440 |
notesOff(false);
|
|
1441 |
}
|
|
1442 |
if (running || tickPos > 0) {
|
|
1443 |
// will also reindex
|
|
1444 |
chaseEvents(oldLastTick, tickPos);
|
|
1445 |
} else {
|
|
1446 |
needReindex = true;
|
|
1447 |
}
|
|
1448 |
if (!hasCachedTempo()) {
|
|
1449 |
setTempoMPQ(getTempoCache().getTempoMPQAt(lastTick, currTempo));
|
|
1450 |
// treat this as if it is a real time tempo change
|
|
1451 |
ignoreTempoEventAt = -1;
|
|
1452 |
}
|
|
1453 |
// trigger re-configuration
|
|
1454 |
checkPointMillis = 0;
|
|
1455 |
}
|
|
1456 |
|
|
1457 |
long getTickPos() {
|
|
1458 |
return lastTick;
|
|
1459 |
}
|
|
1460 |
|
|
1461 |
// hasCachedTempo is only valid if it is the current position
|
|
1462 |
boolean hasCachedTempo() {
|
|
1463 |
if (ignoreTempoEventAt != lastTick) {
|
|
1464 |
ignoreTempoEventAt = -1;
|
|
1465 |
}
|
|
1466 |
return ignoreTempoEventAt >= 0;
|
|
1467 |
}
|
|
1468 |
|
|
1469 |
// this method is also used internally in the pump!
|
|
1470 |
synchronized void setTempoMPQ(float tempoMPQ) {
|
|
1471 |
if (tempoMPQ > 0 && tempoMPQ != currTempo) {
|
|
1472 |
ignoreTempoEventAt = lastTick;
|
|
1473 |
this.currTempo = tempoMPQ;
|
|
1474 |
// re-calculate check point
|
|
1475 |
checkPointMillis = 0;
|
|
1476 |
}
|
|
1477 |
}
|
|
1478 |
|
|
1479 |
float getTempoMPQ() {
|
|
1480 |
return currTempo;
|
|
1481 |
}
|
|
1482 |
|
|
1483 |
synchronized void setTempoFactor(float factor) {
|
|
1484 |
if (factor > 0 && factor != this.tempoFactor) {
|
|
1485 |
tempoFactor = factor;
|
|
1486 |
inverseTempoFactor = 1.0f / factor;
|
|
1487 |
// re-calculate check point
|
|
1488 |
checkPointMillis = 0;
|
|
1489 |
}
|
|
1490 |
}
|
|
1491 |
|
|
1492 |
float getTempoFactor() {
|
|
1493 |
return tempoFactor;
|
|
1494 |
}
|
|
1495 |
|
|
1496 |
synchronized void muteSoloChanged() {
|
|
1497 |
boolean[] newDisabled = makeDisabledArray();
|
|
1498 |
if (running) {
|
|
1499 |
applyDisabledTracks(trackDisabled, newDisabled);
|
|
1500 |
}
|
|
1501 |
trackDisabled = newDisabled;
|
|
1502 |
}
|
|
1503 |
|
|
1504 |
|
|
1505 |
|
|
1506 |
synchronized void setSequence(Sequence seq) {
|
|
1507 |
if (seq == null) {
|
|
1508 |
init();
|
|
1509 |
return;
|
|
1510 |
}
|
|
1511 |
tracks = seq.getTracks();
|
|
1512 |
muteSoloChanged();
|
|
1513 |
resolution = seq.getResolution();
|
|
1514 |
divisionType = seq.getDivisionType();
|
|
1515 |
trackReadPos = new int[tracks.length];
|
|
1516 |
// trigger re-initialization
|
|
1517 |
checkPointMillis = 0;
|
|
1518 |
needReindex = true;
|
|
1519 |
}
|
|
1520 |
|
|
1521 |
synchronized void resetLoopCount() {
|
|
1522 |
currLoopCounter = loopCount;
|
|
1523 |
}
|
|
1524 |
|
|
1525 |
void clearNoteOnCache() {
|
|
1526 |
for (int i = 0; i < 128; i++) {
|
|
1527 |
noteOnCache[i] = 0;
|
|
1528 |
}
|
|
1529 |
}
|
|
1530 |
|
|
1531 |
void notesOff(boolean doControllers) {
|
|
1532 |
int done = 0;
|
|
1533 |
for (int ch=0; ch<16; ch++) {
|
|
1534 |
int channelMask = (1<<ch);
|
|
1535 |
for (int i=0; i<128; i++) {
|
|
1536 |
if ((noteOnCache[i] & channelMask) != 0) {
|
|
1537 |
noteOnCache[i] ^= channelMask;
|
|
1538 |
// send note on with velocity 0
|
|
1539 |
getTransmitterList().sendMessage((ShortMessage.NOTE_ON | ch) | (i<<8), -1);
|
|
1540 |
done++;
|
|
1541 |
}
|
|
1542 |
}
|
|
1543 |
/* all notes off */
|
|
1544 |
getTransmitterList().sendMessage((ShortMessage.CONTROL_CHANGE | ch) | (123<<8), -1);
|
|
1545 |
/* sustain off */
|
|
1546 |
getTransmitterList().sendMessage((ShortMessage.CONTROL_CHANGE | ch) | (64<<8), -1);
|
|
1547 |
if (doControllers) {
|
|
1548 |
/* reset all controllers */
|
|
1549 |
getTransmitterList().sendMessage((ShortMessage.CONTROL_CHANGE | ch) | (121<<8), -1);
|
|
1550 |
done++;
|
|
1551 |
}
|
|
1552 |
}
|
|
1553 |
if (DEBUG_PUMP) Printer.println(" noteOff: sent "+done+" messages.");
|
|
1554 |
}
|
|
1555 |
|
|
1556 |
|
|
1557 |
private boolean[] makeDisabledArray() {
|
|
1558 |
if (tracks == null) {
|
|
1559 |
return null;
|
|
1560 |
}
|
|
1561 |
boolean[] newTrackDisabled = new boolean[tracks.length];
|
|
1562 |
boolean[] solo;
|
|
1563 |
boolean[] mute;
|
|
1564 |
synchronized(RealTimeSequencer.this) {
|
|
1565 |
mute = trackMuted;
|
|
1566 |
solo = trackSolo;
|
|
1567 |
}
|
|
1568 |
// if one track is solo, then only play solo
|
|
1569 |
boolean hasSolo = false;
|
|
1570 |
if (solo != null) {
|
|
1571 |
for (int i = 0; i < solo.length; i++) {
|
|
1572 |
if (solo[i]) {
|
|
1573 |
hasSolo = true;
|
|
1574 |
break;
|
|
1575 |
}
|
|
1576 |
}
|
|
1577 |
}
|
|
1578 |
if (hasSolo) {
|
|
1579 |
// only the channels with solo play, regardless of mute
|
|
1580 |
for (int i = 0; i < newTrackDisabled.length; i++) {
|
|
1581 |
newTrackDisabled[i] = (i >= solo.length) || (!solo[i]);
|
|
1582 |
}
|
|
1583 |
} else {
|
|
1584 |
// mute the selected channels
|
|
1585 |
for (int i = 0; i < newTrackDisabled.length; i++) {
|
|
1586 |
newTrackDisabled[i] = (mute != null) && (i < mute.length) && (mute[i]);
|
|
1587 |
}
|
|
1588 |
}
|
|
1589 |
return newTrackDisabled;
|
|
1590 |
}
|
|
1591 |
|
|
1592 |
/**
|
|
1593 |
* chase all events from beginning of Track
|
|
1594 |
* and send note off for those events that are active
|
|
1595 |
* in noteOnCache array.
|
|
1596 |
* It is possible, of course, to catch notes from other tracks,
|
|
1597 |
* but better than more complicated logic to detect
|
|
1598 |
* which notes are really from this track
|
|
1599 |
*/
|
|
1600 |
private void sendNoteOffIfOn(Track track, long endTick) {
|
|
1601 |
int size = track.size();
|
|
1602 |
int done = 0;
|
|
1603 |
try {
|
|
1604 |
for (int i = 0; i < size; i++) {
|
|
1605 |
MidiEvent event = track.get(i);
|
|
1606 |
if (event.getTick() > endTick) break;
|
|
1607 |
MidiMessage msg = event.getMessage();
|
|
1608 |
int status = msg.getStatus();
|
|
1609 |
int len = msg.getLength();
|
|
1610 |
if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) {
|
|
1611 |
int note = -1;
|
|
1612 |
if (msg instanceof ShortMessage) {
|
|
1613 |
ShortMessage smsg = (ShortMessage) msg;
|
|
1614 |
if (smsg.getData2() > 0) {
|
|
1615 |
// only consider Note On with velocity > 0
|
|
1616 |
note = smsg.getData1();
|
|
1617 |
}
|
|
1618 |
} else {
|
|
1619 |
byte[] data = msg.getMessage();
|
|
1620 |
if ((data[2] & 0x7F) > 0) {
|
|
1621 |
// only consider Note On with velocity > 0
|
|
1622 |
note = data[1] & 0x7F;
|
|
1623 |
}
|
|
1624 |
}
|
|
1625 |
if (note >= 0) {
|
|
1626 |
int bit = 1<<(status & 0x0F);
|
|
1627 |
if ((noteOnCache[note] & bit) != 0) {
|
|
1628 |
// the bit is set. Send Note Off
|
|
1629 |
getTransmitterList().sendMessage(status | (note<<8), -1);
|
|
1630 |
// clear the bit
|
|
1631 |
noteOnCache[note] &= (0xFFFF ^ bit);
|
|
1632 |
done++;
|
|
1633 |
}
|
|
1634 |
}
|
|
1635 |
}
|
|
1636 |
}
|
|
1637 |
} catch (ArrayIndexOutOfBoundsException aioobe) {
|
|
1638 |
// this happens when messages are removed
|
|
1639 |
// from the track while this method executes
|
|
1640 |
}
|
|
1641 |
if (DEBUG_PUMP) Printer.println(" sendNoteOffIfOn: sent "+done+" messages.");
|
|
1642 |
}
|
|
1643 |
|
|
1644 |
|
|
1645 |
/**
|
|
1646 |
* Runtime application of mute/solo:
|
|
1647 |
* if a track is muted that was previously playing, send
|
|
1648 |
* note off events for all currently playing notes
|
|
1649 |
*/
|
|
1650 |
private void applyDisabledTracks(boolean[] oldDisabled, boolean[] newDisabled) {
|
|
1651 |
byte[][] tempArray = null;
|
|
1652 |
synchronized(RealTimeSequencer.this) {
|
|
1653 |
for (int i = 0; i < newDisabled.length; i++) {
|
|
1654 |
if (((oldDisabled == null)
|
|
1655 |
|| (i >= oldDisabled.length)
|
|
1656 |
|| !oldDisabled[i])
|
|
1657 |
&& newDisabled[i]) {
|
|
1658 |
// case that a track gets muted: need to
|
|
1659 |
// send appropriate note off events to prevent
|
|
1660 |
// hanging notes
|
|
1661 |
|
|
1662 |
if (tracks.length > i) {
|
|
1663 |
sendNoteOffIfOn(tracks[i], lastTick);
|
|
1664 |
}
|
|
1665 |
}
|
|
1666 |
else if ((oldDisabled != null)
|
|
1667 |
&& (i < oldDisabled.length)
|
|
1668 |
&& oldDisabled[i]
|
|
1669 |
&& !newDisabled[i]) {
|
|
1670 |
// case that a track was muted and is now unmuted
|
|
1671 |
// need to chase events and re-index this track
|
|
1672 |
if (tempArray == null) {
|
|
1673 |
tempArray = new byte[128][16];
|
|
1674 |
}
|
|
1675 |
chaseTrackEvents(i, 0, lastTick, true, tempArray);
|
|
1676 |
}
|
|
1677 |
}
|
|
1678 |
}
|
|
1679 |
}
|
|
1680 |
|
|
1681 |
/** go through all events from startTick to endTick
|
|
1682 |
* chase the controller state and program change state
|
|
1683 |
* and then set the end-states at once.
|
|
1684 |
*
|
|
1685 |
* needs to be called in synchronized state
|
|
1686 |
* @param tempArray an byte[128][16] to hold controller messages
|
|
1687 |
*/
|
|
1688 |
private void chaseTrackEvents(int trackNum,
|
|
1689 |
long startTick,
|
|
1690 |
long endTick,
|
|
1691 |
boolean doReindex,
|
|
1692 |
byte[][] tempArray) {
|
|
1693 |
if (startTick > endTick) {
|
|
1694 |
// start from the beginning
|
|
1695 |
startTick = 0;
|
|
1696 |
}
|
|
1697 |
byte[] progs = new byte[16];
|
|
1698 |
// init temp array with impossible values
|
|
1699 |
for (int ch = 0; ch < 16; ch++) {
|
|
1700 |
progs[ch] = -1;
|
|
1701 |
for (int co = 0; co < 128; co++) {
|
|
1702 |
tempArray[co][ch] = -1;
|
|
1703 |
}
|
|
1704 |
}
|
|
1705 |
Track track = tracks[trackNum];
|
|
1706 |
int size = track.size();
|
|
1707 |
try {
|
|
1708 |
for (int i = 0; i < size; i++) {
|
|
1709 |
MidiEvent event = track.get(i);
|
|
1710 |
if (event.getTick() >= endTick) {
|
|
1711 |
if (doReindex && (trackNum < trackReadPos.length)) {
|
|
1712 |
trackReadPos[trackNum] = (i > 0)?(i-1):0;
|
|
1713 |
if (DEBUG_PUMP) Printer.println(" chaseEvents: setting trackReadPos["+trackNum+"] = "+trackReadPos[trackNum]);
|
|
1714 |
}
|
|
1715 |
break;
|
|
1716 |
}
|
|
1717 |
MidiMessage msg = event.getMessage();
|
|
1718 |
int status = msg.getStatus();
|
|
1719 |
int len = msg.getLength();
|
|
1720 |
if (len == 3 && ((status & 0xF0) == ShortMessage.CONTROL_CHANGE)) {
|
|
1721 |
if (msg instanceof ShortMessage) {
|
|
1722 |
ShortMessage smsg = (ShortMessage) msg;
|
|
1723 |
tempArray[smsg.getData1() & 0x7F][status & 0x0F] = (byte) smsg.getData2();
|
|
1724 |
} else {
|
|
1725 |
byte[] data = msg.getMessage();
|
|
1726 |
tempArray[data[1] & 0x7F][status & 0x0F] = data[2];
|
|
1727 |
}
|
|
1728 |
}
|
|
1729 |
if (len == 2 && ((status & 0xF0) == ShortMessage.PROGRAM_CHANGE)) {
|
|
1730 |
if (msg instanceof ShortMessage) {
|
|
1731 |
ShortMessage smsg = (ShortMessage) msg;
|
|
1732 |
progs[status & 0x0F] = (byte) smsg.getData1();
|
|
1733 |
} else {
|
|
1734 |
byte[] data = msg.getMessage();
|
|
1735 |
progs[status & 0x0F] = data[1];
|
|
1736 |
}
|
|
1737 |
}
|
|
1738 |
}
|
|
1739 |
} catch (ArrayIndexOutOfBoundsException aioobe) {
|
|
1740 |
// this happens when messages are removed
|
|
1741 |
// from the track while this method executes
|
|
1742 |
}
|
|
1743 |
int numControllersSent = 0;
|
|
1744 |
// now send out the aggregated controllers and program changes
|
|
1745 |
for (int ch = 0; ch < 16; ch++) {
|
|
1746 |
for (int co = 0; co < 128; co++) {
|
|
1747 |
byte controllerValue = tempArray[co][ch];
|
|
1748 |
if (controllerValue >= 0) {
|
|
1749 |
int packedMsg = (ShortMessage.CONTROL_CHANGE | ch) | (co<<8) | (controllerValue<<16);
|
|
1750 |
getTransmitterList().sendMessage(packedMsg, -1);
|
|
1751 |
numControllersSent++;
|
|
1752 |
}
|
|
1753 |
}
|
|
1754 |
// send program change *after* controllers, to
|
|
1755 |
// correctly initialize banks
|
|
1756 |
if (progs[ch] >= 0) {
|
|
1757 |
getTransmitterList().sendMessage((ShortMessage.PROGRAM_CHANGE | ch) | (progs[ch]<<8), -1);
|
|
1758 |
}
|
|
1759 |
if (progs[ch] >= 0 || startTick == 0 || endTick == 0) {
|
|
1760 |
// reset pitch bend on this channel (E0 00 40)
|
|
1761 |
getTransmitterList().sendMessage((ShortMessage.PITCH_BEND | ch) | (0x40 << 16), -1);
|
|
1762 |
// reset sustain pedal on this channel
|
|
1763 |
getTransmitterList().sendMessage((ShortMessage.CONTROL_CHANGE | ch) | (64 << 8), -1);
|
|
1764 |
}
|
|
1765 |
}
|
|
1766 |
if (DEBUG_PUMP) Printer.println(" chaseTrackEvents track "+trackNum+": sent "+numControllersSent+" controllers.");
|
|
1767 |
}
|
|
1768 |
|
|
1769 |
|
|
1770 |
/** chase controllers and program for all tracks */
|
|
1771 |
synchronized void chaseEvents(long startTick, long endTick) {
|
|
1772 |
if (DEBUG_PUMP) Printer.println(">> chaseEvents from tick "+startTick+".."+(endTick-1));
|
|
1773 |
byte[][] tempArray = new byte[128][16];
|
|
1774 |
for (int t = 0; t < tracks.length; t++) {
|
|
1775 |
if ((trackDisabled == null)
|
|
1776 |
|| (trackDisabled.length <= t)
|
|
1777 |
|| (!trackDisabled[t])) {
|
|
1778 |
// if track is not disabled, chase the events for it
|
|
1779 |
chaseTrackEvents(t, startTick, endTick, true, tempArray);
|
|
1780 |
}
|
|
1781 |
}
|
|
1782 |
if (DEBUG_PUMP) Printer.println("<< chaseEvents");
|
|
1783 |
}
|
|
1784 |
|
|
1785 |
|
|
1786 |
// playback related methods (pumping)
|
|
1787 |
|
|
1788 |
private long getCurrentTimeMillis() {
|
|
1789 |
return System.nanoTime() / 1000000l;
|
|
1790 |
//return perf.highResCounter() * 1000 / perfFreq;
|
|
1791 |
}
|
|
1792 |
|
|
1793 |
private long millis2tick(long millis) {
|
|
1794 |
if (divisionType != Sequence.PPQ) {
|
|
1795 |
double dTick = ((((double) millis) * tempoFactor)
|
|
1796 |
* ((double) divisionType)
|
|
1797 |
* ((double) resolution))
|
|
1798 |
/ ((double) 1000);
|
|
1799 |
return (long) dTick;
|
|
1800 |
}
|
|
1801 |
return MidiUtils.microsec2ticks(millis * 1000,
|
|
1802 |
currTempo * inverseTempoFactor,
|
|
1803 |
resolution);
|
|
1804 |
}
|
|
1805 |
|
|
1806 |
private long tick2millis(long tick) {
|
|
1807 |
if (divisionType != Sequence.PPQ) {
|
|
1808 |
double dMillis = ((((double) tick) * 1000) /
|
|
1809 |
(tempoFactor * ((double) divisionType) * ((double) resolution)));
|
|
1810 |
return (long) dMillis;
|
|
1811 |
}
|
|
1812 |
return MidiUtils.ticks2microsec(tick,
|
|
1813 |
currTempo * inverseTempoFactor,
|
|
1814 |
resolution) / 1000;
|
|
1815 |
}
|
|
1816 |
|
|
1817 |
private void ReindexTrack(int trackNum, long tick) {
|
|
1818 |
if (trackNum < trackReadPos.length && trackNum < tracks.length) {
|
|
1819 |
trackReadPos[trackNum] = MidiUtils.tick2index(tracks[trackNum], tick);
|
|
1820 |
if (DEBUG_PUMP) Printer.println(" reindexTrack: setting trackReadPos["+trackNum+"] = "+trackReadPos[trackNum]);
|
|
1821 |
}
|
|
1822 |
}
|
|
1823 |
|
|
1824 |
/* returns if changes are pending */
|
|
1825 |
private boolean dispatchMessage(int trackNum, MidiEvent event) {
|
|
1826 |
boolean changesPending = false;
|
|
1827 |
MidiMessage message = event.getMessage();
|
|
1828 |
int msgStatus = message.getStatus();
|
|
1829 |
int msgLen = message.getLength();
|
|
1830 |
if (msgStatus == MetaMessage.META && msgLen >= 2) {
|
|
1831 |
// a meta message. Do not send it to the device.
|
|
1832 |
// 0xFF with length=1 is a MIDI realtime message
|
|
1833 |
// which shouldn't be in a Sequence, but we play it
|
|
1834 |
// nonetheless.
|
|
1835 |
|
|
1836 |
// see if this is a tempo message. Only on track 0.
|
|
1837 |
if (trackNum == 0) {
|
|
1838 |
int newTempo = MidiUtils.getTempoMPQ(message);
|
|
1839 |
if (newTempo > 0) {
|
|
1840 |
if (event.getTick() != ignoreTempoEventAt) {
|
|
1841 |
setTempoMPQ(newTempo); // sets ignoreTempoEventAt!
|
|
1842 |
changesPending = true;
|
|
1843 |
}
|
|
1844 |
// next loop, do not ignore anymore tempo events.
|
|
1845 |
ignoreTempoEventAt = -1;
|
|
1846 |
}
|
|
1847 |
}
|
|
1848 |
// send to listeners
|
|
1849 |
sendMetaEvents(message);
|
|
1850 |
|
|
1851 |
} else {
|
|
1852 |
// not meta, send to device
|
|
1853 |
getTransmitterList().sendMessage(message, -1);
|
|
1854 |
|
|
1855 |
switch (msgStatus & 0xF0) {
|
|
1856 |
case ShortMessage.NOTE_OFF: {
|
|
1857 |
// note off - clear the bit in the noteOnCache array
|
|
1858 |
int note = ((ShortMessage) message).getData1() & 0x7F;
|
|
1859 |
noteOnCache[note] &= (0xFFFF ^ (1<<(msgStatus & 0x0F)));
|
|
1860 |
break;
|
|
1861 |
}
|
|
1862 |
|
|
1863 |
case ShortMessage.NOTE_ON: {
|
|
1864 |
// note on
|
|
1865 |
ShortMessage smsg = (ShortMessage) message;
|
|
1866 |
int note = smsg.getData1() & 0x7F;
|
|
1867 |
int vel = smsg.getData2() & 0x7F;
|
|
1868 |
if (vel > 0) {
|
|
1869 |
// if velocity > 0 set the bit in the noteOnCache array
|
|
1870 |
noteOnCache[note] |= 1<<(msgStatus & 0x0F);
|
|
1871 |
} else {
|
|
1872 |
// if velocity = 0 clear the bit in the noteOnCache array
|
|
1873 |
noteOnCache[note] &= (0xFFFF ^ (1<<(msgStatus & 0x0F)));
|
|
1874 |
}
|
|
1875 |
break;
|
|
1876 |
}
|
|
1877 |
|
|
1878 |
case ShortMessage.CONTROL_CHANGE:
|
|
1879 |
// if controller message, send controller listeners
|
|
1880 |
sendControllerEvents(message);
|
|
1881 |
break;
|
|
1882 |
|
|
1883 |
}
|
|
1884 |
}
|
|
1885 |
return changesPending;
|
|
1886 |
}
|
|
1887 |
|
|
1888 |
|
|
1889 |
/** the main pump method
|
|
1890 |
* @return true if end of sequence is reached
|
|
1891 |
*/
|
|
1892 |
synchronized boolean pump() {
|
|
1893 |
long currMillis;
|
|
1894 |
long targetTick = lastTick;
|
|
1895 |
MidiEvent currEvent;
|
|
1896 |
boolean changesPending = false;
|
|
1897 |
boolean doLoop = false;
|
|
1898 |
boolean EOM = false;
|
|
1899 |
|
|
1900 |
currMillis = getCurrentTimeMillis();
|
|
1901 |
int finishedTracks = 0;
|
|
1902 |
do {
|
|
1903 |
changesPending = false;
|
|
1904 |
|
|
1905 |
// need to re-find indexes in tracks?
|
|
1906 |
if (needReindex) {
|
|
1907 |
if (DEBUG_PUMP) Printer.println("Need to re-index at "+currMillis+" millis. TargetTick="+targetTick);
|
|
1908 |
if (trackReadPos.length < tracks.length) {
|
|
1909 |
trackReadPos = new int[tracks.length];
|
|
1910 |
}
|
|
1911 |
for (int t = 0; t < tracks.length; t++) {
|
|
1912 |
ReindexTrack(t, targetTick);
|
|
1913 |
if (DEBUG_PUMP_ALL) Printer.println(" Setting trackReadPos["+t+"]="+trackReadPos[t]);
|
|
1914 |
}
|
|
1915 |
needReindex = false;
|
|
1916 |
checkPointMillis = 0;
|
|
1917 |
}
|
|
1918 |
|
|
1919 |
// get target tick from current time in millis
|
|
1920 |
if (checkPointMillis == 0) {
|
|
1921 |
// new check point
|
|
1922 |
currMillis = getCurrentTimeMillis();
|
|
1923 |
checkPointMillis = currMillis;
|
|
1924 |
targetTick = lastTick;
|
|
1925 |
checkPointTick = targetTick;
|
|
1926 |
if (DEBUG_PUMP) Printer.println("New checkpoint to "+currMillis+" millis. "
|
|
1927 |
+"TargetTick="+targetTick
|
|
1928 |
+" new tempo="+MidiUtils.convertTempo(currTempo)+"bpm");
|
|
1929 |
} else {
|
|
1930 |
// calculate current tick based on current time in milliseconds
|
|
1931 |
targetTick = checkPointTick + millis2tick(currMillis - checkPointMillis);
|
|
1932 |
if (DEBUG_PUMP_ALL) Printer.println("targetTick = "+targetTick+" at "+currMillis+" millis");
|
|
1933 |
if ((loopEnd != -1)
|
|
1934 |
&& ((loopCount > 0 && currLoopCounter > 0)
|
|
1935 |
|| (loopCount == LOOP_CONTINUOUSLY))) {
|
|
1936 |
if (lastTick <= loopEnd && targetTick >= loopEnd) {
|
|
1937 |
// need to loop!
|
|
1938 |
// only play until loop end
|
|
1939 |
targetTick = loopEnd - 1;
|
|
1940 |
doLoop = true;
|
|
1941 |
if (DEBUG_PUMP) Printer.println("set doLoop to true. lastTick="+lastTick
|
|
1942 |
+" targetTick="+targetTick
|
|
1943 |
+" loopEnd="+loopEnd
|
|
1944 |
+" jumping to loopStart="+loopStart
|
|
1945 |
+" new currLoopCounter="+currLoopCounter);
|
|
1946 |
if (DEBUG_PUMP) Printer.println(" currMillis="+currMillis
|
|
1947 |
+" checkPointMillis="+checkPointMillis
|
|
1948 |
+" checkPointTick="+checkPointTick);
|
|
1949 |
|
|
1950 |
}
|
|
1951 |
}
|
|
1952 |
lastTick = targetTick;
|
|
1953 |
}
|
|
1954 |
|
|
1955 |
finishedTracks = 0;
|
|
1956 |
|
|
1957 |
for (int t = 0; t < tracks.length; t++) {
|
|
1958 |
try {
|
|
1959 |
boolean disabled = trackDisabled[t];
|
|
1960 |
Track thisTrack = tracks[t];
|
|
1961 |
int readPos = trackReadPos[t];
|
|
1962 |
int size = thisTrack.size();
|
|
1963 |
// play all events that are due until targetTick
|
|
1964 |
while (!changesPending && (readPos < size)
|
|
1965 |
&& (currEvent = thisTrack.get(readPos)).getTick() <= targetTick) {
|
|
1966 |
|
|
1967 |
if ((readPos == size -1) && MidiUtils.isMetaEndOfTrack(currEvent.getMessage())) {
|
|
1968 |
// do not send out this message. Finished with this track
|
|
1969 |
readPos = size;
|
|
1970 |
break;
|
|
1971 |
}
|
|
1972 |
// TODO: some kind of heuristics if the MIDI messages have changed
|
|
1973 |
// significantly (i.e. deleted or inserted a bunch of messages)
|
|
1974 |
// since last time. Would need to set needReindex = true then
|
|
1975 |
readPos++;
|
|
1976 |
// only play this event if the track is enabled,
|
|
1977 |
// or if it is a tempo message on track 0
|
|
1978 |
// Note: cannot put this check outside
|
|
1979 |
// this inner loop in order to detect end of file
|
|
1980 |
if (!disabled ||
|
|
1981 |
((t == 0) && (MidiUtils.isMetaTempo(currEvent.getMessage())))) {
|
|
1982 |
changesPending = dispatchMessage(t, currEvent);
|
|
1983 |
}
|
|
1984 |
}
|
|
1985 |
if (readPos >= size) {
|
|
1986 |
finishedTracks++;
|
|
1987 |
}
|
|
1988 |
if (DEBUG_PUMP_ALL) {
|
|
1989 |
System.out.print(" pumped track "+t+" ("+size+" events) "
|
|
1990 |
+" from index: "+trackReadPos[t]
|
|
1991 |
+" to "+(readPos-1));
|
|
1992 |
System.out.print(" -> ticks: ");
|
|
1993 |
if (trackReadPos[t] < size) {
|
|
1994 |
System.out.print(""+(thisTrack.get(trackReadPos[t]).getTick()));
|
|
1995 |
} else {
|
|
1996 |
System.out.print("EOT");
|
|
1997 |
}
|
|
1998 |
System.out.print(" to ");
|
|
1999 |
if (readPos < size) {
|
|
2000 |
System.out.print(""+(thisTrack.get(readPos-1).getTick()));
|
|
2001 |
} else {
|
|
2002 |
System.out.print("EOT");
|
|
2003 |
}
|
|
2004 |
System.out.println();
|
|
2005 |
}
|
|
2006 |
trackReadPos[t] = readPos;
|
|
2007 |
} catch(Exception e) {
|
|
2008 |
if (Printer.debug) Printer.debug("Exception in Sequencer pump!");
|
|
2009 |
if (Printer.debug) e.printStackTrace();
|
|
2010 |
if (e instanceof ArrayIndexOutOfBoundsException) {
|
|
2011 |
needReindex = true;
|
|
2012 |
changesPending = true;
|
|
2013 |
}
|
|
2014 |
}
|
|
2015 |
if (changesPending) {
|
|
2016 |
break;
|
|
2017 |
}
|
|
2018 |
}
|
|
2019 |
EOM = (finishedTracks == tracks.length);
|
|
2020 |
if (doLoop
|
|
2021 |
|| ( ((loopCount > 0 && currLoopCounter > 0)
|
|
2022 |
|| (loopCount == LOOP_CONTINUOUSLY))
|
|
2023 |
&& !changesPending
|
|
2024 |
&& (loopEnd == -1)
|
|
2025 |
&& EOM)) {
|
|
2026 |
|
|
2027 |
long oldCheckPointMillis = checkPointMillis;
|
|
2028 |
long loopEndTick = loopEnd;
|
|
2029 |
if (loopEndTick == -1) {
|
|
2030 |
loopEndTick = lastTick;
|
|
2031 |
}
|
|
2032 |
|
|
2033 |
// need to loop back!
|
|
2034 |
if (loopCount != LOOP_CONTINUOUSLY) {
|
|
2035 |
currLoopCounter--;
|
|
2036 |
}
|
|
2037 |
if (DEBUG_PUMP) Printer.println("Execute loop: lastTick="+lastTick
|
|
2038 |
+" loopEnd="+loopEnd
|
|
2039 |
+" jumping to loopStart="+loopStart
|
|
2040 |
+" new currLoopCounter="+currLoopCounter);
|
|
2041 |
setTickPos(loopStart);
|
|
2042 |
// now patch the checkPointMillis so that
|
|
2043 |
// it points to the exact beginning of when the loop was finished
|
|
2044 |
|
|
2045 |
// $$fb TODO: although this is mathematically correct (i.e. the loop position
|
|
2046 |
// is correct, and doesn't drift away with several repetition,
|
|
2047 |
// there is a slight lag when looping back, probably caused
|
|
2048 |
// by the chasing.
|
|
2049 |
|
|
2050 |
checkPointMillis = oldCheckPointMillis + tick2millis(loopEndTick - checkPointTick);
|
|
2051 |
checkPointTick = loopStart;
|
|
2052 |
if (DEBUG_PUMP) Printer.println(" Setting currMillis="+currMillis
|
|
2053 |
+" new checkPointMillis="+checkPointMillis
|
|
2054 |
+" new checkPointTick="+checkPointTick);
|
|
2055 |
// no need for reindexing, is done in setTickPos
|
|
2056 |
needReindex = false;
|
|
2057 |
changesPending = false;
|
|
2058 |
// reset doLoop flag
|
|
2059 |
doLoop = false;
|
|
2060 |
EOM = false;
|
|
2061 |
}
|
|
2062 |
} while (changesPending);
|
|
2063 |
|
|
2064 |
return EOM;
|
|
2065 |
}
|
|
2066 |
|
|
2067 |
} // class DataPump
|
|
2068 |
|
|
2069 |
}
|