Client-Side Lag?

Hi there -

I tend to use the Lag UGen to smooth out my numbers. I’m curious if there is a way to do this on the client-side of SC, for nefarious reasons.

Thank for the pointers.

You could use a Routine - for example:

// start routine running
(
d = ();
d.currentVal = 0;
d.newVal = 0;
d.tickTime = 0.05;
d.changePerTick = 0.1;  // 10 percent
d.tolerance = 0.01;
d.updateRoutine = fork {
	loop {
		if (d.currentVal != d.newVal, {
			if ((d.currentVal - d.newVal).abs > d.tolerance, {
				d.currentVal = d.currentVal.blend(d.newVal, d.changePerTick);
			}, {
				d.currentVal = d.newVal;
			});
			("Current Value:"  + d.currentVal).postln;
		});
		d.tickTime.wait;
	}
};

)

// change values
d.newVal = 50;
d.newVal = -30;
d.newVal = 0;

// cleanup - stop routine
d.updateRoutine.stop;
d = ();

Best,
Paul

1 Like

Rethinking this, here’s a better example where the routine only runs when needed:

// setup
(
d = ();
d.currentVal = 0;
d.newVal = 0;
d.tickTime = 0.05;
d.changePerTick = 0.1;  // 10 percent
d.tolerance = 0.01;
d.setValue = {arg val;
	if (d.updateRoutine.notNil, {
		d.updateRoutine.stop;
	});
	d.newVal = val;
	d.updateRoutine = fork {
			loop {
				if (d.currentVal != d.newVal, {
					if ((d.currentVal - d.newVal).abs > d.tolerance, {
						d.currentVal = d.currentVal.blend(d.newVal, d.changePerTick);
					}, {
						d.currentVal = d.newVal;
					});
					("Current Value:"  + d.currentVal).postln;
				}, {
					d.updateRoutine.stop;
				});
				d.tickTime.wait;
			}
		};			
};
)

// change values
d[\setValue].value(50);
d[\setValue].value(-30);
d[\setValue].value(0);

Best,
Paul

1 Like

Final update (I promise!) in case it’s useful, here’s a slightly cleaner version that uses the current routine if it exists:

// setup
(
d = ();
d.currentVal = 0;
d.newVal = 0;
d.tickTime = 0.05;
d.changePerTick = 0.1;  // 10 percent
d.tolerance = 0.01;
d.setValue = {arg val;
	d.newVal = val;
	if (d.updateRoutine.isNil, {
		d.updateRoutine = fork {
			loop {
				if (d.currentVal != d.newVal, {
					if ((d.currentVal - d.newVal).abs > d.tolerance, {
						d.currentVal = d.currentVal.blend(d.newVal, d.changePerTick);
					}, {
						d.currentVal = d.newVal;
					});
					("Current Value:"  + d.currentVal).postln;
				}, {
					{d.updateRoutine = nil;}.defer(0.01); // delay until after stop
					d.updateRoutine.stop;
				});
				d.tickTime.wait;
			}
		};
	});
};
)

// change values
d[\setValue].value(50);
d[\setValue].value(-30);
d[\setValue].value(0);
1 Like