tur/polyrhythm

stdlib/polyrhythm.tur
defn

polymeter

(polymeter [ratio p])

play a pattern with a different effective meter.

ratiotime scaling ratio (2.0 = twice as slow, 0.5 = twice as fast)
ppattern to play with the scaled meter
Pattern<T> evaluated with time * ratio.

  ((polymeter 1.5 my-pat) 1.0)  ; reads my-pat at time 1.5
defn

polyrhythm

(polyrhythm [p1 len1 p2 len2])

play two patterns with independent cycle lengths.

p1first pattern
len1cycle length for the first pattern in beats
p2second pattern
len2cycle length for the second pattern in beats
Pattern<Tuple T1 T2> combining both patterns at their own tempos.

  ((polyrhythm kick-pat 3 snare-pat 2) 0.0)
defn

polyrhythm-n

(polyrhythm-n [& pattern-lengths])

play any number of patterns with independent cycle lengths.

pattern-lengthslist of [pattern length] tuples

Pattern<vec<T>> with one element per sub-pattern.

defn

nest

(nest [outer & [inner-time-scale 1.0])

evaluate a pattern of patterns: outer selects the inner, inner is evaluated.

outerPattern producing inner patterns
inner-time-scaletime scaling applied before evaluating outer (default: 1.0)

Pattern<T> that delegates to the pattern returned by outer.

defn

nest2

(nest2 [outer outer-time-scale inner-time-scale])

nested pattern with independent time scales for outer and inner.

outerPattern producing inner patterns
outer-time-scalescaling applied to time when querying outer
inner-time-scalescaling applied to time when querying the selected inner pattern

Pattern<T>.

defn

canon

(canon [p copies delay])

play delayed copies of a pattern stacked together.

ppattern to copy
copiesnumber of copies to create
delaytime offset between successive copies in beats (default: 0.25)
Pattern<vec<T>> with copies offset by delay * i for i in [0, copies).

  ((canon my-pat 3 0.25) 0.0)
defn

canon-n

(canon-n [p & delays])

play copies of a pattern with individually specified delays.

ppattern to copy
delayslist of per-copy delay offsets

Pattern<vec<T>>.

defn

spread

(spread [p copies spread-amount])

phase-spread copies of a pattern across a time window.

ppattern to spread
copiesnumber of copies
spread-amounttotal time window to spread across in beats (default: 1.0)
Pattern<vec<T>> with copies evenly spaced in [0, spread-amount).

  ((spread my-pat 4 1.0) 0.0)
defn

spread-n

(spread-n [p & spreads])

phase-spread copies with individually specified offsets.

ppattern to spread
spreadslist of per-copy phase offsets

Pattern<vec<T>>.

defn

stack-polymeter

(stack-polymeter [& pattern-meters])

stack patterns each running at their own meter.

pattern-meterslist of [pattern meter] tuples

Pattern<vec<T>> with one element per voice.

defn

stack-polyrhythm

(stack-polyrhythm [& pattern-lengths])

stack patterns each with their own cycle lengths.

pattern-lengthslist of [pattern length] tuples

Pattern<vec<T>>.

defn

timesig

(timesig [p & sigs])

apply a sequence of time signatures to a pattern.

ppattern to apply time signatures to
sigslist of [beats-per-bar sub-beats-per-bar] tuples

Pattern<T> with normalised time within each bar.

defn

make-timesig

(make-timesig [beats-per-bar sub-beats-per-bar])

construct a time signature specification.

beats-per-barbeats per bar (e.g., 4 for 4/4, 3 for 3/4)
sub-beats-per-barsub-divisions per beat (e.g., 4 for 16th notes)

A [beats-per-bar sub-beats-per-bar] pair.

defn

three-against-two

(three-against-two [p1 p2])

classic 3:2 polyrhythm.

p1pattern for the 3-beat group
p2pattern for the 2-beat group

Pattern<Tuple T1 T2>.

defn

four-against-three

(four-against-three [p1 p2])

4:3 polyrhythm.

p1pattern for the 4-beat group
p2pattern for the 3-beat group

Pattern<Tuple T1 T2>.

defn

five-against-four

(five-against-four [p1 p2])

5:4 polyrhythm.

p1pattern for the 5-beat group
p2pattern for the 4-beat group

Pattern<Tuple T1 T2>.

defn

five-against-three

(five-against-three [p1 p2])

5:3 polyrhythm.

p1pattern for the 5-beat group
p2pattern for the 3-beat group

Pattern<Tuple T1 T2>.

defn

seven-against-four

(seven-against-four [p1 p2])

7:4 polyrhythm.

p1pattern for the 7-beat group
p2pattern for the 4-beat group

Pattern<Tuple T1 T2>.

defn

polyrhythm-offset

(polyrhythm-offset [p1 len1 offset1 p2 len2 offset2])

polyrhythm with independent phase offsets per pattern.

p1first pattern
len1cycle length for first pattern
offset1phase offset for first pattern
p2second pattern
len2cycle length for second pattern
offset2phase offset for second pattern

Pattern<Tuple T1 T2>.

defn

polyrhythm-phased

(polyrhythm-phased [& patterns-and-phases])

polyrhythm with per-pattern length and phase.

patterns-and-phaseslist of [pattern length phase] triples

Pattern<vec<T>>.

defn

metric-mod

(metric-mod [p modulation-fn])

apply an arbitrary time mapping to a pattern.

ppattern to modulate
modulation-fnBeats -> Beats time remapping function

Pattern<T> evaluated at modulation-fn(time).

defn

metric-mod-linear

(metric-mod-linear [p & breakpoints])

apply a piecewise linear time mapping.

ppattern to modulate
breakpointslist of [input-time output-time] pairs

Pattern<T> with piecewise linear time mapping.

defn

lcm

(lcm [a b])

least common multiple of two integers.

afirst number
bsecond number

LCM of a and b.

defn

gcd

(gcd [a b])

greatest common divisor using the Euclidean algorithm.

afirst number
bsecond number

GCD of a and b.

defn

polyrhythm-alignment

(polyrhythm-alignment [& lengths])

find the beat at which all polyrhythm patterns realign.

lengthslist of cycle lengths
The LCM of all lengths (time at which all patterns complete together).

  (polyrhythm-alignment 3 4)  ; => 12
defn

cross-rhythm

(cross-rhythm [p1 len1 p2 len2 & [phase1 0 phase2 0])

polyrhythm with per-pattern phase offsets.

p1first pattern
len1cycle length for first pattern
p2second pattern
len2cycle length for second pattern
phase1phase offset for first pattern (default: 0)
phase2phase offset for second pattern (default: 0)

Pattern<Tuple T1 T2>.

defn

hocket

(hocket [p voices & [selector (cycle (range 0 voices))])

distribute a pattern's output across multiple voices.

psource pattern
voicesnumber of voices to distribute across
selectorIntPattern selecting the active voice (default: cycle 0..voices-1)
Pattern<vec<T>> where only the selected voice receives the pattern value.

  ((hocket my-pat 3) 0.0)