1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
(ns unheard.strudel.mini-notation-compiler
"Compiler for Strudel mini-notation to unheard.cycles code.
Note: This namespace excludes clojure.core/compile to avoid naming conflict.
Translates Strudel's text-based pattern notation into equivalent
unheard.cycles expressions.
Supported syntax:
- Spaces: Sequential events (l combinator)
- []: Subdivision/grouping (l combinator)
- <> : Alternating events (f combinator)
- ,: Parallel/simultaneous events (p combinator)
- *N: Speed multiplication (rate modifier)
- /N: Speed division (rate modifier)
- @N: Elongation (elongate modifier)
- !N: Replication (rep modifier)
- ~: Rest literal (becomes :r)
Not yet supported:
- ?: Probabilistic removal
- |: Random selection
- (): Euclidean rhythms
See: https://strudel.cc/learn/mini-notation/
Examples:
(compile \"c e g\")
=> (l :c :e :g)
(compile \"c [e g] b\")
=> (l :c (l :e :g) :b)
(compile \"<c e g>\")
=> (f :c :e :g)
(compile \"[c,e,g] [d,f,a]\")
=> (l (p :c :e :g) (p :d :f :a))
(compile \"c*2\")
=> (rate 2 :c)
(compile \"c@3\")
=> (elongate 3 :c)"
(:refer-clojure :exclude [compile])
(:require [clojure.string :as str]))
(defn- parse-number [s]
"Parse a number, returning either a long or ratio."
(if (str/includes? s "/")
(let [[num denom] (str/split s #"/")]
(/ (parse-long num) (parse-long denom)))
(parse-long s)))
(declare parse-sequence)
(declare parse-element)
(defn- parse-atom [s get-value]
"Parse a single atom (note, number, or rest).
Uses get-value function to convert note names to values."
(cond
(= s "~") :r
(re-matches #"\d+(/\d+)?" s) (parse-number s)
:else (get-value s)))
(defn- apply-modifiers [expr modifiers]
"Apply modifiers to an expression.
Modifiers is a map with keys: :rate, :elongate, :rep
Order: elongate/rep first (innermost), then rate (outermost)"
(cond-> expr
(:elongate modifiers) (#(list 'elongate (:elongate modifiers) %))
(:rep modifiers) (#(list 'rep (:rep modifiers) %))
(:rate modifiers) (#(list 'rate (:rate modifiers) %))))
(defn- parse-token-with-modifiers [token]
"Parse a token that may have modifiers like *2, /3, @2, !3"
(let [;; Extract modifiers (note: /N is division, not a rational number in this context)
;; Check for *N/M first (multiplication with rational)
rate-match (re-find #"\*(\d+(?:/\d+)?)" token)
;; Only match /N if it's NOT part of *N/M (no preceding * and digits)
div-match (when-not rate-match
(re-find #"/(\d+)" token))
elong-match (re-find #"@(\d+(?:/\d+)?)" token)
rep-match (re-find #"!(\d+)" token)
;; Remove modifiers to get base token
base (-> token
(str/replace #"\*\d+(?:/\d+)?" "")
(str/replace #"/\d+" "")
(str/replace #"@\d+(?:/\d+)?" "")
(str/replace #"!\d+" ""))
modifiers (cond-> {}
rate-match (assoc :rate (parse-number (second rate-match)))
div-match (assoc :rate (list '/ 1 (parse-long (second div-match))))
elong-match (assoc :elongate (parse-number (second elong-match)))
rep-match (assoc :rep (parse-long (second rep-match))))]
[base modifiers]))
(defn- split-on-comma [s]
"Split string on commas at depth 0 (not inside nested brackets).
Returns vector of substrings."
(loop [chars (seq s)
groups []
current []
depth 0]
(if (empty? chars)
(conj groups (str/join current))
(let [c (first chars)]
(cond
;; Track bracket depth
(or (= c \[) (= c \<) (= c \())
(recur (rest chars) groups (conj current c) (inc depth))
(or (= c \]) (= c \>) (= c \)))
(recur (rest chars) groups (conj current c) (dec depth))
;; Comma at depth 0 - split here
(and (= c \,) (zero? depth))
(if (seq current)
(recur (rest chars) (conj groups (str/join current)) [] depth)
(recur (rest chars) groups [] depth))
:else
(recur (rest chars) groups (conj current c) depth))))))
(defn- parse-group [s open-char close-char combinator get-value]
"Parse a bracketed group with a specific combinator."
;; Find the matching closing bracket, then extract modifiers after it
(let [close-idx (.lastIndexOf s (int close-char))
brackets-part (subs s 0 (inc close-idx))
modifiers-part (subs s (inc close-idx))
;; Extract modifiers from the part AFTER the closing bracket
[_ modifiers] (parse-token-with-modifiers modifiers-part)
;; Extract content between brackets
inner-content (subs brackets-part 1 close-idx)
;; Special handling for angle brackets with commas (polymeter)
result (if (and (= combinator 'f) (str/includes? inner-content ","))
;; Split on commas and parse each group
;; Create parallel composition of forked groups
(let [groups (split-on-comma inner-content)
parsed-groups (map #(parse-sequence % get-value) groups)
;; Wrap each group in fork (unless single element)
forked-groups (map (fn [group]
(let [elements (vec group)]
(if (= 1 (count elements))
(first elements)
(cons 'f elements))))
parsed-groups)]
(if (= 1 (count forked-groups))
(first forked-groups)
(cons 'p forked-groups)))
;; Normal handling without commas
(let [elements (parse-sequence inner-content get-value)]
(if (= 1 (count elements))
(first elements)
(cons combinator elements))))]
(apply-modifiers result modifiers)))
(defn- tokenize [s]
"Tokenize a mini-notation string, respecting nested brackets."
(loop [chars (seq s)
tokens []
current []
depth 0]
(if (empty? chars)
(if (seq current)
(conj tokens (str/join current))
tokens)
(let [c (first chars)]
(cond
;; Track bracket depth
(or (= c \[) (= c \<) (= c \())
(recur (rest chars) tokens (conj current c) (inc depth))
(or (= c \]) (= c \>) (= c \)))
(recur (rest chars) tokens (conj current c) (dec depth))
;; Space separates tokens only at depth 0
(and (= c \space) (zero? depth))
(if (seq current)
(recur (rest chars) (conj tokens (str/join current)) [] depth)
(recur (rest chars) tokens [] depth))
:else
(recur (rest chars) tokens (conj current c) depth))))))
(defn- parse-parallel [token get-value]
"Parse comma-separated elements into parallel structure."
(if (str/includes? token ",")
(let [parts (str/split token #",")
elements (map #(parse-element % get-value) parts)]
(cons 'p elements))
(parse-element token get-value)))
(defn- parse-element [token get-value]
"Parse a single element (may be atom, group, or have modifiers)."
(cond
;; Square brackets - subdivision (l)
;; Check if starts with [ (may have modifiers at end)
(str/starts-with? token "[")
(parse-group token \[ \] 'l get-value)
;; Angle brackets - alternation (f)
;; Check if starts with < (may have modifiers at end)
(str/starts-with? token "<")
(parse-group token \< \> 'f get-value)
;; Contains comma - parallel (p)
(str/includes? token ",")
(parse-parallel token get-value)
;; Token with modifiers
:else
(let [[base modifiers] (parse-token-with-modifiers token)]
(apply-modifiers (parse-atom base get-value) modifiers))))
(defn- parse-sequence [s get-value]
"Parse a space-separated sequence."
(let [tokens (tokenize s)]
(map #(parse-element % get-value) tokens)))
(defn compile
"Compile a Strudel mini-notation string to unheard.cycles code.
Takes a string in Strudel mini-notation and a function to convert
note names to values.
Arguments:
s - The mini-notation string to compile
get-value - Function of one argument that converts note names to values
(defaults to keyword)
Returns a quoted expression that can be evaluated in the context
where unheard.cycles functions are available.
Newlines in the input string are automatically converted to spaces
to allow for multi-line pattern notation.
Examples:
(compile \"c e g\" keyword)
=> (l :c :e :g)
(compile \"c [e g] b\" keyword)
=> (l :c (l :e :g) :b)
(compile \"<c e g b>*2\" keyword)
=> (rate 2 (f :c :e :g :b))
(compile \"[c,e,g] [d,f,a]\" keyword)
=> (l (p :c :e :g) (p :d :f :a))
(compile \"<a b\\nc d>\" keyword)
=> (f :a :b :c :d)"
([s] (compile s keyword))
([s get-value]
(let [normalized (str/replace s #"\n" " ")
elements (parse-sequence (str/trim normalized) get-value)]
(if (= 1 (count elements))
(first elements)
(cons 'l elements)))))
|