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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/player/impl/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.sequencer;
 
2
 
 
3
import org.herac.tuxguitar.player.base.MidiPlayerException;
 
4
import org.herac.tuxguitar.player.base.MidiPort;
 
5
import org.herac.tuxguitar.player.base.MidiPortEmpty;
 
6
import org.herac.tuxguitar.player.base.MidiSequenceHandler;
 
7
import org.herac.tuxguitar.player.base.MidiSequencer;
 
8
 
 
9
public class MidiSequencerImpl implements MidiSequencer{
 
10
        
 
11
        private boolean reset;
 
12
        private boolean running;
 
13
        private MidiPort midiPort;
 
14
        private MidiTickPlayer midiTickPlayer;
 
15
        private MidiEventPlayer midiEventPlayer;
 
16
        private MidiEventDispacher midiEventDispacher;
 
17
        private MidiTrackController midiTrackController;
 
18
        
 
19
        public MidiSequencerImpl(){
 
20
                this.running = false;
 
21
                this.midiTickPlayer = new MidiTickPlayer();
 
22
                this.midiEventPlayer = new MidiEventPlayer(this);
 
23
                this.midiEventDispacher = new MidiEventDispacher(this);
 
24
                this.midiTrackController = new MidiTrackController(this);
 
25
        }
 
26
        
 
27
        public synchronized MidiTrackController getMidiTrackController(){
 
28
                return this.midiTrackController;
 
29
        }
 
30
        
 
31
        public synchronized void setTempo(int tempo){
 
32
                this.midiTickPlayer.setTempo(tempo);
 
33
        }
 
34
        
 
35
        public synchronized long getTickPosition(){
 
36
                return this.midiTickPlayer.getTick();
 
37
        }
 
38
        
 
39
        public synchronized void setTickPosition(long tickPosition){
 
40
                this.reset = true;
 
41
                this.midiTickPlayer.setTick(tickPosition);
 
42
        }
 
43
        
 
44
        public synchronized long getTickLength(){
 
45
                return this.midiTickPlayer.getTickLength();
 
46
        }
 
47
        
 
48
        public synchronized void sendEvent(MidiEvent event) throws MidiPlayerException{
 
49
                if(!this.reset){
 
50
                        this.midiEventDispacher.dispatch(event);
 
51
                }
 
52
        }
 
53
        
 
54
        public synchronized void addEvent(MidiEvent event){
 
55
                this.midiEventPlayer.addEvent(event);
 
56
                this.midiTickPlayer.notifyTick(event.getTick());
 
57
        }
 
58
        
 
59
        public synchronized boolean isRunning() {
 
60
                return this.running;
 
61
        }
 
62
        
 
63
        public synchronized void setRunning(boolean running) {
 
64
                this.running = running;
 
65
                if(this.running){
 
66
                        this.setTempo(120);
 
67
                        this.setTickPosition( this.getTickPosition() );
 
68
                        new MidiTimer(this).start();
 
69
                }
 
70
        }
 
71
        
 
72
        public synchronized void stop() throws MidiPlayerException{
 
73
                this.setRunning(false);
 
74
        }
 
75
        
 
76
        public synchronized void start() throws MidiPlayerException{
 
77
                this.setRunning(true);
 
78
        }
 
79
        
 
80
        public synchronized void reset(boolean systemReset)  throws MidiPlayerException{
 
81
                this.getMidiPort().out().sendAllNotesOff();
 
82
                for(int channel = 0; channel < 16;channel ++){
 
83
                        this.getMidiPort().out().sendPitchBend(channel, 64);
 
84
                }
 
85
                if( systemReset ){
 
86
                        this.getMidiPort().out().sendSystemReset();
 
87
                }
 
88
        }
 
89
        
 
90
        protected synchronized boolean process() throws MidiPlayerException{
 
91
                boolean running = this.isRunning();
 
92
                if(running){
 
93
                        if(this.reset){
 
94
                                this.reset( false );
 
95
                                this.reset = false;
 
96
                                this.midiEventPlayer.reset();
 
97
                        }
 
98
                        this.midiTickPlayer.process();
 
99
                        this.midiEventPlayer.process();
 
100
                        if(this.getTickPosition() > this.getTickLength()){
 
101
                                this.stop();
 
102
                        }
 
103
                }
 
104
                else{
 
105
                        this.midiEventPlayer.clearEvents();
 
106
                        this.midiTickPlayer.clearTick();
 
107
                        this.reset( true );
 
108
                }
 
109
                return running;
 
110
        }
 
111
        
 
112
        public synchronized MidiPort getMidiPort() {
 
113
                if(this.midiPort == null){
 
114
                        this.midiPort = new MidiPortEmpty();
 
115
                }
 
116
                return this.midiPort;
 
117
        }
 
118
        
 
119
        public synchronized void setMidiPort(MidiPort midiPort) {
 
120
                this.midiPort = midiPort;
 
121
        }
 
122
        
 
123
        public synchronized void open() {
 
124
                //not implemented
 
125
        }
 
126
        
 
127
        public synchronized void close() throws MidiPlayerException {
 
128
                if(isRunning()){
 
129
                        this.stop();
 
130
                }
 
131
        }
 
132
        
 
133
        public synchronized MidiSequenceHandler createSequence(int tracks){
 
134
                return new MidiSequenceHandlerImpl(this,tracks);
 
135
        }
 
136
        
 
137
        public synchronized void setSolo(int index,boolean solo) throws MidiPlayerException{
 
138
                this.getMidiTrackController().setSolo(index, solo);
 
139
        }
 
140
        
 
141
        public synchronized void setMute(int index,boolean mute) throws MidiPlayerException{
 
142
                this.getMidiTrackController().setMute(index, mute);
 
143
        }
 
144
        
 
145
        public String getKey() {
 
146
                return "tuxguitar.sequencer";
 
147
        }
 
148
        
 
149
        public String getName() {
 
150
                return "TuxGuitar Sequencer";
 
151
        }
 
152
        
 
153
        private class MidiTimer extends Thread{
 
154
                
 
155
                private static final int TIMER_DELAY = 15;
 
156
                
 
157
                private MidiSequencerImpl sequencer;
 
158
                
 
159
                public MidiTimer(MidiSequencerImpl sequencer){
 
160
                        this.sequencer = sequencer;
 
161
                }
 
162
                
 
163
                public void run() {
 
164
                        try {
 
165
                                synchronized(this.sequencer) {
 
166
                                        while( this.sequencer.process() ){
 
167
                                                this.sequencer.wait( TIMER_DELAY );
 
168
                                        }
 
169
                                }
 
170
                        } catch (Throwable throwable) {
 
171
                                throwable.printStackTrace();
 
172
                        }
 
173
                }
 
174
        }
 
175
}