nl.captcha.audio
Class Sample

java.lang.Object
  extended by nl.captcha.audio.Sample

public class Sample
extends java.lang.Object

Class representing a sound sample, typically read in from a file. Note that at this time this class only supports wav files with the following characteristics:

Data files in other formats will cause an IllegalArgumentException to be thrown.

Author:
James Childers

Field Summary
static javax.sound.sampled.AudioFormat SC_AUDIO_FORMAT
           
 
Constructor Summary
Sample(java.io.InputStream is)
           
 
Method Summary
 byte[] asByteArray()
          Return the interleaved samples as a byte[].
static byte[] asByteArray(long sampleCount, double[] sample)
          Helper method to convert a double[] to a byte[] in a format that can be used by AudioInputStream.
 void decodeBytes(byte[] audioBytes, double[] audioSamples)
           
 javax.sound.sampled.AudioInputStream getAudioInputStream()
           
 void getChannelSamples(int channel, double[] interleavedSamples, double[] channelSamples)
          Extract samples of a particular channel from interleavedSamples and copy them into channelSamples
 javax.sound.sampled.AudioFormat getFormat()
           
 double[] getInterleavedSamples()
           
 double[] getInterleavedSamples(long begin, long end, double[] samples)
          Get the interleaved decoded samples for all channels, from sample index begin (included) to sample index end (excluded) and copy them into samples.
 long getSampleCount()
          Return the number of samples of all channels
 void getStereoSamples(double[] leftSamples, double[] rightSamples)
          Convenience method.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SC_AUDIO_FORMAT

public static final javax.sound.sampled.AudioFormat SC_AUDIO_FORMAT
Constructor Detail

Sample

public Sample(java.io.InputStream is)
Method Detail

getAudioInputStream

public javax.sound.sampled.AudioInputStream getAudioInputStream()

getFormat

public javax.sound.sampled.AudioFormat getFormat()

getSampleCount

public long getSampleCount()
Return the number of samples of all channels

Returns:
The number of samples for all channels

getInterleavedSamples

public double[] getInterleavedSamples()

getInterleavedSamples

public double[] getInterleavedSamples(long begin,
                                      long end,
                                      double[] samples)
                               throws java.io.IOException,
                                      java.lang.IllegalArgumentException
Get the interleaved decoded samples for all channels, from sample index begin (included) to sample index end (excluded) and copy them into samples. end must not exceed getSampleCount(), and the number of samples must not be so large that the associated byte array cannot be allocated

Parameters:
begin -
end -
samples -
Throws:
java.io.IOException
java.lang.IllegalArgumentException

getChannelSamples

public void getChannelSamples(int channel,
                              double[] interleavedSamples,
                              double[] channelSamples)
Extract samples of a particular channel from interleavedSamples and copy them into channelSamples

Parameters:
channel -
interleavedSamples -
channelSamples -

getStereoSamples

public void getStereoSamples(double[] leftSamples,
                             double[] rightSamples)
                      throws java.io.IOException
Convenience method. Extract left and right channels for common stereo files. leftSamples and rightSamples must be of size getSampleCount()

Parameters:
leftSamples -
rightSamples -
Throws:
java.io.IOException

decodeBytes

public void decodeBytes(byte[] audioBytes,
                        double[] audioSamples)

asByteArray

public final byte[] asByteArray()
Return the interleaved samples as a byte[].

Returns:
The interleaved samples

asByteArray

public static final byte[] asByteArray(long sampleCount,
                                       double[] sample)
Helper method to convert a double[] to a byte[] in a format that can be used by AudioInputStream. Typically this will be used with a Sample that has been modified from its original.

Returns:
A byte[] representing a sample
See Also:
Yak Shaving

toString

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


Copyright © 2010-2011 James Childers. All Rights Reserved.