Path: blob/master/src/java.desktop/share/classes/javax/sound/midi/MidiDevice.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.midi;2627import java.util.List;2829/**30* {@code MidiDevice} is the base interface for all MIDI devices. Common devices31* include synthesizers, sequencers, MIDI input ports, and MIDI output ports.32* <p>33* A {@code MidiDevice} can be a transmitter or a receiver of MIDI events, or34* both. Therefore, it can provide {@link Transmitter} or {@link Receiver}35* instances (or both). Typically, MIDI IN ports provide transmitters, MIDI OUT36* ports and synthesizers provide receivers. A Sequencer typically provides37* transmitters for playback and receivers for recording.38* <p>39* A {@code MidiDevice} can be opened and closed explicitly as well as40* implicitly. Explicit opening is accomplished by calling {@link #open},41* explicit closing is done by calling {@link #close} on the {@code MidiDevice}42* instance. If an application opens a {@code MidiDevice} explicitly, it has to43* close it explicitly to free system resources and enable the application to44* exit cleanly. Implicit opening is done by calling45* {@link MidiSystem#getReceiver} and {@link MidiSystem#getTransmitter}. The46* {@code MidiDevice} used by {@code MidiSystem.getReceiver} and47* {@code MidiSystem.getTransmitter} is implementation-dependent unless the48* properties {@code javax.sound.midi.Receiver} and49* {@code javax.sound.midi.Transmitter} are used (see the description of50* properties to select default providers in {@link MidiSystem}). A51* {@code MidiDevice} that was opened implicitly, is closed implicitly by52* closing the {@code Receiver} or {@code Transmitter} that resulted in opening53* it. If more than one implicitly opening {@code Receiver} or54* {@code Transmitter} were obtained by the application, the device is closed55* after the last {@code Receiver} or {@code Transmitter} has been closed. On56* the other hand, calling {@code getReceiver} or {@code getTransmitter} on the57* device instance directly does not open the device implicitly. Closing these58* {@code Transmitter}s and {@code Receiver}s does not close the device59* implicitly. To use a device with {@code Receiver}s or {@code Transmitter}s60* obtained this way, the device has to be opened and closed explicitly.61* <p>62* If implicit and explicit opening and closing are mixed on the same63* {@code MidiDevice} instance, the following rules apply:64*65* <ul>66* <li>After an explicit open (either before or after implicit opens), the67* device will not be closed by implicit closing. The only way to close an68* explicitly opened device is an explicit close.69* <li>An explicit close always closes the device, even if it also has been70* opened implicitly. A subsequent implicit close has no further effect.71* </ul>72*73* To detect if a MidiDevice represents a hardware MIDI port, the following74* programming technique can be used:75*76* <pre>{@code77* MidiDevice device = ...;78* if (!(device instanceof Sequencer) && !(device instanceof Synthesizer)) {79* // we're now sure that device represents a MIDI port80* // ...81* }82* }</pre>83*84* <p>85* A {@code MidiDevice} includes a {@link Info} object to provide manufacturer86* information and so on.87*88* @author Kara Kytle89* @author Florian Bomers90* @see Synthesizer91* @see Sequencer92* @see Receiver93* @see Transmitter94*/95public interface MidiDevice extends AutoCloseable {9697/**98* Obtains information about the device, including its Java class and99* {@code Strings} containing its name, vendor, and description.100*101* @return device info102*/103Info getDeviceInfo();104105/**106* Opens the device, indicating that it should now acquire any system107* resources it requires and become operational.108* <p>109* An application opening a device explicitly with this call has to close110* the device by calling {@link #close}. This is necessary to release system111* resources and allow applications to exit cleanly.112* <p>113* Note that some devices, once closed, cannot be reopened. Attempts to114* reopen such a device will always result in a115* {@code MidiUnavailableException}.116*117* @throws MidiUnavailableException thrown if the device cannot be opened118* due to resource restrictions119* @throws SecurityException thrown if the device cannot be opened due to120* security restrictions121* @see #close122* @see #isOpen123*/124void open() throws MidiUnavailableException;125126/**127* Closes the device, indicating that the device should now release any128* system resources it is using.129* <p>130* All {@code Receiver} and {@code Transmitter} instances open from this131* device are closed. This includes instances retrieved via132* {@code MidiSystem}.133*134* @see #open135* @see #isOpen136*/137@Override138void close();139140/**141* Reports whether the device is open.142*143* @return {@code true} if the device is open, otherwise {@code false}144* @see #open145* @see #close146*/147boolean isOpen();148149/**150* Obtains the current time-stamp of the device, in microseconds. If a151* device supports time-stamps, it should start counting at 0 when the152* device is opened and continue incrementing its time-stamp in microseconds153* until the device is closed. If it does not support time-stamps, it should154* always return -1.155*156* @return the current time-stamp of the device in microseconds, or -1 if157* time-stamping is not supported by the device158*/159long getMicrosecondPosition();160161/**162* Obtains the maximum number of MIDI IN connections available on this MIDI163* device for receiving MIDI data.164*165* @return maximum number of MIDI IN connections, or -1 if an unlimited166* number of connections is available167*/168int getMaxReceivers();169170/**171* Obtains the maximum number of MIDI OUT connections available on this MIDI172* device for transmitting MIDI data.173*174* @return maximum number of MIDI OUT connections, or -1 if an unlimited175* number of connections is available176*/177int getMaxTransmitters();178179/**180* Obtains a MIDI IN receiver through which the MIDI device may receive MIDI181* data. The returned receiver must be closed when the application has182* finished using it.183* <p>184* Usually the returned receiver implements the {@code MidiDeviceReceiver}185* interface.186* <p>187* Obtaining a {@code Receiver} with this method does not open the device.188* To be able to use the device, it has to be opened explicitly by calling189* {@link #open}. Also, closing the {@code Receiver} does not close the190* device. It has to be closed explicitly by calling {@link #close}.191*192* @return a receiver for the device193* @throws MidiUnavailableException thrown if a receiver is not available194* due to resource restrictions195* @see Receiver#close()196*/197Receiver getReceiver() throws MidiUnavailableException;198199/**200* Returns all currently active, non-closed receivers connected with this201* {@code MidiDevice}. A receiver can be removed from the device by closing202* it.203* <p>204* Usually the returned receivers implement the {@code MidiDeviceReceiver}205* interface.206*207* @return an unmodifiable list of the open receivers208* @since 1.5209*/210List<Receiver> getReceivers();211212/**213* Obtains a MIDI OUT connection from which the MIDI device will transmit214* MIDI data. The returned transmitter must be closed when the application215* has finished using it.216* <p>217* Usually the returned transmitter implements the218* {@code MidiDeviceTransmitter} interface.219* <p>220* Obtaining a {@code Transmitter} with this method does not open the221* device. To be able to use the device, it has to be opened explicitly by222* calling {@link #open}. Also, closing the {@code Transmitter} does not223* close the device. It has to be closed explicitly by calling224* {@link #close}.225*226* @return a MIDI OUT transmitter for the device227* @throws MidiUnavailableException thrown if a transmitter is not available228* due to resource restrictions229* @see Transmitter#close()230*/231Transmitter getTransmitter() throws MidiUnavailableException;232233/**234* Returns all currently active, non-closed transmitters connected with this235* {@code MidiDevice}. A transmitter can be removed from the device by236* closing it.237* <p>238* Usually the returned transmitters implement the239* {@code MidiDeviceTransmitter} interface.240*241* @return an unmodifiable list of the open transmitters242* @since 1.5243*/244List<Transmitter> getTransmitters();245246/**247* A {@code MidiDevice.Info} object contains assorted data about a248* {@link MidiDevice}, including its name, the company who created it, and249* descriptive text.250*251* @see MidiDevice#getDeviceInfo252*/253class Info {254255/**256* The device's name.257*/258private final String name;259260/**261* The name of the company who provides the device.262*/263private final String vendor;264265/**266* A description of the device.267*/268private final String description;269270/**271* Device version.272*/273private final String version;274275/**276* Constructs a device info object.277*278* @param name the name of the device279* @param vendor the name of the company who provides the device280* @param description a description of the device281* @param version version information for the device282*/283protected Info(String name, String vendor, String description,284String version) {285286this.name = name;287this.vendor = vendor;288this.description = description;289this.version = version;290}291292/**293* Indicates whether the specified object is equal to this info object,294* returning {@code true} if the objects are the same.295*296* @param obj the reference object with which to compare297* @return {@code true} if the specified object is equal to this info298* object; {@code false} otherwise299*/300@Override301public final boolean equals(Object obj) {302return super.equals(obj);303}304305/**306* Returns a hash code value for this info object.307*308* @return a hash code value for this info object309*/310@Override311public final int hashCode() {312return super.hashCode();313}314315/**316* Obtains the name of the device.317*318* @return a string containing the device's name319*/320public final String getName() {321return name;322}323324/**325* Obtains the name of the company who supplies the device.326*327* @return device the vendor's name328*/329public final String getVendor() {330return vendor;331}332333/**334* Obtains the description of the device.335*336* @return a description of the device337*/338public final String getDescription() {339return description;340}341342/**343* Obtains the version of the device.344*345* @return textual version information for the device346*/347public final String getVersion() {348return version;349}350351/**352* Returns a string representation of the info object.353*354* @return a string representation of the info object355*/356@Override357public final String toString() {358return name;359}360}361}362363364