HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
AudioBuffer< SampleType > Class Template Reference

Container for audio data. More...

#include <hart_audio_buffer.hpp>

Public Member Functions

 AudioBuffer (size_t numChannels=0, size_t numFrames=0, double sampleRateHz=nan< double >())
 Creates an audio buffer.
 
 AudioBuffer (const AudioBuffer &other)
 Creates an audio buffer by copying.
 
 AudioBuffer (AudioBuffer &&other)
 Creates an audio buffer by moving.
 
 ~AudioBuffer ()=default
 The destructor.
 
AudioBufferoperator= (const AudioBuffer &other)
 Creates an audio buffer by copy-assigning.
 
AudioBufferoperator= (AudioBuffer &&other)
 Creates an audio buffer by move-assigning.
 
const SampleType *constgetArrayOfReadPointers () const
 Gets a raw pointer to the read-only audio data.
 
SampleType *constgetArrayOfWritePointers ()
 Gets a raw pointer to the mutable audio data.
 
size_t getNumChannels () const
 Get number of channels.
 
size_t getNumFrames () const
 Get number of frames (samples)
 
void setNumFrames (size_t newNumFrames)
 Resizes the buffer to hold a new number of frames per channel.
 
bool hasSampleRate () const
 Check if a specific sample rate was assigned to the audio buffer.
 
double getSampleRateHz () const
 Get a sample rate metadata.
 
double getLengthSeconds () const
 Get a duration of the audio buffer.
 
SampleTypeoperator[] (size_t channel)
 Get a raw pointer to a specific channel's mutable audio data.
 
const SampleTypeoperator[] (size_t channel) const
 Get a raw pointer to a specific channel's read-only audio data.
 
void appendFrom (const AudioBuffer< SampleType > &otherBuffer)
 Appends data from another buffer.
 
void erase ()
 Clears the buffer.
 
SampleType getMagnitude (size_t channel, size_t startFrame, size_t numFrames) const
 Get the maximum absolute value in the buffer in a specific channel.
 
SampleType getMagnitude (size_t startFrame, size_t numFrames) const
 Get the maximum absolute value in the buffer across all channels.
 
void copyFrom (size_t destChannel, size_t destStartFrame, const AudioBuffer &source, size_t sourceChannel, size_t sourceStartFrame, size_t numFrames)
 Copies audio from another buffer.
 
void copyFrom (size_t destChannel, size_t destStartFrame, const SampleType *source, size_t numFrames)
 Copies audio from another generic audio buffer.
 
void clear ()
 Clears the entire buffer.
 
void clear (size_t channel, size_t startFrame, size_t numFrames)
 Clears a specific section of a given channel.
 
void represent (std::ostream &stream) const
 Prints readable representation of the audio buffer.
 

Static Public Member Functions

static AudioBuffer emptyLike (const AudioBuffer &other)
 Creates an empty audio buffer with the same number of channels, frames and sample rate as the other buffer.
 

Detailed Description

template<typename SampleType>
class hart::AudioBuffer< SampleType >

Container for audio data.

Note
This class owns a memory block with audio samples - so it's a container, not a view. Treat it like a heavyweight object.

Definition at line 20 of file hart_audio_buffer.hpp.

Constructor & Destructor Documentation

◆ AudioBuffer() [1/3]

template<typename SampleType >
AudioBuffer ( size_t  numChannels = 0,
size_t  numFrames = 0,
double  sampleRateHz = nan<double>() 
)
inline

Creates an audio buffer.

Parameters
numChannelsInitial number of channels
numFramesNumbers of frames (samples) to be allocated in each channel
sampleRateHzMetadata for sample rate (in Hz) in which the data whould be interpreted, whenever applicable

Definition at line 27 of file hart_audio_buffer.hpp.

◆ AudioBuffer() [2/3]

template<typename SampleType >
AudioBuffer ( const AudioBuffer< SampleType > &  other)
inline

Creates an audio buffer by copying.

Definition at line 38 of file hart_audio_buffer.hpp.

◆ AudioBuffer() [3/3]

template<typename SampleType >
AudioBuffer ( AudioBuffer< SampleType > &&  other)
inline

Creates an audio buffer by moving.

Definition at line 49 of file hart_audio_buffer.hpp.

◆ ~AudioBuffer()

