Emscripten: Module.HEAPF32.set RangeError: Source is too large

Created on 4 Jan 2016  路  7Comments  路  Source: emscripten-core/emscripten

Hi, this is the code I'm running, and I get a this error: RangeError: Source is too large

var samples = new Float32Array(1024);
var buffer = Module._malloc(1024*4);
Module.HEAPF32.set(samples, buffer);
Module._free(buffer);

Could you explain to me what I'm doing wrong?
Thanks.

Most helpful comment

The malloc function returns pointer to a memory address, but the typed array .set() function takes as the second parameter an index to the array that is being set. The index value refers to units of float32 (4 bytes in size) since HEAPF32 is a Float32Array, so the memory address needs to be divided by four. See the diagram on this page for an illustration: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

All 7 comments

This looks like a bytes versus indices problem. Try

var samples = new Float32Array(1024);
var buffer = Module._malloc(1024*4);
Module.HEAPF32.set(samples, buffer >> 2);
Module._free(buffer);

Works now.
Can you please explain this to me?
How would I get values out of the HEAP?

The malloc function returns pointer to a memory address, but the typed array .set() function takes as the second parameter an index to the array that is being set. The index value refers to units of float32 (4 bytes in size) since HEAPF32 is a Float32Array, so the memory address needs to be divided by four. See the diagram on this page for an illustration: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

Thanks. got it.

I'm trying to compile an audio re-sampler, but for some reason the total number of samples processed is always 0. Could you take a look, maybe I'm not compiling correctly: https://github.com/noamtcohen/DigiResampler

This is how I'm compiling:
emcc resample.c -o digi-resampler.js --memory-init-file 0 -O3 -s EXPORTED_FUNCTIONS="['_resampler_init','_resampler_set_rate','_resampler_process_float', '_resampler_destroy','_resampler_skip_zeros']"

And on the functions I want to export I added the attribute:
__attribute__((used))

Sorry, it looks a bit too wieldy to start digging into a full codebase to debug. Perhaps try adding console.log() traces to both sides of the relevant function calls in C and JS code to see why it might be passing a zero value? Possibly the function call doesn't pass enough values so the remaining function parameters all come out undefined which becomes zero in arithmetic?

Thanks, I had to learn a bit more on how to use emscripten and there where a few mistakes in the way I was using the code. After a bit of digging I got this working. Thanks again for your help.

Was this page helpful?
0 / 5 - 0 ratings