Subversion Repositories svn.mios

Rev

Rev 628 | Rev 723 | Go to most recent revision | 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();
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
 
48
    protected MidiOutPort midiOutPort;
49
 
628 adamjking 50
    protected long timeStart = System.currentTimeMillis();
51
 
213 tk 52
    public VirtualMidiDevice(String name, int maxNoTransmitters,
53
            int maxNoReceivers) {
721 adamjking 54
 
55
        setName(name);
56
 
213 tk 57
        this.maxNoTransmitters = maxNoTransmitters;
58
        this.maxNoReceivers = maxNoReceivers;
59
        this.transmitters = new LinkedList();
60
        this.receivers = new LinkedList();
61
        this.midiOutPort = new MidiOutPort();
62
    }
721 adamjking 63
 
64
    public void setName(String name) {
65
        this.info = new VirtualMidiDevice.MyInfo(name, "midibox.org",
66
                "Virtual MIDI Device", "v1.0");
67
 
68
        setChanged();
69
 
70
        notifyObservers(NAME);
71
 
72
        clearChanged();
73
    }
74
 
213 tk 75
    public void close() {
76
 
77
    }
78
 
79
    public int getMaxReceivers() {
80
        return maxNoReceivers;
81
    }
82
 
83
    public int getMaxTransmitters() {
84
        return maxNoTransmitters;
85
    }
86
 
87
    public long getMicrosecondPosition() {
628 adamjking 88
        return (System.currentTimeMillis() - timeStart) * 1000;
213 tk 89
    }
90
 
91
    public Receiver getReceiver() throws MidiUnavailableException {
92
        MyReceiver receiver = new MyReceiver();
93
        receivers.add(receiver);
94
        return receiver;
95
    }
96
 
97
    public List getTransmitters() {
98
        return (LinkedList) transmitters.clone();
99
    }
100
 
101
    public boolean isOpen() {
102
        return true;
103
    }
104
 
105
    public void open() throws MidiUnavailableException {
106
 
107
    }
108
 
109
    public List getReceivers() {
110
        return (LinkedList) receivers.clone();
111
    }
112
 
113
    public MidiOutPort getMidiOutPort() {
114
        return midiOutPort;
115
    }
116
 
117
    public Transmitter getTransmitter() {
118
        MyTransmitter transmitter = new MyTransmitter();
119
        transmitters.add(transmitter);
120
        return transmitter;
121
    }
122
 
123
    public MidiDevice.Info getDeviceInfo() {
124
        return info;
125
    }
126
 
127
    protected void receiveFromReceivers(MidiMessage message, long timestamp) {
128
 
129
    }
130
 
131
    protected void sendToTransmitters(MidiMessage message, long timeStamp) {
132
        synchronized (transmitters) {
133
            Iterator it = transmitters.iterator();
134
            while (it.hasNext()) {
135
                Transmitter transmitter = (Transmitter) it.next();
136
                if (transmitter != null) {
137
                    Receiver receiver = transmitter.getReceiver();
138
                    if (receiver != null) {
139
                        receiver.send(message, timeStamp);
140
                    }
141
                }
142
            }
143
        }
144
    }
145
 
146
    public class MyReceiver implements Receiver {
147
 
148
        public void close() {
149
            synchronized (receivers) {
150
                receivers.remove(this);
151
            }
152
        }
153
 
154
        public void send(MidiMessage message, long timeStamp) {
155
            receiveFromReceivers(message, timeStamp);
156
        }
157
    }
158
 
159
    public class MyTransmitter implements Transmitter {
160
 
161
        protected Receiver receiver;
162
 
163
        public void close() {
164
            synchronized (transmitters) {
165
                transmitters.remove(this);
166
            }
167
        }
168
 
169
        public Receiver getReceiver() {
170
            return receiver;
171
        }
172
 
173
        public void setReceiver(Receiver receiver) {
174
            this.receiver = receiver;
175
        }
176
    }
177
 
178
    public class MyInfo extends MidiDevice.Info {
721 adamjking 179
 
213 tk 180
        protected MyInfo(String name, String vendor, String description,
181
                String version) {
182
            super(name, vendor, description, version);
183
        }
184
    }
721 adamjking 185
 
213 tk 186
    public class MidiOutPort implements Receiver {
187
        public void close() {
188
 
189
        }
190
 
191
        public void send(MidiMessage message, long timestamp) {
192
            sendToTransmitters(message, timestamp);
193
        }
194
    }
195
}