View Single Post
Old 02-15-2015, 12:23 PM   #64
Argitoth
Human being with feelings
 
Argitoth's Avatar
 
Join Date: Feb 2008
Location: Phoenix, AZ
Posts: 2,055
Default

trick for functions with optional parameters by overloading functions, where the overloaded functions use previously declared functions.

Code:
function setcolor(r, g, b) (
    gfx_r = r;
    gfx_g = g;
    gfx_b = b;
);
function setalpha(a) (
    gfx_a = a
);
function setcolor(g) (
    setcolor(g,g,g)
);
function setcolor(g,a) ( //g as in grey
    setcolor(g,g,g);
    setalpha(a);
);
function setcolor(r, g, b, a) (
    setcolor(r,g,b);
    setalpha(a);
);
for easy swapping of variables
Code:
function swap(a*,b*) local(t) (a = t; a = b; b = t);
usage:
a=1;
b=2;
swap(a,b);
now a=2 and b=1


bit twiddling hack to get the power of 2 of an integer, useful for checking trigger buttons

Code:
function bitPow2(a) local(b) (
    b=-1;
    while(a) (
      a = a >> 1;
      b+=1;
    );
    b;
);
2^5 is 32
trigger button #5 is 32
bitPow2(32) returns 5.

this is how to make any Log function, here's how to make Log2.
Code:
@init
log2 = log(2); //establish your log constant

function log2(f) ( log(f)/log2 ); //create your function
__________________
Soundemote - Home of the chaosfly and pretty oscilloscope.
MyReaperPlugin - Easy-to-use cross-platform C++ REAPER extension template

Last edited by Argitoth; 02-18-2015 at 03:05 PM.
Argitoth is offline   Reply With Quote