Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/Line.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.sampled;2627/**28* The {@code Line} interface represents a mono or multi-channel audio feed. A29* line is an element of the digital audio "pipeline," such as a mixer, an input30* or output port, or a data path into or out of a mixer.31* <p>32* A line can have controls, such as gain, pan, and reverb. The controls33* themselves are instances of classes that extend the base {@link Control}34* class. The {@code Line} interface provides two accessor methods for obtaining35* the line's controls: {@link #getControls getControls} returns the entire set,36* and {@link #getControl getControl} returns a single control of specified37* type.38* <p>39* Lines exist in various states at different times. When a line opens, it40* reserves system resources for itself, and when it closes, these resources are41* freed for other objects or applications. The {@link #isOpen()} method lets42* you discover whether a line is open or closed. An open line need not be43* processing data, however. Such processing is typically initiated by44* subinterface methods such as45* {@link SourceDataLine#write SourceDataLine.write} and46* {@link TargetDataLine#read TargetDataLine.read}.47* <p>48* You can register an object to receive notifications whenever the line's state49* changes. The object must implement the {@link LineListener} interface, which50* consists of the single method {@link LineListener#update update}. This method51* will be invoked when a line opens and closes (and, if it's a {@link DataLine}52* , when it starts and stops).53* <p>54* An object can be registered to listen to multiple lines. The event it55* receives in its {@code update} method will specify which line created the56* event, what type of event it was ({@code OPEN}, {@code CLOSE}, {@code START},57* or {@code STOP}), and how many sample frames the line had processed at the58* time the event occurred.59* <p>60* Certain line operations, such as open and close, can generate security61* exceptions if invoked by unprivileged code when the line is a shared audio62* resource.63*64* @author Kara Kytle65* @see LineEvent66* @since 1.367*/68public interface Line extends AutoCloseable {6970/**71* Obtains the {@code Line.Info} object describing this line.72*73* @return description of the line74*/75Line.Info getLineInfo();7677/**78* Opens the line, indicating that it should acquire any required system79* resources and become operational. If this operation succeeds, the line is80* marked as open, and an {@code OPEN} event is dispatched to the line's81* listeners.82* <p>83* Note that some lines, once closed, cannot be reopened. Attempts to reopen84* such a line will always result in an {@code LineUnavailableException}.85* <p>86* Some types of lines have configurable properties that may affect resource87* allocation. For example, a {@code DataLine} must be opened with a88* particular format and buffer size. Such lines should provide a mechanism89* for configuring these properties, such as an additional {@code open}90* method or methods which allow an application to specify the desired91* settings.92* <p>93* This method takes no arguments, and opens the line with the current94* settings. For {@link SourceDataLine} and {@link TargetDataLine} objects,95* this means that the line is opened with default settings. For a96* {@link Clip}, however, the buffer size is determined when data is loaded.97* Since this method does not allow the application to specify any data to98* load, an {@code IllegalArgumentException} is thrown. Therefore, you99* should instead use one of the {@code open} methods provided in the100* {@code Clip} interface to load data into the {@code Clip}.101* <p>102* For {@code DataLine}'s, if the {@code DataLine.Info} object which was103* used to retrieve the line, specifies at least one fully qualified audio104* format, the last one will be used as the default format.105*106* @throws IllegalArgumentException if this method is called on a Clip107* instance108* @throws LineUnavailableException if the line cannot be opened due to109* resource restrictions110* @throws SecurityException if the line cannot be opened due to security111* restrictions112* @see #close113* @see #isOpen114* @see LineEvent115* @see DataLine116* @see Clip#open(AudioFormat, byte[], int, int)117* @see Clip#open(AudioInputStream)118*/119void open() throws LineUnavailableException;120121/**122* Closes the line, indicating that any system resources in use by the line123* can be released. If this operation succeeds, the line is marked closed124* and a {@code CLOSE} event is dispatched to the line's listeners.125*126* @throws SecurityException if the line cannot be closed due to security127* restrictions128* @see #open129* @see #isOpen130* @see LineEvent131*/132@Override133void close();134135/**136* Indicates whether the line is open, meaning that it has reserved system137* resources and is operational, although it might not currently be playing138* or capturing sound.139*140* @return {@code true} if the line is open, otherwise {@code false}141* @see #open()142* @see #close()143*/144boolean isOpen();145146/**147* Obtains the set of controls associated with this line. Some controls may148* only be available when the line is open. If there are no controls, this149* method returns an array of length 0.150*151* @return the array of controls152* @see #getControl153*/154Control[] getControls();155156/**157* Indicates whether the line supports a control of the specified type. Some158* controls may only be available when the line is open.159*160* @param control the type of the control for which support is queried161* @return {@code true} if at least one control of the specified type is162* supported, otherwise {@code false}163*/164boolean isControlSupported(Control.Type control);165166/**167* Obtains a control of the specified type, if there is any. Some controls168* may only be available when the line is open.169*170* @param control the type of the requested control171* @return a control of the specified type172* @throws IllegalArgumentException if a control of the specified type is173* not supported174* @see #getControls175* @see #isControlSupported(Control.Type control)176*/177Control getControl(Control.Type control);178179/**180* Adds a listener to this line. Whenever the line's status changes, the181* listener's {@code update()} method is called with a {@code LineEvent}182* object that describes the change.183*184* @param listener the object to add as a listener to this line185* @see #removeLineListener186* @see LineListener#update187* @see LineEvent188*/189void addLineListener(LineListener listener);190191/**192* Removes the specified listener from this line's list of listeners.193*194* @param listener listener to remove195* @see #addLineListener196*/197void removeLineListener(LineListener listener);198199/**200* A {@code Line.Info} object contains information about a line. The only201* information provided by {@code Line.Info} itself is the Java class of the202* line. A subclass of {@code Line.Info} adds other kinds of information203* about the line. This additional information depends on which {@code Line}204* subinterface is implemented by the kind of line that the205* {@code Line.Info} subclass describes.206* <p>207* A {@code Line.Info} can be retrieved using various methods of the208* {@code Line}, {@code Mixer}, and {@code AudioSystem} interfaces. Other209* such methods let you pass a {@code Line.Info} as an argument, to learn210* whether lines matching the specified configuration are available and to211* obtain them.212*213* @author Kara Kytle214* @see Line#getLineInfo()215* @see Mixer#getSourceLineInfo()216* @see Mixer#getTargetLineInfo()217* @see Mixer#getLine(Line.Info)218* @see Mixer#getSourceLineInfo(Line.Info)219* @see Mixer#getTargetLineInfo(Line.Info)220* @see Mixer#isLineSupported(Line.Info)221* @see AudioSystem#getLine(Line.Info)222* @see AudioSystem#getSourceLineInfo(Line.Info)223* @see AudioSystem#getTargetLineInfo(Line.Info)224* @see AudioSystem#isLineSupported(Line.Info)225* @since 1.3226*/227class Info {228229/**230* The class of the line described by the info object.231*/232private final Class<?> lineClass;233234/**235* Constructs an info object that describes a line of the specified236* class. This constructor is typically used by an application to237* describe a desired line.238*239* @param lineClass the class of the line that the new240* {@code Line.Info} object describes241*/242public Info(Class<?> lineClass) {243244if (lineClass == null) {245this.lineClass = Line.class;246} else {247this.lineClass = lineClass;248}249}250251/**252* Obtains the class of the line that this {@code Line.Info} object253* describes.254*255* @return the described line's class256*/257public Class<?> getLineClass() {258return lineClass;259}260261/**262* Indicates whether the specified info object matches this one. To263* match, the specified object must be identical to or a special case of264* this one. The specified info object must be either an instance of the265* same class as this one, or an instance of a sub-type of this one. In266* addition, the attributes of the specified object must be compatible267* with the capabilities of this one. Specifically, the routing268* configuration for the specified info object must be compatible with269* that of this one. Subclasses may add other criteria to determine270* whether the two objects match.271*272* @param info the info object which is being compared to this one273* @return {@code true} if the specified object matches this one,274* {@code false} otherwise275*/276public boolean matches(Info info) {277278// $$kk: 08.30.99: is this backwards?279// dataLine.matches(targetDataLine) == true: targetDataLine is always dataLine280// targetDataLine.matches(dataLine) == false281// so if i want to make sure i get a targetDataLine, i need:282// targetDataLine.matches(prospective_match) == true283// => prospective_match may be other things as well, but it is at least a targetDataLine284// targetDataLine defines the requirements which prospective_match must meet.285286287// "if this Class object represents a declared class, this method returns288// true if the specified Object argument is an instance of the represented289// class (or of any of its subclasses)"290// GainControlClass.isInstance(MyGainObj) => true291// GainControlClass.isInstance(MySpecialGainInterfaceObj) => true292293// this_class.isInstance(that_object) => that object can by cast to this class294// => that_object's class may be a subtype of this_class295// => that may be more specific (subtype) of this296297// "If this Class object represents an interface, this method returns true298// if the class or any superclass of the specified Object argument implements299// this interface"300// GainControlClass.isInstance(MyGainObj) => true301// GainControlClass.isInstance(GenericControlObj) => may be false302// => that may be more specific303304if (! (this.getClass().isInstance(info)) ) {305return false;306}307308// this.isAssignableFrom(that) => this is same or super to that309// => this is at least as general as that310// => that may be subtype of this311312if (! (getLineClass().isAssignableFrom(info.getLineClass())) ) {313return false;314}315316return true;317}318319/**320* Returns a string representation of the info object.321*322* @return a string representation of the info object323*/324@Override325public String toString() {326final String str = getLineClass().toString();327if (getLineClass().getPackage() == Line.class.getPackage()) {328return str.replace("javax.sound.sampled.", "");329}330return str;331}332}333}334335336