Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pseudo-ugen implementation of snap and softRound #3429

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion HelpSource/Classes/UGen.schelp
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
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