tur/timing

stdlib/timing.tur
defstruct

BeatClock

(defstruct BeatClock [])

tracks beat time from a running sample counter.

defn

make-beat-clock

(make-beat-clock [bpm sample-rate])

create a new beat clock at the given BPM and sample rate.

bpmbeats per minute
sample-rateaudio sample rate in Hz
BeatClock starting at sample 0.

  (make-beat-clock 120.0 48000.0)
defn

clock->beats

(clock->beats [clock sample-offset])

convert a sample offset to beats.

clockBeatClock
sample-offsetsample count from clock start

Beats value.

defn

clock->samples

(clock->samples [clock beats])

convert a beat offset to samples.

clockBeatClock
beatsbeat count from clock start

Sample count.

defn

clock-advance

(clock-advance [clock samples])

advance the clock by a number of samples.

clockBeatClock to advance
samplesnumber of samples to advance

Updated BeatClock.

defn

clock-now

(clock-now [clock])

get the current beat time from a clock.

clockBeatClock

Current beat time as a float.

defstruct

ScheduledEvent

(defstruct ScheduledEvent [])

an event queued for future execution at a specific beat.

defstruct

EventQueue

(defstruct EventQueue [])

ordered queue of scheduled events.

defn

queue-new

(queue-new)

create an empty event queue.

Empty EventQueue.

defn

queue-push

(queue-push [queue event])

add an event to an event queue.

queueEventQueue to add to
eventScheduledEvent to enqueue

Updated EventQueue.

defn

queue-pop

(queue-pop [queue])

remove and return the earliest event from the queue.

queueEventQueue

ScheduledEvent or nil if the queue is empty.

defn

queue-peek

(queue-peek [queue])

look at the earliest event without removing it.

queueEventQueue

ScheduledEvent or nil if the queue is empty.

defstruct

PatternScheduler

(defstruct PatternScheduler [])

scheduler combining a beat clock with an event queue.

defn

scheduler-new

(scheduler-new [clock])

create a new pattern scheduler.

clockBeatClock to drive the scheduler

PatternScheduler with empty queues.

defn

schedule-pattern

(schedule-pattern [sched pattern at-beat callback])

schedule a pattern to be evaluated at a specific beat.

schedPatternScheduler
patternpattern to evaluate when triggered
at-beatbeat time to trigger
callbackfunction called with the pattern result

Event ID.

defn

schedule-param

(schedule-param [sched node-id pattern at-beat])

schedule a synth parameter update at a specific beat.

schedPatternScheduler
node-idsynth node ID to update
patternpattern to evaluate
at-beatbeat time to update

Event ID.

defn

process-events

(process-events [sched current-beat])

fire all events due at or before current-beat.

schedPatternScheduler
current-beatcurrent beat time

Number of events processed.

defn

process-samples

(process-samples [sched samples])

advance the clock by samples and fire due events.

schedPatternScheduler
samplesnumber of samples to advance

Number of events processed.

defn

audio-pattern

(audio-pattern [p clock])

wrap a pattern for audio-rate evaluation using sample offsets.

pNumPattern to evaluate
clockBeatClock for timing

Function: Samples -> float that evaluates p at the corresponding beat.

defn

mix-audio-patterns

(mix-audio-patterns [& patterns])

mix multiple audio-rate patterns by summation.

patternslist of audio-rate functions (Samples -> float)

Function: Samples -> float returning the sum of all patterns.

defn

audio-pattern-block

(audio-pattern-block [p clock block-size])

render a block of audio from a pattern.

pNumPattern
clockBeatClock
block-sizenumber of samples per block

Function: (output-buffer, sample-offset) -> void writing block-size samples.

defstruct

SampleAccuratePattern

(defstruct SampleAccuratePattern [])

pattern with sample-accurate timing state.

defn

make-sample-accurate

(make-sample-accurate [p clock])

wrap a pattern for sample-accurate evaluation.

ppattern to wrap
clockBeatClock

SampleAccuratePattern.

defn

sap-eval

(sap-eval [sap sample-offset])

evaluate a sample-accurate pattern at a sample offset.

sapSampleAccuratePattern
sample-offsetsample position to evaluate at

Pattern value at the corresponding beat.

defn

sap-update

(sap-update [sap new-sample])

advance a sample-accurate pattern to a new sample position.

sapSampleAccuratePattern
new-samplenew sample position

Updated SampleAccuratePattern with cached last value.

defn

samples->beats

(samples->beats [samples bpm sample-rate])

convert sample count to beats.

samplessample count
bpmtempo in BPM
sample-rateaudio sample rate

Beat time.

defn

beats->samples

(beats->samples [beats bpm sample-rate])

convert beat time to sample count.

beatsbeat time
bpmtempo in BPM
sample-rateaudio sample rate

Sample count.

defstruct

TempoTracker

(defstruct TempoTracker [])

tracks beat time across multiple tempo changes.

defn

tempo-tracker-new

(tempo-tracker-new [bpm sample-rate])

create a new tempo tracker at a constant initial BPM.

bpminitial BPM
sample-rateaudio sample rate

TempoTracker with no tempo changes.

defn

tempo-change

(tempo-change [tracker at-sample new-bpm])

record a tempo change at a specific sample position.

trackerTempoTracker
at-samplesample position of the tempo change
new-bpmnew BPM after the change

Updated TempoTracker.

defn

tempo-at

(tempo-at [tracker sample])

query the BPM at a specific sample position.

trackerTempoTracker
samplesample position to query

BPM at that sample.

defn

beats-at

(beats-at [tracker sample])

compute beat time at a sample accounting for tempo changes.

trackerTempoTracker
samplesample position to query

Beat time at that sample.

defn

schedule-at-sample

(schedule-at-sample [sched at-sample callback])

schedule a callback to fire at a specific sample position.

schedPatternScheduler
at-samplesample position to trigger
callbackfunction to call

Event ID.

defn

schedule-pattern-at-sample

(schedule-pattern-at-sample [sched at-sample pattern callback])

schedule a pattern evaluation at a sample position.

schedPatternScheduler
at-samplesample position to trigger
patternpattern to evaluate
callbackfunction called with the result

Event ID.