LMMS
Loading...
Searching...
No Matches
water::AudioSampleBuffer Class Reference

#include <AudioSampleBuffer.h>

Public Member Functions

 AudioSampleBuffer () noexcept
 AudioSampleBuffer (const uint32_t numChannelsToAllocate, const uint32_t numSamplesToAllocate, const bool clearData=false) noexcept
 AudioSampleBuffer (float *const *dataToReferTo, const uint32_t numChannelsToUse, const uint32_t numSamples) noexcept
 AudioSampleBuffer (float *const *dataToReferTo, const uint32_t numChannelsToUse, const uint32_t startSample, const uint32_t numSamples) noexcept
 AudioSampleBuffer (const AudioSampleBuffer &other) noexcept
AudioSampleBufferoperator= (const AudioSampleBuffer &other) noexcept
 ~AudioSampleBuffer () noexcept
uint32_t getNumChannels () const noexcept
uint32_t getNumSamples () const noexcept
const float * getReadPointer (const uint32_t channelNumber) const noexcept
const float * getReadPointer (const uint32_t channelNumber, const uint32_t sampleIndex) const noexcept
float * getWritePointer (const uint32_t channelNumber) noexcept
float * getWritePointer (const uint32_t channelNumber, const uint32_t sampleIndex) noexcept
const float ** getArrayOfReadPointers () const noexcept
float ** getArrayOfWritePointers () noexcept
bool setSize (const uint32_t newNumChannels, const uint32_t newNumSamples) noexcept
bool setSizeRT (const uint32_t newNumSamples) noexcept
bool setDataToReferTo (float **dataToReferTo, const uint32_t newNumChannels, const uint32_t newNumSamples) noexcept
void clear () noexcept
void clear (const uint32_t startSample, const uint32_t numSamples) noexcept
void clear (const uint32_t channel, const uint32_t startSample, const uint32_t numSamples) noexcept
bool hasBeenCleared () const noexcept
void addFrom (const uint32_t destChannel, const uint32_t destStartSample, const AudioSampleBuffer &source, const uint32_t sourceChannel, const uint32_t sourceStartSample, const uint32_t numSamples, const float gainToApplyToSource=1.0f) noexcept
void addFrom (const uint32_t destChannel, const uint32_t destStartSample, const float *source, const uint32_t numSamples, float gainToApplyToSource=1.0f) noexcept
void copyFrom (const uint32_t destChannel, const uint32_t destStartSample, const AudioSampleBuffer &source, const uint32_t sourceChannel, const uint32_t sourceStartSample, const uint32_t numSamples) noexcept
void copyFrom (const uint32_t destChannel, const uint32_t destStartSample, const float *source, const uint32_t numSamples) noexcept
void copyFrom (const uint32_t destChannel, const uint32_t destStartSample, const float *source, const uint32_t numSamples, float gain) noexcept
void copyFromInterleavedSource (const uint32_t destChannel, const float *source, const uint32_t totalNumSamples) noexcept

Private Member Functions

bool allocateData (bool clearData=false)
bool allocateChannels (float *const *const dataToReferTo, const uint32_t offset)

Private Attributes

uint32_t numChannels
uint32_t size
size_t allocatedBytes
float ** channels
HeapBlock< char > allocatedData
float * preallocatedChannelSpace [32]
bool isClear

Detailed Description

A multi-channel buffer of floating point audio samples.

See also
AudioSampleBuffer

Constructor & Destructor Documentation

◆ AudioSampleBuffer() [1/5]

water::AudioSampleBuffer::AudioSampleBuffer ( )
inlinenoexcept

Creates an empty buffer with 0 channels and 0 length.

◆ AudioSampleBuffer() [2/5]

water::AudioSampleBuffer::AudioSampleBuffer ( const uint32_t numChannelsToAllocate,
const uint32_t numSamplesToAllocate,
const bool clearData = false )
inlinenoexcept

Creates a buffer with a specified number of channels and samples.

The contents of the buffer will initially be undefined, so use clear() to set all the samples to zero.

