From CSWiki
Jump to: navigation, search

Using a limiter is a useful habit to adopt if you hack audio code with headphones on: instead of routing things straight to the dac, put a Gain and a Dyno (= Dynamic UGen) in the way, and set up the Dyno to act as a limiter. The limiter will (obviously) limit the volume of an incoming signal to a pre-determined maximum threshold, which will save your ears when (not if, but WHEN) you make a mistake and accidentally make a terrible, terrible noise.

SawOsc saw => Gain g => Dyno d => dac;

0.5 => float maxAmp; 
/* 1.0 is maximum output before distortion, 
so choose something less than that */

/* scale everything by that amount, so that what's 1.0 in your
original code is simply scaled to be 1.0 * maxAmp */

d.thresh(maxAmp);  // set the threshold of the limiter
d.slopeBelow(1.0); // sound quieter than thresh is untouched
d.slopeAbove(0.0); // sound louder than thresh is squashed

/* in this for loop, the SawOsc's gain goes from 0 to 10 in steps of 0.1.
This would ordinarily be painfully loud, but the Dyno UGen saves us. 
If you're not impressed, try running this code without the Dyno UGen, but
you've been warned!
for (0 => int i; i < 100; i++) 
	float amp;
	i * 0.1 => amp;
	<<<"setting gain to ", amp, " which, after scaling, is ", amp * maxAmp>>>;
	amp => saw.gain;
	100::ms => now;