template<typename SampleType >
~AudioBuffer ( )
default

The destructor.

Member Function Documentation

◆ operator=() [1/2]

template<typename SampleType >
AudioBuffer & operator= ( const AudioBuffer< SampleType > &  other)
inline

Creates an audio buffer by copy-assigning.

Definition at line 63 of file hart_audio_buffer.hpp.

◆ operator=() [2/2]

template<typename SampleType >
AudioBuffer & operator= ( AudioBuffer< SampleType > &&  other)
inline

Creates an audio buffer by move-assigning.

Definition at line 81 of file hart_audio_buffer.hpp.

◆ getArrayOfReadPointers()

template<typename SampleType >
const SampleType *const * getArrayOfReadPointers ( ) const
inline

Gets a raw pointer to the read-only audio data.

Returns
A pointer to the beginning of the audio data. Guaranteed to be a contiguous non-interleaved block of memory.

Definition at line 98 of file hart_audio_buffer.hpp.

◆ getArrayOfWritePointers()

template<typename SampleType >
SampleType *const * getArrayOfWritePointers ( )
inline

Gets a raw pointer to the mutable audio data.

Returns
A pointer to the beginning of the audio data. Guaranteed to be a contiguous non-interleaved block of memory.

Definition at line 105 of file hart_audio_buffer.hpp.

◆ emptyLike()

template<typename SampleType >
static AudioBuffer emptyLike ( const AudioBuffer< SampleType > &  other)
inlinestatic

Creates an empty audio buffer with the same number of channels, frames and sample rate as the other buffer.

Note
The data from other buffer will not be copied, expect un-initialized values.
Parameters
otherReference buffer

Definition at line 113 of file hart_audio_buffer.hpp.

◆ getNumChannels()

template<typename SampleType >
size_t getNumChannels ( ) const
inline

Get number of channels.

Returns
Number of allocated channels

Definition at line 122 of file hart_audio_buffer.hpp.

◆ getNumFrames()

template<typename SampleType >
size_t getNumFrames ( ) const
inline

Get number of frames (samples)

Returns
Number of allocated frames (samples) in every channel

Definition at line 126 of file hart_audio_buffer.hpp.

◆ setNumFrames()

template<typename SampleType >
void setNumFrames ( size_t  newNumFrames)
inline

Resizes the buffer to hold a new number of frames per channel.

Resizing behaviour:

  • If newNumFrames == current size, it won't do anything.
  • If newNumFrames > current size, it will append silence (zeros) at the end.
  • If newNumFrames < current size: it will jsut truncate, discarding samples from the end. Will always preserve existing channels and sample-rate metadata.
    Attention
    May cause reallocation, thus invalidating previous raw pointers!
    Parameters
    newNumFramesNew number of frames per channel

Definition at line 136 of file hart_audio_buffer.hpp.

◆ hasSampleRate()

template<typename SampleType >
bool hasSampleRate ( ) const
inline

Check if a specific sample rate was assigned to the audio buffer.

Note
Unititialized buffers, as well as some specific Signal types, will not have a specific sample rate.
Returns
true if there is a specific sample rate value, false otherwise

Definition at line 169 of file hart_audio_buffer.hpp.

◆ getSampleRateHz()

template<typename SampleType >
double getSampleRateHz ( ) const
inline

Get a sample rate metadata.

Note
Call hasSampleRate() if you're not sure if the buffer has a specific sample rate
Returns
Buffer's sample rate in Hz

Definition at line 177 of file hart_audio_buffer.hpp.

◆ getLengthSeconds()

template<typename SampleType >
double getLengthSeconds ( ) const
inline

Get a duration of the audio buffer.

Note
Before calling it, it's a good idea to call hasSampleRate() to check if the buffer has a specific sample rate, otherwise the duration is unknown.
Returns
Duration of the audio buffer in seconds

Definition at line 186 of file hart_audio_buffer.hpp.

◆ operator[]() [1/2]

template<typename SampleType >
SampleType * operator[] ( size_t  channel)
inline

Get a raw pointer to a specific channel's mutable audio data.

Note
The data is guaranteed to have at least getNumFrames() amount of items, and guaranteed to be contiguus and non-interleaves block of memory
Returns
Pointer to the audio data

Definition at line 199 of file hart_audio_buffer.hpp.