The buffer will allocate its memory internally, and this will be released when the buffer is deleted. If the memory can't be allocated, this will throw a std::bad_alloc exception.

◆ AudioSampleBuffer() [3/5]

water::AudioSampleBuffer::AudioSampleBuffer ( float *const * dataToReferTo,
const uint32_t numChannelsToUse,
const uint32_t numSamples )
inlinenoexcept

Creates a buffer using a pre-allocated block of memory.

Note that if the buffer is resized or its number of channels is changed, it will re-allocate memory internally and copy the existing data to this new area, so it will then stop directly addressing this memory.

Parameters
dataToReferToa pre-allocated array containing pointers to the data for each channel that should be used by this buffer. The buffer will only refer to this memory, it won't try to delete it when the buffer is deleted or resized.
numChannelsToUsethe number of channels to use - this must correspond to the number of elements in the array passed in
numSamplesthe number of samples to use - this must correspond to the size of the arrays passed in

◆ AudioSampleBuffer() [4/5]

water::AudioSampleBuffer::AudioSampleBuffer ( float *const * dataToReferTo,
const uint32_t numChannelsToUse,
const uint32_t startSample,
const uint32_t numSamples )
inlinenoexcept

Creates a buffer using a pre-allocated block of memory.

Note that if the buffer is resized or its number of channels is changed, it will re-allocate memory internally and copy the existing data to this new area, so it will then stop directly addressing this memory.

Parameters
dataToReferToa pre-allocated array containing pointers to the data for each channel that should be used by this buffer. The buffer will only refer to this memory, it won't try to delete it when the buffer is deleted or resized.
numChannelsToUsethe number of channels to use - this must correspond to the number of elements in the array passed in
startSamplethe offset within the arrays at which the data begins
numSamplesthe number of samples to use - this must correspond to the size of the arrays passed in

◆ AudioSampleBuffer() [5/5]

water::AudioSampleBuffer::AudioSampleBuffer ( const AudioSampleBuffer & other)
inlinenoexcept

Copies another buffer.

This buffer will make its own copy of the other's data, unless the buffer was created using an external data buffer, in which case boths buffers will just point to the same shared block of data.

◆ ~AudioSampleBuffer()

water::AudioSampleBuffer::~AudioSampleBuffer ( )
inlinenoexcept

Destructor. This will free any memory allocated by the buffer.

Member Function Documentation

◆ addFrom() [1/2]

void water::AudioSampleBuffer::addFrom ( const uint32_t destChannel,
const uint32_t destStartSample,
const AudioSampleBuffer & source,
const uint32_t sourceChannel,
const uint32_t sourceStartSample,
const uint32_t numSamples,
const float gainToApplyToSource = 1.0f )
inlinenoexcept

Adds samples from another buffer to this one.

Parameters
destChannelthe channel within this buffer to add the samples to
destStartSamplethe start sample within this buffer's channel
sourcethe source buffer to add from
sourceChannelthe channel within the source buffer to read from
sourceStartSamplethe offset within the source buffer's channel to start reading samples from
numSamplesthe number of samples to process
gainToApplyToSourcean optional gain to apply to the source samples before they are added to this buffer's samples
See also
copyFrom

◆ addFrom() [2/2]

void water::AudioSampleBuffer::addFrom ( const uint32_t destChannel,
const uint32_t destStartSample,
const float * source,
const uint32_t numSamples,
float gainToApplyToSource = 1.0f )
inlinenoexcept

Adds samples from an array of floats to one of the channels.

Parameters
destChannelthe channel within this buffer to add the samples to
destStartSamplethe start sample within this buffer's channel
sourcethe source data to use
numSamplesthe number of samples to process
gainToApplyToSourcean optional gain to apply to the source samples before they are added to this buffer's samples
See also
copyFrom

◆ allocateChannels()

bool water::AudioSampleBuffer::allocateChannels ( float *const *const dataToReferTo,
const uint32_t offset )
inlineprivate

◆ allocateData()

bool water::AudioSampleBuffer::allocateData ( bool clearData = false)
inlineprivate

