Skip to content

Mutations

Mutations

Mutations are formula that can alter the value that is sent out to other plugins. The mutation is applied on the receiver of the data, not at the transmitter, which allows for emitting a single value from a receiving, and handling it differently on various receivers.

The received value is available within the formula as the variable ii .

Let’s take the following settings file as an example:

settings.yml
devices:
- type: modbusTCP
name: mb
options:
ip: 10.0.0.2
- type: websocket
name: ws
- type: osc
name: osc
mappings:
- name: mb to ws - analog
from: { name: mb, address: 0, type: int8 }
to: { name: ws, address: input1, type: int16 }
mutation: scale(i, int8, int16)
- name: mb to osc - analog
from: { name: mb, address: 0, type: int8 }
to: { name: osc, address: /fader/1, type: float }
mutation: scale(i, int8, 100)
- name: mb to ws - digital
from: { name: mb, address: 1, type: bool }
to: { name: ws, address: led1, type: bool }
- name: mb to osc - digital
from: { name: mb, address: 1, type: bool }
to: { name: ws, address: /led/1, type: bool }
mutation: not(i)

The logical representation of these settings looks like this:

<ws: input1> <osc: /fader/1> scale(i, int8, int16) scale(i, int8, 100) <ws: led1> <osc: /led/1> <mb: 0> not(i) <mb: 0> value: 56 value: True value: 14392 value: 21.96 value: True value: False

Variables

the only variable available in the functions is the variable ii which is the input value to be converted.

Constants

the following constants can be used in formula

constantvalue
int8255
int1665535
pi3.141592653589793
epsilon1e-10
infinf

Supported Operators & Functions

the mutation math supports the following basic arithmetic operations, functions and processes:

NOTE

In the following examples we’ll use some other variable names — like xx — to represent numbers. Note thought that these cannot be used in the formula and must be replaced with a valid number.

Arithmetic

  • + addition between ii and xx . eg:

    i + 2

    with i+2i + 244

    arithmetic formula example

    formula example

    mutation: i + 2

    results table

    typeformulainputresult
    doublei + 23.05.0
    int8i + 246
    doublei - 23.01.0
    int8i - 242
    doublei / 23.01.5
    int8i / 242
    doublei * 23.06.0
    int8i * 248
    doublei % 23.01.0
    int8i % 240
    doublei ^ 23.09.0
    int8i ^ 2416
  • - subtraction between ii and xx . eg:

    i - 2

    with i=2i = 200

  • * multiplication between ii and xx . eg:

    i * 2

    with i=3i = 366

  • / division between ii and xx . eg:

    i / 2

    with i=3i = 31.51.5

  • % modulus of ii with respect to xx . eg:

    i % 2

    with i=3i = 311

  • ^ ii to the power of xx . eg:

    i ^ 2

    with i=3i = 399

Assignment

  • := assign the value of xx to ii . eg:

    i := 2

    with i=3i = 322

  • += increment ii by the value of the expression on the right-hand side. eg:

    i += abs(i - 4)

    with i=3i = 344

  • -= decrement ii by the value of the expression on the right-hand side. eg:

    i -= abs(i - 4)

    with i=3i = 322

  • *= assign the multiplication of ii by the value of the expression on the right-hand side to ii . eg:

    i *= abs(i - 5)

    with i=3i = 366

  • /= assign the division of ii by the value of the expression on the right-hand side to ii . eg:

    i /= abs(i * -2)

    with i=3i = 30.50.5

  • %= assign ii modulo the value of the expression on the right-hand side to ii . eg:

    i %= i / 2.5

    with i=3i = 30.60.6

Equalities & Inequalities

  • = == true only if ii is strictly equal to xx . eg:

    x == 3

    with i=3i = 311

    with i=2i = 200

  • <> != True only if ii does not equal xx . eg:

    i != 3

    with i=3i = 300

    with i=2i = 211

  • < True only if ii is less than xx . eg:

    i < 3

    with i=2i = 211

    with i=3i = 300

    with i=4i = 400

  • <= True only if ii is less than or equal to xx . eg:

    i <= 3

    with i=2i = 211

    with i=3i = 311

    with i=4i = 400

  • > True only if ii is greater than xx . eg:

    i > 3

    with i=2i = 200

    with i=3i = 300

    with i=4i = 411

  • >= True only if ii greater than or equal to xx . eg:

    i >= 3

    with i=2i = 200

    with i=3i = 311

    with i=4i = 411

