35 *
36 * @author Florian Bomers
37 */
38 public abstract class AbstractMidiDeviceProvider extends MidiDeviceProvider {
39
40 private static final boolean enabled;
41
42 /**
43 * Create objects representing all MIDI output devices on the system.
44 */
45 static {
46 if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: static");
47 Platform.initialize();
48 enabled = Platform.isMidiIOEnabled();
49 if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: enabled: " + enabled);
50
51 // $$fb number of MIDI devices may change with time
52 // also for memory's sake, do not initialize the arrays here
53 }
54
55
56 final synchronized void readDeviceInfos() {
57 Info[] infos = getInfoCache();
58 MidiDevice[] devices = getDeviceCache();
59 if (!enabled) {
60 if (infos == null || infos.length != 0) {
61 setInfoCache(new Info[0]);
62 }
63 if (devices == null || devices.length != 0) {
64 setDeviceCache(new MidiDevice[0]);
65 }
66 return;
67 }
68
69 int oldNumDevices = (infos==null)?-1:infos.length;
70 int newNumDevices = getNumDevices();
71 if (oldNumDevices != newNumDevices) {
72 if (Printer.trace) Printer.trace(getClass().toString()
73 +": readDeviceInfos: old numDevices: "+oldNumDevices
74 +" newNumDevices: "+ newNumDevices);
75
131 public final MidiDevice getDevice(final MidiDevice.Info info) {
132 Objects.requireNonNull(info);
133 if (info instanceof Info) {
134 readDeviceInfos();
135 MidiDevice[] devices = getDeviceCache();
136 Info[] infos = getInfoCache();
137 Info thisInfo = (Info) info;
138 int index = thisInfo.getIndex();
139 if (index >= 0 && index < devices.length && infos[index] == info) {
140 if (devices[index] == null) {
141 devices[index] = createDevice(thisInfo);
142 }
143 if (devices[index] != null) {
144 return devices[index];
145 }
146 }
147 }
148 throw MidiUtils.unsupportedDevice(info);
149 }
150
151
152 // INNER CLASSES
153
154
155 /**
156 * Info class for MidiDevices. Adds an index value for
157 * making native references to a particular device.
158 */
159 static class Info extends MidiDevice.Info {
160 private int index;
161
162 Info(String name, String vendor, String description, String version, int index) {
163 super(name, vendor, description, version);
164 this.index = index;
165 }
166
167 final boolean equalStrings(Info info) {
168 return (info != null
169 && getName().equals(info.getName())
170 && getVendor().equals(info.getVendor())
171 && getDescription().equals(info.getDescription())
172 && getVersion().equals(info.getVersion()));
173 }
174
175 final int getIndex() {
176 return index;
177 }
178
179 final void setIndex(int index) {
180 this.index = index;
181 }
182
183 } // class Info
184
185
186 // ABSTRACT METHODS
187
188 abstract int getNumDevices();
189 abstract MidiDevice[] getDeviceCache();
190 abstract void setDeviceCache(MidiDevice[] devices);
191 abstract Info[] getInfoCache();
192 abstract void setInfoCache(Info[] infos);
193
194 abstract Info createInfo(int index);
195 abstract MidiDevice createDevice(Info info);
196 }
|
35 *
36 * @author Florian Bomers
37 */
38 public abstract class AbstractMidiDeviceProvider extends MidiDeviceProvider {
39
40 private static final boolean enabled;
41
42 /**
43 * Create objects representing all MIDI output devices on the system.
44 */
45 static {
46 if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: static");
47 Platform.initialize();
48 enabled = Platform.isMidiIOEnabled();
49 if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: enabled: " + enabled);
50
51 // $$fb number of MIDI devices may change with time
52 // also for memory's sake, do not initialize the arrays here
53 }
54
55 final synchronized void readDeviceInfos() {
56 Info[] infos = getInfoCache();
57 MidiDevice[] devices = getDeviceCache();
58 if (!enabled) {
59 if (infos == null || infos.length != 0) {
60 setInfoCache(new Info[0]);
61 }
62 if (devices == null || devices.length != 0) {
63 setDeviceCache(new MidiDevice[0]);
64 }
65 return;
66 }
67
68 int oldNumDevices = (infos==null)?-1:infos.length;
69 int newNumDevices = getNumDevices();
70 if (oldNumDevices != newNumDevices) {
71 if (Printer.trace) Printer.trace(getClass().toString()
72 +": readDeviceInfos: old numDevices: "+oldNumDevices
73 +" newNumDevices: "+ newNumDevices);
74
130 public final MidiDevice getDevice(final MidiDevice.Info info) {
131 Objects.requireNonNull(info);
132 if (info instanceof Info) {
133 readDeviceInfos();
134 MidiDevice[] devices = getDeviceCache();
135 Info[] infos = getInfoCache();
136 Info thisInfo = (Info) info;
137 int index = thisInfo.getIndex();
138 if (index >= 0 && index < devices.length && infos[index] == info) {
139 if (devices[index] == null) {
140 devices[index] = createDevice(thisInfo);
141 }
142 if (devices[index] != null) {
143 return devices[index];
144 }
145 }
146 }
147 throw MidiUtils.unsupportedDevice(info);
148 }
149
150 /**
151 * Info class for MidiDevices. Adds an index value for
152 * making native references to a particular device.
153 */
154 static class Info extends MidiDevice.Info {
155 private int index;
156
157 Info(String name, String vendor, String description, String version, int index) {
158 super(name, vendor, description, version);
159 this.index = index;
160 }
161
162 final boolean equalStrings(Info info) {
163 return (info != null
164 && getName().equals(info.getName())
165 && getVendor().equals(info.getVendor())
166 && getDescription().equals(info.getDescription())
167 && getVersion().equals(info.getVersion()));
168 }
169
170 final int getIndex() {
171 return index;
172 }
173
174 final void setIndex(int index) {
175 this.index = index;
176 }
177
178 } // class Info
179
180 abstract int getNumDevices();
181 abstract MidiDevice[] getDeviceCache();
182 abstract void setDeviceCache(MidiDevice[] devices);
183 abstract Info[] getInfoCache();
184 abstract void setInfoCache(Info[] infos);
185
186 abstract Info createInfo(int index);
187 abstract MidiDevice createDevice(Info info);
188 }
|