O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Play Non-Trivial Audio #76
Chapter 10, Audio
|
387
HACK
Grabbing a DataLine
This hack is going to play an uncompressed (i.e., PCM) AIFF or WAV file of
arbitrary length by getting a
DataLine for the data and then repeatedly read-
ing the data from disk and writing it to the
DataLine
.
PCM stands for Pulse Code Modulation, which means that
analog audio has been sampled at regular intervals and quan-
tized (i.e., each sample is expressed as a numeric value). It’s
the lowest-level, most common denominator data that Java-
Sound understands, since it can be delivered directly to a
sound system for playback.
The class to do this will be called PCMFilePlayer. Given a file, its responsibil-
ities are to:
1. Verify that the file contains PCM data (signed or unsigned).
2. Get a
Line for this format.
3. Kick off a thread to read bytes from the file and write them to the
Line,
which plays them.
Reading and writing bytes doesn’t sound too bad, but JavaSound imposes
another requirement on you: you have to send complete frames, not just a
bunch of bytes, to the
Line. A frame is one complete sample of audio in
whatever format you’re dealing with. For the PCM formats supported by
this hack, a frame can be one of three sizes:
1 byte for 8-bit mono sound
2 bytes for either 8-bit stereo sound or 16-bit mono sound
4 bytes for 16-bit stereo sound
The implication for the read-write loop is that if you read some number of
bytes that leave you off an even frame boundary, then you have to save the
partial frame you’ve read, not send it to the
Line, and instead append it to
the beginning of the next read.
Finally, when you reach the end of the file, you need to call
Line.drain( ) to
make sure it plays out all the data you’ve sent it, and then close the
Line.
The code for the
PCMLinePlayer is shown in Example 10-8.
Example 10-8. Playing uncompressed audio files in JavaSound
import javax.sound.sampled.*;
public class PCMFilePlayer implements Runnable {
File file;
388
|
Chapter 10, Audio
#76 Play Non-Trivial Audio
HACK
AudioInputStream in;
SourceDataLine line;
int frameSize;
byte[] buffer = new byte [32 * 1024]; // 32k is arbitrary
Thread playThread;
boolean playing;
boolean notYetEOF;
public PCMFilePlayer (File f)
throws IOException,
UnsupportedAudioFileException,
LineUnavailableException {
file = f;
in = AudioSystem.getAudioInputStream (f);
AudioFormat format = in.getFormat( );
AudioFormat.Encoding formatEncoding = format.getEncoding( );
if (! (formatEncoding.equals (AudioFormat.Encoding.PCM_SIGNED) ||
formatEncoding.equals (AudioFormat.Encoding.PCM_UNSIGNED)))
throw new UnsupportedAudioFileException (
file.getName( ) + " is not PCM audio");
System.out.println ("got PCM format");
frameSize = format.getFrameSize( );
DataLine.Info info =
new DataLine.Info (SourceDataLine.class, format);
System.out.println ("got info");
line = (SourceDataLine) AudioSystem.getLine (info);
System.out.println ("got line");
line.open( );
System.out.println ("opened line");
playThread = new Thread (this);
playing = false;
notYetEOF = true;
playThread.start( );
}
public void run( ) {
int readPoint = 0;
int bytesRead = 0;
try {
while (notYetEOF) {
if (playing) {
bytesRead = in.read (buffer,
readPoint,
buffer.length - readPoint);
if (bytesRead == -1) {
notYetEOF = false;
break;
}
Example 10-8. Playing uncompressed audio files in JavaSound (continued)
Play Non-Trivial Audio #76
Chapter 10, Audio
|
389
HACK
// how many frames did we get,
// and how many are left over?
int frames = bytesRead / frameSize;
int leftover = bytesRead % frameSize;
// send to line
line.write (buffer, readPoint, bytesRead-leftover);
// save the leftover bytes
System.arraycopy (buffer, bytesRead,
buffer, 0,
leftover);
readPoint = leftover;
} else {
// if not playing
// Thread.yield( );
try { Thread.sleep (10);}
catch (InterruptedException ie) {}
}
} // while notYetEOF
System.out.println ("reached eof");
line.drain( );
line.stop( );
} catch (IOException ioe) {
ioe.printStackTrace( );
} finally {
// line.close( );
}
} // run
public void start( ) {
playing = true;
if (! playThread.isAlive( ))
playThread.start( );
line.start( );
}
public void stop( ) {
playing = false;
line.stop( );
}
public SourceDataLine getLine( ) {
return line;
}
public File getFile( ) {
return file;
}
}
Example 10-8. Playing uncompressed audio files in JavaSound (continued)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required