Logic Operators

  • and & Logical AND, true only if ii and xx are both true. eg:

    i and 2

    with i=3i = 311

    with i=0i = 000

    and truth table

    Input AInput BOutput
    iixxii and xx
    000
    010
    100
    111
  • mand Multi-input logical AND, true only if all inputs are true. Left to right short-circuiting of expressions. eg:

    mand(i > 2, i < 4)

    with i=3i = 311

    with i=4i = 400

  • or | Logical OR, True if either ii or xx is true. eg:

    (i == 2) or (i == 4)

    with i=2i = 211

    with i=3i = 300

    with i=4i = 411

    or truth table

    Input AInput BOutput
    iixxii or xx
    000
    011
    101
    111
  • mor Multi-input logical OR, true if at least one of the inputs are true. Left to right short-circuiting of expressions. eg:

    mor(i > 3, i < -3)

    with i=3i = 300

    with i=4i = 411

  • nand Logical NAND, True only if either ii or xx is false. eg:

    i nand 3

    with i=0i = 011

    with i=1i = 100

    nand truth table

    Input AInput BOutput
    iixxii nand xx
    001
    011
    101
    110
  • nor Logical NOR, True only if the result of ii or xx is false eg:

    i nor 0

    with i=0i = 011

    with i=1i = 100

    nor truth table

    Input AInput BOutput
    iixxii nor xx
    001
    010
    100
    110
  • not Logical NOT, negate the logical sense of the input. eg:

    not(i)

    with i=0i = 011

    with i=1i = 100

    not truth table

    InputOutput
    iinot ii
    01
    10
    Invert / Not formula example

    The not(i) function converts the input ii to a boolean, converts back to the input type and returns it. + When using any number type it will always return either 00 or 11 .

    formula example

    mutation: not(i)
    results table
    TypeFormulaInputResult
    int8not(i)10
    int8not(i)01
    int8not(i)40
    int16not(i)10
    int16not(i)01
    doublenot(i)1.00.0
    doublenot(i)0.01.0
    boolnot(i)truefalse
    boolnot(i)falsetrue
  • xnor Logical XNOR, True if the biconditional of ii and xx is satisfied. eg:

    i xnor 0

    with i=0i = 011

    with i=1i = 100

    xnor truth table

    Input AInput BOutput
    iixxii xnor xx
    001
    010
    100
    111
  • xor Logical XOR, True only if the logical states of ii and xx differ. eg:

    i xor 0

    with i=0i = 000

    with i=1i = 111

    xor truth table

    Input AInput BOutput
    iixxii xor xx
    000
    011
    101
    110
  • true True state or any value other than zero (typically 1). eg:

    true

    with truetrue11

  • false False state, value of exactly zero. eg:

    false

    with falsefalse00

