class:: UGen
summary:: Abstract superclass of all unit generators
categories:: UGens, Server>Abstractions
related:: Browse#UGens, Guides/Tour_of_UGens, Guides/UGens-and-Synths

description::

UGens represent calculations with signals. They are the basic building blocks of synth definitions on the server, and are used to generate or process both audio and control signals. The many subclasses of UGen are the client-side representations of unit generators, and are used to specify their parameters when constructing synth definitions (see link::Classes/SynthDef::).

subsection:: Interface
All UGens respond to one or more of the following class methods:
list::
## code:: ar(arg1, arg2, ... ) ::
## code:: kr(arg1, arg2, ... ) ::
## code:: ir(arg1, arg2, ... ) ::
::

They return a new instance of UGen that calculates at audio/control rate or at initialization only (ir). Some UGens, like link::Classes/Rand::, use the code::*new:: method instead. These methods are implemented in subclasses, where argument names and their meaning depend on the case.

If any argument is an array, they return an array of UGens ( see: link::Guides/Multichannel-Expansion:: ). If the combination of rates between arguments and ugen are not allowed, calling the methods will throw an error. This method adds the UGen to the current SynthDef, so it only fully works inside a UGen function.
code::
{ Blip.ar(Blip.kr(4, 5, 500, 60), 59, 0.1) }.play;
::

subsection:: Documentation of mul and add arguments

A great number of UGens take arguments for code::mul:: and code::add:: in their code::*ar:: and code::*kr:: methods. Because these arguments are so ubiquitous, they are not general documented in the individual help files.
Mul and add simply refer to a constant or signal by which to multiply the output of the UGen, and a constant or signal to add to the output of the UGen. (mul happens before add.) They thus correspond in many cases to scaling the amplitude of the UGen signal in the case of mul, and adding a constant or DC offset in the case of add.
In most cases the defaults for mul and add are 1 and 0 respectively, and they are commonly implemented using a automatically generated link::Classes/MulAdd:: UGen for efficiency. See also the code::range:: and code::madd:: methods below.

classmethods::
private:: categories

method:: buildSynthDef
Returns:: the SynthDef in which the UGen is situated.
Discussion::
code::
{ UGen.buildSynthDef.dump; Silent.ar }.play;
::

subsection:: Internally used class methods

