edu.cmu.sphinx.frontend.util
Class WavWriter

java.lang.Object
  extended by edu.cmu.sphinx.util.props.ConfigurableAdapter
      extended by edu.cmu.sphinx.frontend.BaseDataProcessor
          extended by edu.cmu.sphinx.frontend.util.WavWriter
All Implemented Interfaces:
DataProcessor, Configurable

public class WavWriter
extends BaseDataProcessor

Stores audio data into numbered (MS-)wav files. TODO: currently the WavWriter is only able to write data in bigEndian, support for littleEndian would be nice TODO: currently the WavWriter buffers all audio data until a DataEndSignal occurs.

Author:
Holger Brandl

Field Summary
private  java.io.ByteArrayOutputStream baos
           
private  int bitsPerSample
          Default value for PROP_BITS_PER_SAMPLE.
protected  boolean captureUtts
          The default value of PROP_SIGNED_DATA.
private  java.io.DataOutputStream dos
           
private  java.lang.String dumpFilePath
          The default value for PROP_RAND_STREAM_START
private  boolean isCompletePath
           
private  boolean isInSpeech
           
private  boolean isSigned
          The default value of PROP_SIGNED_DATA.
static java.lang.String PROP_BITS_PER_SAMPLE
          The property for the number of bits per value.
static java.lang.String PROP_CAPTURE_UTTERANCES
          The property specifying whether the input data is signed.
static java.lang.String PROP_IS_COMPLETE_PATH
           
static java.lang.String PROP_OUT_FILE_NAME_PATTERN
          The pathname which must obey the pattern: pattern + i + .wav.
static java.lang.String PROP_SIGNED_DATA
          The property specifying whether the input data is signed.
private  int sampleRate
           
 
Fields inherited from class edu.cmu.sphinx.util.props.ConfigurableAdapter
logger
 
Constructor Summary
WavWriter()
           
WavWriter(java.lang.String dumpFilePath, boolean isCompletePath, int bitsPerSample, boolean isSigned, boolean captureUtts)
           
 
Method Summary
static javax.sound.sampled.AudioInputStream convertDoublesToAudioStream(double[] values, int sampleRate)
           
 Data getData()
          Returns the processed Data output.
private static int getNextFreeIndex(java.lang.String outPattern)
           
private static javax.sound.sampled.AudioFileFormat.Type getTargetType(java.lang.String extension)
           
 void initialize()
          Initializes this DataProcessor.
 void newProperties(PropertySheet ps)
          This method is called when this configurable component needs to be reconfigured.
static byte[] valuesToBytes(double[] values, int bytesPerValue, boolean signedData)
          Converts a big-endian byte array into an array of doubles.
protected  void writeFile(java.lang.String wavName)
          Writes the current stream to disc; override this method if you want to take additional action on file writes
static void writeWavFile(double[] signal, int sampleRate, java.io.File targetFile)
          Writes a given double array into a wav file (given the sample rate of the signal).
 
Methods inherited from class edu.cmu.sphinx.frontend.BaseDataProcessor
getPredecessor, getTimer, setPredecessor
 
Methods inherited from class edu.cmu.sphinx.util.props.ConfigurableAdapter
getName, initLogger, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PROP_OUT_FILE_NAME_PATTERN

@S4String
public static final java.lang.String PROP_OUT_FILE_NAME_PATTERN
The pathname which must obey the pattern: pattern + i + .wav. Only the pattern is required here (e.g. wavdump/file). After each DataEndSignal the smalles unused 'i' is determined.

See Also:
Constant Field Values

PROP_IS_COMPLETE_PATH

@S4Boolean(defaultValue=false)
public static final java.lang.String PROP_IS_COMPLETE_PATH
See Also:
Constant Field Values

isCompletePath

private boolean isCompletePath

dumpFilePath

private java.lang.String dumpFilePath
The default value for PROP_RAND_STREAM_START


PROP_BITS_PER_SAMPLE

@S4Integer(defaultValue=16)
public static final java.lang.String PROP_BITS_PER_SAMPLE
The property for the number of bits per value.

See Also:
Constant Field Values

bitsPerSample

private int bitsPerSample
Default value for PROP_BITS_PER_SAMPLE.


PROP_SIGNED_DATA

@S4Boolean(defaultValue=true)
public static final java.lang.String PROP_SIGNED_DATA
The property specifying whether the input data is signed.

See Also:
Constant Field Values

isSigned

private boolean isSigned
The default value of PROP_SIGNED_DATA.


PROP_CAPTURE_UTTERANCES

@S4Boolean(defaultValue=false)
public static final java.lang.String PROP_CAPTURE_UTTERANCES
The property specifying whether the input data is signed.

See Also:
Constant Field Values

captureUtts

protected boolean captureUtts
The default value of PROP_SIGNED_DATA.


baos

private java.io.ByteArrayOutputStream baos

dos

private java.io.DataOutputStream dos

sampleRate

private int sampleRate

isInSpeech

private boolean isInSpeech
Constructor Detail

WavWriter

public WavWriter(java.lang.String dumpFilePath,
                 boolean isCompletePath,
                 int bitsPerSample,
                 boolean isSigned,
                 boolean captureUtts)

WavWriter

public WavWriter()
Method Detail

newProperties

public void newProperties(PropertySheet ps)
                   throws PropertyException
Description copied from interface: Configurable
This method is called when this configurable component needs to be reconfigured.

Specified by:
newProperties in interface Configurable
Overrides:
newProperties in class ConfigurableAdapter
Parameters:
ps - a property sheet holding the new data
Throws:
PropertyException - if there is a problem with the properties.

getData

public Data getData()
             throws DataProcessingException
Description copied from class: BaseDataProcessor
Returns the processed Data output.

Specified by:
getData in interface DataProcessor
Specified by:
getData in class BaseDataProcessor
Returns:
an Data object that has been processed by this DataProcessor
Throws:
DataProcessingException - if a data processor error occurs

getNextFreeIndex

private static int getNextFreeIndex(java.lang.String outPattern)

initialize

public void initialize()
Initializes this DataProcessor. This is typically called after the DataProcessor has been configured.

Specified by:
initialize in interface DataProcessor
Overrides:
initialize in class BaseDataProcessor

getTargetType

private static javax.sound.sampled.AudioFileFormat.Type getTargetType(java.lang.String extension)

valuesToBytes

public static byte[] valuesToBytes(double[] values,
                                   int bytesPerValue,
                                   boolean signedData)
                            throws java.lang.ArrayIndexOutOfBoundsException
Converts a big-endian byte array into an array of doubles. Each consecutive bytes in the byte array are converted into a double, and becomes the next element in the double array. The size of the returned array is (length/bytesPerValue). Currently, only 1 byte (8-bit) or 2 bytes (16-bit) samples are supported.

Parameters:
values -
bytesPerValue - the number of bytes per value
signedData - whether the data is signed
Returns:
a double array, or null if byteArray is of zero length
Throws:
java.lang.ArrayIndexOutOfBoundsException

convertDoublesToAudioStream

public static javax.sound.sampled.AudioInputStream convertDoublesToAudioStream(double[] values,
                                                                               int sampleRate)

writeWavFile

public static void writeWavFile(double[] signal,
                                int sampleRate,
                                java.io.File targetFile)
Writes a given double array into a wav file (given the sample rate of the signal).

Parameters:
signal -
sampleRate -
targetFile -

writeFile

protected void writeFile(java.lang.String wavName)
Writes the current stream to disc; override this method if you want to take additional action on file writes

Parameters:
wavName - name of the file to be written