Thanks for the reply, that’s a really nice solution. So I guess there’s nothing within JUCE that can condense that down into smaller objects?
I was previously doing this:
std::queue <int> sampleQueue;
int sampleQueueSize = 2100;
...
// sampleValue is being set at the callback to this function
if (sampleQueue.size() <= sampleQueueSize)
{
sampleQueue.push(sampleValue);
}
else
{
sampleQueue.pop();
sampleQueue.push(sampleValue);
}
std::deque isn’t really an improvement. std::queue sucks partly because it by default internally uses std::deque. Anyway, if you don’t need really high real time performance, pretty much anything will work.
Something that doesn’t do surprising memory allocations and has the data elements contiguously in memory. So, a circular buffer (that doesn’t allow the buffer to grow)…
I would advise against any micro-optimisation in the design stage, but while we’re at it, you can do a modulo for any power of two number in one cycle:
There’s nothing better than a wasting a good day unnecessarily micro-optimising something.
This is ours using a handy juce::nextPowerOfTwo function. Where buf is any kind of raw memory-type buffer (juce::Array square bracket operators include an additional safety check…)
Nice one!
For trivial types like float that is a nice solution, but with Array::operator[](int) always be weary, it returns by value and not by reference (i.e. returns a copy), which bit me a few times, when I was starting with JUCE.
I’d be very surprised if compiler didn’t already do this optimisation for you when doing a power of 2 modulo, so better just to write x % 32768 for readability’s sake.
Something I miss from my 68k assembler days is hand optimising algorithms to get the smallest number of CPU cycles possible, but I used to be shockingly bad at commenting and my code would be unreadable after a time away from a particular piece