Skip to content

Commit

Permalink
Merge pull request #3429 from dyfer/topic/add-pseudo-ugen-implementat…
Browse files Browse the repository at this point in the history
…ion-of-snap-and-softRound

Add pseudo-ugen implementation of snap and softRound
  • Loading branch information
mossheim authored Apr 19, 2018
2 parents 87d879c + 742e6f4 commit 0e5c36b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion HelpSource/Classes/UGen.schelp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ method:: minNyquist

Wraps the receiver in a code::min:: link::Classes/BinaryOpUGen::, such that the lesser of the receiver's output and the Nyquist frequency is output. This can be useful to prevent aliasing.

method:: snap

Wraps the receiver so that its values are rounded within code::margin:: distance from a multiple of code::resolution:: to a multiple of resolution. By using code::margin:: and code::strength:: you can control when values will be rounded, and by how much. See link::Classes/SimpleNumber#-snap:: for more details.

This can be used to make control signals (e.g. from sensors) "snap" to defined resolution. Example:
code::
s.boot;
x = {SinOsc.ar((Line.kr(0, 10, 10).snap(1, 0.3, 1) + 60).poll.midicps, 0, -24.dbamp)}.play
::

method:: softRound

Wraps the receiver so that its values are rounded outside of code::margin:: distance from a multiple of code::resolution:: to a multiple of resolution. By using code::margin:: and code::strength:: you can control when values will be rounded, and by how much. See link::Classes/SimpleNumber#-softRound:: for more details.

method:: linlin
Wraps the receiver so that a linear input range is mapped to a linear output range.

Expand Down Expand Up @@ -403,4 +417,3 @@ method:: init
By default, this method stores the inputs (e.g. the arguments to code::*ar:: and code::*kr::) in the UGen.
discussion::
This may be overridden to do other initialisations, as long as the inputs are set correctly.

13 changes: 13 additions & 0 deletions SCClassLibrary/Common/Audio/UGen.sc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ UGen : AbstractFunction {
);
^this
}

snap { arg resolution = 1.0, margin = 0.05, strength = 1.0;
var round = round(this, resolution);
var diff = round - this;
^Select.multiNew(this.rate, abs(diff) < margin, this, this + (strength * diff));
}

softRound { arg resolution = 1.0, margin = 0.05, strength = 1.0;
var round = round(this, resolution);
var diff = round - this;
^Select.multiNew(this.rate, abs(diff) > margin, this, this + (strength * diff));
}

linlin { arg inMin, inMax, outMin, outMax, clip = \minmax;
if (this.rate == \audio) {
^LinLin.ar(this.prune(inMin, inMax, clip), inMin, inMax, outMin, outMax)
Expand Down

0 comments on commit 0e5c36b

Please sign in to comment.