1/*
2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26package javax.sound.midi;
27
28import java.util.List;
29
30/**
31 * {@code MidiDevice} is the base interface for all MIDI devices. Common devices
32 * include synthesizers, sequencers, MIDI input ports, and MIDI output ports.
33 * <p>
34 * A {@code MidiDevice} can be a transmitter or a receiver of MIDI events, or
35 * both. Therefore, it can provide {@link Transmitter} or {@link Receiver}
36 * instances (or both). Typically, MIDI IN ports provide transmitters, MIDI OUT
37 * ports and synthesizers provide receivers. A Sequencer typically provides
38 * transmitters for playback and receivers for recording.
39 * <p>
40 * A {@code MidiDevice} can be opened and closed explicitly as well as
41 * implicitly. Explicit opening is accomplished by calling {@link #open},
42 * explicit closing is done by calling {@link #close} on the {@code MidiDevice}
43 * instance. If an application opens a {@code MidiDevice} explicitly, it has to
44 * close it explicitly to free system resources and enable the application to
45 * exit cleanly. Implicit opening is done by calling
46 * {@link MidiSystem#getReceiver} and {@link MidiSystem#getTransmitter}. The
47 * {@code MidiDevice} used by {@code MidiSystem.getReceiver} and
48 * {@code MidiSystem.getTransmitter} is implementation-dependent unless the
49 * properties {@code javax.sound.midi.Receiver} and
50 * {@code javax.sound.midi.Transmitter} are used (see the description of
51 * properties to select default providers in {@link MidiSystem}). A
52 * {@code MidiDevice} that was opened implicitly, is closed implicitly by
53 * closing the {@code Receiver} or {@code Transmitter} that resulted in opening
54 * it. If more than one implicitly opening {@code Receiver} or
55 * {@code Transmitter} were obtained by the application, the device is closed
56 * after the last {@code Receiver} or {@code Transmitter} has been closed. On
57 * the other hand, calling {@code getReceiver} or {@code getTransmitter} on the
58 * device instance directly does not open the device implicitly. Closing these
59 * {@code Transmitter}s and {@code Receiver}s does not close the device
60 * implicitly. To use a device with {@code Receiver}s or {@code Transmitter}s
61 * obtained this way, the device has to be opened and closed explicitly.
62 * <p>
63 * If implicit and explicit opening and closing are mixed on the same
64 * {@code MidiDevice} instance, the following rules apply:
65 *
66 * <ul>
67 *   <li>After an explicit open (either before or after implicit opens), the
68 *   device will not be closed by implicit closing. The only way to close an
69 *   explicitly opened device is an explicit close.
70 *   <li>An explicit close always closes the device, even if it also has been
71 *   opened implicitly. A subsequent implicit close has no further effect.
72 * </ul>
73 *
74 * To detect if a MidiDevice represents a hardware MIDI port, the following
75 * programming technique can be used:
76 *
77 * <pre>{@code
78 * MidiDevice device = ...;
79 * if (!(device instanceof Sequencer) && !(device instanceof Synthesizer)) {
80 *   // we're now sure that device represents a MIDI port
81 *   // ...
82 * }
83 * }</pre>
84 *
85 * <p>
86 * A {@code MidiDevice} includes a {@link Info} object to provide manufacturer
87 * information and so on.
88 *
89 * @author Kara Kytle
90 * @author Florian Bomers
91 * @see Synthesizer
92 * @see Sequencer
93 * @see Receiver
94 * @see Transmitter
95 */
96public interface MidiDevice extends AutoCloseable {
97
98    /**
99     * Obtains information about the device, including its Java class and
100     * {@code Strings} containing its name, vendor, and description.
101     *
102     * @return device info
103     */
104    Info getDeviceInfo();
105
106    /**
107     * Opens the device, indicating that it should now acquire any system
108     * resources it requires and become operational.
109     * <p>
110     * An application opening a device explicitly with this call has to close
111     * the device by calling {@link #close}. This is necessary to release system
112     * resources and allow applications to exit cleanly.
113     * <p>
114     * Note that some devices, once closed, cannot be reopened. Attempts to
115     * reopen such a device will always result in a
116     * {@code MidiUnavailableException}.
117     *
118     * @throws MidiUnavailableException thrown if the device cannot be opened
119     *         due to resource restrictions
120     * @throws SecurityException thrown if the device cannot be opened due to
121     *         security restrictions
122     * @see #close
123     * @see #isOpen
124     */
125    void open() throws MidiUnavailableException;
126
127    /**
128     * Closes the device, indicating that the device should now release any
129     * system resources it is using.
130     * <p>
131     * All {@code Receiver} and {@code Transmitter} instances open from this
132     * device are closed. This includes instances retrieved via
133     * {@code MidiSystem}.
134     *
135     * @see #open
136     * @see #isOpen
137     */
138    @Override
139    void close();
140
141    /**
142     * Reports whether the device is open.
143     *
144     * @return {@code true} if the device is open, otherwise {@code false}
145     * @see #open
146     * @see #close
147     */
148    boolean isOpen();
149
150    /**
151     * Obtains the current time-stamp of the device, in microseconds. If a
152     * device supports time-stamps, it should start counting at 0 when the
153     * device is opened and continue incrementing its time-stamp in microseconds
154     * until the device is closed. If it does not support time-stamps, it should
155     * always return -1.
156     *
157     * @return the current time-stamp of the device in microseconds, or -1 if
158     *         time-stamping is not supported by the device
159     */
160    long getMicrosecondPosition();
161
162    /**
163     * Obtains the maximum number of MIDI IN connections available on this MIDI
164     * device for receiving MIDI data.
165     *
166     * @return maximum number of MIDI IN connections, or -1 if an unlimited
167     *         number of connections is available
168     */
169    int getMaxReceivers();
170
171    /**
172     * Obtains the maximum number of MIDI OUT connections available on this MIDI
173     * device for transmitting MIDI data.
174     *
175     * @return maximum number of MIDI OUT connections, or -1 if an unlimited
176     *         number of connections is available
177     */
178    int getMaxTransmitters();
179
180    /**
181     * Obtains a MIDI IN receiver through which the MIDI device may receive MIDI
182     * data. The returned receiver must be closed when the application has
183     * finished using it.
184     * <p>
185     * Usually the returned receiver implements the {@code MidiDeviceReceiver}
186     * interface.
187     * <p>
188     * Obtaining a {@code Receiver} with this method does not open the device.
189     * To be able to use the device, it has to be opened explicitly by calling
190     * {@link #open}. Also, closing the {@code Receiver} does not close the
191     * device. It has to be closed explicitly by calling {@link #close}.
192     *
193     * @return a receiver for the device
194     * @throws MidiUnavailableException thrown if a receiver is not available
195     *         due to resource restrictions
196     * @see Receiver#close()
197     */
198    Receiver getReceiver() throws MidiUnavailableException;
199
200    /**
201     * Returns all currently active, non-closed receivers connected with this
202     * {@code MidiDevice}. A receiver can be removed from the device by closing
203     * it.
204     * <p>
205     * Usually the returned receivers implement the {@code MidiDeviceReceiver}
206     * interface.
207     *
208     * @return an unmodifiable list of the open receivers
209     * @since 1.5
210     */
211    List<Receiver> getReceivers();
212
213    /**
214     * Obtains a MIDI OUT connection from which the MIDI device will transmit
215     * MIDI data. The returned transmitter must be closed when the application
216     * has finished using it.
217     * <p>
218     * Usually the returned transmitter implements the
219     * {@code MidiDeviceTransmitter} interface.
220     * <p>
221     * Obtaining a {@code Transmitter} with this method does not open the
222     * device. To be able to use the device, it has to be opened explicitly by
223     * calling {@link #open}. Also, closing the {@code Transmitter} does not
224     * close the device. It has to be closed explicitly by calling
225     * {@link #close}.
226     *
227     * @return a MIDI OUT transmitter for the device
228     * @throws MidiUnavailableException thrown if a transmitter is not available
229     *         due to resource restrictions
230     * @see Transmitter#close()
231     */
232    Transmitter getTransmitter() throws MidiUnavailableException;
233
234    /**
235     * Returns all currently active, non-closed transmitters connected with this
236     * {@code MidiDevice}. A transmitter can be removed from the device by
237     * closing it.
238     * <p>
239     * Usually the returned transmitters implement the
240     * {@code MidiDeviceTransmitter} interface.
241     *
242     * @return an unmodifiable list of the open transmitters
243     * @since 1.5
244     */
245    List<Transmitter> getTransmitters();
246
247    /**
248     * A {@code MidiDevice.Info} object contains assorted data about a
249     * {@link MidiDevice}, including its name, the company who created it, and
250     * descriptive text.
251     *
252     * @see MidiDevice#getDeviceInfo
253     */
254    class Info {
255
256        /**
257         * The device's name.
258         */
259        private final String name;
260
261        /**
262         * The name of the company who provides the device.
263         */
264        private final String vendor;
265
266        /**
267         * A description of the device.
268         */
269        private final String description;
270
271        /**
272         * Device version.
273         */
274        private final String version;
275
276        /**
277         * Constructs a device info object.
278         *
279         * @param  name the name of the device
280         * @param  vendor the name of the company who provides the device
281         * @param  description a description of the device
282         * @param  version version information for the device
283         */
284        protected Info(String name, String vendor, String description,
285                       String version) {
286
287            this.name = name;
288            this.vendor = vendor;
289            this.description = description;
290            this.version = version;
291        }
292
293        /**
294         * Indicates whether the specified object is equal to this info object,
295         * returning {@code true} if the objects are the same.
296         *
297         * @param  obj the reference object with which to compare
298         * @return {@code true} if the specified object is equal to this info
299         *         object; {@code false} otherwise
300         */
301        @Override
302        public final boolean equals(Object obj) {
303            return super.equals(obj);
304        }
305
306        /**
307         * Returns a hash code value for this info object.
308         *
309         * @return a hash code value for this info object
310         */
311        @Override
312        public final int hashCode() {
313            return super.hashCode();
314        }
315
316        /**
317         * Obtains the name of the device.
318         *
319         * @return a string containing the device's name
320         */
321        public final String getName() {
322            return name;
323        }
324
325        /**
326         * Obtains the name of the company who supplies the device.
327         *
328         * @return device the vendor's name
329         */
330        public final String getVendor() {
331            return vendor;
332        }
333
334        /**
335         * Obtains the description of the device.
336         *
337         * @return a description of the device
338         */
339        public final String getDescription() {
340            return description;
341        }
342
343        /**
344         * Obtains the version of the device.
345         *
346         * @return textual version information for the device
347         */
348        public final String getVersion() {
349            return version;
350        }
351
352        /**
353         * Provides a string representation of the device information.
354         *
355         * @return a description of the info object
356         */
357        @Override
358        public final String toString() {
359            return name;
360        }
361    }
362}
363