summaryrefslogtreecommitdiff
path: root/src/unheard
diff options
context:
space:
mode:
Diffstat (limited to 'src/unheard')
-rw-r--r--src/unheard/strudel/mini_notation_compiler.clj85
1 files changed, 48 insertions, 37 deletions
diff --git a/src/unheard/strudel/mini_notation_compiler.clj b/src/unheard/strudel/mini_notation_compiler.clj
index f3f597e..9e7b5cb 100644
--- a/src/unheard/strudel/mini_notation_compiler.clj
+++ b/src/unheard/strudel/mini_notation_compiler.clj
@@ -55,12 +55,13 @@
(declare parse-sequence)
(declare parse-element)
-(defn- parse-atom [s]
- "Parse a single atom (note, number, or rest)."
+(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 (keyword s)))
+ :else (get-value s)))
(defn- apply-modifiers [expr modifiers]
"Apply modifiers to an expression.
@@ -124,7 +125,7 @@
:else
(recur (rest chars) groups (conj current c) depth))))))
-(defn- parse-group [s open-char close-char combinator]
+(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))
@@ -138,20 +139,21 @@
;; 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 groups)
- ;; Zip groups together element-by-element and wrap in parallel
- max-length (apply max (map count parsed-groups))
- zipped (for [i (range max-length)]
- (let [elements (keep #(nth (vec %) i nil) parsed-groups)]
- (if (= 1 (count elements))
- (first elements)
- (cons 'p elements))))]
- (if (= 1 (count zipped))
- (first zipped)
- (cons 'f zipped)))
+ 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)]
+ (let [elements (parse-sequence inner-content get-value)]
(if (= 1 (count elements))
(first elements)
(cons combinator elements))))]
@@ -185,44 +187,52 @@
:else
(recur (rest chars) tokens (conj current c) depth))))))
-(defn- parse-parallel [token]
+(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 parts)]
+ elements (map #(parse-element % get-value) parts)]
(cons 'p elements))
- (parse-element token)))
+ (parse-element token get-value)))
-(defn- parse-element [token]
+(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)
+ (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)
+ (parse-group token \< \> 'f get-value)
;; Contains comma - parallel (p)
(str/includes? token ",")
- (parse-parallel token)
+ (parse-parallel token get-value)
;; Token with modifiers
:else
(let [[base modifiers] (parse-token-with-modifiers token)]
- (apply-modifiers (parse-atom base) modifiers))))
+ (apply-modifiers (parse-atom base get-value) modifiers))))
-(defn- parse-sequence [s]
+(defn- parse-sequence [s get-value]
"Parse a space-separated sequence."
(let [tokens (tokenize s)]
- (map parse-element tokens)))
+ (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.
@@ -230,23 +240,24 @@
to allow for multi-line pattern notation.
Examples:
- (compile \"c e g\")
+ (compile \"c e g\" keyword)
=> (l :c :e :g)
- (compile \"c [e g] b\")
+ (compile \"c [e g] b\" keyword)
=> (l :c (l :e :g) :b)
- (compile \"<c e g b>*2\")
+ (compile \"<c e g b>*2\" keyword)
=> (rate 2 (f :c :e :g :b))
- (compile \"[c,e,g] [d,f,a]\")
+ (compile \"[c,e,g] [d,f,a]\" keyword)
=> (l (p :c :e :g) (p :d :f :a))
- (compile \"<a b\\nc d>\")
+ (compile \"<a b\\nc d>\" keyword)
=> (f :a :b :c :d)"
- [s]
- (let [normalized (str/replace s #"\n" " ")
- elements (parse-sequence (str/trim normalized))]
- (if (= 1 (count elements))
- (first elements)
- (cons 'l elements))))
+ ([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)))))