net.beadsproject.beads.data
Class AudioFile

java.lang.Object
  extended by net.beadsproject.beads.data.AudioFile

public class AudioFile
extends java.lang.Object

Represents an audio file. Handles loading and format conversion. AudioFile is generally not used directly. Rather Sample should be used as it provides many useful features for playing audio files. NOTE: At the moment certain .wav files will be not be able to be loaded. This is due to mp3spi recognizing them incorrectly as mp3s. This will hopefull be fixed in the future, but until then you can resave your .wavs using a different audio util.

Author:
ben

Field Summary
protected  javax.sound.sampled.AudioFileFormat audioFileFormat
           
protected  javax.sound.sampled.AudioInputStream audioInputStream
           
protected  java.io.File file
           
protected  float length
          Length of the file in milliseconds
protected  java.lang.String name
           
protected  int nChannels
          The number of channels.
protected  long nFrames
          The total number of frames.
 boolean trace
          Advanced Trace the open, closing, and resetting of this audio file.
protected  java.net.URL url
           
 
Constructor Summary
AudioFile(java.io.InputStream stream)
           
AudioFile(java.io.InputStream stream, int bufferSize)
           
AudioFile(java.lang.String filename)
          Load an audio file from disk.
AudioFile(java.lang.String filename, int bufferSize)
          Advanced: Create an input stream from a file, but don't keep more than numBytes of data in memory.
 
Method Summary
 int byteDepth()
           
 void close()
          Close the audio file.
 javax.sound.sampled.AudioFormat getDecodedFormat()
           
 javax.sound.sampled.AudioFormat getEncodedFormat()
           
 java.lang.String getName()
           
 long getTimeLengthEstimation(java.util.Map properties)
          THIS CODE IS FROM jlGui PlayerUI.java.
 java.lang.String info()
           
 boolean isClosed()
          Is the file stream closed?
 boolean isOpen()
          Is the file stream open?
 void open()
          Opens the audio file, ready for data access.
 int read(byte[] buffer)
          Read bytes directly from the decoded audiofile.
 int read(float[][] buffer)
          Read decoded audio data in a non-interleaved, Beads-friendly format.
 void reset()
          Reset the audio input stream.
 void seek(int frame)
          Seek to a specific frame number.
 void skip(long frames)
          Skips a number of frames.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

file

protected java.io.File file

url

protected java.net.URL url

audioInputStream

protected javax.sound.sampled.AudioInputStream audioInputStream

name

protected java.lang.String name

audioFileFormat

protected javax.sound.sampled.AudioFileFormat audioFileFormat

nChannels

protected int nChannels
The number of channels.


nFrames

protected long nFrames
The total number of frames. If it equals AudioSystem.NOT_SPECIFIED then the length is unknown.


length

protected float length
Length of the file in milliseconds


trace

public boolean trace
Advanced Trace the open, closing, and resetting of this audio file. Useful to debug and tune the parameters of AudioFile and Sample.

Constructor Detail

AudioFile

public AudioFile(java.lang.String filename)
          throws java.io.IOException,
                 javax.sound.sampled.UnsupportedAudioFileException
Load an audio file from disk. The audiofile needs to be open()'ed before it's data can be read. Note: AudioFile provides low-level access to audio files -- If you just want to access the data of a sound file use a Sample.

Parameters:
filename - The name of the file to open.
Throws:
java.io.IOException - If the file cannot be found or opened.
javax.sound.sampled.UnsupportedAudioFileException - If the file is of an unsupported audio type.
See Also:
Sample

AudioFile

public AudioFile(java.lang.String filename,
                 int bufferSize)
          throws java.io.IOException,
                 javax.sound.sampled.UnsupportedAudioFileException
Advanced: Create an input stream from a file, but don't keep more than numBytes of data in memory.

Parameters:
filename -
bufferSize - The maximum number of bytes the AudioFile can keep in memory. If it is <0 then the length of the audio file is used.
Throws:
java.io.IOException - If the file cannot be found or opened.
javax.sound.sampled.UnsupportedAudioFileException - If the file is of an unsupported audio type.

AudioFile

public AudioFile(java.io.InputStream stream)
          throws java.io.IOException,
                 javax.sound.sampled.UnsupportedAudioFileException
Throws:
java.io.IOException
javax.sound.sampled.UnsupportedAudioFileException

AudioFile

public AudioFile(java.io.InputStream stream,
                 int bufferSize)
          throws java.io.IOException,
                 javax.sound.sampled.UnsupportedAudioFileException
Throws:
java.io.IOException
javax.sound.sampled.UnsupportedAudioFileException
Method Detail

reset

public void reset()
Reset the audio input stream. For some audio formats, this may involve re-opening the associated file.


skip

public void skip(long frames)
Skips a number of frames. Note: this function skips frames, not bytes. Doesn't work for vbr! Known issue, MP3 seeking is not precise. TODO: Fix this issue.

Parameters:
frames - Number of frames to skip

seek

public void seek(int frame)
Seek to a specific frame number. Note that seeking is slower than skipping forward.

Parameters:
frame - The frame number, relative to the start of the audio data.

open

public void open()
          throws javax.sound.sampled.UnsupportedAudioFileException,
                 java.io.IOException
Opens the audio file, ready for data access.

Throws:
javax.sound.sampled.UnsupportedAudioFileException
java.io.IOException

getName

public java.lang.String getName()

info

public java.lang.String info()

close

public void close()
           throws java.io.IOException
Close the audio file. Can be re-opened.

Throws:
java.io.IOException

isOpen

public boolean isOpen()
Is the file stream open?


isClosed

public boolean isClosed()
Is the file stream closed?


byteDepth

public int byteDepth()
Returns:
The number of bytes per frame per channel.

getDecodedFormat

public javax.sound.sampled.AudioFormat getDecodedFormat()

getEncodedFormat

public javax.sound.sampled.AudioFormat getEncodedFormat()

read

public int read(byte[] buffer)
Read bytes directly from the decoded audiofile. The bytes will be in an interleaved format. It is the responsibility of the caller to interpret this data correctly. The number of bytes read is equal to the size of the byte buffer. If that many bytes aren't available the buffer will only be partially filled.

Parameters:
buffer - A buffer to fill.
Returns:
The number of bytes read. A value of -1 indicates the file has no data left.

read

public int read(float[][] buffer)
Read decoded audio data in a non-interleaved, Beads-friendly format. Note: This function is extremely inefficient if the buffer size is constant. Use Sample, it is very efficient!

Parameters:
buffer - The buffer to fill. After execution buffer[i][j] will contain the sample in channel i, frame j. Buffer has size (numChannels,numFramesRequested).
Returns:
The number of frames read.

getTimeLengthEstimation

public long getTimeLengthEstimation(java.util.Map properties)
THIS CODE IS FROM jlGui PlayerUI.java. jlGui can be obtained at: http://www.javazoom.net/jlgui/jlgui.html