Increase performance when reading samples from a var


#1

Hello

I have a var full of samples that I’m writing to disk as follows:

[code]const var writeChunk (const var* params, int numParams)
{
checkParams (3);

jassert (params[0].isObject() && params[1].isObject());
        
const int numSamples = params[2];

if (numSamples <= 0)
	return 0;
		
HeapBlock<float> tempData (sizeof (float) * numSamples * 2);
float* const left = tempData;
float* const right = tempData + numSamples;

varToArray (params[0], left, numSamples);
varToArray (params[1], right, numSamples);

writeToDisk(left, right, numSamples);
tempData.free();
return ret;

}

static void varToArray(const var& samples, float* data, int arrLength)
{
int i = 0;
var::identifier i2 (“0”);

while (i < arrLength)
{
	i2 = (String)i;
	data[i] = (float)(double) samples[i2];
	i++;
}

}
[/code]

This all works fine but it’s very slow, it takes about 1.5 minutes for 10 seconds of audio. I’ve narrowed it down to the varToArray function, possibly the String to Identifier conversion? Does anybody see any way I could speed this up?

Many Thanks


#2

Sorry, but WTF???

Storing samples in var object…!??


#3

I’d say it’s time to re-write the whole thing. You’re method varToArray is always going to be slow.

Try using the Audio classes already in Juce maybe?


#4

hehe, well they’re passed on from the browser, so i don’t see another way.

I tried passing the samples in a json string, but i can only decode that to a var too.
I tried converting the var to an Array and then accessing the data with a rawpointer, but i just get scrambed eggs out of my speakers.

so, please enlighten me on how to do this better :wink:


#5

Ah, that is a bit of a tricky situation!

Might be quicker to send them as a single string, separated by spaces, and then write a routine to quickly iterate that. And if you can do it as 16-bit integers, that’d be quicker than floats.


#6

Ah i hadn’t thought about parsing a string, but it sparked another implementation that I ended up using.

What i now did is convert a ByteArray -> Base64 -> MemoryBlock :wink:
Works super fast now and I’m very happy :smiley:

Thanks for the input.


#7

Yes - good idea to use base64!