Quote:
Originally Posted by Amether
I'm starting to write more JSFX and I'm wondering (as I'm not really a programmer) what sort of things I should be thinking about when coding? What should I avoid putting in the @sample portion if I can help it?
I'm thinking about what locks up CPU the most, that sort of thing... is it, for instance ok to put an if statement within the @sample portion? Or is it a trivial amount of cycles? I have no idea about this sort of thing.
|
As I'm not really a programmer either, I'm just telling you my take on this:
Try to avoid putting things in the @sample code that aren't needed to be evaluated on _every_ sample, e.g. user settings that need to be converted from dB to amplitude value, precalculate this in the @slider code to make shure it is only calculated when the user changes the sliders.
Evenmore you can precalculate constants in the @init code (only loaded at plug-in start), e.g. if you will need the sqrt(2) alot in your code, put it the @init, you can even put it like this 'sqrtof2 = 1.414214' and then every time you would use 'sqrt(2)' in your code just use 'sqrtof2'.
For sure you can put '?' operators in the @sample code they don't take much CPU, it's real just the calculation that takes up CPU. You can try finding quicker ways to calculate things, e.g. use 'amp = exp( dB * log(2)/6 )' (can percalculate the log(2)/6) instead of 'amp = 2 ^ (db/6)' which takes more CPU (IMO anyway), or use the 20*log10(dB), do what ever you like, but try to keep the calculations down to a minimum.
Always try to find more efficent ways to achieve the desired FX, e.g. switching a variable form 0 to 1 to 0 to 1, you could always ask 'foo==0 ? foo=1 : foo=0;' or you just simply do 'foo=1-foo;' (dunno if it's more efficent though) and the list goes on and on, so be creative
But in general just try if you can use particular operators/calculations in the @sample code if you can't you'll notice it (it crackles and stutters
), if you want to optimize your code drop useless calculations and ifs and stuff and use a CPU monitor to check the CPU use and see if you can get it down.