API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.sound.sampled. DataLine View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480

/*
 * @(#)DataLine.java	1.34 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.sound.sampled;

/**
 * <code>DataLine</code> adds media-related functionality to its
 * superinterface, <code>{@link Line}</code>.  This functionality includes
 * transport-control methods that start, stop, drain, and flush
 * the audio data that passes through the line.  A data line can also
 * report the current position, volume, and audio format of the media.
 * Data lines are used for output of audio by means of the
 * subinterfaces <code>{@link SourceDataLine}</code> or
 * <code>{@link Clip}</code>, which allow an application program to write data.  Similarly,
 * audio input is handled by the subinterface <code>{@link TargetDataLine}</code>,
 * which allows data to be read.
 * <p>
 * A data line has an internal buffer in which
 * the incoming or outgoing audio data is queued.  The
 * <code>{@link #drain()}</code> method blocks until this internal buffer
 * becomes empty, usually because all queued data has been processed.  The
 * <code>{@link #flush()}</code> method discards any available queued data
 * from the internal buffer.
 * <p>
 * A data line produces <code>{@link LineEvent.Type#START START}</code> and
 * <code>{@link LineEvent.Type#STOP STOP}</code> events whenever
 * it begins or ceases active presentation or capture of data.  These events
 * can be generated in response to specific requests, or as a result of
 * less direct state changes.  For example, if <code>{@link #start()}</code> is called
 * on an inactive data line, and data is available for capture or playback, a
 * <code>START</code> event will be generated shortly, when data playback
 * or capture actually begins.  Or, if the flow of data to an active data
 * line is constricted so that a gap occurs in the presentation of data,
 * a <code>STOP</code> event is generated.
 * <p>
 * Mixers often support synchronized control of multiple data lines.
 * Synchronization can be established through the Mixer interface's
 * <code>{@link Mixer#synchronize synchronize}</code> method.
 * See the description of the <code>{@link Mixer Mixer}</code> interface
 * for a more complete description.
 *
 * @author Kara Kytle
 * @version 1.34, 05/11/17
 * @see LineEvent
 * @since 1.3
 */
public interface DataLine extends Line {
    
    
    /**
     * Drains queued data from the line by continuing data I/O until the
     * data line's internal buffer has been emptied.
     * This method blocks until the draining is complete.  Because this is a
     * blocking method, it should be used with care.  If <code>drain()</code>
     * is invoked on a stopped line that has data in its queue, the method will
     * block until the line is running and the data queue becomes empty.  If
     * <code>drain()</code> is invoked by one thread, and another continues to
     * fill the data queue, the operation will not complete.
     * This method always returns when the data line is closed.
     *
     * @see #flush()
     */
    public void drain();
    
    /**
     * Flushes queued data from the line.  The flushed data is discarded.
     * In some cases, not all queued data can be discarded.  For example, a
     * mixer can flush data from the buffer for a specific input line, but any
     * unplayed data already in the output buffer (the result of the mix) will
     * still be played.  You can invoke this method after pausing a line (the
     * normal case) if you want to skip the "stale" data when you restart
     * playback or capture. (It is legal to flush a line that is not stopped,
     * but doing so on an active line is likely to cause a discontinuity in the
     * data, resulting in a perceptible click.)
     *
     * @see #stop()
     * @see #drain()
     */
    public void flush();
    
    /**
     * Allows a line to engage in data I/O.  If invoked on a line
     * that is already running, this method does nothing.  Unless the data in
     * the buffer has been flushed, the line resumes I/O starting
     * with the first frame that was unprocessed at the time the line was
     * stopped. When audio capture or playback starts, a
     * <code>{@link LineEvent.Type#START START}</code> event is generated.
     *
     * @see #stop()
     * @see #isRunning()
     * @see LineEvent
     */
    public void start();
    
    /**
     * Stops the line.  A stopped line should cease I/O activity.
     * If the line is open and running, however, it should retain the resources required
     * to resume activity.  A stopped line should retain any audio data in its buffer
     * instead of discarding it, so that upon resumption the I/O can continue where it left off,
     * if possible.  (This doesn't guarantee that there will never be discontinuities beyond the
     * current buffer, of course; if the stopped condition continues
     * for too long, input or output samples might be dropped.)  If desired, the retained data can be
     * discarded by invoking the <code>flush</code> method.
     * When audio capture or playback stops, a <code>{@link LineEvent.Type#STOP STOP}</code> event is generated.
     *
     * @see #start()
     * @see #isRunning()
     * @see #flush()
     * @see LineEvent
     */
    public void stop();
    
    /**
     * Indicates whether the line is running.  The default is <code>false</code>.
     * An open line begins running when the first data is presented in response to an
     * invocation of the <code>start</code> method, and continues
     * until presentation ceases in response to a call to <code>stop</code> or
     * because playback completes.
     * @return <code>true</code> if the line is running, otherwise <code>false</code>
     * @see #start()
     * @see #stop()
     */
    public boolean isRunning();
    
    /**
     * Indicates whether the line is engaging in active I/O (such as playback
     * or capture).  When an inactive line becomes active, it sends a
     * <code>{@link LineEvent.Type#START START}</code> event to its listeners.  Similarly, when
     * an active line becomes inactive, it sends a
     * <code>{@link LineEvent.Type#STOP STOP}</code> event.
     * @return <code>true</code> if the line is actively capturing or rendering
     * sound, otherwise <code>false</code>
     * @see #isOpen
     * @see #addLineListener
     * @see #removeLineListener
     * @see LineEvent
     * @see LineListener
     */
    public boolean isActive();
    
    /**
     * Obtains the current format (encoding, sample rate, number of channels,
     * etc.) of the data line's audio data.
     *
     * <p>If the line is not open and has never been opened, it returns
     * the default format. The default format is an implementation
     * specific audio format, or, if the <code>DataLine.Info</code>
     * object, which was used to retrieve this <code>DataLine</code>,
     * specifies at least one fully qualified audio format, the
     * last one will be used as the default format. Opening the
     * line with a specific audio format (e.g.
     * {@link SourceDataLine#open(AudioFormat)}) will override the
     * default format.
     *
     * @return current audio data format
     * @see AudioFormat
     */
    public AudioFormat getFormat();
    
    /**
     * Obtains the maximum number of bytes of data that will fit in the data line's
     * internal buffer.  For a source data line, this is the size of the buffer to
     * which data can be written.  For a target data line, it is the size of
     * the buffer from which data can be read.  Note that
     * the units used are bytes, but will always correspond to an integral
     * number of sample frames of audio data.
     *
     * @return the size of the buffer in bytes
     */
    public int getBufferSize();
    
    /**
     * Obtains the number of bytes of data currently available to the
     * application for processing in the data line's internal buffer.  For a
     * source data line, this is the amount of data that can be written to the
     * buffer without blocking.  For a target data line, this is the amount of data
     * available to be read by the application.  For a clip, this value is always
     * 0 because the audio data is loaded into the buffer when the clip is opened,
     * and persists without modification until the clip is closed.
     * <p>
     * Note that the units used are bytes, but will always
     * correspond to an integral number of sample frames of audio data.
     * <p>
     * An application is guaranteed that a read or
     * write operation of up to the number of bytes returned from
     * <code>available()</code> will not block; however, there is no guarantee
     * that attempts to read or write more data will block.
     *
     * @return the amount of data available, in bytes
     */
    public int available();
    
    /**
     * Obtains the current position in the audio data, in sample frames.
     * The frame position measures the number of sample
     * frames captured by, or rendered from, the line since it was opened.
     * This return value will wrap around after 2^31 frames. It is recommended
     * to use <code>getLongFramePosition</code> instead.
     *
     * @return the number of frames already processed since the line was opened
     * @see #getLongFramePosition()
     */
    public int getFramePosition();
    

    /**
     * Obtains the current position in the audio data, in sample frames.
     * The frame position measures the number of sample
     * frames captured by, or rendered from, the line since it was opened.
     *
     * @return the number of frames already processed since the line was opened
     * @since 1.5
     */
    public long getLongFramePosition();
 

    /**
     * Obtains the current position in the audio data, in microseconds.
     * The microsecond position measures the time corresponding to the number
     * of sample frames captured by, or rendered from, the line since it was opened.
     * The level of precision is not guaranteed.  For example, an implementation
     * might calculate the microsecond position from the current frame position
     * and the audio sample frame rate.  The precision in microseconds would
     * then be limited to the number of microseconds per sample frame.
     *
     * @return the number of microseconds of data processed since the line was opened
     */
    public long getMicrosecondPosition();
    
