|
|
|
12-11-2009, 04:52 PM
|
#1
|
Human being with feelings
Join Date: Sep 2009
Posts: 623
|
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?
|
|
|
12-12-2009, 02:08 AM
|
#2
|
Human being with feelings
Join Date: Jan 2007
Location: Umeå, Sweden
Posts: 947
|
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
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
|
|
|
12-12-2009, 03:00 AM
|
#3
|
Human being with feelings
Join Date: Apr 2008
Posts: 510
|
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
|
|
|
12-12-2009, 08:17 AM
|
#4
|
Human being with feelings
Join Date: Sep 2009
Posts: 623
|
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.
|
|
|
12-12-2009, 10:40 AM
|
#5
|
Human being with feelings
Join Date: Aug 2008
Posts: 1,144
|
Quote:
Originally Posted by liteon
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 ?
|
|
|
12-12-2009, 12:01 PM
|
#6
|
Human being with feelings
Join Date: Apr 2008
Posts: 510
|
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
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
|
|
|
12-12-2009, 12:33 PM
|
#7
|
Human being with feelings
Join Date: Aug 2008
Posts: 1,144
|
Quote:
Originally Posted by liteon
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 not
|
|
|
12-14-2009, 09:52 PM
|
#8
|
Human being with feelings
Join Date: Mar 2009
Location: Curitiba - Brazil
Posts: 371
|
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
Last edited by Fergo; 12-14-2009 at 10:06 PM.
|
|
|
12-14-2009, 10:29 PM
|
#9
|
Human being with feelings
Join Date: Mar 2008
Location: Sydney, Australia
Posts: 3,955
|
everything is doubles.
|
|
|
12-15-2009, 04:11 AM
|
#10
|
Human being with feelings
Join Date: May 2009
Posts: 1,265
|
Quote:
Originally Posted by Fergo
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
|
|
|
12-15-2009, 05:36 AM
|
#11
|
Human being with feelings
Join Date: Aug 2008
Posts: 1,144
|
Quote:
Originally Posted by Fergo
8 million memory slots of a given data type?
Fergo
|
that's correct.
|
|
|
12-15-2009, 08:10 AM
|
#12
|
Human being with feelings
Join Date: Mar 2009
Location: Curitiba - Brazil
Posts: 371
|
Quote:
Originally Posted by dub3000
everything is doubles.
|
Quote:
Originally Posted by whatsup
that's correct.
|
Thanks for the answer!
Quote:
Originally Posted by Mich
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
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 08:07 AM.
|