gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Sox< FP_TYPE_ > Class Template Reference

#include <Sox.H>

Collaboration diagram for Sox< FP_TYPE_ >:
[legend]

Public Member Functions

 Sox ()
 Constructor. More...
 
virtual ~Sox ()
 Destructor. More...
 
int openRead (string fileName)
 
int openRead (intptr_t buffer, size_t len)
 
template<typename Derived >
int read (Eigen::DenseBase< Derived > &audioData, int count=0)
 
virtual int openWrite (const string &fileName, double fs, int channels, double maxVal)
 
virtual int openWrite (const string &fileName, double fs, int channels, double maxVal, unsigned int wordSize, bool switchEndian, int revBytes, int revNibbles, int revBits)
 
int openMemWrite (void *buffer, size_t len, double fs, int channels, double maxVal)
 
virtual int openMemWrite (void *buffer, size_t len, double fs, int channels, double maxVal, unsigned int wordSize, bool switchEndian, int revBytes, int revNibbles, int revBits)
 
virtual int write (const vector< vector< FP_TYPE_ > > &audioData)
 
template<typename Derived >
int write (const Eigen::DenseBase< Derived > &audioData)
 
template<typename Derived >
int writeTransposed (const Eigen::DenseBase< Derived > &audioData)
 
int closeRead (void)
 
int closeWrite (void)
 
void setMaxVal (double newMax)
 
double getMaxVal (void)
 
double getFSIn (void)
 
double getFSOut (void)
 
int getChCntIn (void)
 
int getChCntOut (void)
 
vector< string > availableFormats (void)
 
void printFormats ()
 

Private Member Functions

int close (bool inputFile)
 

Static Private Member Functions

static void output_message (unsigned level, const char *filename, const char *fmt, va_list ap)
 

Private Attributes

sox_format_t * in
 input file More...
 
double maxVal
 maxVal stored in the file fileName.max More...
 
sox_format_t * out
 output file More...
 
double outputMaxVal
 The maximum value passed to write. More...
 
vector< sox_sample_t > outputBuffer
 The output buffer for interleaving output data before writing. More...
 

Detailed Description

template<typename FP_TYPE_>
class Sox< FP_TYPE_ >

This class handles audio files using the libsox C library. libsox has advantages in that is can read and write to many many different uncompressed, compressed, lossy and non-lossy audio file formats.

The input reading uses the Eigen matrix library as the input matrix to load into.

The output writing uses a generic C++ vector<vector > > type of matrix to write out from.

In general the Eigen matrices have each channel on a column. So for example a 3 channel 1024 long audio file is returned as a matrix which is 1024x3. This class assumes that all counts are specified in per channel, for example, Sox::read(matrix, count) will read count samples for each channel. libsox on the other hand assumes all counts specify the number of samples*channels.

NOTE: In order to be able to scale correctly, all audio files should have an associated max value file, for example test.f32 and test.f32.max. The .max file will specify to what value to rescale the magnitude to get the correct time domain sclaing.

When writing to file, the max file will be auto generated. This class interleaves output data into the private outputBuffer member variable when the Sox::write method is called. If the write method is called with a total number of samples (channels*length) <= the current size of the outputBuffer, then it is not resized. If the total number of samples of the outputBuffer (channels*length) > the current size of the outputBuffer, then it is resized. For this reason, a good approach to writing output data is to use constant output total sample counts OR output total sample counts which don't continuously increase between calls.

Examples:
ALSACaptureTest.C, ALSAPlaybackTest.C, IIOMMapTest.C, IIOQueueTest.C, IIOTest.C, and OverlapAddTest.C.

Definition at line 95 of file Sox.H.

Constructor & Destructor Documentation

◆ Sox()

template<typename FP_TYPE_ >
Sox< FP_TYPE_ >::Sox ( )

Constructor.

Definition at line 35 of file Sox.C.

◆ ~Sox()

template<typename FP_TYPE_ >
Sox< FP_TYPE_ >::~Sox ( )
virtual

Destructor.

Definition at line 44 of file Sox.C.

Member Function Documentation

◆ availableFormats()

template<typename FP_TYPE_ >
vector< string > Sox< FP_TYPE_ >::availableFormats ( void  )

Print a list of the available file formats.

Returns
A vector containing the available file format names.

Definition at line 238 of file Sox.C.

Here is the caller graph for this function:

◆ close()

template<typename FP_TYPE_ >
int Sox< FP_TYPE_ >::close ( bool  inputFile)
private

Close the file

Parameters
inputFileis either true (closes in) or false (closes out)
Returns
a negative error number on failure, 0 on success

Definition at line 51 of file Sox.C.

◆ closeRead()

template<typename FP_TYPE_ >
int Sox< FP_TYPE_ >::closeRead ( void  )

If open, close the read file.

Returns
A negative error code on failure.

Definition at line 226 of file Sox.C.

Here is the caller graph for this function:

◆ closeWrite()

template<typename FP_TYPE_ >
int Sox< FP_TYPE_ >::closeWrite ( void  )

If open, close the write file.

Returns
A negative error code on failure.

