Increase performance when reading samples from a var


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);;
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];


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

Sorry, but WTF???

Storing samples in var object…!??

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?

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:

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.

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.

Yes - good idea to use base64!