49 import javax.sound.sampled.LineEvent;
50 import javax.sound.sampled.LineListener;
51 import javax.sound.sampled.SourceDataLine;
52 import javax.sound.sampled.UnsupportedAudioFileException;
53
54 /**
55 * Java Sound audio clip;
56 *
57 * @author Arthur van Hoff, Kara Kytle, Jan Borgersen
58 * @author Florian Bomers
59 */
60 @SuppressWarnings("deprecation")
61 public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, LineListener {
62
63 private static final boolean DEBUG = false;
64 private static final int BUFFER_SIZE = 16384; // number of bytes written each time to the source data line
65
66 private long lastPlayCall = 0;
67 private static final int MINIMUM_PLAY_DELAY = 30;
68
69 private byte loadedAudio[] = null;
70 private int loadedAudioByteLength = 0;
71 private AudioFormat loadedAudioFormat = null;
72
73 private AutoClosingClip clip = null;
74 private boolean clipLooping = false;
75
76 private DataPusher datapusher = null;
77
78 private Sequencer sequencer = null;
79 private Sequence sequence = null;
80 private boolean sequencerloop = false;
81 private volatile boolean success;
82
83 /**
84 * used for determining how many samples is the
85 * threshhold between playing as a Clip and streaming
86 * from the file.
87 *
88 * $$jb: 11.07.99: the engine has a limit of 1M
89 * samples to play as a Clip, so compare this number
366 } else {
367 intLen = (int) byteLen;
368 }
369 loadedAudio = new byte[intLen];
370 loadedAudioByteLength = 0;
371
372 // this loop may throw an IOException
373 while (true) {
374 int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
375 if (bytesRead <= 0) {
376 as.close();
377 break;
378 }
379 loadedAudioByteLength += bytesRead;
380 }
381 }
382
383 private void readStream(AudioInputStream as) throws IOException {
384
385 DirectBAOS baos = new DirectBAOS();
386 byte buffer[] = new byte[16384];
387 int bytesRead = 0;
388 int totalBytesRead = 0;
389
390 // this loop may throw an IOException
391 while( true ) {
392 bytesRead = as.read(buffer, 0, buffer.length);
393 if (bytesRead <= 0) {
394 as.close();
395 break;
396 }
397 totalBytesRead += bytesRead;
398 baos.write(buffer, 0, bytesRead);
399 }
400 loadedAudio = baos.getInternalBuffer();
401 loadedAudioByteLength = totalBytesRead;
402 }
403
404 // METHODS FOR CREATING THE DEVICE
405
406 private boolean createClip() {
|
49 import javax.sound.sampled.LineEvent;
50 import javax.sound.sampled.LineListener;
51 import javax.sound.sampled.SourceDataLine;
52 import javax.sound.sampled.UnsupportedAudioFileException;
53
54 /**
55 * Java Sound audio clip;
56 *
57 * @author Arthur van Hoff, Kara Kytle, Jan Borgersen
58 * @author Florian Bomers
59 */
60 @SuppressWarnings("deprecation")
61 public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, LineListener {
62
63 private static final boolean DEBUG = false;
64 private static final int BUFFER_SIZE = 16384; // number of bytes written each time to the source data line
65
66 private long lastPlayCall = 0;
67 private static final int MINIMUM_PLAY_DELAY = 30;
68
69 private byte[] loadedAudio = null;
70 private int loadedAudioByteLength = 0;
71 private AudioFormat loadedAudioFormat = null;
72
73 private AutoClosingClip clip = null;
74 private boolean clipLooping = false;
75
76 private DataPusher datapusher = null;
77
78 private Sequencer sequencer = null;
79 private Sequence sequence = null;
80 private boolean sequencerloop = false;
81 private volatile boolean success;
82
83 /**
84 * used for determining how many samples is the
85 * threshhold between playing as a Clip and streaming
86 * from the file.
87 *
88 * $$jb: 11.07.99: the engine has a limit of 1M
89 * samples to play as a Clip, so compare this number
366 } else {
367 intLen = (int) byteLen;
368 }
369 loadedAudio = new byte[intLen];
370 loadedAudioByteLength = 0;
371
372 // this loop may throw an IOException
373 while (true) {
374 int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
375 if (bytesRead <= 0) {
376 as.close();
377 break;
378 }
379 loadedAudioByteLength += bytesRead;
380 }
381 }
382
383 private void readStream(AudioInputStream as) throws IOException {
384
385 DirectBAOS baos = new DirectBAOS();
386 byte[] buffer = new byte[16384];
387 int bytesRead = 0;
388 int totalBytesRead = 0;
389
390 // this loop may throw an IOException
391 while( true ) {
392 bytesRead = as.read(buffer, 0, buffer.length);
393 if (bytesRead <= 0) {
394 as.close();
395 break;
396 }
397 totalBytesRead += bytesRead;
398 baos.write(buffer, 0, bytesRead);
399 }
400 loadedAudio = baos.getInternalBuffer();
401 loadedAudioByteLength = totalBytesRead;
402 }
403
404 // METHODS FOR CREATING THE DEVICE
405
406 private boolean createClip() {
|