~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/midiport/MidiPortOut.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.midiport;
 
2
 
 
3
import javax.sound.midi.MidiDevice;
 
4
import javax.sound.midi.Receiver;
 
5
 
 
6
import org.herac.tuxguitar.player.base.MidiControllers;
 
7
import org.herac.tuxguitar.player.base.MidiOut;
 
8
import org.herac.tuxguitar.player.base.MidiPlayerException;
 
9
import org.herac.tuxguitar.player.base.MidiPort;
 
10
import org.herac.tuxguitar.player.impl.jsa.utils.MidiMessageUtils;
 
11
import org.herac.tuxguitar.util.TGSynchronizer;
 
12
 
 
13
public class MidiPortOut extends MidiPort{
 
14
        
 
15
        private MidiOutImpl out;
 
16
        
 
17
        public MidiPortOut(MidiDevice device){
 
18
                super(device.getDeviceInfo().getName(),device.getDeviceInfo().getName());
 
19
                this.out = new MidiOutImpl(device);
 
20
        }
 
21
        
 
22
        public MidiOut out(){
 
23
                return this.out;
 
24
        }
 
25
        
 
26
        public void open(){
 
27
                this.out.getReceiver();
 
28
        }
 
29
        
 
30
        public void close() throws MidiPlayerException{
 
31
                try {
 
32
                        this.out.close();
 
33
                } catch (Throwable throwable) {
 
34
                        throw new MidiPlayerException(throwable.getMessage(),throwable);
 
35
                }
 
36
        }
 
37
        
 
38
        public void check() throws MidiPlayerException{
 
39
                try {
 
40
                        this.out.open();
 
41
                } catch (Throwable throwable) {
 
42
                        throw new MidiPlayerException(throwable.getMessage(),throwable);
 
43
                }
 
44
        }
 
45
}
 
46
 
 
47
class MidiOutImpl implements MidiOut{
 
48
        
 
49
        private MidiDevice device;
 
50
        private Receiver receiver;
 
51
        
 
52
        public MidiOutImpl(MidiDevice device){
 
53
                this.device = device;
 
54
        }
 
55
        
 
56
        protected synchronized void open() throws Throwable{
 
57
                if(!this.device.isOpen()){
 
58
                        final MidiDevice device = this.device;
 
59
                        TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
60
                                public void run() throws Throwable {
 
61
                                        device.open();
 
62
                                }
 
63
                        });
 
64
                }
 
65
                if(this.receiver == null){
 
66
                        final MidiDevice device = this.device;
 
67
                        TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
68
                                public void run() throws Throwable {
 
69
                                        setReceiver(device.getReceiver());
 
70
                                }
 
71
                        });
 
72
                }
 
73
        }
 
74
        
 
75
        protected synchronized void close() throws Throwable{
 
76
                if(this.receiver != null){
 
77
                        final Receiver receiver = this.receiver;
 
78
                        TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
79
                                public void run() throws Throwable {
 
80
                                        receiver.close();
 
81
                                        setReceiver(null);
 
82
                                }
 
83
                        });
 
84
                }
 
85
                if(this.device.isOpen()){
 
86
                        final MidiDevice device = this.device;
 
87
                        TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
88
                                public void run() throws Throwable {
 
89
                                        device.close();
 
90
                                }
 
91
                        });
 
92
                }
 
93
        }
 
94
        
 
95
        protected void setReceiver(Receiver receiver){
 
96
                this.receiver = receiver;
 
97
        }
 
98
        
 
99
        protected Receiver getReceiver(){
 
100
                try {
 
101
                        this.open();
 
102
                } catch (Throwable throwable) {
 
103
                        // Do nothing
 
104
                }
 
105
                return this.receiver;
 
106
        }
 
107
        
 
108
        public void sendSystemReset(){
 
109
                if(getReceiver() != null){
 
110
                        getReceiver().send(MidiMessageUtils.systemReset(),-1);
 
111
                }
 
112
        }
 
113
        
 
114
        public void sendAllNotesOff(){
 
115
                if(getReceiver() != null){
 
116
                        for(int channel = 0; channel < 16; channel ++){
 
117
                                getReceiver().send(MidiMessageUtils.controlChange(channel, MidiControllers.ALL_NOTES_OFF,0),-1);
 
118
                        }
 
119
                }
 
120
        }
 
121
        
 
122
        public void sendNoteOn(int channel, int key, int velocity) {
 
123
                if(getReceiver() != null){
 
124
                        getReceiver().send(MidiMessageUtils.noteOn(channel, key, velocity),-1);
 
125
                }
 
126
        }
 
127
        
 
128
        public void sendNoteOff(int channel, int key, int velocity) {
 
129
                if(getReceiver() != null){
 
130
                        getReceiver().send(MidiMessageUtils.noteOff(channel, key, velocity),-1);
 
131
                }
 
132
        }
 
133
        
 
134
        public void sendControlChange(int channel, int controller, int value) {
 
135
                if(getReceiver() != null){
 
136
                        getReceiver().send(MidiMessageUtils.controlChange(channel,controller, value),-1);
 
137
                }
 
138
        }
 
139
        
 
140
        public void sendProgramChange(int channel, int value) {
 
141
                if(getReceiver() != null){
 
142
                        getReceiver().send(MidiMessageUtils.programChange(channel, value),-1);
 
143
                }
 
144
        }
 
145
        
 
146
        public void sendPitchBend(int channel, int value) {
 
147
                if(getReceiver() != null){
 
148
                        getReceiver().send(MidiMessageUtils.pitchBend(channel, value),-1);
 
149
                }
 
150
        }
 
151
}
 
 
b'\\ No newline at end of file'