Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/sampled/Line.java
38918 views
/*1* Copyright (c) 1999, 2010, 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</code> interface represents a mono or multi-channel29* audio feed. A line is an element of the digital audio30* "pipeline," such as a mixer, an input or output port,31* or a data path into or out of a mixer.32* <p>33* A line can have controls, such as gain, pan, and reverb.34* The controls themselves are instances of classes that extend the35* base <code>{@link Control}</code> class.36* The <code>Line</code> interface provides two accessor methods for37* obtaining the line's controls: <code>{@link #getControls getControls}</code> returns the38* entire set, and <code>{@link #getControl getControl}</code> returns a single control of39* specified type.40* <p>41* Lines exist in various states at different times. When a line opens, it reserves system42* resources for itself, and when it closes, these resources are freed for43* other objects or applications. The <code>{@link #isOpen()}</code> method lets44* you discover whether a line is open or closed.45* An open line need not be processing data, however. Such processing is46* typically initiated by subinterface methods such as47* <code>{@link SourceDataLine#write SourceDataLine.write}</code> and48* <code>{@link TargetDataLine#read TargetDataLine.read}</code>.49*<p>50* You can register an object to receive notifications whenever the line's51* state changes. The object must implement the <code>{@link LineListener}</code>52* interface, which consists of the single method53* <code>{@link LineListener#update update}</code>.54* This method will be invoked when a line opens and closes (and, if it's a55* {@link DataLine}, when it starts and stops).56*<p>57* An object can be registered to listen to multiple lines. The event it58* receives in its <code>update</code> method will specify which line created59* the event, what type of event it was60* (<code>OPEN</code>, <code>CLOSE</code>, <code>START</code>, or <code>STOP</code>),61* and how many sample frames the line had processed at the time the event occurred.62* <p>63* Certain line operations, such as open and close, can generate security64* exceptions if invoked by unprivileged code when the line is a shared audio65* resource.66*67* @author Kara Kytle68*69* @see LineEvent70* @since 1.371*/72public interface Line extends AutoCloseable {7374/**75* Obtains the <code>Line.Info</code> object describing this76* line.77* @return description of the line78*/79public Line.Info getLineInfo();8081/**82* Opens the line, indicating that it should acquire any required83* system resources and become operational.84* If this operation85* succeeds, the line is marked as open, and an <code>OPEN</code> event is dispatched86* to the line's listeners.87* <p>88* Note that some lines, once closed, cannot be reopened. Attempts89* to reopen such a line will always result in an <code>LineUnavailableException</code>.90* <p>91* Some types of lines have configurable properties that may affect92* resource allocation. For example, a <code>DataLine</code> must93* be opened with a particular format and buffer size. Such lines94* should provide a mechanism for configuring these properties, such95* as an additional <code>open</code> method or methods which allow96* an application to specify the desired settings.97* <p>98* This method takes no arguments, and opens the line with the current99* settings. For <code>{@link SourceDataLine}</code> and100* <code>{@link TargetDataLine}</code> objects, this means that the line is101* opened with default settings. For a <code>{@link Clip}</code>, however,102* the buffer size is determined when data is loaded. Since this method does not103* allow the application to specify any data to load, an IllegalArgumentException104* is thrown. Therefore, you should instead use one of the <code>open</code> methods105* provided in the <code>Clip</code> interface to load data into the <code>Clip</code>.106* <p>107* For <code>DataLine</code>'s, if the <code>DataLine.Info</code>108* object which was used to retrieve the line, specifies at least109* one fully qualified audio format, the last one will be used110* as the default format.111*112* @throws IllegalArgumentException if this method is called on a Clip instance.113* @throws LineUnavailableException if the line cannot be114* opened due to resource restrictions.115* @throws SecurityException if the line cannot be116* opened due to security restrictions.117*118* @see #close119* @see #isOpen120* @see LineEvent121* @see DataLine122* @see Clip#open(AudioFormat, byte[], int, int)123* @see Clip#open(AudioInputStream)124*/125public void open() throws LineUnavailableException;126127128/**129* Closes the line, indicating that any system resources130* in use by the line can be released. If this operation131* succeeds, the line is marked closed and a <code>CLOSE</code> event is dispatched132* to the line's listeners.133* @throws SecurityException if the line cannot be134* closed due to security restrictions.135*136* @see #open137* @see #isOpen138* @see LineEvent139*/140public void close();141142143144/**145* Indicates whether the line is open, meaning that it has reserved146* system resources and is operational, although it might not currently be147* playing or capturing sound.148* @return <code>true</code> if the line is open, otherwise <code>false</code>149*150* @see #open()151* @see #close()152*/153public boolean isOpen();154155156/**157* Obtains the set of controls associated with this line.158* Some controls may only be available when the line is open.159* If there are no controls, this method returns an array of length 0.160* @return the array of controls161* @see #getControl162*/163public Control[] getControls();164165/**166* Indicates whether the line supports a control of the specified type.167* Some controls may only be available when the line is open.168* @param control the type of the control for which support is queried169* @return <code>true</code> if at least one control of the specified type is170* supported, otherwise <code>false</code>.171*/172public boolean isControlSupported(Control.Type control);173174175/**176* Obtains a control of the specified type,177* if there is any.178* Some controls may only be available when the line is open.179* @param control the type of the requested control180* @return a control of the specified type181* @throws IllegalArgumentException if a control of the specified type182* is not supported183* @see #getControls184* @see #isControlSupported(Control.Type control)185*/186public Control getControl(Control.Type control);187188189/**190* Adds a listener to this line. Whenever the line's status changes, the191* listener's <code>update()</code> method is called with a <code>LineEvent</code> object192* that describes the change.193* @param listener the object to add as a listener to this line194* @see #removeLineListener195* @see LineListener#update196* @see LineEvent197*/198public void addLineListener(LineListener listener);199200201/**202* Removes the specified listener from this line's list of listeners.203* @param listener listener to remove204* @see #addLineListener205*/206public void removeLineListener(LineListener listener);207208209/**210* A <code>Line.Info</code> object contains information about a line.211* The only information provided by <code>Line.Info</code> itself212* is the Java class of the line.213* A subclass of <code>Line.Info</code> adds other kinds of information214* about the line. This additional information depends on which <code>Line</code>215* subinterface is implemented by the kind of line that the <code>Line.Info</code>216* subclass describes.217* <p>218* A <code>Line.Info</code> can be retrieved using various methods of219* the <code>Line</code>, <code>Mixer</code>, and <code>AudioSystem</code>220* interfaces. Other such methods let you pass a <code>Line.Info</code> as221* an argument, to learn whether lines matching the specified configuration222* are available and to obtain them.223*224* @author Kara Kytle225*226* @see Line#getLineInfo227* @see Mixer#getSourceLineInfo228* @see Mixer#getTargetLineInfo229* @see Mixer#getLine <code>Mixer.getLine(Line.Info)</code>230* @see Mixer#getSourceLineInfo(Line.Info) <code>Mixer.getSourceLineInfo(Line.Info)</code>231* @see Mixer#getSourceLineInfo(Line.Info) <code>Mixer.getTargetLineInfo(Line.Info)</code>232* @see Mixer#isLineSupported <code>Mixer.isLineSupported(Line.Info)</code>233* @see AudioSystem#getLine <code>AudioSystem.getLine(Line.Info)</code>234* @see AudioSystem#getSourceLineInfo <code>AudioSystem.getSourceLineInfo(Line.Info)</code>235* @see AudioSystem#getTargetLineInfo <code>AudioSystem.getTargetLineInfo(Line.Info)</code>236* @see AudioSystem#isLineSupported <code>AudioSystem.isLineSupported(Line.Info)</code>237* @since 1.3238*/239public static class Info {240241/**242* The class of the line described by the info object.243*/244private final Class lineClass;245246247/**248* Constructs an info object that describes a line of the specified class.249* This constructor is typically used by an application to250* describe a desired line.251* @param lineClass the class of the line that the new Line.Info object describes252*/253public Info(Class<?> lineClass) {254255if (lineClass == null) {256this.lineClass = Line.class;257} else {258this.lineClass = lineClass;259}260}261262263264/**265* Obtains the class of the line that this Line.Info object describes.266* @return the described line's class267*/268public Class<?> getLineClass() {269return lineClass;270}271272273/**274* Indicates whether the specified info object matches this one.275* To match, the specified object must be identical to or276* a special case of this one. The specified info object277* must be either an instance of the same class as this one,278* or an instance of a sub-type of this one. In addition, the279* attributes of the specified object must be compatible with the280* capabilities of this one. Specifically, the routing configuration281* for the specified info object must be compatible with that of this282* one.283* Subclasses may add other criteria to determine whether the two objects284* match.285*286* @param info the info object which is being compared to this one287* @return <code>true</code> if the specified object matches this one,288* <code>false</code> otherwise289*/290public boolean matches(Info info) {291292// $$kk: 08.30.99: is this backwards?293// dataLine.matches(targetDataLine) == true: targetDataLine is always dataLine294// targetDataLine.matches(dataLine) == false295// so if i want to make sure i get a targetDataLine, i need:296// targetDataLine.matches(prospective_match) == true297// => prospective_match may be other things as well, but it is at least a targetDataLine298// targetDataLine defines the requirements which prospective_match must meet.299300301// "if this Class object represents a declared class, this method returns302// true if the specified Object argument is an instance of the represented303// class (or of any of its subclasses)"304// GainControlClass.isInstance(MyGainObj) => true305// GainControlClass.isInstance(MySpecialGainInterfaceObj) => true306307// this_class.isInstance(that_object) => that object can by cast to this class308// => that_object's class may be a subtype of this_class309// => that may be more specific (subtype) of this310311// "If this Class object represents an interface, this method returns true312// if the class or any superclass of the specified Object argument implements313// this interface"314// GainControlClass.isInstance(MyGainObj) => true315// GainControlClass.isInstance(GenericControlObj) => may be false316// => that may be more specific317318if (! (this.getClass().isInstance(info)) ) {319return false;320}321322323// this.isAssignableFrom(that) => this is same or super to that324// => this is at least as general as that325// => that may be subtype of this326327if (! (getLineClass().isAssignableFrom(info.getLineClass())) ) {328return false;329}330331return true;332}333334335/**336* Obtains a textual description of the line info.337* @return a string description338*/339public String toString() {340341String fullPackagePath = "javax.sound.sampled.";342String initialString = new String(getLineClass().toString());343String finalString;344345int index = initialString.indexOf(fullPackagePath);346347if (index != -1) {348finalString = initialString.substring(0, index) + initialString.substring( (index + fullPackagePath.length()), initialString.length() );349} else {350finalString = initialString;351}352353return finalString;354}355356} // class Info357358} // interface Line359360361