ztcl::audio pattern
Starts a pattern of steps in the sequencer of the audio engine.
ztcl::audio pattern TRACKS ?OPTION VALUE ...?
Description
Starts a pattern and gives its id, such as pattern3. The engine plays each hit at its exact frame. Its clock has 960 ticks for each quarter note, and its math of time uses integers only, so a pattern keeps its time over each length of play, and each platform gives the same frames.
TRACKS is a list of SOUND STEPS pairs, as a dict. SOUND is an id of ztcl::audio load, or a list SOUND ?-choke GROUP? ?-gain G? ?-pan P?. -choke puts the hits of the track in a choke group (1 to 65535), the same groups as play -choke, so an open and a closed hi-hat stop each other. -gain (0 to 1, 1 by default) scales each hit of the track, and -pan (-1 to 1, 0 by default) places it.
STEPS is a list with one element for each step: X (an accent, gain 1), x (a hit at the gain of -accent), . (a rest), a gain from 0 to 1, or a list {X TICKS}: the hit and its offset of 0 to 959 ticks after its place on the grid. A track of fewer steps than a bar repeats. A track can also hold the steps of several whole bars. The cycle of the pattern is its longest track, and one bar at least. A shorter track starts again at the start of each cycle.
-command runs SCRIPT from the event loop at the first step of each beat, with a dict appended: bar (0 or less in a count-in), beat, step (the step in the bar, from 1), accent (1 when a hit of the step is an accent), muted (1 in a muted bar of -gap), and frame. The script runs late by design. Use it for the screen, and position for the time.
At most 16 patterns play at once, each with at most 64 tracks of at most 256 steps. The tracks count in the memory limit of the interpreter. A pattern ends at stop, or after the bars of -loop; the voices of its hits play to their end.
Options
| Option | Type | Default | Description |
|---|---|---|---|
-tempo | number | 120 | The tempo in beats a minute, 20 to 400. |
-meter | text | 4/4 | The beats of a bar and the note of a beat. |
-subdivision | integer | 1 | The steps of a beat: 2 for eighths, 4 for sixteenths. |
-swing | number | 0 | Delays each second step by S of a step, 0 to 0.5. |
-accent | number | 0.6 | The gain of a step x, 0 to 1. |
-loop | integer | 0 | The bars to play, or 0 to play until stop. |
-gap | list | {} | Plays PLAY bars, then mutes MUTE bars, and again. |
-countin | integer | 0 | Plays BARS bars of the beat before bar 1, 0 to 16. |
-in | number | none | Starts the pattern MS milliseconds from now. |
-command | script | none | Runs SCRIPT at each beat, with the dict of the beat appended. |
-tempo BPM
The tempo in beats a minute, 20 to 400.
The beat is the note of the denominator of -meter, as musicians count: 7/8 at 210 is 210 eighth notes a minute. ztcl::audio tempo changes it at once, at a bar, or at a frame.
% ztcl::audio cget [ztcl::audio pattern [list $s x] -tempo 90] -tempo
90% ztcl::audio pattern [list $s x] -tempo 500
bad tempo "500": must be 20 to 400
-errorcode ZTCL AUDIO VALUE-meter N/D
The beats of a bar and the note of a beat.
N is 1 to 32, and D is 1, 2, 4, 8, 16, or 32. A beat has 3840 / D ticks.
% ztcl::audio cget [ztcl::audio pattern [list $s {X x x}] -meter 3/4] -meter
3/4% ztcl::audio pattern [list $s x] -meter 5/3
bad meter "5/3": must be N/D with N from 1 to 32 and D 1, 2, 4, 8, 16, or 32
-errorcode ZTCL AUDIO VALUE-subdivision N
The steps of a beat: 2 for eighths, 4 for sixteenths.
N is 1 to 16, and divides the ticks of a beat, so each step has a whole count of ticks.
% ztcl::audio cget [ztcl::audio pattern [list $s {X x x x x x x x}] -subdivision 2] -subdivision
2% ztcl::audio pattern [list $s x] -subdivision 7
bad subdivision "7": must divide the 960 ticks of a beat
-errorcode ZTCL AUDIO VALUE-swing S
Delays each second step by S of a step, 0 to 0.5.
For an off-step ratio r of a pair of steps, S = 2 × (r − 0.5): 0 is straight (r = 0.5), 0.333 a triplet feel (r = 0.667), and 0.5 a hard swing (r = 0.75). With -subdivision 4 the steps of a beat land at 0, 1/4 + S/4, 1/2, and 3/4 + S/4 of the beat. The delay is rounded to a whole tick.
% ztcl::audio cget [ztcl::audio pattern [list $s {X x x x x x x x}] -subdivision 2 -swing 0.333] -swing
0.333% ztcl::audio pattern [list $s x] -swing 0.6
bad swing "0.6": must be 0 to 0.5
-errorcode ZTCL AUDIO VALUE-accent G
The gain of a step x, 0 to 1.
A step X has the gain 1, and a step x the gain G. A count-in plays its first beat at gain 1 and the others at G.
% ztcl::audio cget [ztcl::audio pattern [list $s {X x}] -accent 0.4] -accent
0.4% ztcl::audio pattern [list $s x] -accent 2
bad accent "2": must be 0 to 1
-errorcode ZTCL AUDIO VALUE-loop BARS
The bars to play, or 0 to play until stop.
The pattern ends at the start of bar BARS + 1. The bars of -countin are not in the count.
% set p [ztcl::audio pattern [list $s x] -loop 1]; ztcl::audio render 2100; expr {$p in [ztcl::audio info]}
0% ztcl::audio pattern [list $s x] -loop -1
bad loop "-1": must be 0 to 1000000
-errorcode ZTCL AUDIO VALUE-gap {PLAY MUTE}
Plays PLAY bars, then mutes MUTE bars, and again.
A muted bar plays no hit, and posts its beats with muted 1. position gives muted 1 in it. An empty list plays each bar. PLAY is 1 to 1000, and MUTE 0 to 1000.
% set p [ztcl::audio pattern [list $s x] -gap {1 1}]; ztcl::audio render 2100; ztcl::audio position $p
bar 2 beat 1 step 1 muted 1% ztcl::audio pattern [list $s x] -gap {1 2 3}
bad gap "1 2 3": must be {} or {PLAY MUTE}
-errorcode ZTCL AUDIO VALUE-countin BARS
Plays BARS bars of the beat before bar 1, 0 to 16.
A count-in bar plays the sound of the first track on each beat. Its bars have the numbers 0 and less.
% set p [ztcl::audio pattern [list $s x] -countin 1]; ztcl::audio position $p
bar 0 beat 1 step 1 muted 0% ztcl::audio pattern [list $s x] -countin 20
bad count-in "20": must be 0 to 16
-errorcode ZTCL AUDIO VALUE-in MS
Starts the pattern MS milliseconds from now.
With neither -in nor -at, the pattern starts at the frame of now. -at FRAME starts it at that frame of the clock of the engine.
% set p [ztcl::audio pattern [list $s x] -in 1000]; ztcl::audio render 500; ztcl::audio position $p
bar 1 beat 1 step 1 muted 0% ztcl::audio pattern [list $s x] -at -1
bad frame "-1": must be 0 or more
-errorcode ZTCL AUDIO VALUE-command SCRIPT
Runs SCRIPT at each beat, with the dict of the beat appended.
The script runs from the event loop, so it needs vwait, update, or the event loop of Tk.
% ztcl::audio cget [ztcl::audio pattern [list $s x] -command {lappend ::b}] -command
lappend ::bEach script is a valid value.
Returns
The id of the pattern.
Examples
% ztcl::audio configure -device offline; set s [ztcl::audio load -wave square -freq 1500 -length 30]; ztcl::audio pattern [list $s {X x x x}] -tempo 96
pattern2
# Two beats of 120 BPM in 0.7 seconds at 48 kHz.
% set ::b {}; ztcl::audio pattern [list $s {X x}] -meter 2/4 -command {lappend ::b}; ztcl::audio render 700; update; set ::b
{bar 1 beat 1 step 1 accent 1 muted 0 frame 0} {bar 1 beat 2 step 2 accent 0 muted 0 frame 24000}
Errors
% ztcl::audio pattern [list sound1 {X y}]
bad step "y": must be X, x, ., a gain from 0 to 1, or {X TICKS}
-errorcode NONE
% ztcl::audio pattern [list sound1 {X x x x x x}]
bad track of 6 steps: a track longer than a bar has whole bars of 4 steps
-errorcode ZTCL AUDIO VALUE
% ztcl::audio pattern [list sound9 x]
sound "sound9" doesn't exist
-errorcode TCL LOOKUP AUDIO sound9
% ztcl::audio pattern [list sound1 [lrepeat 300 x]]
too many steps in a track: at most 256
-errorcode ZTCL AUDIO LIMIT
% ztcl::audio pattern
wrong # args: should be "ztcl::audio pattern tracks ?option value ...?"
-errorcode TCL WRONGARGS
Facts
- Capability
audio. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.6