Arithmetic, mathematical functions, constants and unit conversions — as objects in the signal graph rather than as operators between them.

1. Operator or UGen?

Most of what is on this page exists twice: as a message you send to a signal, and as a UGen class you instantiate. aSignal log and PhLog2 do comparable jobs by different routes.

FormWhen to use it
Message — aSignal absInline in an expression. Shorter, and reads naturally
UGen — PhSignum newWhen you need an object: to name it, reuse it, or place it in a collection

The operator forms are documented in Connecting Unit Generators §8. This page catalogues the classes.

Everything here is sample-by-sample These are not computed once when the patch is built. Each one is part of the running signal graph and evaluates at audio rate, which is what makes them usable as modulation shapers.

2. Arithmetic operations

2.1 Binary operations

ClassOperationOperator form
PhAdderSum+
PhSubtractorDifference-
PhMultiplicatorProduct*
PhDividerQuotient/
PhModulorModulo%
PhPowerExponentiation

2.2 Routing operations

ClassRole
PhBinaryOperationSuperclass of the two-input operations
PhUnaryOperationSuperclass of the one-input operations
PhSplitOperationFan one signal out to several destinations
PhMergeOperationFold several channels into fewer
PhCrossnnCross-route n channels

3. Unary functions

3.1 General

ClassComputes
PhNegNegation
PhInverseReciprocal
PhSignumSign: −1, 0 or 1
PhFracFractional part
PhSubSubtraction as a unary operation
PhCubeRootCube root
PhNextPowerOf2Next power of two
PhDiffnDifference between successive samples
PhZeroCrossingZero-crossing detection
PhFTZFlush denormals to zero
PhPrimesPrime number generation
PhDeg2Rad, PhRad2DegDegrees and radians
PhZeroCrossing is a pitch detector's front end Counting zero crossings over a window is the cheapest usable estimate of frequency, and PhDiffn next to it gives you slope. Together they are a lightweight analysis pair for driving something from an input signal.

3.2 Exponential and logarithmic

ClassComputes
PhLog2Base-2 logarithm
PhLogBExponent of the floating-point representation
PhILogBThe same, as an integer
PhLog1plog(1 + x) — accurate for small x
PhExpM1exp(x) − 1 — accurate for small x
Why log1p and expm1 exist For very small x, computing log(1 + x) directly loses most of its precision to rounding. These variants keep it. It matters in filter coefficient computation and in anything working near unity gain.

3.3 Special functions

ClassComputes
PhErfError function
PhErfCComplementary error function
PhLGammaLog gamma function
PhJ0, PhJ1Bessel functions of the first kind, orders 0 and 1
PhY0, PhY1Bessel functions of the second kind, orders 0 and 1
PhChebychevChebyshev polynomial
PhChebychevPolyChebyshev polynomial series
Chebyshev polynomials are a waveshaping tool The nth Chebyshev polynomial maps a sine wave to its nth harmonic exactly. Feeding a sine through a weighted sum of them gives precise control over the harmonic spectrum — classic waveshaping synthesis, and far more predictable than an arbitrary saturator.

The Bessel functions appear in FM and in circular-membrane models, where they describe the distribution of sideband energy and the modes of a drum head respectively.

3.4 Tests and predicates

ClassReturns 1 when
PhIsNanThe value is not a number
PhIsInfThe value is infinite
PhNotLogical negation
A use for PhIsNan Feedback networks and unstable filters can produce NaN, at which point the whole patch goes silent and stays silent. Inserting a NaN test lets you detect that in the graph rather than wondering why the sound stopped.

4. Binary mathematical functions

ClassComputes
PhHypothenuse√(x² + y²) without overflow
PhCopySignMagnitude of one operand with the sign of the other
PhModuloMathMathematical modulo
PhLdExpx × 2ⁿ
PhScalbScale by a power of the radix
PhJn, PhYnBessel functions of arbitrary order

5. Trigonometry and antialiasing

Ordinary trigonometric messages — sin, cos, aTan — are available on any signal. The Trigonometry family under the Antialiased tag is different: these are band-limited implementations built for waveshaping.

ClassFunction
AASine, AASine2Antialiased sine
Cosine1, Cosine2Antialiased cosine
TangentAntialiased tangent
ArcSin, ArcSin2Arcsine
ArcCos, ArcCos2Arccosine
ACosh1, ACosh2Inverse hyperbolic cosine
ATanh1, Atanh2Inverse hyperbolic tangent
Why antialiased versions exist Waveshaping generates harmonics above the Nyquist frequency, which fold back down as inharmonic aliasing — the metallic grit that makes naïve digital distortion sound cheap. These implementations use antiderivative antialiasing to suppress it. Use them wherever a function is being applied to a full-scale audio signal rather than to a control value.

The saturators in Effects §3 are the applied form of the same idea.

6. Constants and the environment

ClassValue
PhConstantPIπ
PhConstantEe
PhConstantEpsilonSmallest representable difference
PhConstantMin, PhConstantMaxRepresentable range bounds
PhConstantTThe sample period
PhSampleRateCurrent sample rate, as a signal
PhSampleDurationDuration of one sample
PhBlockSizeCurrent audio block size
Write sample-rate-independent patches A delay of "2000 samples" means different things at 44.1 and 96 kHz. Expressing it through PhSampleRate or PhSec2Samp keeps a patch sounding the same wherever it runs — which matters as soon as you export it. See Exporting.

7. Unit conversions

Sixteen PhConversionTool subclasses translate between the units audio actually uses. Each has an inverse, and they come in pairs.

7.1 Gain

ClassConverts
PhDb2LinearDecibels to a linear multiplier
PhLinear2DbLinear multiplier to decibels
PhLinear2LogGainLinear to logarithmic gain
PhLog2LinearGainLogarithmic to linear gain

uLevel is linear, but loudness perception is closer to logarithmic. Putting a fader through PhDb2Linear is what makes it feel even across its travel — see Unit Generators §2.1.

7.2 Pitch

ClassConverts
PhMidiKey2HzMIDI note number to hertz — key 69 is 440 Hz
PhHz2MidiKeyHertz to MIDI note number
PhPianoKey2HzPiano key number to hertz
PhHz2PianoKeyHertz to piano key number
PhSemi2RatioSemitones to a frequency ratio
PhRatio2SemitoneFrequency ratio to semitones
PhCent2RatioCents to a frequency ratio
PhRatio2CentFrequency ratio to cents

Semitone and cent conversions are the right tools for transposition and detuning, because a musical interval is a ratio, not an offset — detuning by "2 Hz" means something different at every pitch, while detuning by 5 cents does not.

7.3 Time and filter coefficients

ClassConverts
PhSec2SampSeconds to samples
PhSamp2SecSamples to seconds
PhTau2PoleA time constant to a one-pole filter coefficient
PhPole2tauA one-pole coefficient back to a time constant
Tau and pole A one-pole smoother is normally specified by a coefficient between 0 and 1, which is meaningless to think in. PhTau2Pole lets you say "settle in 30 milliseconds" instead. The same reasoning produces the T60 and Tau filter variants in Filters §8.

8. Interpolation and quantisation

ClassRole
PhInterpolateLinearLinear interpolation between two values
PhInterpolateRaiseModuloInterpolation with modulo wrapping — for phase and index values
PhInterpolateGeneral interpolation
PhQuantizeQuantise a signal to discrete steps

Interpolation controls come from the PhInterpSetter trait — v0:, v1:, dv: and interpControl:. See Setter Traits §7.

Quantise pitch, not frequency PhQuantize on a frequency signal gives arbitrary steps; on a MIDI note number it gives semitones. Convert to note numbers, quantise, convert back — which is how a random-walk melody is kept in a scale.

9. Where to go next

DocumentWhat it covers
Connecting UGensThe operator forms of everything here.
FiltersWhere time constants and coefficients are used.
EffectsWaveshaping as an applied use of these functions.
UGen LibraryThe complete class list.

10. Troubleshooting

My patch went silent and will not come back

Something produced NaN or an infinity — usually a division by zero or a runaway feedback loop. Rebuild the DSP, and consider a PhIsNan test while debugging — §3.4.

Waveshaping sounds gritty and metallic

Aliasing. Use the antialiased functions — §5.

My fader feels uneven — all the action is at the bottom

It is linear. Put it through PhDb2Linear — §7.1.

A detuned pair sounds right low down and wrong high up

You are detuning by a frequency offset rather than a ratio. Use cents — §7.2.

The patch sounds different at a different sample rate

A time is specified in samples. Express it in seconds — §6.

Quantising the frequency gives out-of-tune notes

Quantise MIDI note numbers instead, then convert — §8.