method:: multiNew
These methods are responsible for multichannel expansion. They call code::*new1(rate, ...args):: for each parallel combination. Most code::*ar/*kr:: methods delegate to link::#*multiNewList::.
argument:: ... args
The first argument is rate, then the rest of the arguments. code::(rate, ...args)::

method:: multiNewList
See link::#*multiNew::.
argument:: args
An array where the first argument is rate, then the rest of the arguments. code::([rate, ...args])::

method:: new1
This method returns a single instance of the UGen, not multichannel expanded. It is called inside multiNewList, whenever a new single instance is needed.

method:: methodSelectorForRate
argument:: rate
A link::Classes/Symbol::, code:: \audio, \control, \scalar ::
Returns::
An appropriate message selector ( link::Classes/Symbol:: like code:: \ar, \kr, \ir :: ) for the given rate.

method:: replaceZeroesWithSilence
Returns::
A new link::Classes/Array::, where every zero is replaced by a link::Classes/Silent:: UGen.
discussion::
This is required internally sometimes for UGens like link::Classes/Out::.

instancemethods::

subsection:: Convenience Methods

method:: scope

Displays the output of this UGen in an individual link::Classes/Stethoscope:: window.
argument::name
The name of the window
argument::bufsize
Buffer size
argument::zoom
Zoom factor

discussion::
code::
s.boot

{ Ringz.ar(PinkNoise.ar([0.1, 0.2]).scope(\pink), 2000, 1, 0.25) }.play; // multichannel works
s.scope; // can still separately scope the output of the server
::

method:: poll

Polls the output of this UGen every interval seconds, and posts the result.

argument::trig
Trig frequency
argument::label
A symbol to label the output
argument::trigid
Numerical ID
discussion::
The default trig is 10, which converts to 10 triggers per second (or every 0.1 seconds). See link::Classes/Poll:: for more info on polling.
code::
{ SinOsc.ar(LFNoise0.ar(2).range(420, 460).poll(label: \LFNoise), 0, 0.2) }.play;

// multichannel polling:
(
{
    var freqs = SinOsc.ar([0.2, 0.3]).range(420, 460);
    freqs.poll(label: [\freq1, \freq2]);
    SinOsc.ar(freqs, 0, 0.2);
}.play;
)
::


method:: dpoll

Like code::poll::, only that code::dpoll:: is used for Demand ugens. See link::Classes/Poll:: for more info on polling.

method:: range

Scales the output of this UGen to be within the range of code::lo:: and code::hi::.
discussion::
Note that code::range:: expects the default output range, and thus should not be used in conjunction with mul and add arguments.
code::
{ SinOsc.ar(SinOsc.ar(0.3).range(440, 660), 0, 0.5) * 0.1 }.play;
::

method:: exprange

Maps the output of this UGen exponentially to be within the range of code::lo:: and code::hi:: using a link::Classes/LinExp:: UGen.
discussion::
code::lo:: and code::hi:: should both be non-zero and have the same sign. Note that code::exprange:: expects the default output range, and thus should not be used in conjunction with mul and add arguments.
code::
{ SinOsc.ar(SinOsc.ar(0.3).exprange(440, 6600), 0, 0.5) * 0.1 }.play;
::

method:: curverange

Scales the output of this UGen to be within the range of code::lo:: and code::hi:: using a curve factor of code::curve::.
discussion::
Note that code::curverange:: expects the default output range, and thus should not be used in conjunction with mul and add arguments.
code::
{ SinOsc.ar(SinOsc.ar(0.3).curverange(440, 660, -3), 0, 0.5) * 0.1 }.play;
::



method:: unipolar

Scales the output of this UGen to be between code::(0..mul):: range (default 1).
discussion::
Note that code::unipolar:: expects the default output range, and thus should not be used in conjunction with mul and add arguments.
code::
{ SinOsc.ar(300, 0, 0.5) * SinOsc.kr(2).unipolar * 0.1 }.play;
::

method:: bipolar

Scales the output of this UGen to be between code::(-mul..mul):: range (default 1).
discussion::
Note that code::bipolar:: expects the default output range, and thus should not be used in conjunction with mul and add arguments.
code::
{ SinOsc.ar(500 + LFPulse.ar(4).bipolar(40), 0, 0.5) * 0.1 }.play;
::

method:: degrad
Converts degrees to radians. This method multiplies the receiver's output by code::pi/180::.

method:: raddeg
Converts radians to degrees. This method multiplies the receiver's output by code::180/pi::.

method:: clip
Wraps the receiver in a link::Classes/Clip:: UGen, clipping its output at code::lo:: and code::hi::.

method:: fold
Wraps the receiver in a link::Classes/Fold:: UGen, folding its output at code::lo:: and code::hi::.

method:: wrap
Wraps the receiver in a link::Classes/Wrap:: UGen, wrapping its output at code::lo:: and code::hi::.

method:: blend
Blends code::this:: with code::that:: by wrapping the receiver in an link::Classes/XFade2:: (if code::this:: or code::that:: are audio-rate UGens) or link::Classes/LinXFade2:: UGen.
note:: The code::blendFrac:: argument is between 0 and 1::


method:: lag
Wraps the receiver in a link::Classes/Lag:: UGen, smoothing its output by code::t1:: seconds lagtime. If a second argument is given, it wraps it in a link::Classes/LagUD:: UGen.

method:: lag2
Wraps the receiver in a link::Classes/Lag2:: UGen, smoothing its output by code::t1:: seconds lagtime. If a second argument is given, it wraps it in a link::Classes/Lag2UD:: UGen.

method:: lag3
Wraps the receiver in a link::Classes/Lag3:: UGen, smoothing its output by code::t1:: seconds lagtime. If a second argument is given, it wraps it in a link::Classes/Lag3UD:: UGen.

method:: lagud
Wraps the receiver in a link::Classes/LagUD:: UGen, smoothing its output by code::lagtimeU:: and code::lagtimeD::.

method:: lag2ud
Wraps the receiver in a link::Classes/Lag2UD:: UGen, smoothing its output by code::lagtimeU:: and code::lagtimeD::.

method:: lag3ud
Wraps the receiver in a link::Classes/Lag3UD:: UGen, smoothing its output by code::lagtimeU:: and code::lagtimeD::.

method:: varlag
Wraps the receiver in a link::Classes/VarLag:: UGen, smoothing its output by code::time:: seconds.

method:: slew
Wraps the receiver in a link::Classes/Slew:: UGen, limiting the slope of its output.

method:: degreeToKey

Wraps the receiver in a link::Classes/DegreeToKey:: UGen.

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.

discussion::
The clip argument can be one of the four:
table::
## code::nil:: || do not clip at outMin or outMax
## code::\minmax:: || clip at outMin or outMax
## code::\min:: || clip at outMin
## code::\max:: || clip at outMax
::
Example:
code::
{ Line.ar(-1, 5, 0.1).linlin(0, 3, -1, 1) }.plot(0.1);

// modulate some values
(
{ Line.ar(-1, 5, 0.1).lincurve(SinOsc.ar(100), SinOsc.ar(130) + 3, -1, 1, clip: nil) }
    .plot(0.1, minval: -15, maxval: 5)
)
::

method:: linexp

Wraps the receiver so that a linear inputrange is mapped to an exponential output range.
discussion::
outMin and outMax must be nonzero and of the same sign. For clip argument, see code::linlin:: above.
code::
{ Line.ar(-1, 5, 0.1).linexp(0, 3, 0.01, 1) }.plot(0.1);
::

method:: explin

Wraps the receiver so that an exponential inputrange is mapped to a linear output range.
discussion::
inMin and inMax must be nonzero and of the same sign. For clip argument, see code::linlin:: above.
code::
{ Line.ar(1, 5, 0.1).explin(1, 3, -1, 1) }.plot(0.1);
::

method:: expexp

Wraps the receiver so that an exponential inputrange is mapped to an exponential output range.
discussion::
outMin, outMax, inMin and inMax must be nonzero and of the same sign. For clip argument, see code::linlin:: above.
code::
{ Line.ar(1, 5, 0.1).expexp(1, 3, 0.01, 1) }.plot(0.1);
::

method:: lincurve

Wraps the receiver so that a linear inputrange is mapped to a curve-like exponential output range.
discussion::
outMin and outMax may be zero and of the different sign. For clip argument, see code::linlin:: above.
code::
{ Line.ar(-1, 5, 0.1).lincurve(0, 3, -1, 1, curve: -4) }.plot(0.1);

// modulate the curve. Unlike with numbers and CurveSpec, the curve absolute value
// should not be much smaller than 0.5.
{ SinOsc.ar(100).lincurve(-1, 1, -1, 1, XLine.kr(-3, -100, 0.1)) * 0.1 }.plot(0.1);
::

method:: curvelin
Wraps the receiver so that a  curve-like exponential inputrange is mapped to a linear output range.
discussion::
inMin and inMax may be zero and of the different sign. For clip argument, see code::linlin:: above.
code::
{ Line.ar(-1, 5, 0.1).curvelin(0, 3, -1, 1, curve: -4) }.plot(0.1);
::


method:: bilin
Map the receiver from two assumed linear input ranges (inMin..inCenter) and (inCenter..inMax) to two linear output ranges (outMin..outCenter) and (outCenter..outMax). If the input exceeds the input range, the following behaviours are specified by the clip argument.
argument::inCenter
argument::inMin
assumed input minimum
argument::inMax
assumed input maximum
argument::outCenter
argument::outMin
output minimum
argument::outMax
output maximum
argument::clip
nil (don't clip)
\max (clip ceiling)
\min (clip floor)
\minmax (clip both - this is default).
discussion::
code::
{Line.kr(0, 10, 0.1).bilin(1, 0, 10, 0.6, 0, 1)}.plot(0.1)
::


method:: prune
Limits the receiver range to one of the four clip modes, see code::linlin:: above.

method:: checkBadValues
Wraps the receiver in a link::Classes/CheckBadValues:: UGen with the corresponding code::id:: and code::post:: flag.

method:: if
Outputs trueUGen when the receiver outputs 1, falseUGen when the receiver outputs 0. If the receiver outputs a value between 0 and 1, a mixture of both will be played.
discussion::
This is implemented as: code::(this * (trueUGen - falseUGen)) + falseUGen)::

Note that both trueUGen and falseUGen will be calculated regardless of whether they are output, so this may not be the most efficient approach.
code::
// note different syntax in these two examples
{ if( LFNoise1.kr(1.0, 0.5, 0.5) , SinOsc.ar, Saw.ar ) * 0.1 }.play;

{ Trig1.ar(Dust.ar(3), 0.2).lag(0.1).if(FSinOsc.ar(440), FSinOsc.ar(880)) * 0.1 }.play;
::

method:: @
Dynamic geometry support. Returns code::Point(this, y)::.
discussion::
code::
{ (SinOsc.ar(1001) @ SinOsc.ar(1207)).rho }.scope;
::

method:: asComplex
Complex math support. Returns code::Complex(this, 0.0)::.

method:: dumpArgs
Posts a list of the arguments for this UGen and their values.


subsection:: Other Instance Methods

The following methods and instance variables are largely used in the construction of synth definitions, synth descriptions (see link::Classes/SynthDesc::), UGen class definitions, etc., and are usually not needed for general use.
Users should not attempt to set any of these values in general code.

method:: synthDef
The SynthDef which contains the UGen.

method:: inputs
The array of inputs to the UGen.

method:: rate
The output rate of the UGen which is one of the link::Classes/Symbol::s code::\audio::, or code::\control::.

method:: signalRange
Returns:: A symbol indicating the signal range of the receiver. Either code::\bipolar:: or code::\unipolar::.

method:: numChannels
Returns:: The number of output channels.
discussion::
For a UGen, this will always be 1, but link::Classes/Array:: also implements this method, so multichannel expansion is supported. See link::Guides/Multichannel-Expansion::.

method:: numInputs
Returns:: The number of inputs for this UGen.

method:: numOutputs
Returns:: The number of outputs for this UGen.

method:: name
Returns:: The Class name of the receiver as a link::Classes/String::.

method:: madd
Wraps the receiver in a link::Classes/MulAdd:: UGen.
discussion::
This is for the most part only used in UGen class definitions in order to allow efficient implementation of code::mul:: and code::add:: arguments.

method:: isValidUGenInput
Returns:: true

method:: asUGenInput
Returns:: the receiver
discussion::
This method is implemented in a number of classes in order to allow objects like link::Classes/Node::s, link::Classes/Bus::ses, and link::Classes/Buffer::s to be passed directly as UGen inputs and link::Classes/Synth:: args.

method:: copy
Returns:: the receiver.
discussion::
Thus UGen-dup effectively returns a reference to the original and is a convenient way to copy a mono signal to multiple channels.
code::
{ SinOsc.ar(Rand(200, 4000), 0, 0.2).dup }.plot // this is the same UGen
::
Function-dup evaluates that function multiple times, thus potentially returning distinct UGens.
code::
{ { SinOsc.ar(Rand(200, 4000), 0, 0.2) }.dup }.plot // these are different UGens
::


subsection:: Internally used instance methods

method:: methodSelectorForRate
See link::#*methodSelectorForRate::

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.
