1 /*
2 * Copyright 1999-2003 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.sound.sampled;
27
28
29 /**
30 * A source data line is a data line to which data may be written. It acts as
31 * a source to its mixer. An application writes audio bytes to a source data line,
32 * which handles the buffering of the bytes and delivers them to the mixer.
33 * The mixer may mix the samples with those from other sources and then deliver
34 * the mix to a target such as an output port (which may represent an audio output
35 * device on a sound card).
36 * <p>
37 * Note that the naming convention for this interface reflects the relationship
38 * between the line and its mixer. From the perspective of an application,
39 * a source data line may act as a target for audio data.
40 * <p>
41 * A source data line can be obtained from a mixer by invoking the
42 * <code>{@link Mixer#getLine getLine}</code> method of <code>Mixer</code> with
43 * an appropriate <code>{@link DataLine.Info}</code> object.
44 * <p>
45 * The <code>SourceDataLine</code> interface provides a method for writing
46 * audio data to the data line's buffer. Applications that play or mix
47 * audio should write data to the source data line quickly enough to keep the
48 * buffer from underflowing (emptying), which could cause discontinuities in
49 * the audio that are perceived as clicks. Applications can use the
50 * <code>{@link DataLine#available available}</code> method defined in the
51 * <code>DataLine</code> interface to determine the amount of data currently
52 * queued in the data line's buffer. The amount of data which can be written
53 * to the buffer without blocking is the difference between the buffer size
54 * and the amount of queued data. If the delivery of audio output
55 * stops due to underflow, a <code>{@link LineEvent.Type#STOP STOP}</code> event is
56 * generated. A <code>{@link LineEvent.Type#START START}</code> event is generated
57 * when the audio output resumes.
58 *
59 * @author Kara Kytle
60 * @see Mixer
61 * @see DataLine
62 * @see TargetDataLine
63 * @since 1.3
64 */
65 public interface SourceDataLine extends DataLine {
66
67
68 /**
69 * Opens the line with the specified format and suggested buffer size,
70 * causing the line to acquire any required
71 * system resources and become operational.
72 * <p>
73 * The buffer size is specified in bytes, but must represent an integral
74 * number of sample frames. Invoking this method with a requested buffer
75 * size that does not meet this requirement may result in an
76 * IllegalArgumentException. The actual buffer size for the open line may
77 * differ from the requested buffer size. The value actually set may be
78 * queried by subsequently calling <code>{@link DataLine#getBufferSize}</code>.
79 * <p>
80 * If this operation succeeds, the line is marked as open, and an
81 * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the
82 * line's listeners.
83 * <p>
84 * Invoking this method on a line which is already open is illegal
85 * and may result in an <code>IllegalStateException</code>.
86 * <p>
87 * Note that some lines, once closed, cannot be reopened. Attempts
88 * to reopen such a line will always result in a
89 * <code>LineUnavailableException</code>.
90 *
91 * @param format the desired audio format
92 * @param bufferSize the desired buffer size
93 * @throws LineUnavailableException if the line cannot be
94 * opened due to resource restrictions
95 * @throws IllegalArgumentException if the buffer size does not represent
96 * an integral number of sample frames,
97 * or if <code>format</code> is not fully specified or invalid
98 * @throws IllegalStateException if the line is already open
99 * @throws SecurityException if the line cannot be
100 * opened due to security restrictions
101 *
102 * @see #open(AudioFormat)
103 * @see Line#open
104 * @see Line#close
105 * @see Line#isOpen
106 * @see LineEvent
107 */
108 public void open(AudioFormat format, int bufferSize) throws LineUnavailableException;
109
110
111 /**
112 * Opens the line with the specified format, causing the line to acquire any
113 * required system resources and become operational.
114 *
115 * <p>
116 * The implementation chooses a buffer size, which is measured in bytes but
117 * which encompasses an integral number of sample frames. The buffer size
118 * that the system has chosen may be queried by subsequently calling
119 * <code>{@link DataLine#getBufferSize}</code>.
120 * <p>
121 * If this operation succeeds, the line is marked as open, and an
122 * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the
123 * line's listeners.
124 * <p>
125 * Invoking this method on a line which is already open is illegal
126 * and may result in an <code>IllegalStateException</code>.
127 * <p>
128 * Note that some lines, once closed, cannot be reopened. Attempts
129 * to reopen such a line will always result in a
130 * <code>LineUnavailableException</code>.
131 *
132 * @param format the desired audio format
133 * @throws LineUnavailableException if the line cannot be
134 * opened due to resource restrictions
135 * @throws IllegalArgumentException if <code>format</code>
136 * is not fully specified or invalid
137 * @throws IllegalStateException if the line is already open
138 * @throws SecurityException if the line cannot be
139 * opened due to security restrictions
140 *
141 * @see #open(AudioFormat, int)
142 * @see Line#open
143 * @see Line#close
144 * @see Line#isOpen
145 * @see LineEvent
146 */
147 public void open(AudioFormat format) throws LineUnavailableException;
148
149
150 /**
151 * Writes audio data to the mixer via this source data line. The requested
152 * number of bytes of data are read from the specified array,
153 * starting at the given offset into the array, and written to the data
154 * line's buffer. If the caller attempts to write more data than can
155 * currently be written (see <code>{@link DataLine#available available}</code>),
156 * this method blocks until the requested amount of data has been written.
157 * This applies even if the requested amount of data to write is greater
158 * than the data line's buffer size. However, if the data line is closed,
159 * stopped, or flushed before the requested amount has been written,
160 * the method no longer blocks, but returns the number of bytes
161 * written thus far.
162 * <p>
163 * The number of bytes that can be written without blocking can be ascertained
164 * using the <code>{@link DataLine#available available}</code> method of the
165 * <code>DataLine</code> interface. (While it is guaranteed that
166 * this number of bytes can be written without blocking, there is no guarantee
167 * that attempts to write additional data will block.)
168 * <p>
169 * The number of bytes to write must represent an integral number of
170 * sample frames, such that:
171 * <br>
172 * <center><code>[ bytes written ] % [frame size in bytes ] == 0</code></center>
173 * <br>
174 * The return value will always meet this requirement. A request to write a
175 * number of bytes representing a non-integral number of sample frames cannot
176 * be fulfilled and may result in an <code>IllegalArgumentException</code>.
177 *
178 * @param b a byte array containing data to be written to the data line
179 * @param len the length, in bytes, of the valid data in the array
180 * (in other words, the requested amount of data to write, in bytes)
181 * @param off the offset from the beginning of the array, in bytes
182 * @return the number of bytes actually written
183 * @throws IllegalArgumentException if the requested number of bytes does
184 * not represent an integral number of sample frames,
185 * or if <code>len</code> is negative
186 * @throws ArrayIndexOutOfBoundsException if <code>off</code> is negative,
187 * or <code>off+len</code> is greater than the length of the array
188 * <code>b</code>.
189 *
190 * @see TargetDataLine#read
191 * @see DataLine#available
192 */
193 public int write(byte[] b, int off, int len);
194
195 /**
196 * Obtains the number of sample frames of audio data that can be written to
197 * the mixer, via this data line, without blocking. Note that the return
198 * value measures sample frames, not bytes.
199 * @return the number of sample frames currently available for writing
200 * @see TargetDataLine#availableRead
201 */
202 //public int availableWrite();
203 }