2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.media.sound;
|
|
27 |
|
|
28 |
import java.util.ArrayList;
|
|
29 |
import java.util.List;
|
|
30 |
|
|
31 |
import javax.sound.midi.*;
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
/**
|
|
36 |
* MidiInDevice class representing functionality of MidiIn devices.
|
|
37 |
*
|
|
38 |
* @author David Rivas
|
|
39 |
* @author Kara Kytle
|
|
40 |
* @author Florian Bomers
|
|
41 |
*/
|
|
42 |
class MidiInDevice extends AbstractMidiDevice implements Runnable {
|
|
43 |
|
|
44 |
private Thread midiInThread = null;
|
|
45 |
|
|
46 |
// CONSTRUCTOR
|
|
47 |
|
|
48 |
MidiInDevice(AbstractMidiDeviceProvider.Info info) {
|
|
49 |
super(info);
|
|
50 |
if(Printer.trace) Printer.trace("MidiInDevice CONSTRUCTOR");
|
|
51 |
}
|
|
52 |
|
|
53 |
|
|
54 |
// IMPLEMENTATION OF ABSTRACT MIDI DEVICE METHODS
|
|
55 |
|
|
56 |
// $$kk: 06.24.99: i have this both opening and starting the midi in device.
|
|
57 |
// may want to separate these??
|
|
58 |
protected synchronized void implOpen() throws MidiUnavailableException {
|
|
59 |
if (Printer.trace) Printer.trace("> MidiInDevice: implOpen()");
|
|
60 |
|
|
61 |
int index = ((MidiInDeviceProvider.MidiInDeviceInfo)getDeviceInfo()).getIndex();
|
|
62 |
id = nOpen(index); // can throw MidiUnavailableException
|
|
63 |
|
|
64 |
if (id == 0) {
|
|
65 |
throw new MidiUnavailableException("Unable to open native device");
|
|
66 |
}
|
|
67 |
|
|
68 |
// create / start a thread to get messages
|
|
69 |
if (midiInThread == null) {
|
|
70 |
midiInThread = JSSecurityManager.createThread(this,
|
|
71 |
"Java Sound MidiInDevice Thread", // name
|
|
72 |
false, // daemon
|
|
73 |
-1, // priority
|
|
74 |
true); // doStart
|
|
75 |
}
|
|
76 |
|
|
77 |
nStart(id); // can throw MidiUnavailableException
|
|
78 |
if (Printer.trace) Printer.trace("< MidiInDevice: implOpen() completed");
|
|
79 |
}
|
|
80 |
|
|
81 |
|
|
82 |
// $$kk: 06.24.99: i have this both stopping and closing the midi in device.
|
|
83 |
// may want to separate these??
|
|
84 |
protected synchronized void implClose() {
|
|
85 |
if (Printer.trace) Printer.trace("> MidiInDevice: implClose()");
|
|
86 |
long oldId = id;
|
|
87 |
id = 0;
|
|
88 |
|
|
89 |
super.implClose();
|
|
90 |
|
|
91 |
// close the device
|
|
92 |
nStop(oldId);
|
|
93 |
if (midiInThread != null) {
|
|
94 |
try {
|
|
95 |
midiInThread.join(1000);
|
|
96 |
} catch (InterruptedException e) {
|
|
97 |
// IGNORE EXCEPTION
|
|
98 |
}
|
|
99 |
}
|
|
100 |
nClose(oldId);
|
|
101 |
if (Printer.trace) Printer.trace("< MidiInDevice: implClose() completed");
|
|
102 |
}
|
|
103 |
|
|
104 |
|
|
105 |
public long getMicrosecondPosition() {
|
|
106 |
long timestamp = -1;
|
|
107 |
if (isOpen()) {
|
|
108 |
timestamp = nGetTimeStamp(id);
|
|
109 |
}
|
|
110 |
return timestamp;
|
|
111 |
}
|
|
112 |
|
|
113 |
|
|
114 |
// OVERRIDES OF ABSTRACT MIDI DEVICE METHODS
|
|
115 |
|
|
116 |
|
|
117 |
protected boolean hasTransmitters() {
|
|
118 |
return true;
|
|
119 |
}
|
|
120 |
|
|
121 |
|
|
122 |
protected Transmitter createTransmitter() {
|
|
123 |
return new MidiInTransmitter();
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* An own class to distinguish the class name from
|
|
128 |
* the transmitter of other devices
|
|
129 |
*/
|
|
130 |
private class MidiInTransmitter extends BasicTransmitter {
|
|
131 |
private MidiInTransmitter() {
|
|
132 |
super();
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
// RUNNABLE METHOD
|
|
137 |
|
|
138 |
public void run() {
|
|
139 |
// while the device is started, keep trying to get messages.
|
|
140 |
// this thread returns from native code whenever stop() or close() is called
|
|
141 |
while (id!=0) {
|
|
142 |
// go into native code and retrieve messages
|
|
143 |
nGetMessages(id);
|
|
144 |
if (id!=0) {
|
|
145 |
try {
|
|
146 |
Thread.sleep(1);
|
|
147 |
} catch (InterruptedException e) {}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
if(Printer.verbose) Printer.verbose("MidiInDevice Thread exit");
|
|
151 |
// let the thread exit
|
|
152 |
midiInThread = null;
|
|
153 |
}
|
|
154 |
|
|
155 |
|
|
156 |
// CALLBACKS FROM NATIVE
|
|
157 |
|
|
158 |
/**
|
|
159 |
* Callback from native code when a short MIDI event is received from hardware.
|
|
160 |
* @param packedMsg: status | data1 << 8 | data2 << 8
|
|
161 |
* @param timeStamp time-stamp in microseconds
|
|
162 |
*/
|
|
163 |
void callbackShortMessage(int packedMsg, long timeStamp) {
|
|
164 |
if (packedMsg == 0 || id == 0) {
|
|
165 |
return;
|
|
166 |
}
|
|
167 |
|
|
168 |
/*if(Printer.verbose) {
|
|
169 |
int status = packedMsg & 0xFF;
|
|
170 |
int data1 = (packedMsg & 0xFF00)>>8;
|
|
171 |
int data2 = (packedMsg & 0xFF0000)>>16;
|
|
172 |
Printer.verbose(">> MidiInDevice callbackShortMessage: status: " + status + " data1: " + data1 + " data2: " + data2 + " timeStamp: " + timeStamp);
|
|
173 |
}*/
|
|
174 |
|
|
175 |
getTransmitterList().sendMessage(packedMsg, timeStamp);
|
|
176 |
}
|
|
177 |
|
|
178 |
void callbackLongMessage(byte[] data, long timeStamp) {
|
|
179 |
if (id == 0 || data == null) {
|
|
180 |
return;
|
|
181 |
}
|
|
182 |
getTransmitterList().sendMessage(data, timeStamp);
|
|
183 |
}
|
|
184 |
|
|
185 |
// NATIVE METHODS
|
|
186 |
|
|
187 |
private native long nOpen(int index) throws MidiUnavailableException;
|
|
188 |
private native void nClose(long id);
|
|
189 |
|
|
190 |
private native void nStart(long id) throws MidiUnavailableException;
|
|
191 |
private native void nStop(long id);
|
|
192 |
private native long nGetTimeStamp(long id);
|
|
193 |
|
|
194 |
// go into native code and get messages. May be blocking
|
|
195 |
private native void nGetMessages(long id);
|
|
196 |
|
|
197 |
|
|
198 |
}
|