36template <
typename Type>
43 : channels (
static_cast<Type**> (preallocatedChannelSpace))
62 jassert (size >= 0 && numChannels >= 0);
127 : numChannels (
other.numChannels),
129 allocatedBytes (
other.allocatedBytes)
131 if (allocatedBytes == 0)
133 allocateChannels (
other.channels, 0);
145 for (
int i = 0; i < numChannels; ++i)
168 for (
int i = 0; i < numChannels; ++i)
183 : numChannels (
other.numChannels),
185 allocatedBytes (
other.allocatedBytes),
186 allocatedData (std::move (
other.allocatedData)),
187 isClear (
other.isClear)
189 if (numChannels < (
int) numElementsInArray (preallocatedChannelSpace))
191 channels = preallocatedChannelSpace;
193 for (
int i = 0; i < numChannels; ++i)
194 preallocatedChannelSpace[i] =
other.channels[i];
198 channels =
other.channels;
201 other.numChannels = 0;
203 other.allocatedBytes = 0;
209 numChannels =
other.numChannels;
211 allocatedBytes =
other.allocatedBytes;
212 allocatedData = std::move (
other.allocatedData);
213 isClear =
other.isClear;
215 if (numChannels < (
int) numElementsInArray (preallocatedChannelSpace))
217 channels = preallocatedChannelSpace;
219 for (
int i = 0; i < numChannels; ++i)
220 preallocatedChannelSpace[i] =
other.channels[i];
224 channels =
other.channels;
227 other.numChannels = 0;
229 other.allocatedBytes = 0;
397 channels =
reinterpret_cast<Type**
> (allocatedData.
get());
442 if (allocatedBytes != 0)
445 allocatedData.
free();
484 template <
typename OtherType>
489 if (
other.hasBeenCleared())
499 auto* dest = channels[
chan];
502 for (
int i = 0; i < size; ++i)
503 dest[i] =
static_cast<Type
> (
src[i]);
514 for (
int i = 0; i < numChannels; ++i)
526 void clear (
int startSample,
int numSamples)
noexcept
528 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
532 if (startSample == 0 && numSamples == size)
535 for (
int i = 0; i < numChannels; ++i)
545 void clear (
int channel,
int startSample,
int numSamples)
noexcept
547 jassert (isPositiveAndBelow (channel, numChannels));
548 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
570 jassert (isPositiveAndBelow (channel, numChannels));
582 jassert (isPositiveAndBelow (
destChannel, numChannels));
583 jassert (isPositiveAndBelow (
destSample, size));
595 jassert (isPositiveAndBelow (
destChannel, numChannels));
596 jassert (isPositiveAndBelow (
destSample, size));
606 void applyGain (
int channel,
int startSample,
int numSamples, Type gain)
noexcept
608 jassert (isPositiveAndBelow (channel, numChannels));
609 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
611 if (gain != Type (1) && ! isClear)
613 auto* d = channels[channel] + startSample;
627 void applyGain (
int startSample,
int numSamples, Type gain)
noexcept
629 for (
int i = 0; i < numChannels; ++i)
630 applyGain (i, startSample, numSamples, gain);
659 jassert (isPositiveAndBelow (channel, numChannels));
660 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
663 auto* d = channels[channel] + startSample;
665 while (--numSamples >= 0)
686 for (
int i = 0; i < numChannels; ++i)
712 jassert (isPositiveAndBelow (
destChannel, numChannels));
714 jassert (isPositiveAndBelow (
sourceChannel, source.numChannels));
758 jassert (isPositiveAndBelow (
destChannel, numChannels));
760 jassert (source !=
nullptr);
810 jassert (isPositiveAndBelow (
destChannel, numChannels));
812 jassert (source !=
nullptr);
820 while (--numSamples >= 0)
845 int numSamples)
noexcept
848 jassert (isPositiveAndBelow (
destChannel, numChannels));
850 jassert (isPositiveAndBelow (
sourceChannel, source.numChannels));
882 int numSamples)
noexcept
884 jassert (isPositiveAndBelow (
destChannel, numChannels));
886 jassert (source !=
nullptr);
911 jassert (isPositiveAndBelow (
destChannel, numChannels));
913 jassert (source !=
nullptr);
919 if (gain != Type (1))
966 jassert (isPositiveAndBelow (
destChannel, numChannels));
968 jassert (source !=
nullptr);
976 while (--numSamples >= 0)
993 jassert (isPositiveAndBelow (channel, numChannels));
994 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
997 return { Type (0), Type (0) };
1003 Type
getMagnitude (
int channel,
int startSample,
int numSamples)
const noexcept
1005 jassert (isPositiveAndBelow (channel, numChannels));
1006 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
1011 auto r =
findMinMax (channel, startSample, numSamples);
1013 return jmax (r.getStart(), -r.getStart(), r.getEnd(), -r.getEnd());
1022 for (
int i = 0; i < numChannels; ++i)
1029 Type
getRMSLevel (
int channel,
int startSample,
int numSamples)
const noexcept
1031 jassert (isPositiveAndBelow (channel, numChannels));
1032 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
1037 auto* data = channels[channel] + startSample;
1040 for (
int i = 0; i < numSamples; ++i)
1046 return static_cast<Type
> (std::sqrt (sum / numSamples));
1050 void reverse (
int channel,
int startSample,
int numSamples)
const noexcept
1052 jassert (isPositiveAndBelow (channel, numChannels));
1053 jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
1056 std::reverse (channels[channel] + startSample,
1057 channels[channel] + startSample + numSamples);
1061 void reverse (
int startSample,
int numSamples)
const noexcept
1063 for (
int i = 0; i < numChannels; ++i)
1064 reverse (i, startSample, numSamples);
1073 int numChannels = 0, size = 0;
1074 size_t allocatedBytes = 0;
1077 Type* preallocatedChannelSpace[32];
1078 bool isClear =
false;
1082 jassert (size >= 0);
1085 allocatedData.malloc (allocatedBytes);
1086 channels =
reinterpret_cast<Type**
> (allocatedData.get());
1089 for (
int i = 0; i < numChannels; ++i)
1095 channels[numChannels] =
nullptr;
1099 void allocateChannels (Type*
const*
dataToReferTo,
int offset)
1101 jassert (offset >= 0);
1104 if (numChannels < (
int) numElementsInArray (preallocatedChannelSpace))
1106 channels =
static_cast<Type**
> (preallocatedChannelSpace);
1110 allocatedData.malloc (numChannels + 1,
sizeof (Type*));
1111 channels =
reinterpret_cast<Type**
> (allocatedData.get());
1114 for (
int i = 0; i < numChannels; ++i)
1117 jassert (dataToReferTo[i] !=
nullptr);
1118 channels[i] = dataToReferTo[i] + offset;
1121 channels[numChannels] =
nullptr;
1138using AudioSampleBuffer = AudioBuffer<float>;
Holds a resizable array of primitive or copy-by-value objects.
int size() const noexcept
Returns the current number of elements in the array.
A multi-channel buffer containing floating point audio samples.
AudioBuffer(Type *const *dataToReferTo, int numChannelsToUse, int startSample, int numSamples)
Creates a buffer using a pre-allocated block of memory.
void setSize(int newNumChannels, int newNumSamples, bool keepExistingContent=false, bool clearExtraSpace=false, bool avoidReallocating=false)
Changes the buffer's size or number of channels.
Type getMagnitude(int channel, int startSample, int numSamples) const noexcept
Finds the highest absolute sample value within a region of a channel.
AudioBuffer(Type *const *dataToReferTo, int numChannelsToUse, int numSamples)
Creates a buffer using a pre-allocated block of memory.
Type getSample(int channel, int sampleIndex) const noexcept
Returns a sample from the buffer.
void applyGain(Type gain) noexcept
Applies a gain multiple to all the audio data.
Range< Type > findMinMax(int channel, int startSample, int numSamples) const noexcept
Returns a Range indicating the lowest and highest sample values in a given section.
AudioBuffer(const AudioBuffer &other)
Copies another buffer.
const Type * getReadPointer(int channelNumber, int sampleIndex) const noexcept
Returns a pointer to an array of read-only samples in one of the buffer's channels.
void makeCopyOf(const AudioBuffer< OtherType > &other, bool avoidReallocating=false)
Resizes this buffer to match the given one, and copies all of its content across.
void applyGain(int startSample, int numSamples, Type gain) noexcept
Applies a gain multiple to a region of all the channels.
void setDataToReferTo(Type **dataToReferTo, int newNumChannels, int newNumSamples)
Makes this buffer point to a pre-allocated set of channel data arrays.
void copyFromWithRamp(int destChannel, int destStartSample, const Type *source, int numSamples, Type startGain, Type endGain) noexcept
Copies samples from an array of floats into one of the channels, applying a gain ramp.
void addSample(int destChannel, int destSample, Type valueToAdd) noexcept
Adds a value to a sample in the buffer.
Type getRMSLevel(int channel, int startSample, int numSamples) const noexcept
Returns the root mean squared level for a region of a channel.
Type * getWritePointer(int channelNumber) noexcept
Returns a writeable pointer to one of the buffer's channels.
void copyFrom(int destChannel, int destStartSample, const Type *source, int numSamples) noexcept
Copies samples from an array of floats into one of the channels.
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
Type * getWritePointer(int channelNumber, int sampleIndex) noexcept
Returns a writeable pointer to one of the buffer's channels.
int getNumSamples() const noexcept
Returns the number of samples allocated in each of the buffer's channels.
void clear() noexcept
Clears all the samples in all channels.
void clear(int channel, int startSample, int numSamples) noexcept
Clears a specified region of just one channel.
void copyFrom(int destChannel, int destStartSample, const Type *source, int numSamples, Type gain) noexcept
Copies samples from an array of floats into one of the channels, applying a gain to it.
~AudioBuffer()=default
Destructor.
void applyGainRamp(int channel, int startSample, int numSamples, Type startGain, Type endGain) noexcept
Applies a range of gains to a region of a channel.
void setDataToReferTo(Type **dataToReferTo, int newNumChannels, int newStartSample, int newNumSamples)
Makes this buffer point to a pre-allocated set of channel data arrays.
void copyFrom(int destChannel, int destStartSample, const AudioBuffer &source, int sourceChannel, int sourceStartSample, int numSamples) noexcept
Copies samples from another buffer to this one.
void reverse(int startSample, int numSamples) const noexcept
Reverses a part of the buffer.
void addFromWithRamp(int destChannel, int destStartSample, const Type *source, int numSamples, Type startGain, Type endGain) noexcept
Adds samples from an array of floats, applying a gain ramp to them.
Type getMagnitude(int startSample, int numSamples) const noexcept
Finds the highest absolute sample value within a region on all channels.
void reverse(int channel, int startSample, int numSamples) const noexcept
Reverses a part of a channel.
void setSample(int destChannel, int destSample, Type newValue) noexcept
Sets a sample in the buffer.
void addFrom(int destChannel, int destStartSample, const AudioBuffer &source, int sourceChannel, int sourceStartSample, int numSamples, Type gainToApplyToSource=Type(1)) noexcept
Adds samples from another buffer to this one.
bool hasBeenCleared() const noexcept
Returns true if the buffer has been entirely cleared.
const Type * getReadPointer(int channelNumber) const noexcept
Returns a pointer to an array of read-only samples in one of the buffer's channels.
AudioBuffer & operator=(const AudioBuffer &other)
Copies another buffer onto this one.
AudioBuffer(AudioBuffer &&other) noexcept
Move constructor.
Type SampleType
This allows templated code that takes an AudioBuffer to access its sample type.
AudioBuffer() noexcept
Creates an empty buffer with 0 channels and 0 length.
void clear(int startSample, int numSamples) noexcept
Clears a specified region of all the channels.
const Type ** getArrayOfReadPointers() const noexcept
Returns an array of pointers to the channels in the buffer.
AudioBuffer(int numChannelsToAllocate, int numSamplesToAllocate)
Creates a buffer with a specified number of channels and samples.
void addFrom(int destChannel, int destStartSample, const Type *source, int numSamples, Type gainToApplyToSource=Type(1)) noexcept
Adds samples from an array of floats to one of the channels.
void applyGainRamp(int startSample, int numSamples, Type startGain, Type endGain) noexcept
Applies a range of gains to a region of all channels.
Type ** getArrayOfWritePointers() noexcept
Returns an array of pointers to the channels in the buffer.
void applyGain(int channel, int startSample, int numSamples, Type gain) noexcept
Applies a gain multiple to a region of one channel.
static void JUCE_CALLTYPE multiply(float *dest, const float *src, int numValues) noexcept
Multiplies the destination values by the source values.
static void JUCE_CALLTYPE clear(float *dest, int numValues) noexcept
Clears a vector of floats.
static void JUCE_CALLTYPE copyWithMultiply(float *dest, const float *src, float multiplier, int numValues) noexcept
Copies a vector of floats, multiplying each value by a given multiplier.
static void JUCE_CALLTYPE copy(float *dest, const float *src, int numValues) noexcept
Copies a vector of floats.
static void JUCE_CALLTYPE addWithMultiply(float *dest, const float *src, float multiplier, int numValues) noexcept
Multiplies each source value by the given multiplier, then adds it to the destination value.
static Range< float > JUCE_CALLTYPE findMinAndMax(const float *src, int numValues) noexcept
Finds the minimum and maximum values in the given array.
static void JUCE_CALLTYPE add(float *dest, float amountToAdd, int numValues) noexcept
Adds a fixed value to the destination values.
void clear(SizeType numElements) noexcept
This fills the block with zeros, up to the number of elements specified.
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
Swaps this object's data with the data of another HeapBlock.
ElementType * get() const noexcept
Returns a raw pointer to the allocated data.
void allocate(SizeType newNumElements, bool initialiseToZero)
Allocates a specified amount of memory and optionally clears it.
void free() noexcept
Frees any currently-allocated data.