Old 05-21-2020, 12:18 PM   #1
danmc
Human being with feelings
 
Join Date: May 2014
Posts: 9
Default JSXF/EEL2 arrays, memory management

Hi,

I have what should be a pretty basic question about memory management and arrays in JSFX. I've done a good bit of C programming so you can assume a working knowledge in that context and maybe that is why I'm getting confused.

I was looking at the "JS: Chorus" as a vehicle towards understanding how a real effect is programmed. No particular reason for picking this other than having designed/build an analog chorus ages ago and the source code is fairly short.

The real question I have is with the "[]" operator.
i=0;
loop(numvoices,
i[0]=(i+1)/numvoices*$pi;
i+=1;
);

is it true that something like "x[i]" is pretty similar to C in that x is a pointer and i just offsets from there (but by a step size that depends on the storage size of the variables)? So in the example code block are we just filling up memory like:

addr: value
0 : pi/4
1 : 2*pi/4
2 : 3*pi/4
3 : 4*pi/4

but if this is correct, how do I know nothing else is going to use addresses 0,1,2,3? Then later on

bufofs=4096;

and then in the @sample section, the code is shoving samples into this buffer using bpos to index it like:

bpos >= choruslen } (bpos=0;
bufofs[bpos]=os0;
bpos+=1;

and at the same time reading out with indices that are modulated with respect to bpos.

So I'm back to my original question, does this mean that the buffer sits in memory addresses 4096 on up to 4096+choruslen?

so now I have addresses 0 to numvoices-1 and 4096 to 4096+choruslen being used by these two bits of code. But how do I (or the EEL2 compiler) know that other variables, for example,
tpos = ....
vol=wetmix/numvoices
etc
don't get allocated to one of these addresses that is already being used by these two arrays?

Thanks so much
-Dan
danmc is offline   Reply With Quote
Old 05-21-2020, 03:14 PM   #2
nitsuj
Human being with feelings
 
nitsuj's Avatar
 
Join Date: Nov 2017
Posts: 213
Default

Hey Dan, let's see if I can help.

The JSFX system in Reaper has only one variable type which is C/C++ equivalent of 'double'. So indexing into memory is always indexing into an array of doubles and all variables can only contain doubles. There are no pointers, just indexes.

The system allocates a large double array for all of your storage within a JSFX. That's what you get to play with.

The following are the same:

0[10]

10[0]

Both accessing the double at memory location 10.

Now, the loop example you gave. To make it look a little more conventional:

Code:
voice_mem = 0;
i=0;
loop(numvoices, 
  voice_mem[i]=(i+1)/numvoices*$pi; 
  i+=1; 
);
The example code you've been looking at hardwires the memory locations it uses. For small simple plugins you can get away with that. Remember that the memory it's using is local to only that JSFX.

For larger applications this approach may seem unusual. Unlike C/C++ there's no allocation and deallocation so you have to take the view that you'll be fixing your buffers when the JSFX starts, probably inside the @init section.

Here's what I use:

Code:
/*
 * Initialise memory allocator
 */
function init_memory() instance(index) ( 
  index = 0;
);

/*
 * Allocate memory
 */
function alloc_memory(amount) instance(index) local(i) ( 
  i = index;
  index += amount;
  i;
);
Then inside the @init section when I want to reserve memory:

Code:
MEMORY.init_memory();

buffer1 = MEMORY.alloc_memory(4096);
buffer2 = MEMORY.alloc_memory(1024);

buffer1[0] = 1970;
buffer1[1] = 1971;

buffer2[0] = 1993;
// and so on...
This way you reserve all the buffers you'll need upfront and use them thereafter.

Hope that helps!
nitsuj is offline   Reply With Quote
Old 05-21-2020, 03:51 PM   #3
danmc
Human being with feelings
 
Join Date: May 2014
Posts: 9
Default

This is indeed helpful. To make sure I have it, is it true that in your example buffer1 is 0 and buffer2 is 4096 (i.e. address of the start of the buffers)? What I have a scalar variable as well, for example gain below. How do I know that gain would not get stored somewhere in the memory used by those two buffers? Or do variables live in a totally different space than memory accessed with the [] operator? Maybe that is the key bit of understanding I was missing when I first read over the docs.

Code:
MEMORY.init_memory();

buffer1 = MEMORY.alloc_memory(4096);
buffer2 = MEMORY.alloc_memory(1024);

buffer1[0] = 1970;
buffer1[1] = 1971;

buffer2[0] = 1993;
// and so on...

// ADDED
gain = 37.2
danmc is offline   Reply With Quote
Old 05-21-2020, 04:17 PM   #4
nitsuj
Human being with feelings
 
nitsuj's Avatar
 
Join Date: Nov 2017
Posts: 213
Default

Quote:
Originally Posted by danmc View Post
This is indeed helpful. To make sure I have it, is it true that in your example buffer1 is 0 and buffer2 is 4096 (i.e. address of the start of the buffers)? What I have a scalar variable as well, for example gain below. How do I know that gain would not get stored somewhere in the memory used by those two buffers? Or do variables live in a totally different space than memory accessed with the [] operator? Maybe that is the key bit of understanding I was missing when I first read over the docs.

Code:
MEMORY.init_memory();

buffer1 = MEMORY.alloc_memory(4096);
buffer2 = MEMORY.alloc_memory(1024);

buffer1[0] = 1970;
buffer1[1] = 1971;

buffer2[0] = 1993;
// and so on...

// ADDED
gain = 37.2
Yes, buffer1 is 0, buffer2 is 4096.

Variables are a totally separate/different thing and you don't have to worry about those. The memory buffer is accessed using the [] operator.

Buffer usage is a very common thing in DSP programming and this was the main intention for providing the memory space. Essentially a big array of doubles that you can carve up how you see fit.
nitsuj is offline   Reply With Quote
Old 05-21-2020, 04:41 PM   #5
danmc
Human being with feelings
 
Join Date: May 2014
Posts: 9
Default

OK. I understand now. The bit about the buffers being in a different memory space from variables is what I was missing. I feel much better about this! Thank you so much.

-Dan
danmc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:10 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.