builtins

Math Functions

Mathematical operations for signal processing and control logic.

Mathematical operations for signal processing and control logic.

Note: sin(x), cos(x), tan(x), and tanh(x) are pure math functions operating on values in radians. For audio oscillators, use osc("sin", freq) or osc("saw", freq) etc.

Arithmetic

add

Add - Adds two signals.

ParamTypeDefaultDescription
asignal-First operand
bsignal-Second operand

Equivalent to the + operator.

// Mixing two oscillators
add(osc("sin", 220), osc("sin", 330)) * 0.5 |> out(%, %)

sub

Subtract - Subtracts second signal from first.

ParamTypeDefaultDescription
asignal-First operand
bsignal-Second operand

Equivalent to the - operator.

// Difference of oscillators
sub(osc("sin", 220), osc("sin", 221)) |> out(%, %)

mul

Multiply - Multiplies two signals.

ParamTypeDefaultDescription
asignal-First operand
bsignal-Second operand

Equivalent to the * operator. Commonly used for amplitude modulation.

// Ring modulation
mul(osc("sin", 220), osc("sin", 30)) |> out(%, %)

div

Divide - Divides first signal by second.

ParamTypeDefaultDescription
asignal-Dividend
bsignal-Divisor

Equivalent to the / operator.


pow

Power - Raises base to exponent.

ParamTypeDefaultDescription
basesignal-Base value
expsignal-Exponent

Equivalent to the ^ operator.

// Exponential curve
pow(lfo(0.5), 2) |> out(%, %)

Unary Math

neg

Negate - Inverts the sign of a signal.

ParamTypeDefaultDescription
xsignal-Input
// Invert phase
neg(osc("sin", 220)) |> out(%, %)

abs

Absolute Value - Returns the absolute value.

ParamTypeDefaultDescription
xsignal-Input

Useful for full-wave rectification or envelope following.

// Full-wave rectification
abs(osc("sin", 110)) |> lp(%, 50) |> out(%, %)

sqrt

Square Root - Returns the square root.

ParamTypeDefaultDescription
xsignal-Input (should be >= 0)

log

Natural Logarithm - Returns ln(x).

ParamTypeDefaultDescription
xsignal-Input (should be > 0)

exp

Exponential - Returns e^x.

ParamTypeDefaultDescription
xsignal-Input

Useful for exponential envelopes and frequency scaling.


floor

Floor - Rounds down to nearest integer.

ParamTypeDefaultDescription
xsignal-Input
// Quantize an LFO
floor(lfo(0.5) * 8) / 8 |> out(%, %)

ceil

Ceiling - Rounds up to nearest integer.

ParamTypeDefaultDescription
xsignal-Input

Binary Math

min

Minimum - Returns the smaller of two values.

ParamTypeDefaultDescription
asignal-First value
bsignal-Second value
// Limit signal to 0.5
min(osc("sin", 220), 0.5) |> out(%, %)

max

Maximum - Returns the larger of two values.

ParamTypeDefaultDescription
asignal-First value
bsignal-Second value
// Ensure signal doesn't go below 0
max(osc("sin", 220), 0) |> out(%, %)

Ternary Math

clamp

Clamp - Constrains value between lo and hi.

ParamTypeDefaultDescription
xsignal-Input value
losignal-Lower bound
hisignal-Upper bound
// Keep signal in -0.5 to 0.5 range
clamp(osc("saw", 110), -0.5, 0.5) |> out(%, %)

wrap

Wrap - Wraps value around range boundaries.

ParamTypeDefaultDescription
xsignal-Input value
losignal-Lower bound
hisignal-Upper bound

When the value exceeds hi, it wraps to lo (and vice versa). Useful for creating sawtooth-like modulation.

// Wrapped phasor
wrap(osc("phasor", 1) * 3, 0, 1) |> out(%, %)

Trigonometric Functions

These functions operate on values in radians. They are pure math functions, not audio oscillators.

sin

Sine - Returns the sine of an angle in radians.

ParamTypeDefaultDescription
xsignal-Angle in radians

Important: This is a pure math function, not an oscillator. For a sine oscillator, use osc("sin", freq).

// Create a sine wave manually from a phasor
sin(osc("phasor", 440) * 2 * 3.14159) |> out(%, %)
// Waveshaping with sine
osc("saw", 110) |> sin(% * 3.14159) |> out(%, %)

cos

Cosine - Returns the cosine of an angle in radians.

ParamTypeDefaultDescription
xsignal-Angle in radians
// Cosine is sine shifted by 90 degrees
cos(osc("phasor", 440) * 2 * 3.14159) |> out(%, %)

tan

Tangent - Returns the tangent of an angle in radians.

ParamTypeDefaultDescription
xsignal-Angle in radians
// Tangent waveshaping (careful - goes to infinity!)
osc("sin", 110) * 0.3 |> tan(%) |> clamp(%, -1, 1) |> out(%, %)

asin

Arcsine - Returns the inverse sine (result in radians).

ParamTypeDefaultDescription
xsignal-Value between -1 and 1

acos

Arccosine - Returns the inverse cosine (result in radians).

ParamTypeDefaultDescription
xsignal-Value between -1 and 1

atan

Arctangent - Returns the inverse tangent (result in radians).

ParamTypeDefaultDescription
xsignal-Input value

Useful for soft saturation effects.

// Soft saturation using atan
osc("saw", 110) * 3 |> atan(%) / 1.57 |> out(%, %)

atan2

Two-argument Arctangent - Returns the angle between the positive x-axis and the point (x, y).

ParamTypeDefaultDescription
ysignal-Y coordinate
xsignal-X coordinate

Useful for phase calculations and coordinate conversions.


Hyperbolic Functions

sinh

Hyperbolic Sine - Returns the hyperbolic sine.

ParamTypeDefaultDescription
xsignal-Input value

cosh

Hyperbolic Cosine - Returns the hyperbolic cosine.

ParamTypeDefaultDescription
xsignal-Input value

tanh

Hyperbolic Tangent - Returns the hyperbolic tangent.

ParamTypeDefaultDescription
xsignal-Input value

Important: This is a pure math function. For tanh-based distortion with drive control, use saturate(in, drive).

The tanh function outputs values between -1 and 1, making it useful for custom waveshaping when combined with multiplication.

// Manual saturation using tanh
osc("saw", 110) * 3 |> tanh(%) |> out(%, %)
// Adjustable saturation (multiply input for more drive)
osc("saw", 110) |> tanh(% * 5) |> out(%, %)

Related: saturate