Math

Arithmetic on any signal, lane by lane: add, multiply, divide, power and more on audio or control operands. The output runs at the rate of its fastest input.

  • Utility
  • Since 0.2.0
  • math
  • add
  • multiply
  • divide
  • gain
  • arithmetic
  • utility

Open in TuneToys

Math as the Patch draws it, at default values: inlets on the left, outlets on the right.

Math does arithmetic on signals, lane by lane. Feed it audio and it works on every sample; feed it control lanes and it works once per block.

What it does#

Operation chooses how lane n of A and lane n of B become lane n of the output. The two-operand operations are Add, Subtract, Multiply, Divide, Modulo, Power, Minimum, Maximum, Average and Absolute difference; the one-operand operations (Negate, Absolute, Floor, Ceiling, Round, Square root, Sign) use A and ignore B.

The inputs follow their cables. If A or B carries audio, the output is audio; if both carry control, or nothing at all, the output is control. A control operand against an audio operand is read lane by lane, so a one-lane control B applies to every audio channel and a two-lane control B applies one value per channel. When B is not connected, every lane of B is B when unconnected. When A is not connected it is 0.

Every operation is defined for every input: dividing by 0 gives 0, the square root and power of a negative number keep its sign, and any result that would not be a finite number becomes 0. Math never outputs NaN or infinity.

When to use it#

Use it to scale a signal (Multiply), to offset a control (Add), to ring-modulate two oscillators (Multiply on two audio inputs), to keep a level inside a limit (Minimum, Maximum) or to quantise a control (Round, Floor). For a plain level control with a public knob, Gain is simpler. To turn a signal into a gate, use Compare.

Ports#

PortDirectionSignalLanes
aInputAudio or control (follows the cable)Inferred from the cable
bInputAudio or control (follows the cable)Inferred from the cable
outOutputAudio or control (follows the cable)Inferred from the cable

Parameters#

ParameterLabelRangeDefaultUnitScaleNotes
opOperation0 to 160linearThe lane-wise operation. Add through Absolute difference combine A and B; Negate through Sign transform A alone and ignore B. Choices: 0 Add, 1 Subtract, 2 Multiply, 3 Divide, 4 Modulo, 5 Power, 6 Minimum, 7 Maximum, 8 Average, 9 Absolute difference, 10 Negate, 11 Absolute, 12 Floor, 13 Ceiling, 14 Round, 15 Square root, 16 Sign.
b_defaultB when unconnected-8 to 80linearValue used for every lane of B when nothing is connected to it. Multiply with 0.5 halves a signal. Shown when op is 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9.

Operation (op)#

Which arithmetic to apply, a discrete choice. Divide and Modulo give 0 when B is 0; Modulo takes the sign of B; Power and Square root keep the sign of A; Round rounds halves away from zero. Changing it takes effect at the next block.

B when unconnected (b_default)#

The value used for every lane of B when nothing is connected to B, from -8 to 8. Changes are smoothed over 5 ms so moving it does not click. It is hidden for the one-operand operations, which ignore B.

Tips#

  • To halve audio, set Operation to Multiply and B when unconnected to 0.5. Every channel is halved.
  • For per-channel gain from a control, connect a control bundle with one lane per channel to B and Multiply: lane 1 sets the first channel, lane 2 the second.
  • Multiply two audio oscillators for ring modulation: you hear the sum and difference of their frequencies.
  • Add B when unconnected to an LFO to move its centre before it reaches a parameter socket.
  • Math does not clamp. Follow a boost with a Limiter, or shape a control with Scale.

See also#

Gain, Compare, Scale, Control lanes, Multichannel audio.

Help patches

Each help patch is an ordinary Toy that teaches one idea. The diagram shows the Patch; the preview shows its Frame. Open it in TuneToys to play and edit it.

1. Halve a signal

beginner Teaches: op, b_default

Incoming audio multiplied by 0.5, with a scope before and after.

Patch: Oscillator audio output connected to Math a input. Math out output connected to Patch outputs audio input.
Connections as text
  • Oscillator audio output connected to Math a input
  • Math out output connected to Patch outputs audio input
  • Nothing is connected to B, so every channel of B is B when unconnected.
  • Multiply by 0.5 halves the level of every channel. Compare the two scopes.
  • Try Add with a small B for a DC offset, or Minimum to flatten the peaks.
Frame preview: Halve a signalFrame “Halve a signal” with 5 elements: knob B when unconnected, text heading, segmented Operation, scope Before, scope After.0.5B when unconnec…HALVE A SIGNALAddSubtractMultiplyDivideModuloPowerMinimumMaximumAverageAbsolute …Operationscopescope
Frame preview (static). Controls show their default values.

Open in TuneToys

2. Ring modulation

intermediate Teaches: op

Two oscillators multiplied sample by sample into a ring-modulated tone.

Patch: Oscillator “carrier” audio output connected to Math a input. Oscillator “modulator” audio output connected to Math b input. Math out output connected to Patch outputs audio input.
Connections as text
  • Oscillator “carrier” audio output connected to Math a input
  • Oscillator “modulator” audio output connected to Math b input
  • Math out output connected to Patch outputs audio input
  • Both inputs are audio, so Math runs at audio rate and multiplies every sample.
  • Multiply gives the sum and difference frequencies (550 Hz and 110 Hz) instead of the originals.
  • Switch to Add to hear the two tones simply mixed, or Minimum for a harsher edge.
Frame preview: Ring modulationFrame “Ring modulation” with 3 elements: text heading, segmented Operation, scope Output.RING MODULATIONAddSubtractMultiplyDivideModuloPowerMinimumMaximumAverageAbsolute …Operationscope
Frame preview (static). Controls show their default values.

Open in TuneToys

3. Control arithmetic

intermediate Teaches: op, b_default

An LFO plus an offset, computed at control rate, sweeps a filter cutoff.

Patch: Oscillator audio output connected to Filter audio input. Filter audio output connected to Patch outputs audio input. Control LFO control output connected to Math a input. Math out output connected to Filter @cutoff input.
Connections as text
  • Oscillator audio output connected to Filter audio input
  • Filter audio output connected to Patch outputs audio input
  • Control LFO control output connected to Math a input
  • Math out output connected to Filter @cutoff input
  • The LFO is a control lane, so Math runs at control rate and its output is a control lane too.
  • Add moves the whole sweep up or down; Multiply scales its depth.
  • The result drives the Cutoff socket. Lane values from 0 to 1 map across the cutoff range.
Frame preview: Control arithmeticFrame “Control arithmetic” with 3 elements: knob Offset, text heading, segmented Operation.0.2OffsetCONTROL ARITHMETICAddSubtractMultiplyDivideModuloPowerMinimumMaximumAverageAbsolute …Operation
Frame preview (static). Controls show their default values.

Open in TuneToys

  • Gain A clean stereo gain stage.
  • Compare Turn any signal into a gate or trigger by comparing it to a threshold or to another signal, lane by lane, with hysteresis and tolerance.
  • Scale Map a control range onto another with a curve, clamp and inversion. Depth and offset for any modulation.

Edit this page on GitHub

esc

↑ ↓ move↩ open⌘↩ new tabesc close