Vector of AudioBuffers not allocated right


#1

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?

Thanks!

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?


#2

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;
		allocMem.reserve(numChannels);
		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(allocMem.data(), numChannels, FFTSize);
		m_FFTBuffer[frameIndex].clear();
	}

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


#3

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)


#4

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.


#5

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


#6

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

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


#7

Here you go.
ob_DataStructures.h (10.8 KB)


#8

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…


#9

Huh yeah, I was being dumb!


#10

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


#11

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


#12

Well that’s what it was written for!

Not the greatest class name though :stuck_out_tongue: