Old 04-06-2012, 08:40 AM   #1
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,740
Default JSFX function support discussion thread

Hello everybody,




Function support has the following features/restrictions:
  • Functions can only be declared at the top level of JS code (i.e. not within a () block etc).
  • Functions defined in @init can be used in any other section, but functions defined elsewhere can only be used in that section.
  • Functions can have 0 or more parameters, and any number of local variables.
  • Functions can not call recursively (a function can only call functions defined prior to that function)
  • Functions have very low overhead, and are automatically inlined if they are below a certain size.
  • The last statement to be executed in a function is effectively the return value (matching the logic used elsewhere in JS).

The most basic declarations and use of functions might be:
Code:
@sample

function getSomeValue()  (
  2;
);

spl1 = spl1 * getSomeValue();
or:
Code:
@sample

function applyGain(spl)  (
  spl * 2;
);

spl1 = applyGain(spl1); // same as spl1 = spl1 * 2;
or


Code:
@sample

function applyGain(spl,scale)  (
  spl * scale;
);

spl1 = applyGain(spl1,2); // same as spl1 = spl1 * 2;
If you wish to define local variables for the function, you can do it by adding one or more local() definitions to the declaration:

Code:
@sample

function applyWave(spl)  
  local(somecounter, scale)
(
  scale < 1.0 ? scale += 1.0/srate;
  somecounter += 1.0/srate;
  spl * sin(somecounter) * scale;
);

spl1 = applyWave(spl1);
Local variable can be used for temporary storage within the function, but they also persist across calls. If you define a function in @init and use it within two other sections (say, @gfx and @sample), your local variables will (now as of April 14) be in separate spaces -- so if you need them shared, you would want to use instance variables.

OK, post any questions or feedback you have here (especially if you see any regressions on existing FX or other code).

Last edited by Justin; 01-17-2014 at 09:15 AM.
Justin is online now   Reply With Quote
Old 04-06-2012, 10:30 AM   #2
Banned
Human being with feelings
 
Banned's Avatar
 
Join Date: Mar 2008
Location: Unwired (probably in the proximity of Amsterdam)
Posts: 4,868
Default

Wow... That is awesome.
__________________
˙lɐd 'ʎɐʍ ƃuoɹʍ ǝɥʇ ǝɔıʌǝp ʇɐɥʇ ƃuıploɥ ǝɹ,noʎ
Banned is offline   Reply With Quote
Old 04-06-2012, 01:18 PM   #3
dub3000
Human being with feelings
 
dub3000's Avatar
 
Join Date: Mar 2008
Location: Sydney, Australia
Posts: 3,955
Default

OK, this is pretty awesome. Thanks!
dub3000 is offline   Reply With Quote
Old 04-06-2012, 01:33 PM   #4
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,652
Default

Cool!!1!
Tale is offline   Reply With Quote
Old 04-06-2012, 02:31 PM   #5
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,891
Default

w00t!
IXix is offline   Reply With Quote
Old 04-06-2012, 02:56 PM   #6
captain_caveman
Human being with feelings
 
captain_caveman's Avatar
 
Join Date: Dec 2011
Posts: 999
Default

Great stuff!

It doesn't appear to be possible to use gfx_ draw stuff within functions....
Code:
@gfx  400 400

function drawSquare(tlx, tly, size)
(
    x = tlx; y = tly;
    gfx_x = tlx;
    gfx_y = tly;
    gfx_lineto(tlx+size, tly, 1);
    gfx_lineto(tlx+size, tly+size, 1);
    gfx_lineto(tlx, tly+size, 1);
    gfx_lineto(tlx, tly, 1);
    //gfx_rectto(200,200);
    1;
);

function setColours(r,g b) 
(
    gfx_r = r;
    gfx_g = g;
    gfx_g = b;
    a = 5;
    1;
);

setColours(100,255,255);

gfx_a = 255; 
//gfx_r = 255; gfx_g = 255; gfx_b = 255;

setColours(100,255,255);
drawSquare(10,50,100);
gfx_lineto(100,100,1);
SetColours works and x and y are set in drawSquare, but nothing is drawn, although gfx_x and gfx_y change. gfx_lineto draws a line.

It's probably me, although it seems it should work.
captain_caveman 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 03:14 PM.


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