Piecewise functions of time - davidpanderson/Numula GitHub Wiki

In Numula, variation in musical parameters can be expressed as a "piecewise function of time" (PFT). A PFT consists of a list of "primitives", each of which defines the function over a time interval. For example,

from numula.nuance import *
pft = [
    Linear(1, 2, 4/4),
    Linear(2, 0, 2/4)
]

defines a function that increases linearly from 1 to 2 over 4 beats, then decreases to 0 over 2 beats. It uses a PFT primitive

Linear(
    y0: float,
    y1: float,
    dt: float,
    closed_start = True,
    closed_end = False
)

that defines a segment varying linearly from y0 to y1 over a time interval dt.

PFTs can be used for a variety of purposes:

When a PFT is used for tempo control, its value is integrated, so the value at segment boundaries doesn't matter. When a PFT is used for other purposes, these values do matter. Segments can be "closed" or "open" at their endpoints. By default a segment is closed at the start and open at the end; you can specify this with optional closed_start and closed_end arguments. For example,

[
    Linear(1, 2, 4/4, closed_end = True),
    Linear(3, 4, 4/4)
]

specifies a function whose value at time 4/4 (the start of the 2nd measure) is 2. Numula will complain if a PFT used for volume control has missing or inconsistent values at segment boundaries (it's OK if adjacent segments are both closed at their boundary as long as they have the same value there).

The primitive

Unity(dt)

is equivalent to

Linear(1, 1, dt)

Exponential primitives

A more general PFT primitive is

ExpCurve(
    curvature: float,
    y0: float,
    y1: float,
    dt: float,
    closed_start = True,
    closed_end = False
)

This specifies an exponential function going from y0 to y1 over a time dt. If curvature is positive, the change is mostly at the end of the interval; if it's negative it's mostly at the beginning. If curvature is zero, it's the same as Linear.

Examples:

images/exp5.png

Tempo · Volume


images/exp2.png

Tempo · Volume


Curvature zero (linear):

Tempo · Volume


images/exp-2.png

Tempo · Volume


images/exp-5.png

Tempo · Volume