~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar-jsa/src/org/herac/tuxguitar/player/impl/jsa/sequencer/MidiSequencerImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.player.impl.jsa.sequencer;
 
2
 
 
3
import javax.sound.midi.Sequence;
 
4
import javax.sound.midi.Sequencer;
 
5
import javax.sound.midi.Track;
 
6
import javax.sound.midi.Transmitter;
 
7
 
 
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;
 
12
 
 
13
public class MidiSequencerImpl implements MidiSequencer,MidiSequenceLoader{
 
14
        
 
15
        private static final int TICK_MOVE = 1;
 
16
        
 
17
        private Sequencer sequencer;
 
18
        private Transmitter transmitter;
 
19
        private MidiPort midiPort;
 
20
        
 
21
        public MidiSequencerImpl(Sequencer sequencer){
 
22
                this.sequencer = sequencer;
 
23
        }
 
24
        
 
25
        public synchronized void open() {
 
26
                try {
 
27
                        if(!this.sequencer.isOpen()){
 
28
                                this.sequencer.open();
 
29
                                this.closeTransmitter();
 
30
                                this.openTransmitter();
 
31
                        }
 
32
                } catch (Throwable throwable) {
 
33
                        throwable.printStackTrace();
 
34
                }
 
35
        }
 
36
        
 
37
        public synchronized void close() {
 
38
                try {
 
39
                        if(this.sequencer.isOpen()){
 
40
                                this.sequencer.close();
 
41
                                this.closeTransmitter();
 
42
                        }
 
43
                } catch (Throwable throwable) {
 
44
                        throwable.printStackTrace();
 
45
                }
 
46
        }
 
47
        
 
48
        public void openTransmitter(){
 
49
                try {
 
50
                        this.transmitter = getSequencer().getTransmitter();
 
51
                        this.transmitter.setReceiver( new MidiReceiverImpl(this) );
 
52
                } catch (Throwable throwable) {
 
53
                        throwable.printStackTrace();
 
54
                }
 
55
        }
 
56
        
 
57
        public void closeTransmitter(){
 
58
                try {
 
59
                        if(this.transmitter != null){
 
60
                                this.transmitter.close();
 
61
                                this.transmitter = null;
 
62
                        }
 
63
                } catch (Throwable throwable) {
 
64
                        throwable.printStackTrace();
 
65
                }
 
66
        }
 
67
        
 
68
        protected Sequencer getSequencer() {
 
69
                this.open();
 
70
                return this.sequencer;
 
71
        }
 
72
        
 
73
        public MidiSequenceHandler createSequence(int tracks) {
 
74
                this.resetTracks();
 
75
                return new MidiSequenceHandlerImpl(this,tracks);
 
76
        }
 
77
        
 
78
        public MidiPort getMidiPort() {
 
79
                if(this.midiPort == null){
 
80
                        this.midiPort = new MidiPortEmpty();
 
81
                }
 
82
                return this.midiPort;
 
83
        }
 
84
        
 
85
        public void setMidiPort(MidiPort port) {
 
86
                this.midiPort = port;
 
87
        }
 
88
        
 
89
        public long getTickLength() {
 
90
                try {
 
91
                        return getSequencer().getTickLength();
 
92
                } catch (Throwable throwable) {
 
93
                        throwable.printStackTrace();
 
94
                }
 
95
                return 0;
 
96
        }
 
97
        
 
98
        public long getTickPosition() {
 
99
                try {
 
100
                        return (getSequencer().getTickPosition() + TICK_MOVE);
 
101
                } catch (Throwable throwable) {
 
102
                        throwable.printStackTrace();
 
103
                }
 
104
                return 0;
 
105
        }
 
106
        
 
107
        public void setTickPosition(long tickPosition) {
 
108
                try {
 
109
                        this.getSequencer().setTickPosition(tickPosition - TICK_MOVE);
 
110
                        this.reset( false );
 
111
                } catch (Throwable throwable) {
 
112
                        throwable.printStackTrace();
 
113
                }
 
114
        }
 
115
        
 
116
        public boolean isRunning() {
 
117
                try {
 
118
                        return getSequencer().isRunning();
 
119
                } catch (Throwable throwable) {
 
120
                        throwable.printStackTrace();
 
121
                }
 
122
                return false;
 
123
        }
 
124
        
 
125
        public void setMute(int index, boolean mute) {
 
126
                try {
 
127
                        getSequencer().setTrackMute(index, mute);
 
128
                } catch (Throwable throwable) {
 
129
                        throwable.printStackTrace();
 
130
                }
 
131
        }
 
132
        
 
133
        public void setSolo(int index, boolean solo) {
 
134
                try {
 
135
                        getSequencer().setTrackSolo(index, solo);
 
136
                } catch (Throwable throwable) {
 
137
                        throwable.printStackTrace();
 
138
                }
 
139
        }
 
140
        
 
141
        public void setSequence(Sequence sequence){
 
142
                try {
 
143
                        getSequencer().setSequence(sequence);
 
144
                } catch (Throwable throwable) {
 
145
                        throwable.printStackTrace();
 
146
                }
 
147
        }
 
148
        
 
149
        public void start() {
 
150
                try {
 
151
                        getSequencer().start();
 
152
                } catch (Throwable throwable) {
 
153
                        throwable.printStackTrace();
 
154
                }
 
155
        }
 
156
        
 
157
        public void stop() {
 
158
                try {
 
159
                        this.getSequencer().stop();
 
160
                        this.reset( true );
 
161
                } catch (Throwable throwable) {
 
162
                        throwable.printStackTrace();
 
163
                }
 
164
        }
 
165
        
 
166
        public void reset(boolean systemReset){
 
167
                try {
 
168
                        this.getMidiPort().out().sendAllNotesOff();
 
169
                        for(int channel = 0; channel < 16;channel ++){
 
170
                                this.getMidiPort().out().sendPitchBend(channel, 64);
 
171
                        }
 
172
                        if( systemReset ){
 
173
                                this.getMidiPort().out().sendSystemReset();
 
174
                        }
 
175
                } catch (Throwable throwable) {
 
176
                        throwable.printStackTrace();
 
177
                }
 
178
        }
 
179
        
 
180
        public void resetTracks(){
 
181
                try {
 
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 );
 
190
                                        }
 
191
                                }
 
192
                        }
 
193
                } catch (Throwable throwable) {
 
194
                        throwable.printStackTrace();
 
195
                }
 
196
        }
 
197
        
 
198
        public String getKey() {
 
199
                return this.sequencer.getDeviceInfo().getName();
 
200
        }
 
201
        
 
202
        public String getName() {
 
203
                return this.sequencer.getDeviceInfo().getName();
 
204
        }
 
205
}