Provides a number of utilities for pulling useful data from audio files. More...
#include <AudioUtil.h>
Public Types | |
enum | FileHandlingMode { FULL_CACHE, DISK_MODE } |
Public Member Functions | |
AudioUtil () | |
Default constructor. | |
AudioUtil (string filePath) | |
Alternate, single-parameter constructor. | |
bool | setFile (string filePath) |
Sets the file to be wrapped by an instance of AudioUtil. | |
int | getNumChannels () |
The number of channels of the wrapped audio file. | |
int | getSampleRate () |
The sample rate of the wrapped audio file. | |
int | getTotalFrames () |
The total number of frames of the wrapped audio file. | |
vector< double > | calculateNormalizedPeaks () |
Calculates peak values for the normalized audio data of the audio file wrapped by an instance of AudioUtil. | |
vector< double > | grabFrame (int frameIndex) |
Get a given frame of the wrapped audio file. | |
vector< double > | peakForRegion (int region_start_frame, int region_end_frame) |
Peak for a given region of the wrapped audio file. | |
vector< double > | getAllFrames () |
The content of the wrapped audio file. | |
FileHandlingMode | getFileHandlingMode () |
Accessor for an instance of AudioUtil's file-handling mode. | |
void | setFileHandlingMode (FileHandlingMode mode) |
The mutator for the file-handling mode of an instance of AudioUtil. |
Provides a number of utilities for pulling useful data from audio files.
This class began as a nice, object-oriented wrapper for certain functions that I found myself frequently using in Erik de Castro Lopo's libsndfile. It now supports an optional caching scheme (enabled by calling setFileHandlingMode(AudioUtil::FULL_CACHE) on an instance of AudioUtil) to dramatically speed up the performance of certain functions, like that for accessing arbitrary frames (grabFrame()) of an audio file and that for determining the peak value for a given region of an audio file (peakForRegion()).
Definition at line 27 of file AudioUtil.h.
AudioUtil::AudioUtil | ( | ) |
Default constructor.
Constructs instance of AudioUtil. An AudioUtil instance created using this constructor is effectively useless until its setFile() function has been invoked with a valid path to a WAV file. For this reason, you may elect to use the alternate AudioUtil constructor, which takes such a path as a parameter.
Definition at line 15 of file AudioUtil.cpp.
AudioUtil::AudioUtil | ( | string | filePath | ) |
Alternate, single-parameter constructor.
Alternate constructor. An instance of AudioUtil created using this constructor does not need to have its setFile() function explicitly invoked to be useful.
filePath | path to a WAV file |
Definition at line 30 of file AudioUtil.cpp.
vector< double > AudioUtil::calculateNormalizedPeaks | ( | ) |
Calculates peak values for the normalized audio data of the audio file wrapped by an instance of AudioUtil.
Definition at line 142 of file AudioUtil.cpp.
vector< double > AudioUtil::getAllFrames | ( | ) |
The content of the wrapped audio file.
Function to get all frames of the audio file wrapped by an instance of AudioUtil as a vector of double-precision floating-point values.
Definition at line 462 of file AudioUtil.cpp.
AudioUtil::FileHandlingMode AudioUtil::getFileHandlingMode | ( | ) |
Accessor for an instance of AudioUtil's file-handling mode.
Definition at line 82 of file AudioUtil.cpp.
int AudioUtil::getNumChannels | ( | ) |
The number of channels of the wrapped audio file.
Definition at line 174 of file AudioUtil.cpp.
int AudioUtil::getSampleRate | ( | ) |
The sample rate of the wrapped audio file.
Definition at line 191 of file AudioUtil.cpp.
int AudioUtil::getTotalFrames | ( | ) |
The total number of frames of the wrapped audio file.
Definition at line 201 of file AudioUtil.cpp.
vector< double > AudioUtil::grabFrame | ( | int | frameIndex | ) |
Get a given frame of the wrapped audio file.
Function to get the frame at a given index in the audio file wrapped by an instance of AudioUtil. Will return a double-precision floating point vector of dimension equal to the number_of_channels in the wrapped file (either 1 or 2) containing the data of the requested frame. In the case that a frame is requested which is out of bounds, an error message will be printed and an empty vector returned.
frameIndex | The desired frame. |
Definition at line 227 of file AudioUtil.cpp.
vector< double > AudioUtil::peakForRegion | ( | int | region_start_frame, | |
int | region_end_frame | |||
) |
Peak for a given region of the wrapped audio file.
Function to get the peak for each channel of a given region of the audio file wrapped by an instance of AudioUtil. Returns a vector of double-precision floating point values representing this data. In the case that an invalid region is specified by the function's parameters, prints an error message and returns an empty vector. This function performs drastically better when the AudioUtil instance it is being invoked with is operating in FULL_CACHE mode rather than DISK_MODE.
region_start_frame | The frame marking the beginning of the region to be analyzed | |
region_end_frame | The frame marking the end of the region to be analyzed |
Definition at line 317 of file AudioUtil.cpp.
bool AudioUtil::setFile | ( | string | filePath | ) |
Sets the file to be wrapped by an instance of AudioUtil.
Set the audio file to be wrapped by an instance of AudioUtil. This function must be passed a string representing a valid path to a WAV file. In the case that it is not given such a string, it will print an error message and return false. Otherwise, it will return true.
filePath | a string representing a valid path to a WAV file. |
Definition at line 98 of file AudioUtil.cpp.
void AudioUtil::setFileHandlingMode | ( | FileHandlingMode | mode | ) |
The mutator for the file-handling mode of an instance of AudioUtil.
AudioUtil objects can function in one of two modes: AudioUtil::DISK_MODE and AudioUtil::FULL_CACHE mode. The default mode for AudioUtil objects is DISK_MODE. In DISK_MODE, an instance of AudioUtil will dynamically load a region of the audio file it wraps from disk into memory when asked to analyze or return this region (when the peakForRegion, getAllFrames and grabFrame function are invoked, for example). This keeps memory use minimal, but has an immense consequence with respect to performance. Upon being set to FULL_CACHE mode, an AudioUtil instance will load the entire audio file that it wraps into memory (storing it as a vector of double-precision floating point values), and use this cached data to perform the operations that, in DISK_MODE, require that data be loaded dynamically from disk for processing. In FULL_CACHE mode, you get drastically increased performance, but pay a penalty in increased memory consumption.
mode | file-handling scheme for the AudioUtil instance. Valid options: AudioUtil::DISK_MODE, AudioUtil::FULL_CACHE |
Definition at line 68 of file AudioUtil.cpp.