Definition at line 232 of file Sox.C.

Here is the caller graph for this function:

◆ getChCntIn()

template<typename FP_TYPE_>
int Sox< FP_TYPE_ >::getChCntIn ( void  )
inline

Get the input audio channel count.

Returns
the channel count if the input exists, SOX_READ_FILE_NOT_OPENED_ERROR if the file isn't open for reading.

Definition at line 333 of file Sox.H.

Here is the caller graph for this function:

◆ getChCntOut()

template<typename FP_TYPE_>
int Sox< FP_TYPE_ >::getChCntOut ( void  )
inline

Get the input audio channel count.

Returns
the channel count if the input exists, SOX_WRITE_FILE_NOT_OPENED_ERROR if the file isn't open for writing.

Definition at line 342 of file Sox.H.

Here is the caller graph for this function:

◆ getFSIn()

template<typename FP_TYPE_>
double Sox< FP_TYPE_ >::getFSIn ( void  )
inline

Get the input audio sample rate.

Returns
the sample rate if the input exists, 0. otherwise.

Definition at line 315 of file Sox.H.

Here is the caller graph for this function:

◆ getFSOut()

template<typename FP_TYPE_>
double Sox< FP_TYPE_ >::getFSOut ( void  )
inline

Get the output audio sample rate.

Returns
the sample rate if the output exists, 0. otherwise.

Definition at line 324 of file Sox.H.

◆ getMaxVal()

template<typename FP_TYPE_>
double Sox< FP_TYPE_ >::getMaxVal ( void  )
inline

Get the maximum value to scale samples by. The maximum value for input files is read in from the max value file or set manually.

See also
setMaxVal
Returns
The maximum value used for audio input reading.

Definition at line 308 of file Sox.H.

◆ openMemWrite() [1/2]

template<typename FP_TYPE_>
int Sox< FP_TYPE_ >::openMemWrite ( void *  buffer,
size_t  len,
double  fs,
int  channels,
double  maxVal 
)
inline

Open a memory pointer for reading

Parameters
fsThe same rate in Hz.
bufferThe pointer to the memory block
lenThe length of the memory block in bytes
channelsThe number of channels the file should carry.
maxValThe maximum value to be contained in the output vector when passed to Sox::write
Returns
A negative error code on failure.

Definition at line 202 of file Sox.H.

◆ openMemWrite() [2/2]

template<typename FP_TYPE_ >
int Sox< FP_TYPE_ >::openMemWrite ( void *  buffer,
size_t  len,
double  fs,
int  channels,
double  maxVal,
unsigned int  wordSize,
bool  switchEndian,
int  revBytes,
int  revNibbles,
int  revBits 
)
virtual

Open a memory pointer for writing. Specifying the precision. If the output file is already open, then it is closed first.

Parameters
bufferThe pointer to the memory block
lenThe length of the memory block in bytes
fsThe same rate in Hz.
channelsThe number of channels the file should carry.
maxValThe maximum value to be contained in the output vector when passed to Sox::write
wordSizeThe number of bits in the word.
switchEndianSwitch the endian from big to little or little to big
revBytesReverse the bytes {sox_option_no = 0, sox_option_yes = 1, sox_option_default = 2}
revNibblesReverse the nibbles {sox_option_no = 0, sox_option_yes = 1, sox_option_default = 2}
revBitsReverse the bits {sox_option_no = 0, sox_option_yes = 1, sox_option_default = 2}
Returns
A negative error code on failure.

Definition at line 171 of file Sox.C.

◆ openRead() [1/2]

template<typename FP_TYPE_ >
int Sox< FP_TYPE_ >::openRead ( string  fileName)

Open a file for reading. If the input file is already open, then it is closed first.

Parameters
fileNamethe audio file to open, can be of many different types, wav, mp3, ogg, etc. etc.
Returns
A negative error code on failure. SOX_READ_MAXSCALE_ERROR is returned when fileName.max is not found or opened - indicating that audio will not be rescaled correctly.

Definition at line 70 of file Sox.C.

Here is the caller graph for this function:

◆ openRead() [2/2]

template<typename FP_TYPE_ >
int Sox< FP_TYPE_ >::openRead ( intptr_t  buffer,
size_t  len 
)

Open a memory pointer for reading

Parameters
bufferThe pointer to the memory block
lenThe length of the memory block in bytes

Definition at line 94 of file Sox.C.

◆ openWrite() [1/2]

template<typename FP_TYPE_>
virtual int Sox< FP_TYPE_ >::openWrite ( const string &  fileName,
double  fs,
int  channels,
double  maxVal 
)
inlinevirtual

Open a file for writing. If the output file is already open, then it is closed first.

The size of FP_TYPE_ specifies the bit width for the output file to be created.

Parameters
fileNameThe name and path of the file to open for writing. The extension determines the type of file.
fsThe same rate in Hz.
channelsThe number of channels the file should carry.
maxValThe maximum value to be contained in the output vector when passed to Sox::write
Returns
A negative error code on failure.

Definition at line 174 of file Sox.H.

Here is the caller graph for this function:

