Subversion Repositories svn.mios

Rev

Rev 723 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 tk 1
/*
628 adamjking 2
 * @(#)VitualMidiDevice.java    beta8   2006/04/23
213 tk 3
 *
628 adamjking 4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
213 tk 5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.midi;
22
 
23
import java.util.Iterator;
24
import java.util.LinkedList;
25
import java.util.List;
628 adamjking 26
import java.util.Observable;
213 tk 27
 
28
import javax.sound.midi.MidiDevice;
29
import javax.sound.midi.MidiMessage;
30
import javax.sound.midi.MidiUnavailableException;
31
import javax.sound.midi.Receiver;
32
import javax.sound.midi.Transmitter;
33
 
628 adamjking 34
public class VirtualMidiDevice extends Observable implements MidiDevice {
213 tk 35
 
721 adamjking 36
    public static Object NAME = new Object();
723 adamjking 37
 
213 tk 38
    protected VirtualMidiDevice.Info info;
39
 
40
    protected int maxNoTransmitters;
41
 
42
    protected int maxNoReceivers;
43
 
44
    protected LinkedList transmitters;
45
 
46
    protected LinkedList receivers;
47
 
808 adamjking 48
    protected Receiver midiInReceiver;
213 tk 49
 
808 adamjking 50
    protected Receiver midiOutReceiver;
51
 
628 adamjking 52
    protected long timeStart = System.currentTimeMillis();
53
 
213 tk 54
    public VirtualMidiDevice(String name, int maxNoTransmitters,
55
            int maxNoReceivers) {
723 adamjking 56
 
721 adamjking 57
        setName(name);
723 adamjking 58
 
213 tk 59
        this.maxNoTransmitters = maxNoTransmitters;
60
        this.maxNoReceivers = maxNoReceivers;
61
        this.transmitters = new LinkedList();
62
        this.receivers = new LinkedList();
808 adamjking 63
        this.midiOutReceiver = new MidiOutReceiver();
213 tk 64
    }
723 adamjking 65
 
721 adamjking 66
    public void setName(String name) {
808 adamjking 67
 
721 adamjking 68
        this.info = new VirtualMidiDevice.MyInfo(name, "midibox.org",
69
                "Virtual MIDI Device", "v1.0");
723 adamjking 70
 
721 adamjking 71
        setChanged();
723 adamjking 72
 
721 adamjking 73
        notifyObservers(NAME);
723 adamjking 74
 
721 adamjking 75
        clearChanged();
76
    }
723 adamjking 77
 
213 tk 78
    public void close() {
79
 
80
    }
81
 
82
    public int getMaxReceivers() {
83
        return maxNoReceivers;
84
    }
85
 
86
    public int getMaxTransmitters() {
87
        return maxNoTransmitters;
88
    }
89
 
90
    public long getMicrosecondPosition() {
628 adamjking 91
        return (System.currentTimeMillis() - timeStart) * 1000;
213 tk 92
    }
93
 
94
    public Receiver getReceiver() throws MidiUnavailableException {
95
        MyReceiver receiver = new MyReceiver();
96
        receivers.add(receiver);
97
        return receiver;
98
    }
99
 
100
    public List getTransmitters() {
101
        return (LinkedList) transmitters.clone();
102
    }
103
 
104
    public boolean isOpen() {
105
        return true;
106
    }
107
 
108
    public void open() throws MidiUnavailableException {
109
 
110
    }
111
 
112
    public List getReceivers() {
113
        return (LinkedList) receivers.clone();
114
    }
115
 
808 adamjking 116
    public Receiver getMidiOutReceiver() {
117
        return midiOutReceiver;
213 tk 118
    }
119
 
808 adamjking 120
    public Receiver getMidiInReceiver() {
121
        return midiInReceiver;
122
    }
123
 
124
    public void setMidiInReceiver(Receiver midiInReceiver) {
125
        this.midiInReceiver = midiInReceiver;
126
    }
127
 
213 tk 128
    public Transmitter getTransmitter() {
129
        MyTransmitter transmitter = new MyTransmitter();
130
        transmitters.add(transmitter);
131
        return transmitter;
132
    }
133
 
134
    public MidiDevice.Info getDeviceInfo() {
135
        return info;
136
    }
137
 
138
    public class MyReceiver implements Receiver {
139
 
140
        public void close() {
141
            synchronized (receivers) {
142
                receivers.remove(this);
143
            }
144
        }
145
 
146
        public void send(MidiMessage message, long timeStamp) {
808 adamjking 147
 
148
            if (midiInReceiver != null) {
149
 
150
                midiInReceiver.send(message, timeStamp);
151
            }
213 tk 152
        }
153
    }
154
 
155
    public class MyTransmitter implements Transmitter {
156
 
157
        protected Receiver receiver;
158
 
159
        public void close() {
160
            synchronized (transmitters) {
161
                transmitters.remove(this);
162
            }
163
        }
164
 
165
        public Receiver getReceiver() {
166
            return receiver;
167
        }
168
 
169
        public void setReceiver(Receiver receiver) {
170
            this.receiver = receiver;
171
        }
172
    }
173
 
174
    public class MyInfo extends MidiDevice.Info {
723 adamjking 175
 
213 tk 176
        protected MyInfo(String name, String vendor, String description,
177
                String version) {
178
            super(name, vendor, description, version);
179
        }
180
    }
723 adamjking 181
 
808 adamjking 182
    public class MidiOutReceiver implements Receiver {
183
 
213 tk 184
        public void close() {
185
 
186
        }
187
 
808 adamjking 188
        public void send(MidiMessage message, long timeStamp) {
189
            synchronized (transmitters) {
190
                Iterator it = transmitters.iterator();
191
                while (it.hasNext()) {
192
                    Transmitter transmitter = (Transmitter) it.next();
193
                    if (transmitter != null) {
194
                        Receiver receiver = transmitter.getReceiver();
195
                        if (receiver != null) {
196
                            receiver.send(message, timeStamp);
197
                        }
198
                    }
199
                }
200
            }
213 tk 201
        }
202
    }
203
}