Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/sampled/DataLine.java
38918 views
/*1* Copyright (c) 1999, 2013, 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;2627import java.util.Arrays;2829/**30* <code>DataLine</code> adds media-related functionality to its31* superinterface, <code>{@link Line}</code>. This functionality includes32* transport-control methods that start, stop, drain, and flush33* the audio data that passes through the line. A data line can also34* report the current position, volume, and audio format of the media.35* Data lines are used for output of audio by means of the36* subinterfaces <code>{@link SourceDataLine}</code> or37* <code>{@link Clip}</code>, which allow an application program to write data. Similarly,38* audio input is handled by the subinterface <code>{@link TargetDataLine}</code>,39* which allows data to be read.40* <p>41* A data line has an internal buffer in which42* the incoming or outgoing audio data is queued. The43* <code>{@link #drain()}</code> method blocks until this internal buffer44* becomes empty, usually because all queued data has been processed. The45* <code>{@link #flush()}</code> method discards any available queued data46* from the internal buffer.47* <p>48* A data line produces <code>{@link LineEvent.Type#START START}</code> and49* <code>{@link LineEvent.Type#STOP STOP}</code> events whenever50* it begins or ceases active presentation or capture of data. These events51* can be generated in response to specific requests, or as a result of52* less direct state changes. For example, if <code>{@link #start()}</code> is called53* on an inactive data line, and data is available for capture or playback, a54* <code>START</code> event will be generated shortly, when data playback55* or capture actually begins. Or, if the flow of data to an active data56* line is constricted so that a gap occurs in the presentation of data,57* a <code>STOP</code> event is generated.58* <p>59* Mixers often support synchronized control of multiple data lines.60* Synchronization can be established through the Mixer interface's61* <code>{@link Mixer#synchronize synchronize}</code> method.62* See the description of the <code>{@link Mixer Mixer}</code> interface63* for a more complete description.64*65* @author Kara Kytle66* @see LineEvent67* @since 1.368*/69public interface DataLine extends Line {707172/**73* Drains queued data from the line by continuing data I/O until the74* data line's internal buffer has been emptied.75* This method blocks until the draining is complete. Because this is a76* blocking method, it should be used with care. If <code>drain()</code>77* is invoked on a stopped line that has data in its queue, the method will78* block until the line is running and the data queue becomes empty. If79* <code>drain()</code> is invoked by one thread, and another continues to80* fill the data queue, the operation will not complete.81* This method always returns when the data line is closed.82*83* @see #flush()84*/85public void drain();8687/**88* Flushes queued data from the line. The flushed data is discarded.89* In some cases, not all queued data can be discarded. For example, a90* mixer can flush data from the buffer for a specific input line, but any91* unplayed data already in the output buffer (the result of the mix) will92* still be played. You can invoke this method after pausing a line (the93* normal case) if you want to skip the "stale" data when you restart94* playback or capture. (It is legal to flush a line that is not stopped,95* but doing so on an active line is likely to cause a discontinuity in the96* data, resulting in a perceptible click.)97*98* @see #stop()99* @see #drain()100*/101public void flush();102103/**104* Allows a line to engage in data I/O. If invoked on a line105* that is already running, this method does nothing. Unless the data in106* the buffer has been flushed, the line resumes I/O starting107* with the first frame that was unprocessed at the time the line was108* stopped. When audio capture or playback starts, a109* <code>{@link LineEvent.Type#START START}</code> event is generated.110*111* @see #stop()112* @see #isRunning()113* @see LineEvent114*/115public void start();116117/**118* Stops the line. A stopped line should cease I/O activity.119* If the line is open and running, however, it should retain the resources required120* to resume activity. A stopped line should retain any audio data in its buffer121* instead of discarding it, so that upon resumption the I/O can continue where it left off,122* if possible. (This doesn't guarantee that there will never be discontinuities beyond the123* current buffer, of course; if the stopped condition continues124* for too long, input or output samples might be dropped.) If desired, the retained data can be125* discarded by invoking the <code>flush</code> method.126* When audio capture or playback stops, a <code>{@link LineEvent.Type#STOP STOP}</code> event is generated.127*128* @see #start()129* @see #isRunning()130* @see #flush()131* @see LineEvent132*/133public void stop();134135/**136* Indicates whether the line is running. The default is <code>false</code>.137* An open line begins running when the first data is presented in response to an138* invocation of the <code>start</code> method, and continues139* until presentation ceases in response to a call to <code>stop</code> or140* because playback completes.141* @return <code>true</code> if the line is running, otherwise <code>false</code>142* @see #start()143* @see #stop()144*/145public boolean isRunning();146147/**148* Indicates whether the line is engaging in active I/O (such as playback149* or capture). When an inactive line becomes active, it sends a150* <code>{@link LineEvent.Type#START START}</code> event to its listeners. Similarly, when151* an active line becomes inactive, it sends a152* <code>{@link LineEvent.Type#STOP STOP}</code> event.153* @return <code>true</code> if the line is actively capturing or rendering154* sound, otherwise <code>false</code>155* @see #isOpen156* @see #addLineListener157* @see #removeLineListener158* @see LineEvent159* @see LineListener160*/161public boolean isActive();162163/**164* Obtains the current format (encoding, sample rate, number of channels,165* etc.) of the data line's audio data.166*167* <p>If the line is not open and has never been opened, it returns168* the default format. The default format is an implementation169* specific audio format, or, if the <code>DataLine.Info</code>170* object, which was used to retrieve this <code>DataLine</code>,171* specifies at least one fully qualified audio format, the172* last one will be used as the default format. Opening the173* line with a specific audio format (e.g.174* {@link SourceDataLine#open(AudioFormat)}) will override the175* default format.176*177* @return current audio data format178* @see AudioFormat179*/180public AudioFormat getFormat();181182/**183* Obtains the maximum number of bytes of data that will fit in the data line's184* internal buffer. For a source data line, this is the size of the buffer to185* which data can be written. For a target data line, it is the size of186* the buffer from which data can be read. Note that187* the units used are bytes, but will always correspond to an integral188* number of sample frames of audio data.189*190* @return the size of the buffer in bytes191*/192public int getBufferSize();193194/**195* Obtains the number of bytes of data currently available to the196* application for processing in the data line's internal buffer. For a197* source data line, this is the amount of data that can be written to the198* buffer without blocking. For a target data line, this is the amount of data199* available to be read by the application. For a clip, this value is always200* 0 because the audio data is loaded into the buffer when the clip is opened,201* and persists without modification until the clip is closed.202* <p>203* Note that the units used are bytes, but will always204* correspond to an integral number of sample frames of audio data.205* <p>206* An application is guaranteed that a read or207* write operation of up to the number of bytes returned from208* <code>available()</code> will not block; however, there is no guarantee209* that attempts to read or write more data will block.210*211* @return the amount of data available, in bytes212*/213public int available();214215/**216* Obtains the current position in the audio data, in sample frames.217* The frame position measures the number of sample218* frames captured by, or rendered from, the line since it was opened.219* This return value will wrap around after 2^31 frames. It is recommended220* to use <code>getLongFramePosition</code> instead.221*222* @return the number of frames already processed since the line was opened223* @see #getLongFramePosition()224*/225public int getFramePosition();226227228/**229* Obtains the current position in the audio data, in sample frames.230* The frame position measures the number of sample231* frames captured by, or rendered from, the line since it was opened.232*233* @return the number of frames already processed since the line was opened234* @since 1.5235*/236public long getLongFramePosition();237238239/**240* Obtains the current position in the audio data, in microseconds.241* The microsecond position measures the time corresponding to the number242* of sample frames captured by, or rendered from, the line since it was opened.243* The level of precision is not guaranteed. For example, an implementation244* might calculate the microsecond position from the current frame position245* and the audio sample frame rate. The precision in microseconds would246* then be limited to the number of microseconds per sample frame.247*248* @return the number of microseconds of data processed since the line was opened249*/250public long getMicrosecondPosition();251252/**253* Obtains the current volume level for the line. This level is a measure254* of the signal's current amplitude, and should not be confused with the255* current setting of a gain control. The range is from 0.0 (silence) to256* 1.0 (maximum possible amplitude for the sound waveform). The units257* measure linear amplitude, not decibels.258*259* @return the current amplitude of the signal in this line, or260* <code>{@link AudioSystem#NOT_SPECIFIED}</code>261*/262public float getLevel();263264/**265* Besides the class information inherited from its superclass,266* <code>DataLine.Info</code> provides additional information specific to data lines.267* This information includes:268* <ul>269* <li> the audio formats supported by the data line270* <li> the minimum and maximum sizes of its internal buffer271* </ul>272* Because a <code>Line.Info</code> knows the class of the line its describes, a273* <code>DataLine.Info</code> object can describe <code>DataLine</code>274* subinterfaces such as <code>{@link SourceDataLine}</code>,275* <code>{@link TargetDataLine}</code>, and <code>{@link Clip}</code>.276* You can query a mixer for lines of any of these types, passing an appropriate277* instance of <code>DataLine.Info</code> as the argument to a method such as278* <code>{@link Mixer#getLine Mixer.getLine(Line.Info)}</code>.279*280* @see Line.Info281* @author Kara Kytle282* @since 1.3283*/284public static class Info extends Line.Info {285286private final AudioFormat[] formats;287private final int minBufferSize;288private final int maxBufferSize;289290/**291* Constructs a data line's info object from the specified information,292* which includes a set of supported audio formats and a range for the buffer size.293* This constructor is typically used by mixer implementations294* when returning information about a supported line.295*296* @param lineClass the class of the data line described by the info object297* @param formats set of formats supported298* @param minBufferSize minimum buffer size supported by the data line, in bytes299* @param maxBufferSize maximum buffer size supported by the data line, in bytes300*/301public Info(Class<?> lineClass, AudioFormat[] formats, int minBufferSize, int maxBufferSize) {302303super(lineClass);304305if (formats == null) {306this.formats = new AudioFormat[0];307} else {308this.formats = Arrays.copyOf(formats, formats.length);309}310311this.minBufferSize = minBufferSize;312this.maxBufferSize = maxBufferSize;313}314315316/**317* Constructs a data line's info object from the specified information,318* which includes a single audio format and a desired buffer size.319* This constructor is typically used by an application to320* describe a desired line.321*322* @param lineClass the class of the data line described by the info object323* @param format desired format324* @param bufferSize desired buffer size in bytes325*/326public Info(Class<?> lineClass, AudioFormat format, int bufferSize) {327328super(lineClass);329330if (format == null) {331this.formats = new AudioFormat[0];332} else {333this.formats = new AudioFormat[]{format};334}335336this.minBufferSize = bufferSize;337this.maxBufferSize = bufferSize;338}339340341/**342* Constructs a data line's info object from the specified information,343* which includes a single audio format.344* This constructor is typically used by an application to345* describe a desired line.346*347* @param lineClass the class of the data line described by the info object348* @param format desired format349*/350public Info(Class<?> lineClass, AudioFormat format) {351this(lineClass, format, AudioSystem.NOT_SPECIFIED);352}353354355/**356* Obtains a set of audio formats supported by the data line.357* Note that <code>isFormatSupported(AudioFormat)</code> might return358* <code>true</code> for certain additional formats that are missing from359* the set returned by <code>getFormats()</code>. The reverse is not360* the case: <code>isFormatSupported(AudioFormat)</code> is guaranteed to return361* <code>true</code> for all formats returned by <code>getFormats()</code>.362*363* Some fields in the AudioFormat instances can be set to364* {@link javax.sound.sampled.AudioSystem#NOT_SPECIFIED NOT_SPECIFIED}365* if that field does not apply to the format,366* or if the format supports a wide range of values for that field.367* For example, a multi-channel device supporting up to368* 64 channels, could set the channel field in the369* <code>AudioFormat</code> instances returned by this370* method to <code>NOT_SPECIFIED</code>.371*372* @return a set of supported audio formats.373* @see #isFormatSupported(AudioFormat)374*/375public AudioFormat[] getFormats() {376return Arrays.copyOf(formats, formats.length);377}378379/**380* Indicates whether this data line supports a particular audio format.381* The default implementation of this method simply returns <code>true</code> if382* the specified format matches any of the supported formats.383*384* @param format the audio format for which support is queried.385* @return <code>true</code> if the format is supported, otherwise <code>false</code>386* @see #getFormats387* @see AudioFormat#matches388*/389public boolean isFormatSupported(AudioFormat format) {390391for (int i = 0; i < formats.length; i++) {392if (format.matches(formats[i])) {393return true;394}395}396397return false;398}399400/**401* Obtains the minimum buffer size supported by the data line.402* @return minimum buffer size in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>403*/404public int getMinBufferSize() {405return minBufferSize;406}407408409/**410* Obtains the maximum buffer size supported by the data line.411* @return maximum buffer size in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>412*/413public int getMaxBufferSize() {414return maxBufferSize;415}416417418/**419* Determines whether the specified info object matches this one.420* To match, the superclass match requirements must be met. In421* addition, this object's minimum buffer size must be at least as422* large as that of the object specified, its maximum buffer size must423* be at most as large as that of the object specified, and all of its424* formats must match formats supported by the object specified.425* @return <code>true</code> if this object matches the one specified,426* otherwise <code>false</code>.427*/428public boolean matches(Line.Info info) {429430if (! (super.matches(info)) ) {431return false;432}433434Info dataLineInfo = (Info)info;435436// treat anything < 0 as NOT_SPECIFIED437// demo code in old Java Sound Demo used a wrong buffer calculation438// that would lead to arbitrary negative values439if ((getMaxBufferSize() >= 0) && (dataLineInfo.getMaxBufferSize() >= 0)) {440if (getMaxBufferSize() > dataLineInfo.getMaxBufferSize()) {441return false;442}443}444445if ((getMinBufferSize() >= 0) && (dataLineInfo.getMinBufferSize() >= 0)) {446if (getMinBufferSize() < dataLineInfo.getMinBufferSize()) {447return false;448}449}450451AudioFormat[] localFormats = getFormats();452453if (localFormats != null) {454455for (int i = 0; i < localFormats.length; i++) {456if (! (localFormats[i] == null) ) {457if (! (dataLineInfo.isFormatSupported(localFormats[i])) ) {458return false;459}460}461}462}463464return true;465}466467/**468* Obtains a textual description of the data line info.469* @return a string description470*/471public String toString() {472473StringBuffer buf = new StringBuffer();474475if ( (formats.length == 1) && (formats[0] != null) ) {476buf.append(" supporting format " + formats[0]);477} else if (getFormats().length > 1) {478buf.append(" supporting " + getFormats().length + " audio formats");479}480481if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (maxBufferSize != AudioSystem.NOT_SPECIFIED) ) {482buf.append(", and buffers of " + minBufferSize + " to " + maxBufferSize + " bytes");483} else if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (minBufferSize > 0) ) {484buf.append(", and buffers of at least " + minBufferSize + " bytes");485} else if (maxBufferSize != AudioSystem.NOT_SPECIFIED) {486buf.append(", and buffers of up to " + minBufferSize + " bytes");487}488489return new String(super.toString() + buf);490}491} // class Info492493} // interface DataLine494495496