◆ operator[]() [2/2]

template<typename SampleType >
const SampleType * operator[] ( size_t  channel) const
inline

Get a raw pointer to a specific channel's read-only audio data.

Note
The data is guaranteed to have at least getNumFrames() amount of items, and guaranteed to be contiguus and non-interleaves block of memory
Returns
Pointer to the audio data

Definition at line 207 of file hart_audio_buffer.hpp.

◆ appendFrom()

template<typename SampleType >
void appendFrom ( const AudioBuffer< SampleType > &  otherBuffer)
inline

Appends data from another buffer.

Warning
This operation will resize the current buffer and potentially invalidate the previous raw data pointers returned by getArrayOfReadPointers(), getArrayOfWritePointers() and the "`[]`" operator, so make sure to keep those external pointers up to date.
Parameters
otherBufferA buffer to append from

Definition at line 216 of file hart_audio_buffer.hpp.

◆ erase()

template<typename SampleType >
void erase ( )
inline

Clears the buffer.

The number of frames after this operaion will be zero, but channel number will persist.

Definition at line 244 of file hart_audio_buffer.hpp.

◆ getMagnitude() [1/2]

template<typename SampleType >
SampleType getMagnitude ( size_t  channel,
size_t  startFrame,
size_t  numFrames 
) const
inline

Get the maximum absolute value in the buffer in a specific channel.

Parameters
channelChannel of which the magnitude should be measured
startFrameThe beginnig of the range of the frames to look for magnitude
numFramesNumber of frames, beginning with startFrame, to look for the magnitude
Returns
The maximum value, in linear domain (i.e. not decibels)

Definition at line 263 of file hart_audio_buffer.hpp.

◆ getMagnitude() [2/2]

template<typename SampleType >
SampleType getMagnitude ( size_t  startFrame,
size_t  numFrames 
) const
inline

Get the maximum absolute value in the buffer across all channels.

Parameters
startFrameThe beginnig of the range of the frames to look for magnitude
numFramesNumber of frames, beginning with startFrame, to look for the magnitude
Returns
The maximum value, in linear domain (i.e. not decibels)

Definition at line 285 of file hart_audio_buffer.hpp.

◆ copyFrom() [1/2]

template<typename SampleType >
void copyFrom ( size_t  destChannel,
size_t  destStartFrame,
const AudioBuffer< SampleType > &  source,
size_t  sourceChannel,
size_t  sourceStartFrame,
size_t  numFrames 
)
inline

Copies audio from another buffer.

Parameters
destChannelChannel within this buffer to copy the frames to
destStartFrameStart frame within this buffer's channel
sourceSource buffer to read from
sourceChannelChannel within the source buffer to read from
sourceStartFrameOffset within the source buffer's channel to start reading frames from
numFramesNumber of frames to copy

Definition at line 315 of file hart_audio_buffer.hpp.

◆ copyFrom() [2/2]

template<typename SampleType >
void copyFrom ( size_t  destChannel,
size_t  destStartFrame,
const SampleType source,
size_t  numFrames 
)
inline

Copies audio from another generic audio buffer.

Parameters
destChannelChannel within this buffer to copy the frames to
destStartFrameStart frame within this buffer's channel
sourcePointer to the source sample data, must contain at least numFrames samples
numFramesNumber of frames to copy

Definition at line 338 of file hart_audio_buffer.hpp.

◆ clear() [1/2]

template<typename SampleType >
void clear ( )
inline

Clears the entire buffer.

Sets all frames in all channels to zeros, keeping the sample rate value intact

Definition at line 351 of file hart_audio_buffer.hpp.

◆ clear() [2/2]

template<typename SampleType >
void clear ( size_t  channel,
size_t  startFrame,
size_t  numFrames 
)
inline

Clears a specific section of a given channel.

Overwrites a selected section of the channel with zeros

Parameters
channelCnannel in which to clear a frame range
startFrameStart of the frame range to clear (inclusive)
numFramesAmount of frames to clear

Definition at line 361 of file hart_audio_buffer.hpp.

◆ represent()

template<typename SampleType >
void represent ( std::ostream &  stream) const
inline

Prints readable representation of the audio buffer.

Parameters
streamString stream to append the representation to

Definition at line 374 of file hart_audio_buffer.hpp.


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