Functions

  • map map a value ii from range f0,f1f0, f1 to range t0,t1t0, t1 where f0<i<f1f0 < i < f1 eg:

    map(i, 50, 100, 100, 200)

    with i=75i = 75150150

    Map formula example

    The map function scales a given input value from one value range aa (specified with a lower and upper bound) to another value range bb (again, specified with a lower and upper bound). eg:

    formula example

    mutation: map(i, in_min, in_max, out_min, out_max)

    results table

    TypeFormulaInputResult
    int8map(i, 10, 80, 0, 100)00
    int8map(i, 10, 80, 0, 100)2521
    int8map(i, 10, 80, 0, 100)7592
  • scale scale a value ii from range 0,f10, f1 to range 0,f20, f2 where 0<i<f10 < i < f1 . eg:

    scale(i, 100, 200)

    with i=50i = 50100100

    Scale formula example

    The scale function is similar to the map function, but assumes that the in_min and out_min values are both 0. + This function is useful to scale between let’s say a percentage and a number range (be it the int8 number range 0 - 255, the int16 number range 0 - 65535, or any custom range starting at 0).

    formula example

    mutation: scale(i, in_max, out_max)

    results table

    TypeFormulaInputResult
    int8scale(i, 255, 65535)00
    int8scale(i, 255, 65535)127255
    int8scale(i, 255, 65535)255255
    int16scale(i, 65535, 255)00
    int16scale(i, 65535, 255)32639127
    int16scale(i, 65535, 255)65535255
    boolscale(i, 1, 255)falsefalse
    boolscale(i, 1, 255)truetrue
    int8scale(i, 1, 255)00
    int8scale(i, 1, 255)1255
    int8scale(i, 1, 255)4255
  • abs Absolute value of ii . eg:

    abs(i)

    with i=2i = 222

    with i=2i = -222

  • avg Average of all the inputs. eg:

    avg(i, 2)

    with i=2i = 222

    with i=6i = 644

  • ceil Smallest integer that is greater than or equal to ii . eg:

    ceil(i)

    with i=3.2i = 3.244

  • floor Largest integer that is less than or equal to ii . eg:

    floor(i)

    with i=1.7i = 1.711

  • clamp Clamp ii in range between r0r0 and r1r1 , where r0<r1r0 < r1 . eg:

    clamp(0, i, 10)

    with i=2i = -200

    with i=2i = 222

    with i=20i = 201010

    Clamp formula example

    formula example

    mutation: clamp(limit_low, i, limit_high)

    results table

    TypeFormulaInputResult
    doubleclamp(20, i, 80)3.020.0
    int8clamp(20, i, 80)420
    int8clamp(20, i, 80)2525
    int8clamp(20, i, 80)12580
  • iclamp Inverse-clamp ii outside the range r0r0 and r1r1 . Where r0<r1r0 < r1 . If ii is within the range it will snap to the closest bound. eg:

    iclamp(10, i, 20)

    with i=2i = 222

    with i=12i = 121010

    with i=18i = 182020

  • round Round ii to the nearest integer. eg:

    round(x)

    with i=2.3i = 2.322

    with i=2.49i = 2.4922

    with i=2.5i = 2.533

  • roundn Round ii to nn decimal places (eg: roundn(i,3)]roundn(i, 3)] where n>0n > 0 and is an integer. eg:

    roundn(i, 4)

    with i=1.2345678i = 1.23456781.23461.2346

  • equal Equality test between ii and xx using normalised epsilon. eg:

    equal(i, 2.3)

    with i=2i = 200

    with i=2.3i = 2.311

  • pow ii to the power of xx . eg:

    pow(i, 3)

    with i=2i = 288

    Pow formula example

    formula example

    mutation: pow(i, power)

    results table

    TypeFormulaInputResult
    doublepow(i, 2)3.09.0
    int8pow(i, 2)416
  • exp ee to the power of ii . eg:

    exp(i)

    with i=2i = 288

  • expm1 ee to the power of ii minus 1, where ii is very small. eg:

    expm1(i)

    with i=0.1i = 0.10.1051710.105171

  • sqrt Square root of ii , where ii >= 0. eg:

    sqrt(i)

    with i=100i = 1001010

  • root Nth-Root of ii . where nn is a positive integer. eg:

    root(i, 3)

    with i=8i = 822

  • log Natural logarithm (a logarithm to the base of ee ) of ii . eg:

    log(i)

    with i=100i = 1004.605174.60517

  • log10 Base 10 logarithm of ii . eg:

    log10(i)

    with i=100i = 10022

  • log2 Base 2 logarithm of ii . eg:

    log2(i)

    with i=16i = 1644

  • log1p Natural logarithm of 1 + ii , where ii is very small. eg:

    log1p(i)

    with i=0.1i = 0.10.09531020.0953102

  • logn Base N logarithm of ii . where nn is a positive integer. eg:

    logn(i, 8)

    with i=64i = 6422

  • trunc Integer portion of ii . eg:

    trunc(i)

    with i=1.7i = 1.711

  • frac Fractional portion of ii . eg:

    frac(i)

    with i=1.7i = 1.70.70.7

  • hypot Hypotenuse of ii and xx eg:

    hypot(i, 4)

    with i=3i = 355

  • inrange In-range returns true when ii is within the range r0r0 and r1r1 . Where r0<r1r0 < r1 . eg:

    inrange(10, i, 20)

    with i=2i = 200

    with i=12i = 1211

    with i=22i = 2200

  • max Largest value of all the inputs. eg:

    max(i, 2, 5)

    with i=1i = 155

    with i=10i = 101010

  • min Smallest value of all the inputs. eg:

    min(i, 2, 5)

    with i=1i = 111

    with i=10i = 1022

  • sum Sum of all the inputs. eg:

    sum(i, 2, 3)

    with i=1i = 166

  • mul Product of all the inputs. eg:

    mul(i, 2, 3)

    with i=1i = 166

    with i=2i = 21212

  • ncdf Normal cumulative distribution function. eg:

    ncdf(i)
  • sgn Sign of ii , -1 where ii < 0, +1 where ii > 0, else zero. eg:

    sgn(x)

    with i=4i = -41-1

    with i=4i = 411

    with i=0i = 000

