src/share/classes/javax/sound/sampled/TargetDataLine.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2003, 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 target data line is a type of <code>{@link DataLine}</code> from which
  30  * audio data can be read.  The most common example is a data line that gets
  31  * its data from an audio capture device.  (The device is implemented as a
  32  * mixer that writes to the target data line.)
  33  * <p>
  34  * Note that the naming convention for this interface reflects the relationship
  35  * between the line and its mixer.  From the perspective of an application,
  36  * a target data line may act as a source for audio data.
  37  * <p>
  38  * The target data line can be obtained from a mixer by invoking the
  39  * <code>{@link Mixer#getLine getLine}</code>
  40  * method of <code>Mixer</code> with an appropriate
  41  * <code>{@link DataLine.Info}</code> object.
  42  * <p>
  43  * The <code>TargetDataLine</code> interface provides a method for reading the
  44  * captured data from the target data line's buffer.Applications
  45  * that record audio should read data from the target data line quickly enough
  46  * to keep the buffer from overflowing, which could cause discontinuities in
  47  * the captured data that are perceived as clicks.  Applications can use the
  48  * <code>{@link DataLine#available available}</code> method defined in the
  49  * <code>DataLine</code> interface to determine the amount of data currently
  50  * queued in the data line's buffer.  If the buffer does overflow,
  51  * the oldest queued data is discarded and replaced by new data.
  52  *
  53  * @author Kara Kytle
  54  * @see Mixer
  55  * @see DataLine
  56  * @see SourceDataLine
  57  * @since 1.3
  58  */
  59 public interface TargetDataLine extends DataLine {
  60 
  61 
  62     /**
  63      * Opens the line with the specified format and requested buffer size,
  64      * causing the line to acquire any required system resources and become
  65      * operational.
  66      * <p>
  67      * The buffer size is specified in bytes, but must represent an integral
  68      * number of sample frames.  Invoking this method with a requested buffer
  69      * size that does not meet this requirement may result in an
  70      * IllegalArgumentException.  The actual buffer size for the open line may
  71      * differ from the requested buffer size.  The value actually set may be
  72      * queried by subsequently calling <code>{@link DataLine#getBufferSize}</code>
  73      * <p>
  74      * If this operation succeeds, the line is marked as open, and an
  75      * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the
  76      * line's listeners.
  77      * <p>
  78      * Invoking this method on a line that is already open is illegal
  79      * and may result in an <code>IllegalStateException</code>.
  80      * <p>
  81      * Some lines, once closed, cannot be reopened.  Attempts
  82      * to reopen such a line will always result in a
  83      * <code>LineUnavailableException</code>.
  84      *
  85      * @param format the desired audio format
  86      * @param bufferSize the desired buffer size, in bytes.
  87      * @throws LineUnavailableException if the line cannot be
  88      * opened due to resource restrictions
  89      * @throws IllegalArgumentException if the buffer size does not represent
  90      * an integral number of sample frames,
  91      * or if <code>format</code> is not fully specified or invalid
  92      * @throws IllegalStateException if the line is already open
  93      * @throws SecurityException if the line cannot be
  94      * opened due to security restrictions
  95      *
  96      * @see #open(AudioFormat)
  97      * @see Line#open
  98      * @see Line#close
  99      * @see Line#isOpen
 100      * @see LineEvent
 101      */
 102     public void open(AudioFormat format, int bufferSize) throws LineUnavailableException;
 103 
 104 
 105     /**
 106      * Opens the line with the specified format, causing the line to acquire any
 107      * required system resources and become operational.
 108      *
 109      * <p>
 110      * The implementation chooses a buffer size, which is measured in bytes but
 111      * which encompasses an integral number of sample frames.  The buffer size
 112      * that the system has chosen may be queried by subsequently calling <code>{@link DataLine#getBufferSize}</code>

 113      * <p>
 114      * If this operation succeeds, the line is marked as open, and an
 115      * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the
 116      * line's listeners.
 117      * <p>
 118      * Invoking this method on a line that is already open is illegal
 119      * and may result in an <code>IllegalStateException</code>.
 120      * <p>
 121      * Some lines, once closed, cannot be reopened.  Attempts
 122      * to reopen such a line will always result in a
 123      * <code>LineUnavailableException</code>.
 124      *
 125      * @param format the desired audio format
 126      * @throws LineUnavailableException if the line cannot be
 127      * opened due to resource restrictions
 128      * @throws IllegalArgumentException if <code>format</code>
 129      * is not fully specified or invalid
 130      * @throws IllegalStateException if the line is already open
 131      * @throws SecurityException if the line cannot be
 132      * opened due to security restrictions
 133      *
 134      * @see #open(AudioFormat, int)
 135      * @see Line#open
 136      * @see Line#close
 137      * @see Line#isOpen
 138      * @see LineEvent
 139      */
 140     public void open(AudioFormat format) throws LineUnavailableException;
 141 
 142 
 143     /**
 144      * Reads audio data from the data line's input buffer.   The requested
 145      * number of bytes is read into the specified array, starting at
 146      * the specified offset into the array in bytes.  This method blocks until
 147      * the requested amount of data has been read.  However, if the data line
 148      * is closed, stopped, drained, or flushed before the requested amount has
 149      * been read, the method no longer blocks, but returns the number of bytes
 150      * read thus far.
 151      * <p>
 152      * The number of bytes that can be read without blocking can be ascertained
 153      * using the <code>{@link DataLine#available available}</code> method of the
 154      * <code>DataLine</code> interface.  (While it is guaranteed that
 155      * this number of bytes can be read without blocking, there is no guarantee
 156      * that attempts to read additional data will block.)
 157      * <p>
 158      * The number of bytes to be read must represent an integral number of
 159      * sample frames, such that:
 160      * <br>
 161      * <center><code>[ bytes read ] % [frame size in bytes ] == 0</code></center>
 162      * <br>
 163      * The return value will always meet this requirement.  A request to read a
 164      * number of bytes representing a non-integral number of sample frames cannot
 165      * be fulfilled and may result in an IllegalArgumentException.
 166      *
 167      * @param b a byte array that will contain the requested input data when
 168      * this method returns
 169      * @param off the offset from the beginning of the array, in bytes
 170      * @param len the requested number of bytes to read
 171      * @return the number of bytes actually read
 172      * @throws IllegalArgumentException if the requested number of bytes does
 173      * not represent an integral number of sample frames.
 174      * or if <code>len</code> is negative.
 175      * @throws ArrayIndexOutOfBoundsException if <code>off</code> is negative,
 176      * or <code>off+len</code> is greater than the length of the array
 177      * <code>b</code>.
 178      *
 179      * @see SourceDataLine#write
 180      * @see DataLine#available
 181      */
 182     public int read(byte[] b, int off, int len);
 183 
 184     /**
 185      * Obtains the number of sample frames of audio data that can be read from
 186      * the target data line without blocking.  Note that the return value
 187      * measures sample frames, not bytes.

 188      * @return the number of sample frames currently available for reading
 189      * @see SourceDataLine#availableWrite
 190      */
 191     //public int availableRead();
 192 }
   1 /*
   2  * Copyright (c) 1999, 2014, 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 target data line is a type of {@link DataLine} from which audio data can be
  30  * read. The most common example is a data line that gets its data from an audio
  31  * capture device. (The device is implemented as a mixer that writes to the
  32  * target data line.)
  33  * <p>
  34  * Note that the naming convention for this interface reflects the relationship
  35  * between the line and its mixer. From the perspective of an application, a
  36  * target data line may act as a source for audio data.
  37  * <p>
  38  * The target data line can be obtained from a mixer by invoking the
  39  * {@link Mixer#getLine getLine} method of {@code Mixer} with an appropriate
  40  * {@link DataLine.Info} object.
  41  * <p>
  42  * The {@code TargetDataLine} interface provides a method for reading the
  43  * captured data from the target data line's buffer. Applications that record
  44  * audio should read data from the target data line quickly enough to keep the
  45  * buffer from overflowing, which could cause discontinuities in the captured
  46  * data that are perceived as clicks. Applications can use the
  47  * {@link DataLine#available available} method defined in the {@code DataLine}
  48  * interface to determine the amount of data currently queued in the data line's
  49  * buffer. If the buffer does overflow, the oldest queued data is discarded and
  50  * replaced by new data.

  51  *
  52  * @author Kara Kytle
  53  * @see Mixer
  54  * @see DataLine
  55  * @see SourceDataLine
  56  * @since 1.3
  57  */
  58 public interface TargetDataLine extends DataLine {
  59 

  60     /**
  61      * Opens the line with the specified format and requested buffer size,
  62      * causing the line to acquire any required system resources and become
  63      * operational.
  64      * <p>
  65      * The buffer size is specified in bytes, but must represent an integral
  66      * number of sample frames. Invoking this method with a requested buffer
  67      * size that does not meet this requirement may result in an
  68      * {@code IllegalArgumentException}. The actual buffer size for the open
  69      * line may differ from the requested buffer size. The value actually set
  70      * may be queried by subsequently calling {@link DataLine#getBufferSize}
  71      * <p>
  72      * If this operation succeeds, the line is marked as open, and an
  73      * {@link LineEvent.Type#OPEN OPEN} event is dispatched to the line's
  74      * listeners.
  75      * <p>
  76      * Invoking this method on a line that is already open is illegal and may
  77      * result in an {@code IllegalStateException}.
  78      * <p>
  79      * Some lines, once closed, cannot be reopened. Attempts to reopen such a
  80      * line will always result in a {@code LineUnavailableException}.

  81      *
  82      * @param  format the desired audio format
  83      * @param  bufferSize the desired buffer size, in bytes
  84      * @throws LineUnavailableException if the line cannot be opened due to
  85      *         resource restrictions
  86      * @throws IllegalArgumentException if the buffer size does not represent an
  87      *         integral number of sample frames, or if {@code format} is not
  88      *         fully specified or invalid
  89      * @throws IllegalStateException if the line is already open
  90      * @throws SecurityException if the line cannot be opened due to security
  91      *         restrictions

  92      * @see #open(AudioFormat)
  93      * @see Line#open
  94      * @see Line#close
  95      * @see Line#isOpen
  96      * @see LineEvent
  97      */
  98     void open(AudioFormat format, int bufferSize) throws LineUnavailableException;

  99 
 100     /**
 101      * Opens the line with the specified format, causing the line to acquire any
 102      * required system resources and become operational.

 103      * <p>
 104      * The implementation chooses a buffer size, which is measured in bytes but
 105      * which encompasses an integral number of sample frames. The buffer size
 106      * that the system has chosen may be queried by subsequently calling
 107      * {@link DataLine#getBufferSize}
 108      * <p>
 109      * If this operation succeeds, the line is marked as open, and an
 110      * {@link LineEvent.Type#OPEN OPEN} event is dispatched to the line's
 111      * listeners.
 112      * <p>
 113      * Invoking this method on a line that is already open is illegal and may
 114      * result in an {@code IllegalStateException}.
 115      * <p>
 116      * Some lines, once closed, cannot be reopened. Attempts to reopen such a
 117      * line will always result in a {@code LineUnavailableException}.

 118      *
 119      * @param  format the desired audio format
 120      * @throws LineUnavailableException if the line cannot be opened due to
 121      *         resource restrictions
 122      * @throws IllegalArgumentException if {@code format} is not fully specified
 123      *         or invalid
 124      * @throws IllegalStateException if the line is already open
 125      * @throws SecurityException if the line cannot be opened due to security
 126      *         restrictions

 127      * @see #open(AudioFormat, int)
 128      * @see Line#open
 129      * @see Line#close
 130      * @see Line#isOpen
 131      * @see LineEvent
 132      */
 133     void open(AudioFormat format) throws LineUnavailableException;

 134 
 135     /**
 136      * Reads audio data from the data line's input buffer. The requested number
 137      * of bytes is read into the specified array, starting at the specified
 138      * offset into the array in bytes. This method blocks until the requested
 139      * amount of data has been read. However, if the data line is closed,
 140      * stopped, drained, or flushed before the requested amount has been read,
 141      * the method no longer blocks, but returns the number of bytes read thus
 142      * far.
 143      * <p>
 144      * The number of bytes that can be read without blocking can be ascertained
 145      * using the {@link DataLine#available available} method of the
 146      * {@code DataLine} interface. (While it is guaranteed that this number of
 147      * bytes can be read without blocking, there is no guarantee that attempts
 148      * to read additional data will block.)
 149      * <p>
 150      * The number of bytes to be read must represent an integral number of
 151      * sample frames, such that:
 152      * <br>
 153      * <center>{@code [ bytes read ] % [frame size in bytes ] == 0}</center>
 154      * <br>
 155      * The return value will always meet this requirement. A request to read a
 156      * number of bytes representing a non-integral number of sample frames
 157      * cannot be fulfilled and may result in an IllegalArgumentException.
 158      *
 159      * @param  b a byte array that will contain the requested input data when
 160      *         this method returns
 161      * @param  off the offset from the beginning of the array, in bytes
 162      * @param  len the requested number of bytes to read
 163      * @return the number of bytes actually read
 164      * @throws IllegalArgumentException if the requested number of bytes does
 165      *         not represent an integral number of sample frames, or if
 166      *         {@code len} is negative
 167      * @throws ArrayIndexOutOfBoundsException if {@code off} is negative, or
 168      *         {@code off+len} is greater than the length of the array {@code b}

 169      *
 170      * @see SourceDataLine#write
 171      * @see DataLine#available
 172      */
 173     int read(byte[] b, int off, int len);
 174 
 175     /**
 176      * Obtains the number of sample frames of audio data that can be read from
 177      * the target data line without blocking. Note that the return value
 178      * measures sample frames, not bytes.
 179      *
 180      * @return the number of sample frames currently available for reading
 181      * @see SourceDataLine#availableWrite
 182      */
 183     //public int availableRead();
 184 }