1
package org.herac.tuxguitar.player.impl.jsa.sequencer;
3
import javax.sound.midi.Sequence;
4
import javax.sound.midi.Sequencer;
5
import javax.sound.midi.Track;
6
import javax.sound.midi.Transmitter;
8
import org.herac.tuxguitar.player.base.MidiPort;
9
import org.herac.tuxguitar.player.base.MidiPortEmpty;
10
import org.herac.tuxguitar.player.base.MidiSequenceHandler;
11
import org.herac.tuxguitar.player.base.MidiSequencer;
13
public class MidiSequencerImpl implements MidiSequencer,MidiSequenceLoader{
15
private static final int TICK_MOVE = 1;
17
private Sequencer sequencer;
18
private Transmitter transmitter;
19
private MidiPort midiPort;
21
public MidiSequencerImpl(Sequencer sequencer){
22
this.sequencer = sequencer;
25
public synchronized void open() {
27
if(!this.sequencer.isOpen()){
28
this.sequencer.open();
29
this.closeTransmitter();
30
this.openTransmitter();
32
} catch (Throwable throwable) {
33
throwable.printStackTrace();
37
public synchronized void close() {
39
if(this.sequencer.isOpen()){
40
this.sequencer.close();
41
this.closeTransmitter();
43
} catch (Throwable throwable) {
44
throwable.printStackTrace();
48
public void openTransmitter(){
50
this.transmitter = getSequencer().getTransmitter();
51
this.transmitter.setReceiver( new MidiReceiverImpl(this) );
52
} catch (Throwable throwable) {
53
throwable.printStackTrace();
57
public void closeTransmitter(){
59
if(this.transmitter != null){
60
this.transmitter.close();
61
this.transmitter = null;
63
} catch (Throwable throwable) {
64
throwable.printStackTrace();
68
protected Sequencer getSequencer() {
70
return this.sequencer;
73
public MidiSequenceHandler createSequence(int tracks) {
75
return new MidiSequenceHandlerImpl(this,tracks);
78
public MidiPort getMidiPort() {
79
if(this.midiPort == null){
80
this.midiPort = new MidiPortEmpty();
85
public void setMidiPort(MidiPort port) {
89
public long getTickLength() {
91
return getSequencer().getTickLength();
92
} catch (Throwable throwable) {
93
throwable.printStackTrace();
98
public long getTickPosition() {
100
return (getSequencer().getTickPosition() + TICK_MOVE);
101
} catch (Throwable throwable) {
102
throwable.printStackTrace();
107
public void setTickPosition(long tickPosition) {
109
this.getSequencer().setTickPosition(tickPosition - TICK_MOVE);
111
} catch (Throwable throwable) {
112
throwable.printStackTrace();
116
public boolean isRunning() {
118
return getSequencer().isRunning();
119
} catch (Throwable throwable) {
120
throwable.printStackTrace();
125
public void setMute(int index, boolean mute) {
127
getSequencer().setTrackMute(index, mute);
128
} catch (Throwable throwable) {
129
throwable.printStackTrace();
133
public void setSolo(int index, boolean solo) {
135
getSequencer().setTrackSolo(index, solo);
136
} catch (Throwable throwable) {
137
throwable.printStackTrace();
141
public void setSequence(Sequence sequence){
143
getSequencer().setSequence(sequence);
144
} catch (Throwable throwable) {
145
throwable.printStackTrace();
149
public void start() {
151
getSequencer().start();
152
} catch (Throwable throwable) {
153
throwable.printStackTrace();
159
this.getSequencer().stop();
161
} catch (Throwable throwable) {
162
throwable.printStackTrace();
166
public void reset(boolean systemReset){
168
this.getMidiPort().out().sendAllNotesOff();
169
for(int channel = 0; channel < 16;channel ++){
170
this.getMidiPort().out().sendPitchBend(channel, 64);
173
this.getMidiPort().out().sendSystemReset();
175
} catch (Throwable throwable) {
176
throwable.printStackTrace();
180
public void resetTracks(){
182
Sequence sequence = this.getSequencer().getSequence();
183
if(sequence != null){
184
Track[] tracks = sequence.getTracks();
185
if( tracks != null ){
186
int count = tracks.length;
187
for( int i = 0 ; i < count; i++ ){
188
this.setSolo( i , false );
189
this.setMute( i , false );
193
} catch (Throwable throwable) {
194
throwable.printStackTrace();
198
public String getKey() {
199
return this.sequencer.getDeviceInfo().getName();
202
public String getName() {
203
return this.sequencer.getDeviceInfo().getName();