net.beadsproject.beads.data
Class Sample

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

public class Sample
extends java.lang.Object

A Sample is a multi-channel buffer of audio data which can be read from a file or recorded from a UGen and written to a file.

Author:
ollie
See Also:
Recorder

Field Summary
 javax.sound.sampled.AudioFormat audioFormat
          The audio format.
 float[][] buf
          The buffer containing the audio data.
 float length
          The length in milliseconds.
 int nChannels
          The number of channels.
 long nFrames
          The number of sample frames.
 
Constructor Summary
Sample(javax.sound.sampled.AudioFormat audioFormat, long totalFrames)
          Instantiates a new empty buffer with the specified audio format and number of frames.
Sample(java.lang.String fn)
          Creates a new Sample from the specified file.
 
Method Summary
 java.lang.String getFileName()
          Gets the full file name.
 float[] getFrameCubic(int currentSample, float fractionOffset)
          Retrieves a frame of audio using cubic interpolation.
 float[] getFrameLinear(int currentSample, float fractionOffset)
          Retrieves a frame of audio using linear interpolation.
 java.lang.String getSimpleFileName()
          Gets the simple file name.
 double msToSamples(double msTime)
          Converts from milliseconds to samples based on the sample rate specified by audioFormat.
 void printAudioFormatInfo()
          Prints audio format info to System.out.
 double samplesToMs(double sampleTime)
          Converts from samples to milliseconds based on the sample rate specified by audioFormat.
 java.lang.String toString()
           
 void write(java.lang.String fn)
          Write Sample to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

audioFormat

public final javax.sound.sampled.AudioFormat audioFormat
The audio format.


nChannels

public final int nChannels
The number of channels.


nFrames

public final long nFrames
The number of sample frames.


length

public final float length
The length in milliseconds.


buf

public final float[][] buf
The buffer containing the audio data. The data is stored in the form float[nChannels][nFrames]

Constructor Detail

Sample

public Sample(javax.sound.sampled.AudioFormat audioFormat,
              long totalFrames)
Instantiates a new empty buffer with the specified audio format and number of frames.

Parameters:
audioFormat - the audio format.
totalFrames - the number of sample frames.

Sample

public Sample(java.lang.String fn)
       throws javax.sound.sampled.UnsupportedAudioFileException,
              java.io.IOException
Creates a new Sample from the specified file.

In many cases it is preferable to use SampleManager.sample(String), which adds the data to a static repository once it is loaded.

Parameters:
fn - the file path.
Throws:
javax.sound.sampled.UnsupportedAudioFileException - Signals that the file format is not supported.
java.io.IOException - Signals that an I/O exception has occurred.
Method Detail

msToSamples

public double msToSamples(double msTime)
Converts from milliseconds to samples based on the sample rate specified by audioFormat.

Parameters:
msTime - the time in milliseconds.
Returns:
the time in samples.

samplesToMs

public double samplesToMs(double sampleTime)
Converts from samples to milliseconds based on the sample rate specified by audioFormat.

Parameters:
sampleTime - the time in samples.
Returns:
the time in milliseconds.

getFrameLinear

public float[] getFrameLinear(int currentSample,
                              float fractionOffset)
Retrieves a frame of audio using linear interpolation.

Parameters:
currentSample - the current sample.
fractionOffset - the offset from the current sample as a fraction of the time to the next sample.
Returns:
the interpolated frame.

getFrameCubic

public float[] getFrameCubic(int currentSample,
                             float fractionOffset)
Retrieves a frame of audio using cubic interpolation.

Parameters:
currentSample - the current sample.
fractionOffset - the offset from the current sample as a fraction of the time to the next sample.
Returns:
the interpolated frame.

printAudioFormatInfo

public void printAudioFormatInfo()
Prints audio format info to System.out.


write

public void write(java.lang.String fn)
           throws java.io.IOException
Write Sample to a file.

Parameters:
fn - the file name.
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getFileName

public java.lang.String getFileName()
Gets the full file name.

Returns:
the file name

getSimpleFileName

public java.lang.String getSimpleFileName()
Gets the simple file name.

Returns:
the file name