◆ openWrite() [2/2]

template<typename FP_TYPE_ >
int Sox< FP_TYPE_ >::openWrite ( const string &  fileName,
double  fs,
int  channels,
double  maxVal,
unsigned int  wordSize,
bool  switchEndian,
int  revBytes,
int  revNibbles,
int  revBits 
)
virtual

Open a file for writing. Specifying the precision. If the output file is already open, then it is closed first.

Parameters
fileNameThe name and path of the file to open for writing. The extension determines the type of file.
fsThe same rate in Hz.
channelsThe number of channels the file should carry.
maxValThe maximum value to be contained in the output vector when passed to Sox::write
wordSizeThe number of bits in the word.
switchEndianSwitch the endian from big to little or little to big
revBytesReverse the bytes {sox_option_no = 0, sox_option_yes = 1, sox_option_default = 2}
revNibblesReverse the nibbles {sox_option_no = 0, sox_option_yes = 1, sox_option_default = 2}
revBitsReverse the bits {sox_option_no = 0, sox_option_yes = 1, sox_option_default = 2}
Returns
A negative error code on failure.

Definition at line 127 of file Sox.C.

◆ output_message()

template<typename FP_TYPE_ >
void Sox< FP_TYPE_ >::output_message ( unsigned  level,
const char *  filename,
const char *  fmt,
va_list  ap 
)
staticprivate

Definition at line 113 of file Sox.C.

◆ printFormats()

template<typename FP_TYPE_ >
void Sox< FP_TYPE_ >::printFormats ( )

Print out the available formats

Definition at line 260 of file Sox.C.

◆ read()

template<typename FP_TYPE_>
template<typename Derived >
int Sox< FP_TYPE_ >::read ( Eigen::DenseBase< Derived > &  audioData,
int  count = 0 
)
inline

Read audio data from an already opened file. The audioData is returned with each column as an audio channel.

Parameters
audioDataThe Matrix to place the read audio into. It is resized as required and will match the size of the number of samples read where each column is a channel.
countthe number of samples per channel to read, 0 for everything
Returns
NO_ERROR on success, or the error code otherwise. audioData holds the read data.

Definition at line 135 of file Sox.H.

Here is the caller graph for this function:

◆ setMaxVal()

template<typename FP_TYPE_>
void Sox< FP_TYPE_ >::setMaxVal ( double  newMax)
inline

Set the maximum value to scale input samples by. All read audio data will be scaled by newMax divided by the sox maximum sample value.

Parameters
newMaxThe new maximum value.

Definition at line 300 of file Sox.H.

Here is the caller graph for this function:

◆ write() [1/2]

template<typename FP_TYPE_>
int Sox< FP_TYPE_ >::write ( const vector< vector< FP_TYPE_ > > &  audioData)
virtual

Write the audio data to an already open file. The audioData is written with each column as an audio channel.

Parameters
audioDataThe matrix to store to file. Each vector is a channel.
Returns
The number of samples written otherwise the appropriate error otherwise.

Definition at line 203 of file Sox.C.

Here is the caller graph for this function:

◆ write() [2/2]

template<typename FP_TYPE_>
template<typename Derived >
int Sox< FP_TYPE_ >::write ( const Eigen::DenseBase< Derived > &  audioData)
inline

Write the audio data to an already open file. The audioData is written with each column as an audio channel.

Parameters
audioDataThe matrix to store to file. Each column is a channel.
Returns
The number of samples written otherwise the appropriate error otherwise.

Definition at line 236 of file Sox.H.

◆ writeTransposed()

template<typename FP_TYPE_>
template<typename Derived >
int Sox< FP_TYPE_ >::writeTransposed ( const Eigen::DenseBase< Derived > &  audioData)
inline

Write the audio data to an already open file. The audioData is written with each row as an audio channel.

Parameters
audioDataThe matrix to store to file.
Returns
NO_ERROR on success, or the number written otherwise.

Definition at line 264 of file Sox.H.

Here is the caller graph for this function:

Member Data Documentation

◆ in

template<typename FP_TYPE_>
sox_format_t* Sox< FP_TYPE_ >::in
private

input file

Definition at line 96 of file Sox.H.

◆ maxVal

template<typename FP_TYPE_>
double Sox< FP_TYPE_ >::maxVal
private

maxVal stored in the file fileName.max

Definition at line 97 of file Sox.H.

◆ out

template<typename FP_TYPE_>
sox_format_t* Sox< FP_TYPE_ >::out
private

output file

Definition at line 98 of file Sox.H.

◆ outputBuffer

template<typename FP_TYPE_>
vector<sox_sample_t> Sox< FP_TYPE_ >::outputBuffer
private

The output buffer for interleaving output data before writing.

Definition at line 101 of file Sox.H.

◆ outputMaxVal

template<typename FP_TYPE_>
double Sox< FP_TYPE_ >::outputMaxVal
private

The maximum value passed to write.

Definition at line 100 of file Sox.H.


The documentation for this class was generated from the following files:
gtkIOStream: Sox< FP_TYPE_ > Class Template Reference
GTK+ IOStream  Beta