◆ clear() [1/3]

void water::AudioSampleBuffer::clear ( )
inlinenoexcept

Clears all the samples in all channels.

◆ clear() [2/3]

void water::AudioSampleBuffer::clear ( const uint32_t channel,
const uint32_t startSample,
const uint32_t numSamples )
inlinenoexcept

Clears a specified region of just one channel.

For speed, this doesn't check whether the channel and sample number are in-range, so be careful!

◆ clear() [3/3]

void water::AudioSampleBuffer::clear ( const uint32_t startSample,
const uint32_t numSamples )
inlinenoexcept

Clears a specified region of all the channels.

For speed, this doesn't check whether the channel and sample number are in-range, so be careful!

◆ copyFrom() [1/3]

void water::AudioSampleBuffer::copyFrom ( const uint32_t destChannel,
const uint32_t destStartSample,
const AudioSampleBuffer & source,
const uint32_t sourceChannel,
const uint32_t sourceStartSample,
const uint32_t numSamples )
inlinenoexcept

Copies samples from another buffer to this one.

Parameters
destChannelthe channel within this buffer to copy the samples to
destStartSamplethe start sample within this buffer's channel
sourcethe source buffer to read from
sourceChannelthe channel within the source buffer to read from
sourceStartSamplethe offset within the source buffer's channel to start reading samples from
numSamplesthe number of samples to process
See also
addFrom

◆ copyFrom() [2/3]

void water::AudioSampleBuffer::copyFrom ( const uint32_t destChannel,
const uint32_t destStartSample,
const float * source,
const uint32_t numSamples )
inlinenoexcept

Copies samples from an array of floats into one of the channels.

Parameters
destChannelthe channel within this buffer to copy the samples to
destStartSamplethe start sample within this buffer's channel
sourcethe source buffer to read from
numSamplesthe number of samples to process
See also
addFrom

◆ copyFrom() [3/3]

void water::AudioSampleBuffer::copyFrom ( const uint32_t destChannel,
const uint32_t destStartSample,
const float * source,
const uint32_t numSamples,
float gain )
inlinenoexcept

Copies samples from an array of floats into one of the channels, applying a gain to it.

Parameters
destChannelthe channel within this buffer to copy the samples to
destStartSamplethe start sample within this buffer's channel
sourcethe source buffer to read from
numSamplesthe number of samples to process
gainthe gain to apply
See also
addFrom

◆ copyFromInterleavedSource()

void water::AudioSampleBuffer::copyFromInterleavedSource ( const uint32_t destChannel,
const float * source,
const uint32_t totalNumSamples )
inlinenoexcept

Copies samples from an array of floats into one of the channels.

Parameters
destChannelthe channel within this buffer to copy the samples to
destStartSamplethe start sample within this buffer's channel
sourcethe source buffer to read from
numSamplesthe number of samples to process
See also
addFrom

◆ getArrayOfReadPointers()

const float ** water::AudioSampleBuffer::getArrayOfReadPointers ( ) const
inlinenoexcept

Returns an array of pointers to the channels in the buffer.

Don't modify any of the pointers that are returned, and bear in mind that these will become invalid if the buffer is resized.

◆ getArrayOfWritePointers()

float ** water::AudioSampleBuffer::getArrayOfWritePointers ( )
inlinenoexcept

Returns an array of pointers to the channels in the buffer.

Don't modify any of the pointers that are returned, and bear in mind that these will become invalid if the buffer is resized.

◆ getNumChannels()

uint32_t water::AudioSampleBuffer::getNumChannels ( ) const
inlinenoexcept

Returns the number of channels of audio data that this buffer contains.

See also
getSampleData

◆ getNumSamples()

uint32_t water::AudioSampleBuffer::getNumSamples ( ) const
inlinenoexcept

Returns the number of samples allocated in each of the buffer's channels.

See also
getSampleData

◆ getReadPointer() [1/2]

const float * water::AudioSampleBuffer::getReadPointer ( const uint32_t channelNumber) const
inlinenoexcept

