Subversion Repositories svn.mios

Rev

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