Trigonometry

  • sin Sine of ii . eg:

    sin(i)

    with i=2i = 20.9092970.909297

  • cos Cosine of ii . eg:

    cos(i)

    with i=2i = 20.41647-0.41647

  • tan Tangent of ii . eg:

    tan(i)

    with i=2i = 22.18504-2.18504

  • sinh Hyperbolic sine of ii . eg:

    sinh(i)

    with i=2i = 23.626863.62686

  • cosh Hyperbolic cosine of ii . eg:

    cosh(i)

    with i=2i = 23.76223.7622

  • tanh Hyperbolic tangent of ii . eg:

    tanh(i)

    with i=2i = 20.9640280.964028

  • asin Arc sine of ii expressed in radians. Interval [1,+1][-1, +1] . eg:

    asin(i)

    with i=0.5i = 0.50.5235990.523599

  • acos Arc cosine of ii expressed in radians. Interval [1,+1][-1, +1] . eg:

    acos(i)

    with i=0.5i = 0.51.04721.0472

  • atan Arc tangent of ii expressed in radians. Interval [1,+1][-1, +1] . eg:

    atan(i)

    with i=0.5i = 0.50.4636480.463648

  • asinh Inverse hyperbolic sine of ii expressed in radians. eg:

    asinh(i)

    with i=2i = 21.443641.44364

  • acosh Inverse hyperbolic cosine of ii expressed in radians. eg:

    acosh(i)

    with i=2i = 21.316961.31696

  • atanh Inverse hyperbolic tangent of ii expressed in radians. eg:

    atanh(i)

    with i=0.5i = 0.50.5493060.549306

  • sinc Sine cardinal of ii . eg:

    sinc(i)

    with i=2i = 20.4546490.454649

  • cot Cotangent of ii . eg:

    cot(i)

    with i=2i = 20.457658-0.457658

  • csc Cosecant of ii . eg:

    csc(i)

    with i=2i = 21.099751.09975

  • sec Secant of ii . eg:

    sec(i)

    with i=2i = 22.403-2.403

  • atan2 Arc tangent of (i/x)(i / x) expressed in radians. [pi,+pi][-pi, +pi] . eg:

    atan2(i, 1)

    with i=2i = 21.107151.10715

  • rad2deg Convert ii from radians to degrees. eg:

    rad2deg(i)

    with i=2i = 2114.592114.592

  • deg2rad Convert ii from degrees to radians. eg:

    deg2rad(i)

    with i=114i = 1141.989681.98968

  • grad2deg Convert ii from gradians to degrees. eg:

    grad2deg(i)

    with i=2i = 20.90.9

  • deg2grad Convert ii from degrees to gradians. eg:

    deg2grad(i)

    with i=0.9i = 0.922

String Processing

NOTE

Note that the input type of mapping can be of type string, but the returned value of the mutation formula must be a number (a number will be converted to a boolean or string if required). It is currently not possible to mutate a string to a different string. The input string is available as the variable ii , any other string must be enclosed in quotes.

  • = == != <> \<= >= < > All common equality / inequality operators are applicable to strings and are applied in a case-sensitive manner. eg:

    i == 'test'

    with i="test"i = "test"11

    with i="testing"i = "testing"00

  • in True only if ii is a substring of xx . eg:

    i in 'testing'

    with i="test"i = "test"11

    with i="hello"i = "hello"00

  • like True only if the string ii matches the pattern xx . Available wildcard characters are * and ? denoting zero or more and zero or one matches respectively. eg:

    i like 'te?t*'

    with i="test"i = "test"11

    with i="testing"i = "testing"11

    with i="texting"i = "texting"11

    with i="taste"i = "taste"00

  • ilike True only if the string ii matches the pattern xx in a case-insensitive manner. Available wildcard characters are ’*’ and ’?’ denoting zero or more and zero or one matches respectively. eg:

    i ilike 'te?t*'

    with i="Test"i = "Test"11

    with i="teSting"i = "teSting"11

    with i="textinG"i = "textinG"11

    with i="taste"i = "taste"00

  • [r0:r1] Substring of ii starting at character index r0r0 up to and including r1r1 . Both r0r0 and r1r1 are assumed to be integers, where r0<=r1r0 <= r1 . They may also be the result of an expression; in the event they have a fractional component they will be truncated (eg: 1.67>11.67 -> 1 ). eg:

    with i=abcdefghi = 'abcdefgh' :

    • i[1:4] == 'bcde'
    • i[:10 / 2] == 'abcdef'
    • i[3:] =='defgh'
    • i[:] == 'abcdefgh'
    • i[2:5] == 'cdef'
  • [] The string size operator returns the size of the string being actioned. eg:

    i[]

    with i="test"i = "test"44

Control Structures

  • if If ii is true then return xx else return yy . eg:

    if (i, 100, 200)

    with i=1i = 1100100

    with i=0i = 0200200

  • if-else The if-else/else-if statement. Subject to the condition branch the statement will return either the value of the consequent or the alternative branch. eg:

    if (i > 100) 1; else 2;

    with i=1i = 122

    with i=101i = 10111

  • switch The first true case condition that is encountered will determine the result of the switch. If none of the case conditions resolve true, the default action is assumed as the final return value. eg:

    switch
    {
    case i == 'connected' : 1;
    case i == 'disconnected' : 0;
    default : -1;
    }

    with i="error"i = "error"1-1

    with i="connected"i = "connected"11

    with i="disconnected"i = "disconnected"00

  • ?: Ternary conditional statement, similar to that of the above denoted if-statement. eg:

    i > 100 ? 1 : 2

    with i=1i = 122

    with i=101i = 10111