remap
Maps a value through a curve defined by a table of breakpoints.
The breakpoints are given as two arrays of the same length, one with the x coordinates and one with the y coordinates. For each input value, remap finds the two breakpoints that bracket it and returns the matching y, interpolated in one of five ways: linear, nearest, previous, next, or a shape-preserving cubic. A separate setting decides what happens when the input falls outside the table.
The input can be a k-rate value, a k-rate array, or an audio signal. With an audio input, every sample goes through the curve, so remap works as a waveshaper with a transfer function drawn by the breakpoints.
Syntax
xdata and ydata may be i-rate or k-rate arrays, in any combination.
Initialization
mode -- how to interpolate between the two breakpoints that bracket the input:
- 0 = linear.
- 1 = nearest: the y of the closer breakpoint. When the input is exactly halfway, the left breakpoint wins.
- 2 = previous: the y of the breakpoint at or to the left of the input. This is a step function that changes on each breakpoint.
- 3 = next: the y of the first breakpoint to the right of the input. This is a step function, one breakpoint ahead of previous.
- 4 = cubic: a shape-preserving piecewise cubic (PCHIP, Fritsch-Carlson). The curve is smooth and passes through every breakpoint. Unlike a natural cubic spline, it does not overshoot: between two breakpoints the curve stays within their y values, so a monotonic table gives a monotonic curve and a flat segment stays flat.
bounds -- what to do when the input is lower than the first x or higher than the last x:
- 0 = error: stop the instrument with a performance error.
- 1 = clamp: output the first or the last y.
- 2 = fill: output fill.
- 3 = extrapolate: continue the first or the last segment beyond the table. With mode 0 the segment is a straight line, and with mode 4 it is the cubic of the end segment. The step modes (1, 2 and 3) pick a y from the two breakpoints of the end segment: below the table, nearest and previous give the first y and next gives the second; above it, nearest and next give the last y and previous gives the one before.
fill -- the value output when bounds is 2. Defaults to 0. It is ignored for any other bounds setting.
Any other value of mode or bounds causes an initialization error.
Performance
x -- the input: a k-rate value, a k-rate array, or an audio signal.
xdata -- the x coordinates of the breakpoints. They must be strictly increasing.
ydata -- the y coordinates of the breakpoints. They can take any values.
y -- the output. For an array input, y is an array of the same length as x, and each element is computed on its own, so x does not need to be sorted. If x changes length during performance, y follows.
Both tables must hold at least two breakpoints and have the same length. A length mismatch causes a performance error. The lengths are checked at every call, since a global array can be resized by another instrument while the note runs.
When both tables are i-rate arrays, remap checks at initialization that xdata is strictly increasing and reports an initialization error otherwise. k-rate tables can be rewritten at any control period, and remap uses the new values at once. They are not checked: keeping a k-rate xdata strictly increasing is up to the orchestra, and an unsorted xdata gives meaningless results.
The output array may also be one of the tables, for example ydata = remap(x, xdata, ydata, 1, 1). Every element of the input is then mapped through the table as it was before the call.
remap has no i-rate version. Its output is first computed in the first control period, so an init-time read of a k-rate output gives 0.
Breakpoints and step modes
On a breakpoint, linear, nearest and cubic return the y of that breakpoint. previous also returns it, except on the last breakpoint, where it returns the y of the one before. next returns the y of the following breakpoint, except on the last one, where it returns its own y. With x = [0, 1, 3, 4] and y = [0, 2, 2, 6], an input of 4 gives 2 with previous and 6 with next.
The search for the bracketing breakpoints takes the same time whatever the input, so an unsorted array costs the same as a ramp. In cubic mode, the coefficients of a segment are computed only when needed, without any extra memory, even when the breakpoints change at k-rate.
Examples
Modes and bounds
This example prints what each mode and each bounds policy returns for a few inputs. The flat middle segment shows that the cubic mode does not overshoot. It uses the file remap.csd.
Its output is:
x 0.250 1.000 2.000 3.750
linear 0.500 2.000 2.000 5.000
nearest 0.000 2.000 2.000 6.000
previous 0.000 2.000 2.000 2.000
next 2.000 2.000 2.000 6.000
cubic 0.688 2.000 2.000 4.625
x -1.000 5.000
clamp 0.000 6.000
fill -100 -100.000 -100.000
extrapolate lin. -2.000 10.000
extrapolate cub. -0.667 7.333
Scale quantizer
When the same array is used for xdata and ydata, the nearest mode snaps any input to the closest value in the table. Here the table holds a pentatonic scale, and a single call with an array input quantizes a three-note random chord. It uses the file remap-quantize.csd.
Waveshaper
With an audio input, the breakpoints draw a transfer curve. This one is a soft saturation that never goes above 0.8. The cubic mode rounds off the corners that the linear mode leaves at each breakpoint, which gives a smoother tone. It uses the file remap-waveshaper.csd.
See also
Linear and Exponential Generators
Credits
Author: Pasquale Mainolfi
Italy
New in Csound version 7.0
October 2026