Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/sampled/SourceDataLine.java
38918 views
/*1* Copyright (c) 1999, 2003, 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;262728/**29* A source data line is a data line to which data may be written. It acts as30* a source to its mixer. An application writes audio bytes to a source data line,31* which handles the buffering of the bytes and delivers them to the mixer.32* The mixer may mix the samples with those from other sources and then deliver33* the mix to a target such as an output port (which may represent an audio output34* device on a sound card).35* <p>36* Note that the naming convention for this interface reflects the relationship37* between the line and its mixer. From the perspective of an application,38* a source data line may act as a target for audio data.39* <p>40* A source data line can be obtained from a mixer by invoking the41* <code>{@link Mixer#getLine getLine}</code> method of <code>Mixer</code> with42* an appropriate <code>{@link DataLine.Info}</code> object.43* <p>44* The <code>SourceDataLine</code> interface provides a method for writing45* audio data to the data line's buffer. Applications that play or mix46* audio should write data to the source data line quickly enough to keep the47* buffer from underflowing (emptying), which could cause discontinuities in48* the audio that are perceived as clicks. Applications can use the49* <code>{@link DataLine#available available}</code> method defined in the50* <code>DataLine</code> interface to determine the amount of data currently51* queued in the data line's buffer. The amount of data which can be written52* to the buffer without blocking is the difference between the buffer size53* and the amount of queued data. If the delivery of audio output54* stops due to underflow, a <code>{@link LineEvent.Type#STOP STOP}</code> event is55* generated. A <code>{@link LineEvent.Type#START START}</code> event is generated56* when the audio output resumes.57*58* @author Kara Kytle59* @see Mixer60* @see DataLine61* @see TargetDataLine62* @since 1.363*/64public interface SourceDataLine extends DataLine {656667/**68* Opens the line with the specified format and suggested buffer size,69* causing the line to acquire any required70* system resources and become operational.71* <p>72* The buffer size is specified in bytes, but must represent an integral73* number of sample frames. Invoking this method with a requested buffer74* size that does not meet this requirement may result in an75* IllegalArgumentException. The actual buffer size for the open line may76* differ from the requested buffer size. The value actually set may be77* queried by subsequently calling <code>{@link DataLine#getBufferSize}</code>.78* <p>79* If this operation succeeds, the line is marked as open, and an80* <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the81* line's listeners.82* <p>83* Invoking this method on a line which is already open is illegal84* and may result in an <code>IllegalStateException</code>.85* <p>86* Note that some lines, once closed, cannot be reopened. Attempts87* to reopen such a line will always result in a88* <code>LineUnavailableException</code>.89*90* @param format the desired audio format91* @param bufferSize the desired buffer size92* @throws LineUnavailableException if the line cannot be93* opened due to resource restrictions94* @throws IllegalArgumentException if the buffer size does not represent95* an integral number of sample frames,96* or if <code>format</code> is not fully specified or invalid97* @throws IllegalStateException if the line is already open98* @throws SecurityException if the line cannot be99* opened due to security restrictions100*101* @see #open(AudioFormat)102* @see Line#open103* @see Line#close104* @see Line#isOpen105* @see LineEvent106*/107public void open(AudioFormat format, int bufferSize) throws LineUnavailableException;108109110/**111* Opens the line with the specified format, causing the line to acquire any112* required system resources and become operational.113*114* <p>115* The implementation chooses a buffer size, which is measured in bytes but116* which encompasses an integral number of sample frames. The buffer size117* that the system has chosen may be queried by subsequently calling118* <code>{@link DataLine#getBufferSize}</code>.119* <p>120* If this operation succeeds, the line is marked as open, and an121* <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the122* line's listeners.123* <p>124* Invoking this method on a line which is already open is illegal125* and may result in an <code>IllegalStateException</code>.126* <p>127* Note that some lines, once closed, cannot be reopened. Attempts128* to reopen such a line will always result in a129* <code>LineUnavailableException</code>.130*131* @param format the desired audio format132* @throws LineUnavailableException if the line cannot be133* opened due to resource restrictions134* @throws IllegalArgumentException if <code>format</code>135* is not fully specified or invalid136* @throws IllegalStateException if the line is already open137* @throws SecurityException if the line cannot be138* opened due to security restrictions139*140* @see #open(AudioFormat, int)141* @see Line#open142* @see Line#close143* @see Line#isOpen144* @see LineEvent145*/146public void open(AudioFormat format) throws LineUnavailableException;147148149/**150* Writes audio data to the mixer via this source data line. The requested151* number of bytes of data are read from the specified array,152* starting at the given offset into the array, and written to the data153* line's buffer. If the caller attempts to write more data than can154* currently be written (see <code>{@link DataLine#available available}</code>),155* this method blocks until the requested amount of data has been written.156* This applies even if the requested amount of data to write is greater157* than the data line's buffer size. However, if the data line is closed,158* stopped, or flushed before the requested amount has been written,159* the method no longer blocks, but returns the number of bytes160* written thus far.161* <p>162* The number of bytes that can be written without blocking can be ascertained163* using the <code>{@link DataLine#available available}</code> method of the164* <code>DataLine</code> interface. (While it is guaranteed that165* this number of bytes can be written without blocking, there is no guarantee166* that attempts to write additional data will block.)167* <p>168* The number of bytes to write must represent an integral number of169* sample frames, such that:170* <br>171* <center><code>[ bytes written ] % [frame size in bytes ] == 0</code></center>172* <br>173* The return value will always meet this requirement. A request to write a174* number of bytes representing a non-integral number of sample frames cannot175* be fulfilled and may result in an <code>IllegalArgumentException</code>.176*177* @param b a byte array containing data to be written to the data line178* @param len the length, in bytes, of the valid data in the array179* (in other words, the requested amount of data to write, in bytes)180* @param off the offset from the beginning of the array, in bytes181* @return the number of bytes actually written182* @throws IllegalArgumentException if the requested number of bytes does183* not represent an integral number of sample frames,184* or if <code>len</code> is negative185* @throws ArrayIndexOutOfBoundsException if <code>off</code> is negative,186* or <code>off+len</code> is greater than the length of the array187* <code>b</code>.188*189* @see TargetDataLine#read190* @see DataLine#available191*/192public int write(byte[] b, int off, int len);193194/**195* Obtains the number of sample frames of audio data that can be written to196* the mixer, via this data line, without blocking. Note that the return197* value measures sample frames, not bytes.198* @return the number of sample frames currently available for writing199* @see TargetDataLine#availableRead200*/201//public int availableWrite();202}203204205