Simplify/make CPU efficient

hi list

I would like to simplify a block of code. At the moment evaluating it sends my CPU up to 40-50%. What I am trying to achieve is to have a set of Ndefs with selectable oscillators for modulation. The code example uses wavefold extension from Daniel Meyer’s MiSCellaneous.

(
~modulators = 3.collect{|k| 
	4.collect{|i|
		Ndef(("mod_"++k++i).asSymbol, {
					arg modSel = 0, modFrequency = 1, modIndex = 1, wfLo = 0, wfHi = 0;

					var mod = Select.ar(modSel,
						[
							SmoothFoldS2.ar(
								in: modIndex * SinOsc.ar(modFrequency, mul: 1),
								lo: -1,
								hi: 1,
								foldRangeLo: wfLo,
								foldRangeHi: wfHi
							),
							SmoothFoldS2.ar(
								in: modIndex *  LFSaw.ar(modFrequency, mul: 1),
								lo: -1,
								hi: 1,
								foldRangeLo: wfLo,
								foldRangeHi: wfHi
							),
							SmoothFoldS2.ar(
								in: modIndex *  LatoocarfianC.ar(
									freq: modFrequency,
									a: LFNoise2.kr(2,1.5,1.5),
									b: LFNoise2.kr(2,1.5,1.5),
									c: LFNoise2.kr(2,0.5,1.5),
									d: LFNoise2.kr(2,0.5,1.5),
									mul: 1, add: 0),
								lo: -1,
								hi: 1,
								foldRangeLo: wfLo,
								foldRangeHi: wfHi
							),
							SmoothFoldS2.ar(
								in: modIndex * HenonC.ar(
								freq: modFrequency,
								a: LFNoise2.kr(1, 0.2, 1.2),
								b: LFNoise2.kr(1, 0.15, 0.15),
								mul: 1, add: 0),
								lo: -1,
								hi: 1,
								foldRangeLo: wfLo,
								foldRangeHi: wfHi
							),
							SmoothFoldS2.ar(
								in: modIndex *  Gendy3.ar(
									ampdist: 6,
									durdist: 3,
									adparam: 0.9,
									ddparam: 0.9,
									freq: modFrequency,
									ampscale: 1),
								lo: -1,
								hi: 1,
								foldRangeLo: wfLo,
								foldRangeHi: wfHi
							)
					]);
					mod
				})
		}}
)

perhaps there is a way to define Ndef without activating it?

thanks for your help

If you’re in Mac, you can grab a copy of Xcode and use Instruments with the “Time Profiler” template, and point it to scsynth to get a fairly readable report of where your CPU time is going.

One thing to be aware of: when you have a Select like what you’re using, you are running ALL of the Synths in that Array, but only hearing the one you have selected. So, your overall cost here is 3*4*5=60 modulators. You would be much better off simply assigning one modulator to each Ndef, and swapping them out. Ndefs can cross-fade when you assign them a new Synth, so this actually works quite well as a modulator:

~sinMod = {
	arg modSel = 0, modFrequency = 1, modIndex = 1, wfLo = 0, wfHi = 0;
	
	SmoothFoldS2.ar(
		in: modIndex * SinOsc.ar(modFrequency, mul: 1),
		lo: -1,
		hi: 1,
		foldRangeLo: wfLo,
		foldRangeHi: wfHi
	)
};

Ndef(\mod_12).fadeTime = 0.2;
Ndef(\mod_12, ~sinMod);

This should probably decrease your CPU usage by a factor of 5, because you’re only running one modulator per Ndef, vs 5.

And, re your original question, you can also do this by running MANY Ndefs and then pausing them when not in use, via Ndef(\mod_34).pause - in this state, they shouldn’t be using CPU.

thanks Scott, that should do the trick.

m