public class Sample
extends java.lang.Object
SampleManager
. For example, to load an
mp3, you would do the following.
Sample wicked = SampleManager.sample("wickedTrack.mp3");
Samples are usually played with a
SamplePlayer
. Sample data can also be
accessed through the methods: getFrame
,
getFrameLinear
, and
getFrames
. Sample data can be written
with: putFrame
or
putFrames
.
SampleManager
,
RecordToSample
Constructor and Description |
---|
Sample(double length)
Instantiates a new writable sample with specified length and default
audio format: 44.1KHz, float, stereo.
|
Sample(double length,
int nChannels)
Instantiates a new writable sample with specified length and number of channels and default
audio format: 44.1KHz, float.
|
Sample(double length,
int nChannels,
float sampleRate)
Instantiates a new writeable Sample with the specified audio format and
length;
The sample isn't initialised, so may contain junk.
|
Sample(java.lang.String filename)
Create a sample from a file.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the (writeable) sample.
|
java.lang.Class<? extends AudioFileReader> |
getAudioFileReaderClass()
Gets the current AudioFileReaderClass.
|
java.lang.Class<? extends AudioFileWriter> |
getAudioFileWriterClass()
Gets the current AudioFileWriterClass.
|
java.lang.String |
getFileName()
Gets the full file path.
|
void |
getFrame(int frame,
float[] frameData)
Return a single frame.
|
void |
getFrameCubic(double posInMS,
float[] result)
Retrieves a frame of audio using cubic interpolation.
|
void |
getFrameLinear(double posInMS,
float[] result)
Retrieves a frame of audio using linear interpolation.
|
void |
getFrameNoInterp(double posInMS,
float[] result)
Retrieves a frame of audio using no interpolation.
|
void |
getFrames(int frame,
float[][] frameData)
Get a series of frames.
|
double |
getLength()
Return length of sample in ms
|
int |
getNumChannels()
Get the number of channels
|
long |
getNumFrames()
Get the number of frames
|
float |
getSampleRate()
Get the sample rate
|
java.lang.String |
getSimpleName()
Gets the simple name.
|
double |
msToSamples(double msTime)
Converts from milliseconds to samples based on the sample rate specified
by
#audioFormat . |
void |
putFrame(int frame,
float[] frameData)
Write a single frame into this sample.
|
void |
putFrames(int frame,
float[][] frameData)
Write multiple frames into the sample.
|
void |
putFrames(int frame,
float[][] frameData,
int offset,
int numFrames)
Write multiple frames into the sample.
|
void |
resize(long frames)
Advanced
Change the number of frames in the (writeable) sample.
|
void |
resizeWithZeros(long frames)
Just like
resize(long) but initialises the new frames with
zeros. |
double |
samplesToMs(double sampleTime)
Converts from samples to milliseconds based on the sample rate specified
by
#audioFormat . |
void |
setAudioFileReaderClass(java.lang.Class<? extends AudioFileReader> audioFileReaderClass)
Set the audioFileReaderClass.
|
void |
setAudioFileWriterClass(java.lang.Class<? extends AudioFileWriter> audioFileWriterClass)
Set the audioFileWriterClass.
|
void |
setSimpleName(java.lang.String simpleName)
Sets the simple name.
|
java.lang.String |
toString() |
void |
write(java.lang.String fn)
This records the sample to a WAV format audio file.
|
void |
write(java.lang.String fn,
AudioFileType type)
This records the sample to a file with the specified
AudioFile.Type.
|
void |
write(java.lang.String fn,
AudioFileType type,
SampleAudioFormat saf)
This records the sample to a file with the specified
AudioFile.Type.
|
public Sample(double length)
length
- the length in ms.public Sample(double length, int nChannels)
length
- the length in ms.nChannels
- the number of channels.public Sample(double length, int nChannels, float sampleRate)
clear()
to clear it.length
- The length of the sample in ms.nChannels
- The number of channelssampleRate
- The sampleRatepublic Sample(java.lang.String filename) throws java.io.IOException
javax.sound.sampled.UnsupportedAudioFileException
java.io.IOException
AudioFileUnsupportedException
public java.lang.Class<? extends AudioFileReader> getAudioFileReaderClass()
AudioFileReader
that Sample will use for file reading operations.
If unset or set to null Sample will use a default.public java.lang.Class<? extends AudioFileWriter> getAudioFileWriterClass()
AudioFileWriter
that Sample will use for file writing operations.
If unset or set to null Sample will use a default.public void setAudioFileReaderClass(java.lang.Class<? extends AudioFileReader> audioFileReaderClass)
AudioFileReader
that Sample will use for file reading operations.
If unset or set to null Sample use a default.audioFileReaderClass
- public void setAudioFileWriterClass(java.lang.Class<? extends AudioFileWriter> audioFileWriterClass)
AudioFileWriter
that Sample will use for file writing operations.
If unset or set to null Sample use a default.audioFileWriterClass
- public void getFrame(int frame, float[] frameData)
frame
- Must be in range, else framedata is unchanged.frameData
- public void getFrameNoInterp(double posInMS, float[] result)
posInMS
- The frame to read -- will take the last frame before this one.result
- The framedata to fill.public void getFrameLinear(double posInMS, float[] result)
posInMS
- The frame to read -- can be fractional (e.g., 4.4).result
- The framedata to fill.public void getFrameCubic(double posInMS, float[] result)
posInMS
- The frame to read -- can be fractional (e.g., 4.4).result
- The framedata to fill.public void getFrames(int frame, float[][] frameData)
min(nFrames - frame, frameData[0].length)
frames in
frameData are valid.
If the data is not readily available this doesn't do anything.frame
- The frame number (NOTE: This parameter is in frames, not in
ms!)frameData
- public void clear()
public void putFrame(int frame, float[] frameData)
frame
- The frame to write into. Must be >=0 and frameData
- The frame data to write.public void putFrames(int frame, float[][] frameData)
frame
- The frame to write into.frameData
- The frames to write.public void putFrames(int frame, float[][] frameData, int offset, int numFrames)
frame
- The frame to write into.frameData
- The frames to write.offset
- The offset into frameDatanumFrames
- The number of frames from frameData to writepublic void write(java.lang.String fn) throws java.io.IOException
fn
- The filename (should have the .aif extension).java.io.IOException
- Signals that an I/O exception has occurred.public void write(java.lang.String fn, AudioFileType type) throws java.io.IOException
fn
- The filename.type
- The type (AIFF, WAVE, etc.)java.io.IOException
- Signals that an I/O exception has occurred.public void write(java.lang.String fn, AudioFileType type, SampleAudioFormat saf) throws java.io.IOException
fn
- The filename.type
- The type (AIFF, WAVE, etc.)saf
- The SampleAudioFormatjava.io.IOException
- Signals that an I/O exception has occurred.public void resize(long frames)
resizeWithZeros(long)
.frames
- The total number of frames the sample should have.public void resizeWithZeros(long frames)
resize(long)
but initialises the new frames with
zeros.frames
- The total number of frames the sample should have.public double msToSamples(double msTime)
#audioFormat
.msTime
- the time in milliseconds.public double samplesToMs(double sampleTime)
#audioFormat
.sampleTime
- the time in samples.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String getFileName()
public java.lang.String getSimpleName()
public void setSimpleName(java.lang.String simpleName)
simpleName
- the name.public float getSampleRate()
public int getNumChannels()
public long getNumFrames()
public double getLength()