Bridges-C++  3.4.2
Bridges(C++ API)
Classes | Public Member Functions | List of all members
bridges::datastructure::AudioClip Class Reference

#include <AudioClip.h>

Inheritance diagram for bridges::datastructure::AudioClip:
bridges::datastructure::DataStructure

Detailed Description

This class provides support for reading, modifying, and playing audio waveforms.

This class provides a way to represent an AudioClip (think of a .WAV file) in Bridges as waveforms.

An AudioClip can be composed of multiple channels: a stereo sound would be composed of 2 channels (Left and Right), a mono sound would be composed of a single channel. A 5.1 sound would be composed of 6 channels. When building an AudioClip from a file, the number of channels is taken from the file; some constructors have a numChannels that enables to pass the number of channels explicitly. If unsure, one can know how many channels are in an audio clip using getNumChannels().

Each channel is essentially a 1D signal. That is to say, it is an array of values that represent how far the membrane of a speaker should be from its resting position. The quality of the sound is controlled by two parameters: sampling rate and sampling depth.

Sampling rate tells how many positions per second are encoded by the AudioClip. It is expressed in Hertz. CD quality is 44100Hz; while walkie-talkies use 8000Hz. It is set automatically if read from a file; or it can be passed as the sampleRate parameter to some of the constructors. The sampling rate can be obtained from an AudioClip using getSampleRate().

The length of an AudioClip is expressed in number of samples. So if an AudioClip is composed of 16,000 samples with a sampling rate of 8000Hz, the clip would be 2 seconds long. The number of samples can obtained with getSampleCount(); it is set from a file or can be passed as the sampleCount parameter of some of the constructor.

The sampling depth indicates how many different positions the membrane can take. It is typically expressed in bits with supported values being 8-bit, 16-bit, 24-bit, and 32-bit. If a clip is encoded with a depth of 8 bits, the membrane can take 2^8 different position ranging from -128 to +127, with 0 being the resting position. The sampling depth is read from files or passed as the sampleBits parameter of the constructor. The sampling depth of an existing clip can be obtained with getSampleBits().

The individual samples are accessed with the getSample() and setSample() functions. The samples are integer values in the [-2^(getSampleBits()-1) ; 2^(getSampleBits()-1)[ range. The functions allow to specify for channel and sample index.

See also
There is a tutorial for processing audio at https://bridgesuncc.github.io/tutorials/AudioClip.html
Author
Luke Sloop, Kalpathi Subramanian, Erik Saule
Date
2020

Acknowledgements: The Wav file parser is adapted from Amit Sengupta's C version, posted at TRUELOGIC BLOG, http://truelogic.org/wordpress/2015/09/04/parsing-a-wav-file-in-c/

It was converted into C++ in this implementation.

Public Member Functions

 AudioClip (int sampleCount, int numChannels, int sampleBits, int sampleRate)
 create an audio clip More...
 
virtual const string getDStype () const override
 
 AudioClip (const string &wave_file)
 create an audio clip from a File More...
 
virtual const string getDataStructureRepresentation () const override final
 
int getNumChannels () const
 returns the number of channels of the clip More...
 
int getSampleRate () const
 returns the sampling rate of the clip More...
 
int getSampleCount () const
 returns the number of samples in the clip More...
 
int getSampleBits () const
 returns the sampling depth. More...
 
int getSample (int channelIndex, int sampleIndex) const
 access a particular sample More...
 
void setSample (int channelIndex, int sampleIndex, int value)
 change a particular sample More...
 
- Public Member Functions inherited from bridges::datastructure::DataStructure
virtual ~DataStructure ()=default
 

Constructor & Destructor Documentation

◆ AudioClip() [1/2]

bridges::datastructure::AudioClip::AudioClip ( int  sampleCount,
int  numChannels,
int  sampleBits,
int  sampleRate 
)
inline

create an audio clip

creates an AudioClip with numChannels channels, sampleCount samples at sampleRate Hz with a depth of sampleBits

◆ AudioClip() [2/2]

bridges::datastructure::AudioClip::AudioClip ( const string &  wave_file)
inline

create an audio clip from a File

Parameters
wave_filename of the file (should be a Wave file)

Member Function Documentation

◆ getDataStructureRepresentation()

virtual const string bridges::datastructure::AudioClip::getDataStructureRepresentation ( ) const
inlinefinaloverridevirtual

Ease of use function for the deletion of an entire datastructure. Overrides should call delete on itself and each linked data structure

Warning
Only call if all these data structures were all dynamicaly allocated(aka: using new) Gets the JSON representation of this DataStructure's nodes and links
Returns
The JSON representation of the data structure: A pair holding the nodes and links JSON strings respectively

Implements bridges::datastructure::DataStructure.

◆ getDStype()

virtual const string bridges::datastructure::AudioClip::getDStype ( ) const
inlineoverridevirtual
Returns
The string representation of this data structure type

Implements bridges::datastructure::DataStructure.

◆ getNumChannels()

int bridges::datastructure::AudioClip::getNumChannels ( ) const
inline

returns the number of channels of the clip

Returns
the number of channels of the clip (1 for mono, 2 for stereo, ...)

◆ getSample()

int bridges::datastructure::AudioClip::getSample ( int  channelIndex,
int  sampleIndex 
) const
inline

access a particular sample

Parameters
channelIndexthe index of the channel that will be accessed (in the [0;getNumChannels()-1] range).
sampleIndexthe index of the sample that will be accessed (in the [0;getSampleCount()-1] range).
Returns
the sample value (in [-2^(getSampleBits()-1) ; 2^(getSampleBits()-1)) range).

◆ getSampleBits()

int bridges::datastructure::AudioClip::getSampleBits ( ) const
inline

returns the sampling depth.

The sampling depth indicates how many bits are used to encode each individual samples. The values supported are only 8, 16, 24, and 32.

All samples must be in the [-2^(getSampleBits()-1) ; 2^(getSampleBits()-1)) range. that is to say, for 8-bit, in the [-256;255] range.

Returns
the sampling depth.

◆ getSampleCount()

int bridges::datastructure::AudioClip::getSampleCount ( ) const
inline

returns the number of samples in the clip

The length of the clip in second is getSampleCount()/((double) getSampleRate())

Returns
the number of samples in the clip

◆ getSampleRate()

int bridges::datastructure::AudioClip::getSampleRate ( ) const
inline

returns the sampling rate of the clip

Returns
the sampling rate of the clip in Hertz. (CD quality is 44100Hz for instance)

◆ setSample()

void bridges::datastructure::AudioClip::setSample ( int  channelIndex,
int  sampleIndex,
int  value 
)
inline

change a particular sample

Parameters
channelIndexthe index of the channel that will be accessed (in the [0;getNumChannels()-1] range).
sampleIndexthe index of the sample that will be accessed (in the [0;getSampleCount()-1] range).
valuethe sample value (in [-2^(getSampleBits()-1) ; 2^(getSampleBits()-1)) range).

The documentation for this class was generated from the following file: