summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJake Zerrer <him@jakezerrer.com>2025-11-26 15:10:20 -0500
committerJake Zerrer <him@jakezerrer.com>2025-12-02 13:28:33 -0500
commit3c626a88ec37c0ab4a87e6730556ac4d4e1eb90e (patch)
tree794ab9adbaba6553466f3ad6fde6e7945da7695c
parent2d421d7354d2be61c4a474896e163d48b4640d63 (diff)
mini-notation-compiler: change default note parser
-rw-r--r--src/unheard/strudel/mini_notation_compiler.clj52
1 files changed, 39 insertions, 13 deletions
diff --git a/src/unheard/strudel/mini_notation_compiler.clj b/src/unheard/strudel/mini_notation_compiler.clj
index 9e7b5cb..6f469bb 100644
--- a/src/unheard/strudel/mini_notation_compiler.clj
+++ b/src/unheard/strudel/mini_notation_compiler.clj
@@ -43,7 +43,26 @@
(compile \"c@3\")
=> (elongate 3 :c)"
(:refer-clojure :exclude [compile])
- (:require [clojure.string :as str]))
+ (:require [clojure.string :as str]
+ [unheard.midi.notes :as notes]))
+
+(defn- note-name->var
+ "Convert a note name string to the corresponding var from unheard.midi.notes.
+
+ Supports both '#' and 's' for sharps (e.g., 'c#4' and 'cs4').
+ Returns a symbol that refers to the var, e.g., 'unheard.midi.notes/c4.
+
+ Examples:
+ (note-name->var \"c4\") => unheard.midi.notes/c4
+ (note-name->var \"cs4\") => unheard.midi.notes/cs4
+ (note-name->var \"c#4\") => unheard.midi.notes/cs4
+ (note-name->var \"e5\") => unheard.midi.notes/e5"
+ [s]
+ (let [;; Replace # with s for sharp notation
+ normalized (str/replace s #"#" "s")
+ ;; Create fully qualified symbol
+ sym (symbol "unheard.midi.notes" normalized)]
+ sym))
(defn- parse-number [s]
"Parse a number, returning either a long or ratio."
@@ -231,7 +250,7 @@
Arguments:
s - The mini-notation string to compile
get-value - Function of one argument that converts note names to values
- (defaults to keyword)
+ (defaults to note-name->var which maps to unheard.midi.notes)
Returns a quoted expression that can be evaluated in the context
where unheard.cycles functions are available.
@@ -239,22 +258,29 @@
Newlines in the input string are automatically converted to spaces
to allow for multi-line pattern notation.
+ By default, note names are mapped to vars from unheard.midi.notes:
+ \"c4\" => unheard.midi.notes/c4
+ \"c#4\" or \"cs4\" => unheard.midi.notes/cs4
+ \"e5\" => unheard.midi.notes/e5
+
Examples:
- (compile \"c e g\" keyword)
- => (l :c :e :g)
+ (compile \"c4 e4 g4\")
+ => (l unheard.midi.notes/c4 unheard.midi.notes/e4 unheard.midi.notes/g4)
- (compile \"c [e g] b\" keyword)
- => (l :c (l :e :g) :b)
+ (compile \"c4 [e4 g4] b4\")
+ => (l unheard.midi.notes/c4 (l unheard.midi.notes/e4 unheard.midi.notes/g4) unheard.midi.notes/b4)
- (compile \"<c e g b>*2\" keyword)
- => (rate 2 (f :c :e :g :b))
+ (compile \"<c4 e4 g4 b4>*2\")
+ => (rate 2 (f unheard.midi.notes/c4 unheard.midi.notes/e4 unheard.midi.notes/g4 unheard.midi.notes/b4))
- (compile \"[c,e,g] [d,f,a]\" keyword)
- => (l (p :c :e :g) (p :d :f :a))
+ (compile \"[c4,e4,g4] [d4,f4,a4]\")
+ => (l (p unheard.midi.notes/c4 unheard.midi.notes/e4 unheard.midi.notes/g4)
+ (p unheard.midi.notes/d4 unheard.midi.notes/f4 unheard.midi.notes/a4))
- (compile \"<a b\\nc d>\" keyword)
- => (f :a :b :c :d)"
- ([s] (compile s keyword))
+ ;; Using custom get-value function
+ (compile \"c e g\" keyword)
+ => (l :c :e :g)"
+ ([s] (compile s note-name->var))
([s get-value]
(let [normalized (str/replace s #"\n" " ")
elements (parse-sequence (str/trim normalized) get-value)]