Old 12-11-2009, 04:52 PM   #1
bozmillar
Human being with feelings
 
bozmillar's Avatar
 
Join Date: Sep 2009
Posts: 623
Default js arrays

Hey, I'm hoping someone can help me out with this because it's driving me insane. I'm doing this in osX 10.6.2.

Is it true that you can only have 1 array in a js plugin? I have one array, let's call it 'array1'.

If array1[4] is 8, every other array with index 4 has the same value as array1[4]. Is every array storing values in the same place in memory? Is this a bug or is it supposed to be like that? has anyone else seen this?
bozmillar is offline   Reply With Quote
Old 12-12-2009, 02:08 AM   #2
Jonas_Eriksson_Swe
Human being with feelings
 
Jonas_Eriksson_Swe's Avatar
 
Join Date: Jan 2007
Location: Umeå, Sweden
Posts: 947
Default

This thread will solve your problems (I hope : )

http://forum.cockos.com/showthread.php?t=6027

... this is quoted from that thread (post #8)

Quote:
Originally Posted by dsilver View Post
I am the original poster of this question and at first the explanations given still didn't make sense to me, but upon further reflection I realized that it is all very simple and I couldn't understand it at first cause I was overcomplicating it.

What I believe to be the case is this:

There is no difference between the statements

buffer = 1024;
and
x = 5;

in that each one creates a local variable and initializes it.

It is the act of putting square brackets after one of these variables that turns the variable into a pointer, i.e. makes it operate by reference.

So, buffer[0] references the value stored at the 1024th location in local memory. And buffer[1], buffer[2] reference subsequent values. But there is nothing special about the variable "buffer".

So when the docs say:
buffer = 1024; // set buffer to reference the local memory starting at 1024

I think this is misleading. "buffer" isn't being set to reference memory starting at 1024 any more than every other statement which creates and initializes a variable, such as "x = 5;" sets that variable to "reference memory starting at location whatever (in this case 5)."

The fact is that any variable can be used as a pointer into local memory if [] is put after it. So if "buffer" happens to be set the VALUE 1024, then buffer[0] is essentially the same as 1024[0].

All in all it's very handy and very simple. But I think the docs should make clear that there is no special voodoo going on in the buffering example. It is also important to realize that the space which is allocated for the variables themselves, i.e. "buffer", "x", etc. is allocated elsewhere. So one is free to use any location one wants to use within local memory.

So, looking at each of the following lines individually,

1000[1] = 6;
1[1000] = 6;
x = 500; x[501] = 6;
y = 501; y[500] = 6;
buffer = 1001; buffer[0] = 6;
buffer = 1000; buffer[1] = 6;
buffer = 0; buffer[1001] = 6;

All of them do the EXACT same thing with regard to memory location 1001.

"x" and "y" and "buffer" are just variables which can be used in any way variables can be used.

Sorry if this seems all too obvious, but if I can help clear up confusion for someone I think it is worth it.
Regards,
- Jonas
Jonas_Eriksson_Swe is offline   Reply With Quote
Old 12-12-2009, 03:00 AM   #3
liteon
Human being with feelings
 
liteon's Avatar
 
Join Date: Apr 2008
Posts: 510
Default

there is only one big set of memory slots (around 8 million) and you can access these slots in the form of 'offset[index]' where both 'offset' and 'index' can be variables (with any value) and the slot index is accessed like 'index+offset'. the result should be between -1 and 8388607. assigning a fractional number to 'index' or 'offset', will have its fractional part discarded.

take a look at this example:

Code:
// we set a first offset to be at 0.
// and assign 2.5 to the first index
offset0 = 0;
offset0[0] = 2.5;
// second offset is at 100
// and we assign 3.5 to the first index
offset1 = 100;
offset1[0] = 3.5;
// checking the two values
test0 = offset0[0];
test1 = offset1[0];
// test0 = 2.5; test1 = 3.5
but since there is only one big memory array:
Code:
offset0[100] = offset1[0] = 3.5;
setting an offset could also be done with:
Code:
100[0] = 3.5;
// which is equal to 0[100] = 3.5;

Last edited by liteon; 12-15-2009 at 09:27 AM. Reason: clarification
liteon is offline   Reply With Quote
Old 12-12-2009, 08:17 AM   #4
bozmillar
Human being with feelings
 
bozmillar's Avatar
 
Join Date: Sep 2009
Posts: 623
Default

oh man, awesome. That was the issue. That was driving me insane. In my init section I was declaring array1=0; array2=0;

This explains my issue. Thanks a ton.
bozmillar is offline   Reply With Quote
Old 12-12-2009, 10:40 AM   #5
whatsup
Human being with feelings
 
whatsup's Avatar
 
Join Date: Aug 2008
Posts: 1,144
Default

Quote:
Originally Posted by liteon View Post
both 'offset' and 'index' can be variables holding any number between -1 and 8388607.
i was suprised to read that, so i checked it,
result: can hold from -8388607 up 8388607
(infact any number, but valid numbers those which are in the memory range)

maybe that what you ment ?
__________________
you can contack me on
whatsup@inn.co.il
whatsup is offline   Reply With Quote
Old 12-12-2009, 12:01 PM   #6
liteon
Human being with feelings
 
liteon's Avatar
 
Join Date: Apr 2008
Posts: 510
Default

yes the explanation isn't precise (fixed),

a memory slot is accessed in the form of 'offset + index'
so, for a '9000000[-9000000]' call, the actual slot that is accessed will be slot 0.

example:
Code:
a = 9000000;
b = -9000000;
a[b] = 254;
c = a[b];
// c is 254;

0[0] = 253;
c = a[b];
// c is 253;
Quote:
Originally Posted by whatsup View Post
result: can hold from -8388607 up 8388607
(infact any number, but valid numbers those which are in the memory range)
if the available slots are with indexes from -8388607 up 8388607, then the total number of slots will be 16mil+, which isn't the case.

Last edited by liteon; 12-12-2009 at 03:51 PM. Reason: typos
liteon is offline   Reply With Quote
Old 12-12-2009, 12:33 PM   #7
whatsup
Human being with feelings
 
whatsup's Avatar
 
Join Date: Aug 2008
Posts: 1,144
Default

Quote:
Originally Posted by liteon View Post
if the available slot are with indexes from -8388607 up 8388607, then total number of slots will be 16mil+, which isn't the case.
correct, but I said
Quote:
can hold
not
Quote:
available
__________________
you can contack me on
whatsup@inn.co.il
whatsup is offline   Reply With Quote
Old 12-14-2009, 09:52 PM   #8
Fergo
Human being with feelings
 
Fergo's Avatar
 
Join Date: Mar 2009
Location: Curitiba - Brazil
Posts: 371
Default

What kind of data type does Reaper/JS uses for variables/arrays? Floats? Doubles? We have 8 million bytes to use or 8 million memory slots of a given data type?

Fergo
__________________
My new application: Fergo JoystickMIDI - Send commands using your joystick/gamepad
Portfolio: www.fbirck.com
Homepage: www.fergonez.net - Programming stuff

Last edited by Fergo; 12-14-2009 at 10:06 PM.
Fergo is offline   Reply With Quote
Old 12-14-2009, 10:29 PM   #9
dub3000
Human being with feelings
 
dub3000's Avatar
 
Join Date: Mar 2008
Location: Sydney, Australia
Posts: 3,955
Default

everything is doubles.
dub3000 is offline   Reply With Quote
Old 12-15-2009, 04:11 AM   #10
Mich
Human being with feelings
 
Join Date: May 2009
Posts: 1,265
Default

Quote:
Originally Posted by Fergo View Post
What kind of data type does Reaper/JS uses for variables/arrays? Floats? Doubles? We have 8 million bytes to use or 8 million memory slots of a given data type?

Fergo
It's in the Docs:
Quote:
[...]
All variables represent real values (i.e. 0, 1, 3.14159, -3, 3000000000000000.0 are all values that they can represent).
[...]
(so neither float nor double but a datatype that can hold real values)

and

Quote:
[...]
[ ]
Example: z=x[y]; or x[y]=z;
Example: z=gmem[y]; or gmem[y]=z;
In the first form, you may use brackets to index into memory that is local to your effect. Your effect has approximately 8 million (8,388,608) slots of memory and you may access them either with fixed offsets (i.e. 16811[0]) or with variables (myBuffer[5]). If a value in the brackets is omitted then 0 is used instead.

If 'gmem' is specified (the second form), then instead of local effect memory, the buffer is the global storage buffer, which is approximately 1 million (1,048,576) slots that are shared across all effects.
[...]
I guess I know now why so few programmers actually bother writting docs for their stuff ... because noone seems to read it /rant .. so try and make a difference: http://www.reaper.fm/sdk/js/js.php
Mich is offline   Reply With Quote
Old 12-15-2009, 05:36 AM   #11
whatsup
Human being with feelings
 
whatsup's Avatar
 
Join Date: Aug 2008
Posts: 1,144
Default

Quote:
Originally Posted by Fergo View Post
8 million memory slots of a given data type?

Fergo
that's correct.
__________________
you can contack me on
whatsup@inn.co.il
whatsup is offline   Reply With Quote
Old 12-15-2009, 08:10 AM   #12
Fergo
Human being with feelings
 
Fergo's Avatar
 
Join Date: Mar 2009
Location: Curitiba - Brazil
Posts: 371
Default

Quote:
Originally Posted by dub3000 View Post
everything is doubles.
Quote:
Originally Posted by whatsup View Post
that's correct.
Thanks for the answer!

Quote:
Originally Posted by Mich View Post
I guess I know now why so few programmers actually bother writting docs for their stuff ... because noone seems to read it /rant .. so try and make a difference: http://www.reaper.fm/sdk/js/js.php
Calm down fellow.
I do my effects programming with VSTs, not JS. I was just asking that out of curiosity.

Thanks anyway.

Fergo
__________________
My new application: Fergo JoystickMIDI - Send commands using your joystick/gamepad
Portfolio: www.fbirck.com
Homepage: www.fergonez.net - Programming stuff
Fergo 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 08:07 AM.


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