Simple fm synthesis question

One suggestion here:

	var detuneFactor = freq * detuneCurve.(detune);
	var freqs = [
		(freq - (detuneFactor * 0.11002313)),
		(freq - (detuneFactor * 0.06288439)),
		(freq - (detuneFactor * 0.01952356)),
		(freq + (detuneFactor * 0.01991221)),
		(freq + (detuneFactor * 0.06216538)),
		(freq + (detuneFactor * 0.10745242))
	];

detuneCurve produces 43 UGens.

var detuneCurve = { |x|
	(10028.7312891634 * x.pow(11)) -
	(50818.8652045924 * x.pow(10)) +
	(111363.4808729368 * x.pow(9)) -
	(138150.6761080548 * x.pow(8)) +
	(106649.6679158292 * x.pow(7)) -
	(53046.9642751875 * x.pow(6)) +
	(17019.9518580080 * x.pow(5)) -
	(3425.0836591318 * x.pow(4)) +
	(404.2703938388 * x.pow(3)) -
	(24.1878824391 * x.pow(2)) +
	(0.6717417634 * x) +
	0.0030115596
};

a = detuneCurve.(DC.kr(1));

(
c = 0;

f = { |ugen|
	c = c + 1;  // for this UGen
	ugen.inputs.do { |input|
		if(input.isUGen) { f.(input) };
	};
	c
};

f.(a);
)

-> 43

When you call it six times, you get 43*6 = 258 UGens.

When you call it six times with the same input value, you still get 258 UGens except 215 of them are simply duplicating the work done by the first 43. Math ops are relatively fast but caching the one detuneValue result will cut out 215 UGens and that can’t be a bad thing. (Edit: Actually a few more than 215 because I re-edited my post to eliminate the redundant freq * as well.)

SC’s SynthDef builder is not smart enough to detect that these subchains are identical. It’s up to you to cache calculations that you will use repeatedly.

hjh