Vector of AudioBuffers not allocated right


First of all I have to admit I’m quite new to this so it’s most likely I’m doing something wrong here :wink:

When instantiating an AudioBuffer it seems like it allocates memory already in a way it is correctly aligned to use with PFFFT (16 byte boundary). At least PFFFT is not throwing an error when using it. Am I just lucky?

However I also need a vector of AudioBuffers which I create like this in my class:

std::vector< AudioBuffer<float> > m_FFTBuffer;

Then in my construcor I use this in my member initialisation list:

m_FFTBuffer((oversamplingFactor * 2), AudioBuffer<float>(numChannels, FFTSize)),

Now PFFFT throws an error because the allocated memory is not aligned. Is there an easy way to fix this? Or do I need to push_back AudioBuffers using preallocated memory in my constructor?


p.s. I could also copy it to a single preallocated AudioBuffer before feeding PFFFT, then copy it back - but that doesn’t feel right. What about performance when using AudioBuffer::copyFrom a lot in the processBlock?


I came up with this code in my constructor, however PFFFT still complains…

for (int frameIndex = 0; frameIndex < (oversamplingFactor * 2); frameIndex++)
		std::vector<float*> allocMem;
		for (int channel = 0; channel < numChannels; channel++)
			allocMem.push_back(static_cast<float*>(pffft_aligned_malloc(static_cast<size_t>(FFTSize * sizeof(float)))));
		m_FFTBuffer[frameIndex].setDataToReferTo(, numChannels, FFTSize);

Has anyone ever used a vector containing AudioBuffers using preallocated memory? What am I doing wrong here?


I can think of a couple of potential ways to approach this,

1 - Use an aligned allocator as per this gist

2 - Create your own buffer classes which guarantee alignment (I can post some example code if you’re interested)

Also, for some history see this topic: Alignment of AudioSampleBuffers)


Ouch! Don’t put AudioBuffer<float> in a std::vector. It’s not easy to predict what std::vector will do: it can copy, delete, swap and move around the buffers as desired. std::vector should only be used for types which are “easily” copyable. Consider using an OwnedArray instead.


Wrt your second post - when you call reserve, you’re only reserving a single float for each channel!


Yes, I would be really interested in some example code! Thanks!

Thanks for that, I’ll change for an OwnedArray then!


Here you go.
ob_DataStructures.h (10.8 KB)


Oh, am I? I thought by using vector<float*> myVector followed by myVector.reserve() I reserve a pointer for each channel? Anyway, vector doesn’t seem to be an option…


Huh yeah, I was being dumb!


No worries :wink: Thanks a lot for the code example!!!


your AlignedRectangularArray made it work!!! Thanks again :slight_smile:


Well that’s what it was written for!

Not the greatest class name though :stuck_out_tongue: