1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sound.sampled;
  27 
  28 /**
  29  * A source data line is a data line to which data may be written. It acts as a
  30  * 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. The
  32  * mixer may mix the samples with those from other sources and then deliver the
  33  * mix to a target such as an output port (which may represent an audio output
  34  * device on a sound card).
  35  * <p>
  36  * Note that the naming convention for this interface reflects the relationship
  37  * between the line and its mixer. From the perspective of an application, a
  38  * 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 the
  41  * {@link Mixer#getLine getLine} method of {@code Mixer} with an appropriate
  42  * {@link DataLine.Info} object.
  43  * <p>
  44  * The {@code SourceDataLine} interface provides a method for writing audio data
  45  * to the data line's buffer. Applications that play or mix audio should write
  46  * data to the source data line quickly enough to keep the buffer from
  47  * underflowing (emptying), which could cause discontinuities in the audio that
  48  * are perceived as clicks. Applications can use the
  49  * {@link DataLine#available available} method defined in the {@code DataLine}
  50  * interface to determine the amount of data currently queued in the data line's
  51  * buffer. The amount of data which can be written to the buffer without
  52  * blocking is the difference between the buffer size and the amount of queued
  53  * data. If the delivery of audio output stops due to underflow, a
  54  * {@link LineEvent.Type#STOP STOP} event is generated. A
  55  * {@link LineEvent.Type#START START} event is generated when the audio output
  56  * resumes.
  57  *
  58  * @author Kara Kytle
  59  * @see Mixer
  60  * @see DataLine
  61  * @see TargetDataLine
  62  * @since 1.3
  63  */
  64 public interface SourceDataLine extends DataLine {
  65 
  66     /**
  67      * Opens the line with the specified format and suggested buffer size,
  68      * causing the line to acquire any required system resources and become
  69      * operational.
  70      * <p>
  71      * The buffer size is specified in bytes, but must represent an integral
  72      * number of sample frames. Invoking this method with a requested buffer
  73      * size that does not meet this requirement may result in an
  74      * {@code IllegalArgumentException}. The actual buffer size for the open
  75      * line may differ from the requested buffer size. The value actually set
  76      * may be queried by subsequently calling {@link DataLine#getBufferSize}.
  77      * <p>
  78      * If this operation succeeds, the line is marked as open, and an
  79      * {@link LineEvent.Type#OPEN OPEN} event is dispatched to the line's
  80      * listeners.
  81      * <p>
  82      * Invoking this method on a line which is already open is illegal and may
  83      * result in an {@code IllegalStateException}.
  84      * <p>
  85      * Note that some lines, once closed, cannot be reopened. Attempts to reopen
  86      * such a line will always result in a {@code LineUnavailableException}.
  87      *
  88      * @param  format the desired audio format
  89      * @param  bufferSize the desired buffer size
  90      * @throws LineUnavailableException if the line cannot be opened due to
  91      *         resource restrictions
  92      * @throws IllegalArgumentException if the buffer size does not represent an
  93      *         integral number of sample frames, or if {@code format} is not
  94      *         fully specified or invalid
  95      * @throws IllegalStateException if the line is already open
  96      * @throws SecurityException if the line cannot be opened due to security
  97      *         restrictions
  98      * @see #open(AudioFormat)
  99      * @see Line#open
 100      * @see Line#close
 101      * @see Line#isOpen
 102      * @see LineEvent
 103      */
 104     void open(AudioFormat format, int bufferSize)
 105             throws LineUnavailableException;
 106 
 107     /**
 108      * Opens the line with the specified format, causing the line to acquire any
 109      * required system resources and become operational.
 110      * <p>
 111      * The implementation chooses a buffer size, which is measured in bytes but
 112      * which encompasses an integral number of sample frames. The buffer size
 113      * that the system has chosen may be queried by subsequently calling
 114      * {@link DataLine#getBufferSize}.
 115      * <p>
 116      * If this operation succeeds, the line is marked as open, and an
 117      * {@link LineEvent.Type#OPEN OPEN} event is dispatched to the line's
 118      * listeners.
 119      * <p>
 120      * Invoking this method on a line which is already open is illegal and may
 121      * result in an {@code IllegalStateException}.
 122      * <p>
 123      * Note that some lines, once closed, cannot be reopened. Attempts to reopen
 124      * such a line will always result in a {@code LineUnavailableException}.
 125      *
 126      * @param  format the desired audio format
 127      * @throws LineUnavailableException if the line cannot be opened due to
 128      *         resource restrictions
 129      * @throws IllegalArgumentException if {@code format} is not fully specified
 130      *         or invalid
 131      * @throws IllegalStateException if the line is already open
 132      * @throws SecurityException if the line cannot be opened due to security
 133      *         restrictions
 134      * @see #open(AudioFormat, int)
 135      * @see Line#open
 136      * @see Line#close
 137      * @see Line#isOpen
 138      * @see LineEvent
 139      */
 140     void open(AudioFormat format) throws LineUnavailableException;
 141 
 142     /**
 143      * Writes audio data to the mixer via this source data line. The requested
 144      * number of bytes of data are read from the specified array, starting at
 145      * the given offset into the array, and written to the data line's buffer.
 146      * If the caller attempts to write more data than can currently be written
 147      * (see {@link DataLine#available available}), this method blocks until the
 148      * requested amount of data has been written. This applies even if the
 149      * requested amount of data to write is greater than the data line's buffer
 150      * size. However, if the data line is closed, stopped, or flushed before the
 151      * requested amount has been written, the method no longer blocks, but
 152      * returns the number of bytes written thus far.
 153      * <p>
 154      * The number of bytes that can be written without blocking can be
 155      * ascertained using the {@link DataLine#available available} method of the
 156      * {@code DataLine} interface. (While it is guaranteed that this number of
 157      * bytes can be written without blocking, there is no guarantee that
 158      * attempts to write additional data will block.)
 159      * <p>
 160      * The number of bytes to write must represent an integral number of sample
 161      * frames, such that:
 162      * <p style="text-align:center">
 163      * {@code [ bytes written ] % [frame size in bytes ] == 0}
 164      * <p>
 165      * The return value will always meet this requirement. A request to write a
 166      * number of bytes representing a non-integral number of sample frames
 167      * cannot be fulfilled and may result in an
 168      * {@code IllegalArgumentException}.
 169      *
 170      * @param  b a byte array containing data to be written to the data line
 171      * @param  len the length, in bytes, of the valid data in the array (in
 172      *         other words, the requested amount of data to write, in bytes)
 173      * @param  off the offset from the beginning of the array, in bytes
 174      * @return the number of bytes actually written
 175      * @throws IllegalArgumentException if the requested number of bytes does
 176      *         not represent an integral number of sample frames, or if
 177      *         {@code len} is negative
 178      * @throws ArrayIndexOutOfBoundsException if {@code off} is negative, or
 179      *         {@code off+len} is greater than the length of the array {@code b}
 180      * @see TargetDataLine#read
 181      * @see DataLine#available
 182      */
 183     int write(byte[] b, int off, int len);
 184 
 185     /**
 186      * Obtains the number of sample frames of audio data that can be written to
 187      * the mixer, via this data line, without blocking. Note that the return
 188      * value measures sample frames, not bytes.
 189      *
 190      * @return the number of sample frames currently available for writing
 191      * @see TargetDataLine#availableRead
 192      */
 193     //public int availableWrite();
 194 }