Skip to content

Functions

Serge Zaitsev edited this page May 7, 2017 · 6 revisions

Instruments

Oscillators

Oscillators take frequency as an input and return an auto-advancing wave form as an output.

Oscillators should start with a zero amplitude on their first call. One exception is sqr() oscillator, which simply never returns zero amplitude. Oscillators should propagate NaN values, but should not reset their current phase, that's how the NaN signal can be used to reset the envelopers, but doesn't interrupt the smoothness of the wave signal.

Oscillators may take a negative frequency, in this case they should mirror the signal vertically. Oscillators should ignore zero frequency value and return 0 as an output.

sin(freq)

Sine oscillator. Plays a sine wave with the given frequency.

tri(freq)

Triangular oscillator. Plays a symmetric triangular wave with the given frequency.

saw(freq)

Saw-tooth oscillator. Plays a saw-tooth wave with the given frequency. The wave ramps upward and then sharply drops.

sqr(freq, pwm=0.5)

Square wave oscillator with controlled pulse width (duty cycle). The amplitude alternates with the given frequency. Default duty cycle is 50%. Dynamically changing the duty cycle is called "pulse width modulation", an effect used in 8-bit or 1-bit synthesizers.

Syntherizers

fm(freq, m1, v1, m2, v2, m3, v3)

FM-synthesizer with 3 operators, v1..v3 is operator strength, m1..m3 is operator multiplier. Operators 1 and 2 are connected in parallel, operator 3 is sequential to operator 1.

pluck(freq, decay=0.5, fill=r())

Karplus-Strong plucked string synthesizer. fill is a function used to prepare the initial values in the delay buffer, by default the buffer is filled with random values in the range [-1..1]. decay determines how fast the sound fades out. Lower values add more sustain, higher values damp the string faster. Decay must be in the range [0..1].

Samplers

piano(freq)

A very basic piano sound, built of only 3 samples played with different speed. Good enough to quickly play a tune.

tr808(drum, volume=1, pitch=0)

Roland TR808 samples. Drum is a number in the range [0..8]. There are helpful variables for drum names:

  • BD = 0 (bass drum)
  • SD = 1 (snare drum)
  • MT = 2 (middle tom)
  • MC = 3 (maracas)
  • RS = 4 (rimshot)
  • CL = 5 (clap)
  • CB = 6 (cowbell)
  • OH = 7 (open hat)
  • HH = 8 (hi hat)

pitch adjusts the playback speed of a given sample, allowing to alter the drum note.

user_sample(index, volume, pitch)

Create a directory called "samples" inside the working directory of Glitch. Omit it if you already have one. Then create a subdirectory for each sampled instrument. For example you have 3 samples of a cat meowing, they are called "meow.wav", "howl.wav" and "yowl.wav", and we want to call our instrument "cat". Put 3 WAV samples into "samples/cat" directory. Samples must be in WAV format, 44.1 kHz, 16-bit, mono. Alphabetically they will be sorted like this:

  • 0 = howl.wav
  • 1 = meow.wav
  • 2 = yowl.wav

Now if you open Glitch - you may use your samples as cat(0) to play howl.wav, cat(1) to play meow.wav and cat(2) to play yowl.wav. You can change the pitch of the sound with the third parameter, for example to meow major chord notes you may do the following:

n = seq(120, 0, 4, 7)
cat(0, 1, n)

Effects

Envelope

env(signal, attack, release, bend_attack, bend_release)

Modulates the amplitude of a signal with the given attack and release time. By default, attack time is 0.01 and release time is 10x of the attack time.

Default envelope is linear, however one may specify custom "bend" of the envelope curve. Bend should be in the range [0..1], where 0.5 means linear. The value of bend shows what would be the value of the envelope when 50% of the slope is done. For example env(z, 0.01, 0.2, 0.3, 0.3) means logarithmic attack and exponential decay. If only one bend value is specified - it's applied to both, attack and decay parts.

Envelope is reset as soon as the signal gets a NaN value. Attack and release parameters are "latched" at the beginning of the envelope and are evaluated only once per envelope.

Filters

lpf(signal, freq, q), hpf(signal, freq, q), bpf(signal, freq, q), bsf(signal, freq, q)

Apply a bi-quad filter to the given signal. Freq is a cutoff frequency and q is a filter's Q factor (often in the range [0..10]).

lpf is a low-pass filter, hpf is a high-pass filter, bpf is a band-pass filter and bsf is a band-stop filter (aka Notch filter).

Filters are reset when the input signal or any of the other parameters is NaN.

Delay

delay(signal, time, level, feedback)

A simple delay line with optional feedback. Level and feedback should be in the range [0..1]. Time is given in seconds. With little or no feedback the delay will sound like a slap-back echo. Use multiple delays and, optionally, modulate the time parameter to achieve chorus or reverberation effect.

Sequencers

seq(tempo, values...)

Returns values in a loop moving from one value to another at the given tempo. Tempo is a number of beats per minute, for example seq(120, 1, 2, 3) returns a sequencer of [1, 2, 3, 1, 2, 3, 1, 2...] and the values are advanced twice a second.

Seq() latches the value at the beginning of the beat, so seq(120, r()) returns random values that change twice a second.

Values can be pairs of numbers or tuples. If a value is a pair - the first part is a relative duration of the value. For example, seq(120, (0.5, C4), (1.5, G4)) returns a sequence of C4-G4-----C4-G4---- (dashes show how time increases, so you could see that "G4" lasts 3 times longer than "C4").

If a value is a tuple - the returned returned value slides between the given break points. For example seq(120, (1, C4, G4)) returns a slide from C4 to G4 twice a second.

loop(tempo, values...)

Loop behaves similarly to seq(), except for it doesn't latch the values and evalute them on every call. Loop is often slower than seq(), but allows to have nested sequences as values.

a(index, values...)

A building block for custom sequencers. Returns a value by its index. Value is evaluated every time it's returned. For example, a(0, 1, 2, 3) returns 1, a(2, A4, B4, C4) return C4 and a(10, 1, 2, 3) returns 2, because of the index overflow.

Utils

s(phase)

Returns sine wave amplitude at the given phase [0..1]. The resulting value is in the range [-1..1].

l(x)

Returns log2(x).

r(max)

Returns a random value in the range [0..max].

hz(note)

Returns note frequency. Works with microtones, too. We assume that zero note is A4.

scale(index, mode)

Returns note from the given scale at the given index.

mix(signals...)

Returns a mix of signals, normalizes the output signal and cuts off if needed.