Returns a pointer to an array of read-only samples in one of the buffer's channels. For speed, this doesn't check whether the channel number is out of range, so be careful when using it! If you need to write to the data, do NOT call this method and const_cast the result! Instead, you must call getWritePointer so that the buffer knows you're planning on modifying the data.

◆ getReadPointer() [2/2]

const float * water::AudioSampleBuffer::getReadPointer ( const uint32_t channelNumber,
const uint32_t sampleIndex ) const
inlinenoexcept

Returns a pointer to an array of read-only samples in one of the buffer's channels. For speed, this doesn't check whether the channel number or index are out of range, so be careful when using it! If you need to write to the data, do NOT call this method and const_cast the result! Instead, you must call getWritePointer so that the buffer knows you're planning on modifying the data.

◆ getWritePointer() [1/2]

float * water::AudioSampleBuffer::getWritePointer ( const uint32_t channelNumber)
inlinenoexcept

Returns a writeable pointer to one of the buffer's channels. For speed, this doesn't check whether the channel number is out of range, so be careful when using it! Note that if you're not planning on writing to the data, you should always use getReadPointer instead.

◆ getWritePointer() [2/2]

float * water::AudioSampleBuffer::getWritePointer ( const uint32_t channelNumber,
const uint32_t sampleIndex )
inlinenoexcept

Returns a writeable pointer to one of the buffer's channels. For speed, this doesn't check whether the channel number or index are out of range, so be careful when using it! Note that if you're not planning on writing to the data, you should use getReadPointer instead.

◆ hasBeenCleared()

bool water::AudioSampleBuffer::hasBeenCleared ( ) const
inlinenoexcept

Returns true if the buffer has been entirely cleared. Note that this does not actually measure the contents of the buffer - it simply returns a flag that is set when the buffer is cleared, and which is reset whenever functions like getWritePointer() are invoked. That means the method does not take any time, but it may return false negatives when in fact the buffer is still empty.

◆ operator=()

AudioSampleBuffer & water::AudioSampleBuffer::operator= ( const AudioSampleBuffer & other)
inlinenoexcept

◆ setDataToReferTo()

bool water::AudioSampleBuffer::setDataToReferTo ( float ** dataToReferTo,
const uint32_t newNumChannels,
const uint32_t newNumSamples )
inlinenoexcept

Makes this buffer point to a pre-allocated set of channel data arrays.

There's also a constructor that lets you specify arrays like this, but this lets you change the channels dynamically.

Note that if the buffer is resized or its number of channels is changed, it will re-allocate memory internally and copy the existing data to this new area, so it will then stop directly addressing this memory.

Parameters
dataToReferToa pre-allocated array containing pointers to the data for each channel that should be used by this buffer. The buffer will only refer to this memory, it won't try to delete it when the buffer is deleted or resized.
newNumChannelsthe number of channels to use - this must correspond to the number of elements in the array passed in
newNumSamplesthe number of samples to use - this must correspond to the size of the arrays passed in

◆ setSize()

bool water::AudioSampleBuffer::setSize ( const uint32_t newNumChannels,
const uint32_t newNumSamples )
inlinenoexcept

Changes the buffer's size or number of channels.

This can expand the buffer's length, and add or remove channels.

◆ setSizeRT()

bool water::AudioSampleBuffer::setSizeRT ( const uint32_t newNumSamples)
inlinenoexcept

Changes the buffer's size in a real-time safe manner.

Returns true if the required memory is available.

Member Data Documentation

◆ allocatedBytes

size_t water::AudioSampleBuffer::allocatedBytes
private

◆ allocatedData

HeapBlock<char> water::AudioSampleBuffer::allocatedData
private

◆ channels

float** water::AudioSampleBuffer::channels
private

◆ isClear

bool water::AudioSampleBuffer::isClear
private

◆ numChannels

uint32_t water::AudioSampleBuffer::numChannels
private

◆ preallocatedChannelSpace

float* water::AudioSampleBuffer::preallocatedChannelSpace[32]
private

◆ size

uint32_t water::AudioSampleBuffer::size
private

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