    /**
     * Obtains the current volume level for the line.  This level is a measure
     * of the signal's current amplitude, and should not be confused with the
     * current setting of a gain control. The range is from 0.0 (silence) to
     * 1.0 (maximum possible amplitude for the sound waveform).  The units
     * measure linear amplitude, not decibels.
     *
     * @return the current amplitude of the signal in this line, or
     * <code>{@link AudioSystem#NOT_SPECIFIED}</code>
     */
    public float getLevel();
    
    /**
     * Besides the class information inherited from its superclass,
     * <code>DataLine.Info</code> provides additional information specific to data lines.
     * This information includes:
     * <ul>
     * <li> the audio formats supported by the data line
     * <li> the minimum and maximum sizes of its internal buffer
     * </ul>
     * Because a <code>Line.Info</code> knows the class of the line its describes, a
     * <code>DataLine.Info</code> object can describe <code>DataLine</code>
     * subinterfaces such as <code>{@link SourceDataLine}</code>,
     * <code>{@link TargetDataLine}</code>, and <code>{@link Clip}</code>.
     * You can query a mixer for lines of any of these types, passing an appropriate
     * instance of <code>DataLine.Info</code> as the argument to a method such as
     * <code>{@link Mixer#getLine Mixer.getLine(Line.Info)}</code>.
     *
     * @see Line.Info
     * @author Kara Kytle
     * @version 1.34, 05/11/17
     * @since 1.3
     */
    public static class Info extends Line.Info {
	
	private AudioFormat[] formats;
	private int minBufferSize;
	private int maxBufferSize;
	
	/**
	 * Constructs a data line's info object from the specified information,
	 * which includes a set of supported audio formats and a range for the buffer size.
	 * This constructor is typically used by mixer implementations
	 * when returning information about a supported line.
	 *
	 * @param lineClass the class of the data line described by the info object
	 * @param formats set of formats supported
	 * @param minBufferSize minimum buffer size supported by the data line, in bytes
	 * @param maxBufferSize maximum buffer size supported by the data line, in bytes
	 */
	public Info(Class<?> lineClass, AudioFormat[] formats, int minBufferSize, int maxBufferSize) {
	    
	    super(lineClass);
	    
	    if (formats == null) {
		this.formats = new AudioFormat[0];
	    } else {
		this.formats = formats;
	    }
	    
	    this.minBufferSize = minBufferSize;
	    this.maxBufferSize = maxBufferSize;
	}
	
	
	/**
	 * Constructs a data line's info object from the specified information,
	 * which includes a single audio format and a desired buffer size.
	 * This constructor is typically used by an application to
	 * describe a desired line.
	 *
	 * @param lineClass the class of the data line described by the info object
	 * @param format desired format
	 * @param bufferSize desired buffer size in bytes
	 */
	public Info(Class<?> lineClass, AudioFormat format, int bufferSize) {
	    
	    super(lineClass);
	    
	    if (format == null) {
		this.formats = new AudioFormat[0];
	    } else {
		AudioFormat[] formats = { format };
		this.formats = formats;
	    }
	    
	    this.minBufferSize = bufferSize;
	    this.maxBufferSize = bufferSize;
	}
	
	
	/**
	 * Constructs a data line's info object from the specified information,
	 * which includes a single audio format.
	 * This constructor is typically used by an application to
	 * describe a desired line.
	 *
	 * @param lineClass the class of the data line described by the info object
	 * @param format desired format
	 */
	public Info(Class<?> lineClass, AudioFormat format) {
	    this(lineClass, format, AudioSystem.NOT_SPECIFIED);
	}
	
	
	/**
	 * Obtains a set of audio formats supported by the data line.
	 * Note that <code>isFormatSupported(AudioFormat)</code> might return
	 * <code>true</code> for certain additional formats that are missing from
	 * the set returned by <code>getFormats()</code>.  The reverse is not
	 * the case: <code>isFormatSupported(AudioFormat)</code> is guaranteed to return
	 * <code>true</code> for all formats returned by <code>getFormats()</code>.
	 *
	 * Some fields in the AudioFormat instances can be set to
	 * {@link javax.sound.sampled.AudioSystem#NOT_SPECIFIED NOT_SPECIFIED}
	 * if that field does not apply to the format,
	 * or if the format supports a wide range of values for that field.
	 * For example, a multi-channel device supporting up to
	 * 64 channels, could set the channel field in the
	 * <code>AudioFormat</code> instances returned by this
	 * method to <code>NOT_SPECIFIED</code>.
	 *
	 * @return a set of supported audio formats.
	 * @see #isFormatSupported(AudioFormat)
	 */
	public AudioFormat[] getFormats() {
	    
	    AudioFormat[] returnedArray = new AudioFormat[formats.length];
	    System.arraycopy(formats, 0, returnedArray, 0, formats.length);
	    return returnedArray;
	}
	
