tur/pattern

stdlib/pattern.tur
defn

pat-at

(pat-at [p time])

evaluate a pattern at a specific time.

ppattern to evaluate
timetime in beats
Pattern value at the given time.

  (pat-at (const 5) 2.3)  ; => 5
defn

pat-start

(pat-start [p])

evaluate a pattern at time 0.

ppattern to evaluate
Pattern value at time 0.

  (pat-start (cycle 10 20 30))  ; => 10
defn

const

(const [value])

constant pattern that always returns the same value.

valuethe value to return regardless of time
Pattern<T> that always returns value.

  (const 0.5)
defn

silence

(silence)

pattern that always returns none (for option types).

Pattern<option<T>> that always returns none.

defn

cycle

(cycle [& values])

cycle through values one per beat.

valuesone or more values to cycle through
Pattern<T> advancing to the next value each beat.

  ((cycle 1 2 3) 0.0)  ; => 1
  ((cycle 1 2 3) 1.0)  ; => 2
defn

fast-cycle

(fast-cycle [rate & values])

cycle through values at a faster rate.

ratecycles per whole (default: 1 = same as cycle)
valuesvalues to cycle through
Pattern<T> cycling rate times faster than cycle.

  ((fast-cycle 2 1 2 3) 0.5)  ; evaluates as if time=1.0
defn

phase

(phase [& [cycle_length 1.0])

return the fractional phase within a cycle.

cycle_lengthlength of the cycle in beats (default: 1.0)
NumPattern returning phase in [0, cycle_length).

  ((phase 2.0) 1.5)  ; => 1.5
defmacro

P

(P [& exprs])

macro shorthand: (P x) => (const x), (P x y ...) => (cycle x y ...).

(P 1 2 3)  ; same as (cycle 1 2 3)
  (P 5)      ; same as (const 5)
defn

rand-choice

(rand-choice [& values])

randomly pick a value from the supplied list on each evaluation.

valuesone or more values to choose from
Pattern<T> returning a random element each time it is evaluated.

  ((rand-choice 1 2 3) 0.0)  ; => 1, 2, or 3 at random
defn

rand-int

(rand-int [min max])

pattern returning a random integer in [min, max) each evaluation.

minminimum value (inclusive)
maxmaximum value (exclusive)

NumPattern of random integers.

defn

rand-float

(rand-float)

pattern returning a random float in [0, 1) each evaluation.

NumPattern of random floats.

defn

rand-range

(rand-range [min max])

pattern returning a random float in [min, max) each evaluation.

minminimum value (inclusive)
maxmaximum value (exclusive)

NumPattern.

defn

identity

(identity)

pattern that returns its time argument unchanged.

NumPattern returning the raw time value.

defn

time-pat

(time-pat)

pattern returning the current time value.

NumPattern that is the identity function over time.

defn

beat-number

(beat-number)

pattern returning the integer beat number.

NumPattern returning floor(time).

  ((beat-number) 2.7)  ; => 2.0
defn

beat-fraction

(beat-fraction)

pattern returning the fractional part of the current beat.

NumPattern returning time - floor(time).

  ((beat-fraction) 2.7)  ; => 0.7
defn

alt

(alt [a b])

alternate between two values on even/odd beats.

avalue on even beats
bvalue on odd beats
Pattern<T> alternating between a and b.

  ((alt 1 2) 0.0)  ; => 1
  ((alt 1 2) 1.0)  ; => 2
defn

if-pat

(if-pat [condition a b])

choose between two values based on a boolean pattern.

conditionBoolean pattern or time -> bool function
avalue when condition is true
bvalue when condition is false

Pattern<T>.

defn

every-n

(every-n [n])

true when the beat number is a multiple of n.

nmodulo base
BoolPattern that is true when floor(time) mod n == 0.

  ((every-n 4) 4.0)  ; => true
defn

when-mod

(when-mod [n & [offset 0])

true when beat number mod n equals a given offset.

nmodulo base
offsettarget remainder (default: 0)

BoolPattern.

defn

when

(when [cond p & [zero 0])

gate a pattern through a boolean condition.

condBoolean pattern controlling when p fires
ppattern to gate
zerovalue returned when cond is false (default: 0)
Pattern<T>.

  (when (every-n 2) (const 1) 0)
defn

every

(every [n p & [zero 0])

play a pattern only every n beats.

nbeat interval
ppattern to play
zerovalue between triggers (default: 0)
Pattern<T>.

  (every 4 my-pat)
defn

pattern-length

(pattern-length [p])

attempt to determine the cycle length of a pattern.

nil (cannot be determined statically in general).

defn

pattern-constant?

(pattern-constant? [p])

check whether a pattern is constant.

false (cannot be determined statically in general).

defstruct

PatternWithMeta<T>

(defstruct PatternWithMeta<T> [])

pattern bundled with introspection metadata.

defn

with-meta

(with-meta [pattern & [name "" description "" tags [])

attach metadata to a pattern.

patternthe underlying pattern
namehuman-readable name (default: "")
descriptiondescription of the pattern (default: "")
tagscategorisation tags (default: [])

PatternWithMeta<T>.

defn

pattern-of

(pattern-of [p])

extract the underlying pattern from a PatternWithMeta value.

pPatternWithMeta<T>

Pattern<T>.