Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/SourceDataLine.java
41159 views
/*1* Copyright (c) 1999, 2017, 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* A source data line is a data line to which data may be written. It acts as a29* source to its mixer. An application writes audio bytes to a source data line,30* which handles the buffering of the bytes and delivers them to the mixer. The31* mixer may mix the samples with those from other sources and then deliver the32* mix to a target such as an output port (which may represent an audio output33* device on a sound card).34* <p>35* Note that the naming convention for this interface reflects the relationship36* between the line and its mixer. From the perspective of an application, a37* source data line may act as a target for audio data.38* <p>39* A source data line can be obtained from a mixer by invoking the40* {@link Mixer#getLine getLine} method of {@code Mixer} with an appropriate41* {@link DataLine.Info} object.42* <p>43* The {@code SourceDataLine} interface provides a method for writing audio data44* to the data line's buffer. Applications that play or mix audio should write45* data to the source data line quickly enough to keep the buffer from46* underflowing (emptying), which could cause discontinuities in the audio that47* are perceived as clicks. Applications can use the48* {@link DataLine#available available} method defined in the {@code DataLine}49* interface to determine the amount of data currently queued in the data line's50* buffer. The amount of data which can be written to the buffer without51* blocking is the difference between the buffer size and the amount of queued52* data. If the delivery of audio output stops due to underflow, a53* {@link LineEvent.Type#STOP STOP} event is generated. A54* {@link LineEvent.Type#START START} event is generated when the audio output55* resumes.56*57* @author Kara Kytle58* @see Mixer59* @see DataLine60* @see TargetDataLine61* @since 1.362*/63public interface SourceDataLine extends DataLine {6465/**66* Opens the line with the specified format and suggested buffer size,67* causing the line to acquire any required system resources and become68* operational.69* <p>70* The buffer size is specified in bytes, but must represent an integral71* number of sample frames. Invoking this method with a requested buffer72* size that does not meet this requirement may result in an73* {@code IllegalArgumentException}. The actual buffer size for the open74* line may differ from the requested buffer size. The value actually set75* may be queried by subsequently calling {@link DataLine#getBufferSize}.76* <p>77* If this operation succeeds, the line is marked as open, and an78* {@link LineEvent.Type#OPEN OPEN} event is dispatched to the line's79* listeners.80* <p>81* Invoking this method on a line which is already open is illegal and may82* result in an {@code IllegalStateException}.83* <p>84* Note that some lines, once closed, cannot be reopened. Attempts to reopen85* such a line will always result in a {@code LineUnavailableException}.86*87* @param format the desired audio format88* @param bufferSize the desired buffer size89* @throws LineUnavailableException if the line cannot be opened due to90* resource restrictions91* @throws IllegalArgumentException if the buffer size does not represent an92* integral number of sample frames, or if {@code format} is not93* fully specified or invalid94* @throws IllegalStateException if the line is already open95* @throws SecurityException if the line cannot be opened due to security96* restrictions97* @see #open(AudioFormat)98* @see Line#open99* @see Line#close100* @see Line#isOpen101* @see LineEvent102*/103void open(AudioFormat format, int bufferSize)104throws LineUnavailableException;105106/**107* Opens the line with the specified format, causing the line to acquire any108* required system resources and become operational.109* <p>110* The implementation chooses a buffer size, which is measured in bytes but111* which encompasses an integral number of sample frames. The buffer size112* that the system has chosen may be queried by subsequently calling113* {@link DataLine#getBufferSize}.114* <p>115* If this operation succeeds, the line is marked as open, and an116* {@link LineEvent.Type#OPEN OPEN} event is dispatched to the line's117* listeners.118* <p>119* Invoking this method on a line which is already open is illegal and may120* result in an {@code IllegalStateException}.121* <p>122* Note that some lines, once closed, cannot be reopened. Attempts to reopen123* such a line will always result in a {@code LineUnavailableException}.124*125* @param format the desired audio format126* @throws LineUnavailableException if the line cannot be opened due to127* resource restrictions128* @throws IllegalArgumentException if {@code format} is not fully specified129* or invalid130* @throws IllegalStateException if the line is already open131* @throws SecurityException if the line cannot be opened due to security132* restrictions133* @see #open(AudioFormat, int)134* @see Line#open135* @see Line#close136* @see Line#isOpen137* @see LineEvent138*/139void open(AudioFormat format) throws LineUnavailableException;140141/**142* Writes audio data to the mixer via this source data line. The requested143* number of bytes of data are read from the specified array, starting at144* the given offset into the array, and written to the data line's buffer.145* If the caller attempts to write more data than can currently be written146* (see {@link DataLine#available available}), this method blocks until the147* requested amount of data has been written. This applies even if the148* requested amount of data to write is greater than the data line's buffer149* size. However, if the data line is closed, stopped, or flushed before the150* requested amount has been written, the method no longer blocks, but151* returns the number of bytes written thus far.152* <p>153* The number of bytes that can be written without blocking can be154* ascertained using the {@link DataLine#available available} method of the155* {@code DataLine} interface. (While it is guaranteed that this number of156* bytes can be written without blocking, there is no guarantee that157* attempts to write additional data will block.)158* <p>159* The number of bytes to write must represent an integral number of sample160* frames, such that:161* <p style="text-align:center">162* {@code [ bytes written ] % [frame size in bytes ] == 0}163* <p>164* The return value will always meet this requirement. A request to write a165* number of bytes representing a non-integral number of sample frames166* cannot be fulfilled and may result in an167* {@code IllegalArgumentException}.168*169* @param b a byte array containing data to be written to the data line170* @param off the offset from the beginning of the array, in bytes171* @param len the length, in bytes, of the valid data in the array (in172* other words, the requested amount of data to write, in bytes)173* @return the number of bytes actually written174* @throws IllegalArgumentException if the requested number of bytes does175* not represent an integral number of sample frames, or if176* {@code len} is negative177* @throws ArrayIndexOutOfBoundsException if {@code off} is negative, or178* {@code off+len} is greater than the length of the array {@code b}179* @see TargetDataLine#read180* @see DataLine#available181*/182int write(byte[] b, int off, int len);183184/**185* Obtains the number of sample frames of audio data that can be written to186* the mixer, via this data line, without blocking. Note that the return187* value measures sample frames, not bytes.188*189* @return the number of sample frames currently available for writing190* @see TargetDataLine#availableRead191*/192//public int availableWrite();193}194195196