	/**
	 * Indicates whether this data line supports a particular audio format.
	 * The default implementation of this method simply returns <code>true</code> if
	 * the specified format matches any of the supported formats.
	 *
	 * @param format the audio format for which support is queried.
	 * @return <code>true</code> if the format is supported, otherwise <code>false</code>
	 * @see #getFormats
	 * @see AudioFormat#matches
	 */
	public boolean isFormatSupported(AudioFormat format) {
	    
	    for (int i = 0; i < formats.length; i++) {
		if (format.matches(formats[i])) {
		    return true;
		}
	    }
	    
	    return false;
	}
	
	/**
	 * Obtains the minimum buffer size supported by the data line.
	 * @return minimum buffer size in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>
	 */
	public int getMinBufferSize() {
	    return minBufferSize;
	}
	
	
	/**
	 * Obtains the maximum buffer size supported by the data line.
	 * @return maximum buffer size in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>
	 */
	public int getMaxBufferSize() {
	    return maxBufferSize;
	}
	
	
	/**
	 * Determines whether the specified info object matches this one.
	 * To match, the superclass match requirements must be met.  In
	 * addition, this object's minimum buffer size must be at least as
	 * large as that of the object specified, its maximum buffer size must
	 * be at most as large as that of the object specified, and all of its
	 * formats must match formats supported by the object specified.
	 * @return <code>true</code> if this object matches the one specified,
	 * otherwise <code>false</code>.
	 */
	public boolean matches(Line.Info info) {
	    
	    if (! (super.matches(info)) ) {
		return false;
	    }
	    
	    Info dataLineInfo = (Info)info;

	    // treat anything < 0 as NOT_SPECIFIED
	    // demo code in old Java Sound Demo used a wrong buffer calculation
	    // that would lead to arbitrary negative values
	    if ((getMaxBufferSize() >= 0) && (dataLineInfo.getMaxBufferSize() >= 0)) {
		if (getMaxBufferSize() > dataLineInfo.getMaxBufferSize()) {
		    return false;
		}
	    }
	    
	    if ((getMinBufferSize() >= 0) && (dataLineInfo.getMinBufferSize() >= 0)) {
		if (getMinBufferSize() < dataLineInfo.getMinBufferSize()) {
		    return false;
		}
	    }
	    
	    AudioFormat[] localFormats = getFormats();
	    
	    if (localFormats != null) {
		
		for (int i = 0; i < localFormats.length; i++) {
		    if (! (localFormats[i] == null) ) {
			if (! (dataLineInfo.isFormatSupported(localFormats[i])) ) {
			    return false;
			}
		    }
		}
	    }
	    
	    return true;
	}
	
	/**
	 * Obtains a textual description of the data line info.
	 * @return a string description
	 */
	public String toString() {
	    
	    StringBuffer buf = new StringBuffer();
	    
	    if ( (formats.length == 1) && (formats[0] != null) ) {
		buf.append(" supporting format " + formats[0]);
	    } else if (getFormats().length > 1) {
		buf.append(" supporting " + getFormats().length + " audio formats");
	    }
	    
	    if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (maxBufferSize != AudioSystem.NOT_SPECIFIED) ) {
		buf.append(", and buffers of " + minBufferSize + " to " + maxBufferSize + " bytes");
	    } else if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (minBufferSize > 0) ) {
		buf.append(", and buffers of at least " + minBufferSize + " bytes");
	    } else if (maxBufferSize != AudioSystem.NOT_SPECIFIED) {
		buf.append(", and buffers of up to " + minBufferSize + " bytes");
	    }
	    
	    return new String(super.toString() + buf);
	}
    } // class Info
    
} // interface DataLine

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar