adsr_setr(time)-- Release
Example: adsr.adsr_seta(0.003);
Sets the ADSR envelope time (specified in seconds) or the gain
[0.0..1.0] factor, and returns the coefficient or the gain factor.
Processing Functions
adsr_a(scale)-- Attack
Example: adsr.adsr_a(1.0);
Starts processing the ADSR envelope, scaling it to [0.0..scale], and
returns the state.
adsr_d()-- Decay
adsr_s()-- Sustain
adsr_r()-- Release
Example: adsr.adsr_r();
Continues processing the ADSR envelope, and returns the state.
adsr_process()
Example: state = adsr.adsr_process();
Processes the ADSR envelope, and returns its state.
Note: You should call this function from the @sample section.
adsr_reset()
Example: adsr.adsr_reset();
Resets the ADSR envelope, and returns the state.
Instance Variables
state
Example: released = !adsr.state;
The current state (1=attack, 2=decay, 4=sustain, 8=release, 0=off).
env
Example: spl0 *= adsr.env;
The scaled ADSR envelope output.
scale
Example: adsr2.adsr_a(adsr1.scale);
The current scale factor.
desc:Last-note priority mono synth
import Tale/array.jsfx-inc
import Tale/midi_queue.jsfx-inc
import Tale/poly_blep.jsfx-inc
@init
voice.array_init(0, 128, 2);
@sample
while(midi.midiq_recv()) (
midi.msg1 &= 0xF0;
// Note On
midi.msg1 == 0x90 && midi.msg3 ? (
// Remove note if somehow it is already playing.
ptr = voice.array_find(midi.msg2);
ptr >= 0 ? voice.array_remove(ptr);
// Add note, and set pointer to it.
ptr = voice.array_add();
ptr[0] = midi.msg2;
// Set oscillator frequency.
ptr[1] = osc.poly_setf(midi.msg2, 440);
osc.a *= 0.5;
) :
// Note Off
midi.msg1 == 0x80 || midi.msg1 == 0x90 ? (
// Remove note.
ptr = voice.array_find(midi.msg2);
ptr >= 0 ? (
voice.array_remove(ptr);
!voice.size ? osc.a = 0 : (
// Update pointer to new last note.
ptr = voice.array_get(voice.size - 1);
osc.poly_setdt(ptr[1]);
osc.a *= 0.5;
);
);
) :
// All Notes Off
midi.msg1 == 0xB0 && midi.msg2 == 123 ? (
voice.array_clear();
);
);
spl0 = spl1 = osc.poly_saw();
Initialization Functions
array_init(index, max_rows[, cols])
Example: array.array_init(0, 64, 2);
Sets the offset and size of the local memory buffer to store the array
in, and returns the next available memory index (i.e.
index+rows*cols). If cols is omitted, then it defaults to 1.
array_alloc(max_rows[, cols])
array_free()
Example: array.array_alloc(64, 2);
Allocates/deallocates a block of local memory to store the array in,
and returns its index.
Note: Requires Tale/malloc.jsfx-inc.
Array Functions
array_get(row)
Example: ptr = array.array_get(0);
Returns a pointer to the local memory index of the specified row.
array_add()
Example: ptr = array.array_add();
Adds a row to the end of the array and returns its local memory index.
Note that the row is added but not initialized (i.e. it does not
contain any data yet, nor is it zeroed.).
array_insert(ptr)
Example: array.array_insert(array.array_get(0));
Inserts a row into the array. Note that the row is inserted but not
initialized.
array_remove(ptr)
Example: array.array_remove(array.array_get(0));
Removes a row from the array.
array_clear()
Example: array.array_clear();
Removes all rows from the array.
Miscellaneous Functions
array_first()
Example: ptr = array.array_first();
Returns a pointer to the local memory index of the first row, or -1 if
there are no rows.
array_next(ptr)
Example: ptr = array.array_next(ptr);
Returns a pointer to the local memory index of the next row, or -1 if
there is no next row.
array_last()
Example: ptr = array.array_last();
Returns a pointer to the local memory index of the last row, or -1 if
there are no rows.
array_find(value[, col[, ptr]])
Example: ptr = array_find(123);
Finds a value in the array at the specified column (0 by default),
starting at the specified row pointer (first row by default), and
returns the local memory index of the entire row, or -1 if the value
was not found.
Instance Variables
buf
Example: ptr = array.buf;
The local memory address of the buffer in which the array is stored.
size
Example: num_rows = array.size;
The current size of the array in rows.
num
Example: num_cols = array.num;
The number of columns in each row.
complex.jsfx-inc - Complex math operations
Setting Functions
cplx_set_real(a)
Example: z.cplx_set_real(1.0);
Sets the complex number to a + 0*i.
cplx_set(a, b)
Example: z.cplx_set(1.0, -1.0);
Sets the complex number to a + b*i.
cplx_set(z*)
Example: z2.cplx_set(z1); // z2 = z1
Sets the complex number to the value of another complex number.
Polar Functions
cplx_polar(r, phi)
Example: z.cplx_polar(1.0, $pi/4);
Sets the complex number using polar coordinates, converting from polar
to trigonometric form.
cplx_norm()
Example: norm = z.cplx_norm();
Returns the norm of the complex number i.e. Re(z)^2 + Im(z)^2.
cplx_abs()
Example: mag = z.cplx_abs();
Returns the absolute value (or modulus or magnitude) of the complex
number.
cplx_arg()
Example: phase = z.cplx_arg();
Returns the argument (or phase) of the complex number.
Equality Functions
cplx_eq(z*)
Example: is_eq = z1.cplx_eq(z2); // z1 == z2
Returns 1 if the complex numbers are equal, or 0 otherwise.
cplx_not_eq(z*)
Example: is_diff = z1.cplx_not_eq(z2); // z1 != z2
Returns 1 if the complex numbers are not equal, or 0 otherwise.
Elementary Functions
cplx_add(x*[, y*])-- Add
cplx_sub(x*[, y*])-- Subtract
cplx_mul(x*[, y*])-- Multiply
cplx_div(x*[, y*])-- Divide
Example: z.cplx_add(x, y); // z = x + y
Example: z.cplx_add(x); // z = z + x
Sets the complex number to the result of the operation on two complex
numbers.
cplx_add_real([z*,] a)-- Add
cplx_sub_real([z*,] a)-- Subtract
cplx_mul_real([z*,] a)-- Multiply
cplx_div_real([z*,] a)-- Divide
Example: z.cplx_add_real(x, a); // z = x + a
Example: z.cplx_add_real(a); // z = z + a
Sets the complex number to the result of the operation on a complex
number and a real number.
cplx_conj(z*)-- Complex conjugate
cplx_neg(z*)-- Negation (-z)
cplx_recip(z*)-- Reciprocal (1/z)
cplx_sqrt(z*)-- Square root
cplx_sqr(z*)-- Square (z^2)
Example: z.cplx_recip(x); // z = 1/x
Sets the complex number to the result of the operation on a complex
number.
Exponentiation Functions
cplx_exp(z*)-- Exponential function
cplx_log(z*)-- Natural logarithm
cplx_log10(z*)-- Base 10 logarithm
Example: z.cplx_log(x); // z = ln(x)
Sets the complex number to the result of the function of a complex
number.
cplx_pow(x*, y*)
cplx_pow_real(z*, a)
Example: z.cplx_pow(x, y); // z = x^y
Sets the complex number to a complex number raised to the power of
another complex or real number.
Trigonometric functions
cplx_sin(z*)-- Sine
cplx_cos(z*)-- Cosine
cplx_tan(z*)-- Tangent
cplx_sinh(z*)-- Hyperbolic sine
cplx_cosh(z*)-- Hyperbolic cosine
cplx_tanh(z*)-- Hyperbolic tangent
Example: z.cplx_sin(x);
Sets the complex number to the trigonometric function of a complex
number.
Instance Variables
re
im
Example: a = z.re;
Example: b = z.im;
The real/imaginary part of the complex number.
fft_real_synth.jsfx-inc - Real FFT bandlimited synthesis
Uses the real instead of the complex FFT, which is almost 2x as fast, but
requires REAPER v5.25+. See Tale/fft_synth.jsfx-inc for more
information.
four_init(index, size)
Example: osc.four_init(0, 1024);
Sets the offset and size of the local memory buffers to render the
waveform in, and returns the next available memory index. If necessary
the offset will automatically be realigned (i.e. rounded up) to
prevent the FFT from crossing a 65,536 item boundary.
The size of the FFT is specified by the second parameter, and must be
16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, or 32768.
Note that the FFT requires real/imaginary input pairs, and two buffers
are used internally, so a size of 1024 actually uses a total of 4096
items.
Note: There is also a real FFT optimized version of this library,
which is almost 2x as fast, but requires REAPER v5.25+, see
Tale/fft_real_synthesis.jsfx-inc.
four_align(index, size)
four_real_align(index, size)
Example: aligned_index = four_align(63489, 1024);
Realigns index so index+size will not cross a 65,536 item boundary.
four_alloc(size)
four_free()
Example: osc.four_alloc(1024);
Allocates/deallocates two blocks of local memory to render the
waveform in, and returns its index.
Note: Requires Tale/malloc.jsfx-inc.
Setting Functions
four_setf(freq)
Example: osc.four_setf(440);
Sets the oscillator frequency (specified in Hz), and returns the
frequency in seconds/sample.
(To convert from Hz to seconds/sample, divide by srate. To convert
from seconds/sample to Hz, multiply with srate.)
four_setf(note, tuning)
Example: osc.four_setf(60, 440);
Sets the oscillator frequency to the specified MIDI note and tuning
(in Hz), and returns the frequency in seconds/sample.
four_setdt(time)
Example: osc2.four_setdt(osc1.dt);
Sets the oscillator frequency (specified in seconds/sample), and
returns this value.
four_update()
four_update_odd()
four_update_one()
Example: osc.four_update() ? osc.four_ifft();
Recalculates the number of harmonics, and returns this (non-zero)
number if it has changed, indicating that the waveform should be
rerendered. Returns zero if the number has not changed.
If the waveform contains only odd harmonics, then you can use
four_update_odd() instead of four_update() to ignore changes in even
harmonics. Similarly four_update_one() only checks the first harmonic.
four_reset()
Example: osc.four_reset();
Resets the number of hamonics, forcing the next call to one of the
update functions to return non-zero.
four_setdc(dc[, phase])
Example: osc.four_setdc(0);
Example: ptr = osc.four_setdc(-0.5, 0.0);
The first form sets the DC component in the Fourier coefficients, and
returns this value.
The second form sets both the DC component and the phase offset
[0.0..1.0), and returns the local memory index of the first harmonic
cosine/sine pair.
FFT Functions
four_fft()
Example: osc.four_fft();
Takes the FFT of the (non-bandimited) single cycle waveform in the
wavetable buffer (a real signal of length size*1).
four_ifft([sigma])
Example: osc.four_ifft();
Renders a single cycle waveform in the wavetable buffer by taking the
inverse FFT of the Fourier coefficients.
If sigma is specified and is non-zero, then sigma approximation is
used to eliminate (most of) the ringing artifacts caused by the Gibbs
phenomenon.
Miscellaneous Functions
four_getdc()
four_getrms()
Example: dc = osc.four_getdc();
Extracts the DC/RMS value from the Fourier coefficients.
four_sum(phase)
Example: output = osc.four_sum(osc.t);
Calculates and returns a sample by adding cosines/sines.
Note: Because this function needs to add m-1 cosines/sines, it can be
really, really slow.
four_sigma(index, size)
Example: four_sigma(65535, 73);
Applies sigma approximation to the Fourier coefficients (cosine/sine
pairs) at the specified local memory index.
Instance Variables
buf
Example: wavetbl_index = osc.buf;
The local memory index of the wavetable/FFT workspace.
coef
Example: coef_index = osc.coef;
The local memory index of the Fourier coefficients (size/2 cosine/sine
pairs).
size
Example: wavetbl_size = osc.size;
Example: num_coef = osc.size / 2;
The size of the wavetable in samples, which is also the number of
Fourier coefficient cosines/sines (i.e. there are size/2 Fourier
coefficient cosine/sine pairs).
dt
Example: freq = osc.dt * srate;
The oscillator frequency, in seconds/sample.
m
Example: num_harm = osc.m - 1;
The number of harmonics (up to and including half the Nyquist
frequency).
fourier_series.jsfx-inc - Fourier series waveforms
four_setpw(pw)
Example: osc.four_setpw(0.30);
Sets the pulse width [0.0..1.0] of the waveform, and returns this
value.
four_setf2(freq)
four_setfc(freq)
Example: osc.four_setf2(660);
Example: osc.four_setfc(660);
Sets the follower/cutoff frequency (specified in Hz) of the hard sync
or filtered waveform, and returns the follower/leader frequency ratio.
Note: You should always first set the leader oscillator frequency, and
then set the follower/cutoff frequency. If you change the leader
frequency, then you will probably also want to update the follower
frequency.
four_setn(num_steps)
Example: osc.four_setn(3);
Sets the integer number of steps (>=1) for the stepped waveform, and
returns this value.
Waveform Functions
four_sin()-- Sine
four_cos()-- Cosine
four_tri()-- Triangle
four_sqr()-- Square (fixed 0.5 pulse width)
four_rect()-- Rectangle (variable pulse width)
four_saw()-- Sawtooth
four_ramp()-- Ramp
four_tri2()-- Modified triangle
four_sqr2()-- Modified square
four_half()-- Half-wave rectified sine (fixed 0.5 pulse width)
four_half2()-- Half-wave rectified sine (variable pulse width)
four_full()-- Full-wave rectified sine
four_sinp()-- Pulse sine (fixed 0.5 pulse width)
four_sawp()-- Saw pulse sine (fixed 0.5 pulse width)
four_trip()-- Triangular pulse
four_hwsaw()-- Half-wave rectified sawtooth
four_alt()-- Alternating sine
four_camel()-- Camel sine
four_camela()-- Alternating camel sine
four_camel2()-- Bipolar camel sine
four_sin2()-- Bipolar squared sine
four_para()-- Parabola
four_circ()-- Circle
four_arc()-- Cycloid
four_trap()-- Trapezoid (fixed 0.5 pulse width)
four_trap2()-- Trapezoid (variable pulse width)
four_hyptri()-- Hyper triangular wave
four_lpsqr()-- Low-pass filtered square
four_lpsqrN()-- LPF square approximation (order N = 2 or 3)
four_intlpsqrN()-- Integrated LPF square (order N = 2 or 3)
four_bpsqr2()-- BPF square approximation
four_hpsqr()-- High-pass filtered square
four_intsaw()-- Integrated sawtooth
four_cubsaw()-- Cubic sawtooth
four_sinsaw()-- Sine sawtooth
four_hpsaw()-- High-pass filtered sawtooth
four_hpsaw6()-- HPF sawtooth approximation
four_logit3()-- Logit approximation
four_sqrm1()-- Square wave minus fundamental
four_sawm1()-- Sawtooth minus fundamental
four_sinn()-- Stepped sine
four_trin()-- Stepped triangle
four_sawn()-- Stepped sawtooth
four_hssaw()-- Hard sync sawtooth
four_ham()-- Hammond 88 8000 000
four_stairs()-- Staircase (fixed 0.5 pulse width)
four_stairs3()-- Staircase (variable pulse width)
four_stairs2()-- Uneven staircase
Example: osc.four_saw();
Calculates the Fourier coefficients that define the waveform, and
stores them in the coefficient buffer.
Miscellaneous Functions
_four_j1_pi(n)
Example: y = _four_j1_pi(42);
Approximates the Bessel function of the first kind order 1 on the
integer n (>=0) multiplied with $pi, i.e. y = j1($pi*n).
Instance Variables
coef
Example: coef_index = osc.coef;
The local memory index of the Fourier coefficients (cosine/sine
pairs).
size
Example: num_coef = osc.size / 2;
The number of cosines/sines, i.e. there are size/2 Fourier
coefficient cosine/sine pairs.
m
Example: num_harm = osc.m - 1;
The number of harmonics (up to and including half the Nyquist
frequency).
pw
Example: duty_cycle = osc.pw;
The pulse width [0.0..1.0] of the waveform.
n
Example: num_steps = osc.n;
The integer number of steps (>=1) of the stepped waveform.
fc
Example: cutoff_freq = osc.fc * osc.dt * srate;
The cutoff/oscillator frequency ratio of the filtered waveform.
lfo_setf(freq)
Example: lfo.lfo_setf(4.0);
Sets the oscillator frequency (specified in Hz), and returns the
frequency in seconds/sample.
(To convert from Hz to seconds/sample, divide by srate. To convert
from seconds/sample to Hz, multiply with srate.)
lfo_setf(note, tuning)
Example: osc.lfo_setf(0, 440);
Sets the oscillator frequency to the specified MIDI note and tuning
(in Hz), and returns the frequency in seconds/sample.
lfo_setdt(time)
Example: lfo2.lfo_setdt(lfo1.dt);
Example: lfo2.dt = lfo1.dt;
Sets the oscillator frequency (specified in seconds/sample), and
returns this value.
lfo_setpw(pw)
Example: lfo.lfo_setpw(0.3);
Example: lfo.pw = 0.3;
Sets the pulse width [0.0..1.0] of the waveform, and returns this
value.
lfo_setf2(freq)
lfo_setfc(freq)
Example: lfo.lfo_setf2(6.0);
Example: lfo.lfo_setfc(6.0);
Sets the follower/cutoff frequency (specified in Hz) of the hard sync
or filtered waveform, and returns the follower/leader frequency ratio.
Note: You should always first set the leader oscillator frequency, and
then set the follower/cutoff frequency. If you change the leader
frequency, then you will probably also want to update the follower
frequency.
lfo_setn(num_steps)
Example: lfo.lfo_setn(3);
Sets the integer number of steps (>=1) for the stepped waveform, and
returns this value.
Waveform Functions
lfo_sin()-- Sine approximation
lfo_cos()-- Cosine approximation
lfo_tri()-- Triangle
lfo_sqr()-- Square (fixed 0.5 pulse width)
lfo_rect()-- Rectangle (variable pulse width)
lfo_saw()-- Sawtooth
lfo_ramp()-- Ramp
lfo_sin_precise()-- Sine
lfo_cos_precise()-- Cosine
lfo_tri2()-- Modified triangle
lfo_sqr2()-- Modified square
lfo_half()-- Half-wave rectified sine (fixed 0.5 pulse width)
lfo_half2()-- Half-wave rectified sine (variable pulse width)
lfo_lpsqrN()-- LPF square approximation (order N = 2 or 3)
lfo_intlpsqrN()-- Integrated LPF square (order N = 2 or 3)
lfo_bpsqr2()-- BPF square approximation
lfo_hpsqr()-- High-pass filtered square
lfo_intsaw()-- Integrated sawtooth
lfo_cubsaw()-- Cubic sawtooth
lfo_sinsaw()-- Sine sawtooth
lfo_hpsaw()-- High-pass filtered sawtooth
lfo_hpsaw6()-- HPF sawtooth approximation
lfo_logit3()-- Logit approximation
lfo_sqrm1()-- Square wave minus fundamental
lfo_sawm1()-- Sawtooth minus fundamental
lfo_sinn()-- Stepped sine
lfo_trin()-- Stepped triangle
lfo_sawn()-- Stepped sawtooth
lfo_yawn()-- Very boring :-O
lfo_hssaw()-- Hard sync sawtooth
lfo_ham()-- Hammond 88 8000 000
lfo_stairs()-- Staircase (fixed 0.5 pulse width)
lfo_stairs3()-- Staircase (variable pulse width)
lfo_stairs2()-- Uneven staircase
lfo_sh()-- Sample and hold noise
lfo_sh2(x)-- Sample and hold X
Example: sample = lfo.lfo_tri();
Returns a sample of a waveform, and increments its phase.
Note: In v20151024 the phase of lfo_full() and lfo_trip() has been
corrected. To convert code relying on the old behavior, synchronize
the phase to t-0.25 for lfo_full(), and to t-(0.75+0.5*pw) for
lfo_trip().
Miscellaneous Functions
lfo_sync(phase)
Example: lfo2.lfo_sync(lfo1.t + 0.5);
Synchronizes the oscillator with the specified phase, and returns the
normalized phase.
Note: You can safely specify out or range (and even negative) values
here.
lfo_inc()
Example: lfo.lfo_inc();
Increments the oscillator phase, and returns it.
Note: All waveform functions automatically increment the phase.
lfo_rect_dc()-- Rectangle
lfo_half_dc()-- Half-wave rectified sine (fixed 0.5 pulse width)
lfo_half2_dc()-- Half-wave rectified sine (variable pulse width)
lfo_full_dc()-- Full-wave rectified sine
lfo_sinp_dc()-- Pulse sine (fixed 0.5 pulse width)
lfo_sawp_dc()-- Saw pulse
lfo_trip_dc()-- Triangular pulse
lfo_hwsaw_dc()-- Half-wave rectified sawtooth
lfo_camel_dc()-- Camel sine
lfo_camela_dc()-- Alternating camel sine
lfo_arc_dc()-- Cycloid
lfo_hyptri_dc()-- Hyper triangular wave
lfo_intsaw_dc()-- Integrated sawtooth
lfo_hpsaw_dc()-- High-pass filtered sawtooth
lfo_hpsaw6_dc()-- HPF sawtooth approximation
lfo_hssaw_dc()-- Hard sync sawtooth
lfo_stairs3_dc()-- Staircase (variable pulse width)
Example: sample = lfo.lfo_rect() - lfo.lfo_rect_dc();
Returns the (constant or pulse width/follower frequency dependent) DC
value for the waveform.
_lfo_sin(x)
_lfo_cos(x)
Example: y = _lfo_sin(x);
Example: y = _lfo_cos(x);
Fast approximations of sin(x) and cos(x) for x in the ranges of
[-$pi..$pi] and [0..2*$pi].
_lfo_tanh(x)
Example: y = _lfo_tanh(x);
Fast approximation of tanh(x).
Instance Variables
t
Example: phase = lfo.t;
The current phase [0.0..1.0) of the oscillator.
dt
Example: freq = lfo.dt * srate;
The oscillator frequency, in seconds/sample.
pw
Example: duty_cycle = lfo.pw;
The pulse width [0.0..1.0] of the waveform.
fc
Example: cutoff = lfo.fc * lfo.dt * srate;
The cutoff/oscillator frequency ratio of the filtered waveform.
n
Example: num_steps = lfo.n;
The integer number of steps (>=1) of the stepped waveform.
malloc(size)
Example: buf = malloc(128);
Example: top = malloc(0);
Allocates a block of local memory, and returns its index.
If size is 0, then returns the next available index.
aligned_malloc(size)
Example: fft_buf = aligned_malloc(1024);
Allocates a block of local memory that is aligned in such a way that
it will not cross a 65,536 item boundary, and returns its index.
calloc(num, size)
Example: buf = calloc(64, 2);
Allocates a block of local memory of num*size items, and initializes
it to zero.
realloc(ptr, size)
aligned_realloc(ptr, size)
Example: buf = realloc(buf, 256);
Example: fft_buf = aligned_realloc(fft_buf, 2048);
Changes the size of the memory block, moving it to a new location if
necessary. If ptr is 0, then it calls malloc(size) or
aligned_malloc(size).
free(ptr)
Example: free(buf);
Deallocates the memory block, and returns 0.
desc:MIDI queue mono synth
import Tale/midi_queue.jsfx-inc
import Tale/poly_blep.jsfx-inc
@init
// Set MIDI queue local memory index and size.
midi.midiq_init(0, 1024);
@block
// Receive MIDI messages and add them to queue.
midi.midiq_collect();
@sample
// Remove MIDI message from the head of queue.
while(midi.midiq_remove()) (
// Parse MIDI message.
midi.msg1 &= 0xF0;
// Note On
midi.msg1 == 0x90 && midi.msg3 ? (
osc.poly_setf(note = midi.msg2, 440);
osc.a *= 0.5 * midi.msg3 / 128;
) :
// Note Off
midi.msg1 == 0x80 || midi.msg1 == 0x90 ? (
midi.msg2 == note ? osc.a = 0;
);
);
// Sawtooth oscillator.
spl0 = spl1 = osc.poly_saw();
Simple Interface
midiq_recv()
Context: @sample
Example: is_avail = midi.midiq_recv();
Receives any MIDI message, and stores it in the msg1, msg2, and msg3
instance variables.
Note: The simple interface doesn't actually queue any MIDI messages,
so it can and should be used on its own (i.e. without the other
functions).
midiq_recv_sysex()
Context: @sample
Example: is_avail = (len = midi.midiq_recv_sysex()) > 0;
Like midiq_recv(), but supports regular 3-byte MIDI messages as well
as SysEx messages. Stores the first 3 bytes in msg1, msg2, and msg3,
but also stores the full message in the buf[] instance variable, and
returns the message length.
Initialization Functions
midiq_init(index[, size])
Example: midi.midiq_init(0, 1024);
Sets the offset and size (omit for non-bounded size) of the local
memory buffer to store the queue in, and returns the next available
memory index (i.e. index+size*3).
midiq_init_sysex(index, size)
Example: midi.midiq_init_sysex(0, 1024);
Sets the offset and size (>=4) of the local memory buffer to store a
single SysEx message in, and returns the next available memory index
(i.e. index+size).
midiq_alloc(size)
midiq_alloc_sysex(size)
midiq_free()
Example: midi.midiq_alloc(1024);
Allocates/deallocates a block of local memory to store the queue in,
and returns its index.
Note: Requires Tale/malloc.jsfx-inc.
Queue Functions
midiq_collect()
midiq_collect(ch, mask)
Context: @block
Example: midi.midiq_collect();
Receives any MIDI messages, optionally filtering by channel (0..15,
<0=any) and status byte (1=Note Off, 2=Note On, 4=Poly Aftertouch,
8=Control Change, 16=Program Change, 32=Channel Atertouch, 64=Pitch
Bend).
midiq_remove()
Context: @sample
Example: is_avail = midi.midiq_remove();
Checks if a MIDI message is available, removes it from the queue, and
stores it in the msg1, msg2, and msg3 instance variables.
Miscellaneous Functions
midiq_add(ofs, msg1, msg2, msg3)
Example: midi.midiq_add(0, 0x90, 60, 127);
Adds a MIDI message to the queue.
midiq_rewind()
Context: @block
Example: midi.midiq_rewind();
Rewinds (clears) the queue, so new MIDI messages can be added.
Note: This function is automatically called by midiq_collect().
_midiq_rewind()
Context: @block
Example: midi._midiq_rewind();
Rewinds the queue, preserving any left-over messages.
_midiq_collect()
_midiq_collect(ch, mask)
Context: @block
Example: midi._midiq_collect();
Same as midiq_collect(), but without automatically rewinding the queue
first.
Instance Variables
ofs
Example: offset_in_seconds = midi.ofs / srate;
The offset within the current sample block, in samples.
msg1
msg2
msg3
Example: ch = midi.msg1 & 0x0F;
The MIDI message that was last removed from the queue.
pend
idx
Example: ofs_in_spls = midi.pend ? midi.ofs - midi.idx + 1;
A flag indicating that a MIDI message is pending (i.e. idx<=ofs), and
the sample index [0..samplesblock-1] of the next midiq_recv() call.
buf
size
Example: next_avail_index = midi.buf + midi.size * 3;
The local memory index and maximum size (in MIDI messages) of the
queue, or the index and maximum size (in bytes) of a single SysEx
message.
head
tail
Example: num_msgs = (midi.tail - midi.head) / 3;
Example: num_avail = midi.size - (midi.tail - midi.buf) / 3;
Pointers to the head and tail of the queue.
lcg_init(seed)
Example: rng.lcg_init(12345);
Initialises the random number generator with the specified seed value
(an integer in the range 1 through 2^31-2), and returns this value.
lcg_density(density)
Example: black.lcg_density(0.25);
Sets the black noise density (0..1), and returns this value.
lcg_leaky_set(rc)
Example: rng.lcg_leaky_set(0.0082);
Sets the feedback coefficient of the leaky integrator used for
generating Brownian noise to the specified RC time constant (in
seconds), and returns the filter coefficient.
The leaky integrator behaves like a high-pass filter with a
cutoff frequency (in Hz) of 1/(2*$pi*rc).
Note: lcg_brown() will automatically set the default RC time constant
if none has been set.
Noise Functions
lcg_white()-- White
lcg_pink()-- Pink (1/f)
lcg_brown()-- Brownian
lcg_blue()-- Blue
lcg_violet()-- Violet
lcg_grey()-- Grey
lcg_black()-- Black
Example: sample = rng.lcg_white();
Returns a sample of noise.
Miscellaneous Functions
lcg_rand(x)
lcg_rand2(x)
Example: y = lcg_rand(x);
Example: y = rng.lcg_rand2(x);
Returns a psuedorandom number between 0 and the parameter.
Instance Variables
seed
Example: rng2.lcg_init(rng1.seed);
The current seed value.
density
Example: rng2.lcg_density(rng1.density);
The black noise density.
poly_setf(freq)
Example: osc.poly_setf(440);
Sets the oscillator frequency (specified in Hz), and returns the
frequency in seconds/sample.
(To convert from Hz to seconds/sample, divide by srate. To convert
from seconds/sample to Hz, multiply with srate.)
Note: Although the maximum frequency supported is srate/4, you can
safely specify higher frequencies, even beyond srate/2.
poly_setf(note, tuning)
Example: osc.poly_setf(60, 440);
Sets the oscillator frequency to the specified MIDI note and tuning
(in Hz), and returns the frequency in seconds/sample.
poly_setdt(time)
Example: osc2.poly_setdt(osc1.dt);
Sets the oscillator frequency (specified in seconds/sample), and
returns this value.
poly_setpw(pw)
Example: osc.poly_setpw(0.3);
Example: osc.pw = 0.3;
Sets the pulse width [0.0..1.0] of the waveform, and returns this
value.
poly_setf2(freq)
Example: osc.poly_setf2(6.0);
Sets the follower frequency (specified in Hz) of the hard sync
waveform, and returns the follower/leader frequency ratio.
Note: You should always first set the leader oscillator frequency, and
then set the follower frequency. If you change the leader frequency,
then you will probably also want to update the follower frequency.
poly_setn(num_steps)
Example: osc.poly_setn(3);
Sets the integer number of steps (>=1) for the stepped waveform, and
returns this value.
Waveform Functions
poly_sin()-- Sine
poly_cos()-- Cosine
poly_tri()-- Triangle
poly_sqr()-- Square
poly_rect()-- Rectangle (pulse)
poly_saw()-- Sawtooth
poly_ramp()-- Ramp
poly_tri2()-- Modified triangle
poly_sqr2()-- Modified square
poly_half()-- Half-wave rectified sine (fixed 0.5 pulse width)
poly_half2()-- Half-wave rectified sine (variable pulse width)
poly_full()-- Full-wave rectified sine
poly_sinp()-- Pulse sine (fixed 0.5 pulse width)
poly_sawp()-- Saw pulse
poly_trip()-- Triangular pulse
poly_hwsaw()-- Half-wave rectified sawtooth
poly_alt()-- Alternating sine
poly_camel()-- Camel sine
poly_camela()-- Alternating camel sine
poly_camel2()-- Bipolar camel sine
poly_sin2()-- Bipolar squared sine
poly_para()-- Parabola
poly_trap()-- Trapezoid (fixed 0.5 pulse width)
poly_trap2()-- Trapezoid (variable pulse width)
poly_hyptri()-- Hyper triangular wave
poly_lpsqrN()-- LPF square approximation (order N = 2 or 3)
poly_intlpsqrN()-- Integrated LPF square (order N = 2 or 3)
poly_bpsqr2()-- BPF square approximation
poly_intsaw()-- Integrated sawtooth
poly_cubsaw()-- Cubic sawtooth
poly_sinsaw()-- Sine sawtooth
poly_hpsaw6()-- HPF sawtooth approximation
poly_logit3()-- Logit approximation
poly_sqrm1()-- Square wave minus fundamental
poly_sawm1()-- Sawtooth minus fundamental
poly_sinn()-- Stepped sine
poly_trin()-- Stepped triangle
poly_sawn()-- Stepped sawtooth
poly_hssaw()-- Hard sync sawtooth
poly_ham()-- Might not be suitable for vegetarians
poly_stairs()-- Staircase (fixed 0.5 pulse width)
poly_stairs3()-- Staircase (variable pulse width)
poly_stairs2()-- Uneven staircase
Example: sample = osc.poly_saw();
Returns a sample of a waveform, and increments its phase.
Note: In v20151024 the phase of poly_full() and poly_trip() has been
corrected. To convert code relying on the old behavior, synchronize
the phase to t-0.25 for poly_full(), and to t-(0.75+0.5*pw) for
poly_trip().
Miscellaneous Functions
poly_sync(phase)
Example: osc2.poly_sync(osc1.t + 0.5);
Synchronizes the oscillator with the specified phase, and returns the
normalized phase.
Note: You can safely specify out or range (and even negative) values
here.
poly_inc()
Example: osc.poly_inc();
Increments the oscillator phase, and returns it.
Note: All waveform functions automatically increment the phase.
poly_resetf()
Example: osc.poly_resetf();
Call this before changing the waveform to poly_stairs() or poly_ham().
poly_blep(t, dt)-- Band-limited step
poly_blamp(t, dt)-- Band-limited ramp
poly_bluh(t, dt)-- Band-limited curve
Example: y = poly_blep(osc.t, osc.dt);
Returns a polynomial around a discontinuity (i.e. when it passes 1.0
and wraps to 0.0), or 0.0 otherwise.
Instance Variables
t
Example: phase = osc.t;
The current phase [0.0..1.0) of the oscillator.
dt
Example: freq = osc.dt * srate;
The oscillator frequency, in seconds/sample.
pw
Example: duty_cycle = osc.pw;
The pulse width [0.0..1.0] of the waveform.
fc
Example: freq2 = poly.fc * lfo.dt * srate;
The hard sync oscillator frequency ratio.
a
a2
a3
Example: osc2.a = osc1.a;
The frequency dependent gain [0.0..1.0].
rbj_hs(freq, q, gain)-- High-shelf
Example: lp.rbj_lp(1000, 0.5);
Sets up the filter for the specified cutoff frequency (in Hz), and Q
and gain factors, and returns the a0 coefficient.
(To convert from dB to gain: gain=10^(db/20).)
rbj_gain(gain)
rbj_dry_wet(dry, wet)
Example: lp.rbj_lp(1000, 0.5); lp.rbj_gain(-2.0);
Example: lp.rbj_lp(1000, 0.5); lp.rbj_dry_wet(0.1, 0.9);
Modifies the filter by applying the specified output gain or dry/wet
mix.
Note: You should always first setup the filter, and then modify it. If
you change the filter frequency/Q afterwards, then this will reset the
gain and dry/wet values, and so you will have to modify them again.
Filter Functions
rbj_df1(sample)-- Direct Form 1
rbj_df2(sample)-- Direct Form 2
rbj_tdf2(sample)-- Transposed Direct Form 2
Example: output = lp.rbj_tdf2(input);
Sends a sample through the filter, and returns its output.
Miscellaneous Functions
rbj_reset_df1([input])-- Direct Form 1
rbj_reset_df2([input])-- Direct Form 2
rbj_reset_tdf2([input])-- Transposed Direct Form 2
Example: lp.rbj_reset_tdf2();
Resets the filter state to the specified input value, or to zero if
the value is omitted.
rbj_bwtoq(bw)
rbj_qtobw(q)
Example: q = rbj_bwtoq(2.0);
Converts bandwidth (in octaves) to Q factor, or vice versa.
Note: The first coefficient (a0) is not included here, because all
coefficients are scaled (i.e. divided) by a0, after which a0 itself
would always be 1. The setting functions return the original a0 value,
should you need it (e.g. to get the original, non-scaled
coefficients).
x0
x1
y0
y1
Example: current_input = lp.x0;
Example: previous_output = lp.y1;
Direct Form 1 inputs/outputs.
w0
w1
Example: lp2.w0 = lp1.w0; lp2.w1 = lp1.w1;
Direct Form 2 filter state.
s0
s1
Example: lp2.s0 = lp1.s0; lp2.s1 = lp1.s1;
Transposed Direct Form 2 filter state.
rc_setf(freq)
Example: lp.rc_setf(1000);
Sets the filter cutoff frequency (specified in Hz), and returns the
filter coefficient.
rc_set(rc)
Example: lp.rc_set(0.00015);
Sets the filter cutoff frequency by means of the RC time constant
(specified in seconds), and returns the filter coefficient.
(To convert from Hz to RC time constant or vice versa, do 1/(2*$pi*x),
i.e. freq=1/(2*$pi*rc) and rc=1/(2*$pi*freq).)
rc_sett(time)
Example: env.rc_sett(0.100);
Sets the filter cutoff frequency so that it takes the specified time
(in seconds) to almost fully (99.3%) change from one constant value to
another (e.g. from 0.0 to 1.0), and returns the filter coefficient.
(To convert from seconds to RC time constant, divide by 5. To convert
from RC time constant to seconds, multiple with 5.)
rc_leaky_set(rc)
Example: sum.rc_leaky_set(0.022);
Sets the feedback coefficient of the leaky integrator to the specified
RC time constant (in seconds), and returns the filter coefficient.
Filter Functions
rc_lp(sample)-- Low-pass
rc_hp(sample)-- High-pass
rc_ap(sample)-- All-pass
Example: output = lp.rc_lp(input);
Sends a sample through the filter, and returns its output.
rc_leaky(sample)
Example: output = sum.rc_leaky(input);
Sends a sample through the leaky integrator, and returns its output.
High Precision Functions
rc_setf_precise(freq)
rc_sett_precise(time)
rc_set_precise(rc)
rc_leaky_set_precise(rc)
rc_hp_precise(sample)
Example: lp.rc_setf_precise(1000);
Example: output = hp.rc_hp_precise(input);
High precision versions of setting/filter functions, which are less
efficient but more precise.
Note: There are no specialized high precision versions of the
low-pass, all-pass, or leaky integrator filter funtions.
Instance Variables
a
Example: lp2.a = lp1.a;
The filter coefficient.
lp
Example: output = lp.lp;
The low-pass filter output value.
hp
Example: output = hp.hp;
The high precision high-pass filter output value.
in
Example: input = hp.in;
The high precision high-pass filter input value.
sin_setf(freq)
Example: osc.sin_setf(440);
Sets the oscillator frequency (specified in Hz), and returns the
frequency in seconds/sample.
(To convert from Hz to seconds/sample, divide by srate. To convert
from seconds/sample to Hz, multiply with srate.)
Note: Although the maximum frequency supported is srate/2, you can
safely specify higher frequencies.
sin_setf(note, tuning)
Example: osc.sin_setf(60, 440);
Sets the oscillator frequency to the specified MIDI note and tuning
(in Hz), and returns the frequency in seconds/sample.
sin_setdt(time)
Example: osc2.sin_setdt(osc1.dt);
Sets the oscillator frequency (specified in seconds/sample), and
returns this value.
Waveform Functions
sin_sin()-- Sine
sin_cos()-- Cosine
Example: sample = osc.sin_sin();
Returns a sample of a waveform, and increments its phase.
Miscellaneous Functions
sin_sync(phase)
Example: osc2.sin_sync(osc1.t + 0.5);
Synchronizes the oscillator with the specified phase, and returns the
normalized phase.
Note: You can safely specify out or range (and even negative) values
here.
sin_inc()
Example: osc.sin_inc();
Increments the oscillator phase, and returns it.
Note: The waveform functions automatically increment the phase.
Instance Variables
t
Example: phase = osc.t;
The current phase [0.0..1.0) of the oscillator.
dt
Example: freq = osc.dt * srate;
The oscillator frequency, in seconds/sample.
a
Example: osc2.a = osc1.a;
The frequency dependent gain [0.0..1.0].
sin
cos
Example: sample = osc.sin;
The current sine/cosine output values.
Correctional Facilities
Sing Sing
Example: Sing Sing prison
Maximum security prison in Ossining, New York, USA.
ftoi32(x)-- Convert from floating point to unsigned
Example: y = add32(x, y); // y = x + y
Both x and y should be unsigned 32-bit integers; n should be an
integer in the [0..31] range. Returns an unsigned 32-bit integer.
64-Bit Functions
and64(x*, y*)-- Bitwise AND
or64(x*, y*)-- Bitwise OR
xor64(x*, y*)-- Bitwise XOR
not64(x*)-- Bitwise complement
shl64(x*, n)-- Shift left
shr64(x*, n)-- Logical (unsigned) shift right
sar64(x*, n)-- Arithmetic (signed) shift right
rol64(x*, n)-- Rotate left
ror64(x*, n)-- Rotate right
neg64(x*)-- Two's complement negation
abs64(x*)-- Absolute value
add64(x*, y*)-- Add
sub64(x*, y*)-- Subtract
mul64(x*, y*)-- Multiply
Example: y.add64(x, y); // y = x + y
Both x* and y* are unsigned 64-bit integer namespaces; n should be an
integer in the [0..63] range. Returns the low-order unsigned 32-bit
word of the unsigned 64-bit result.
Instance Variables
hw
lw
Example: y = 2^32 * x.hw + x.lw;
The high/low-order unsigned 32-bit word of the unsigned 64-bit
integer.
wave_init(index, size)
Example: osc.wave_init(0, 1024);
Sets the offset and size of the local memory buffer that will hold the
waveform, and returns the next available memory index (i.e.
index+size).
wave_alloc(size)
wave_free()
Example: osc.wave_alloc(1024);
Allocates/deallocates a block of local memory that will hold the
waveform, and returns its index.
Note: Requires malloc.jsfx-inc.
Setting Functions
wave_setf(freq)
Example: osc.wave_setf(440);
Sets the oscillator frequency (specified in Hz), and returns the
frequency in seconds/sample.
(To convert from Hz to seconds/sample, divide by srate. To convert
from seconds/sample to Hz, multiply with srate.)
wave_setf(note, tuning)
Example: osc.wave_setf(60, 440);
Sets the oscillator frequency to the specified MIDI note and tuning
(in Hz), and returns the frequency in seconds/sample.
wave_setdt(time)
Example: osc2.wave_setdt(osc1.dt);
Sets the oscillator frequency (specified in seconds/sample), and
returns this value.
wave_lagrange(n)-- Lagrange (N+1-point, Nth-order)
Example: sample = osc.wave_lerp();
Returns a sample from the wavetable, and increments the oscillator
phase.
Miscellaneous Functions
wave_sync(phase)
Example: osc2.wave_sync(osc1.t + 0.5);
Synchronizes the oscillator with the specified phase, and returns the
normalized phase.
Note: You can safely specify out or range (and even negative) values
here.
wave_inc()
Example: osc.wave_inc();
Increments the oscillator phase, and returns it.
Note: The interpolation functions automatically increment the phase.
wave_getdc()
wave_getrms()
Example: dc = osc.wave_getdc();
Calculates the DC/RMS value of the waveform.
Instance Variables
buf
Example: wavetbl_index = osc.buf;
The local memory index of the wavetable.
size
Example: wavetbl_size = osc.size;
The size of the wavetable, in samples.
t
Example: phase = osc.t;
The current phase [0.0..1.0) of the oscillator.
dt
Example: freq = osc.dt * srate;
The oscillator frequency, in seconds/sample.
wnd_rife_vincent1(i, n, order)-- Rife-Vincent class I
wnd_rife_vincent2(i, n, order, rho)-- Rife-Vincent class II
wnd_rife_vincent3(i, n, order)-- Rife-Vincent class III
wnd_pow_cos(i, n, alpha)-- Power-of-cosine
wnd_cos(i, n)-- Cosine
wnd_bohman(i, n)-- Bohman
wnd_gauss(i, n, sigma)-- Gaussian
wnd_gauss_conf(i, n, sigma)-- Confined Gaussian
wnd_gauss(i, n, sigma, p)-- Generalized normal
wnd_tukey(i, n, alpha)-- Tukey
wnd_planck_taper(i, n, eps)-- Planck-taper
wnd_kaiser(i, n, alpha)-- Kaiser
wnd_dolph_chebyshev(i, n, alpha)-- Dolph-Chebyshev
wnd_poisson(i, n, alpha)-- Poisson
wnd_bartlett_hann(i, n)-- Bartlett-Hann
wnd_planck_bessel(i, n, eps, alpha)-- Planck-Bessel
wnd_hann_poisson(i, n, alpha)-- Hann-Poisson
wnd_lanczos(i, n)-- Lanczos
wnd_cauchy(i, n, alpha)-- Cauchy
wnd_connes(i, n, alpha)-- Connes
Example: y = wnd_blackman(0, 1024);
Returns the normalized window function for integers i = [0..n-1],
n >= 16.
Miscellaneous Functions
_wnd_i0(x)
Example: y = _wnd_i0(3*$pi);
Approximates the zero-th order modified Bessel function of the first
kind.
_wnd_kaiser(i, n, alpha)
Example: y = _wnd_kaiser(0, 1024, 3);
Returns the non-normalized Kaiser window function. To normalize divide
by _wnd_i0($pi*alpha).
zdf_filter.jsfx-inc - 2nd-order zero-delay feedback state variable filter
zdf_hs(freq, q, gain)-- High-shelf
Example: lp.zdf_lp(1000, 0.7);
Sets up the filter for the specified cutoff frequency (in Hz), and Q
and gain factors, and returns the feedback precomputation factor (h).
(To convert from dB to gain: gain=10^(db/20).)
Note: In v20151024 the behavior of zdf_bp2() and zdf_ap() has been
changed in such a way that these functions are not backward
compatible. To convert code relying on the old behavior, replace
zdf_bp2(freq, bw) with zdf_bp(freq, zdf_bwtoq(bw)), and
zdf_ap(freq, bw) with zdf_ap(freq, zdf_bwtoq(bw)).
zdf_gain(gain)
Example: lp.zdf_lp(1000, 0.5); lp.zdf_gain(2.0);
Modifies the filter by applying the specified output gain.
Note: You should always first setup the filter, and then modify it. If
you change the filter frequency/Q afterwards, then this will reset the
gain to 1.0, and so you will have to modify it again.
zdf_setf(freq, q)
Example: lp.zdf_setf(1000, 0.7);
Sets up the specialized low-pass, high-pass, or band-pass filter.
Note: This works only with zdf_svf_lp(), zdf_svf_hp(), or zdf_svf_bp().
Filter Functions
zdf_svf(sample)
Example: output = lp.zdf_svf(input);
Sends a sample through the filter, and returns its output.
zdf_svf_multi(sample)
Example: output = lp.zdf_svf_multi(input);
Sends a sample through the filter, returns its output, and also stores
the individual low-pass, band-pass, and high-pass outputs.
zdf_svf_lp(sample)-- Low-pass
zdf_svf_hp(sample)-- High-pass
zdf_svf_bp(sample)-- Band-pass
Example: output = lp.zdf_svf_lp(input);
Specialized versions of zdf_svf(), each optimized for a specific
filter type.
Miscellaneous Functions
zdf_reset([input])
Example: lp.zdf_reset();
Resets the filter state to the specified input value, or to zero if
the value is omitted.
zdf_bwtoq(bw)
zdf_qtobw(q)
Example: q = zdf_bwtoq(2.0);
Converts bandwidth (in octaves) to Q factor, or vice versa.