summaryrefslogtreecommitdiff
path: root/src/unheard/cycles.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/unheard/cycles.clj')
-rw-r--r--src/unheard/cycles.clj50
1 files changed, 42 insertions, 8 deletions
diff --git a/src/unheard/cycles.clj b/src/unheard/cycles.clj
index b134ad7..acf1900 100644
--- a/src/unheard/cycles.clj
+++ b/src/unheard/cycles.clj
@@ -59,9 +59,32 @@
[ratio node]
{:v node :rate ratio :type :rate})
+(defn elongate
+ "Elongation modifier: gives an element temporal weight.
+
+ When used within a list, an elongated element takes up proportionally
+ more time than other elements based on its weight. Elements without
+ elongation have a default weight of 1.
+
+ Equivalent to TidalCycles/Strudel '@' operator.
+
+ Examples:
+ (l (elongate 2 :a) :b :c) - :a takes twice as long as :b or :c
+ (l :x (elongate 3 :y) :z) - :y takes 3x as long as :x or :z"
+ [weight node]
+ {:v node :weight weight :type :elongate})
+
(defn scalar? [x]
(not (and (map? x) (:type x))))
+(defn get-weight
+ "Returns the weight of a node. Elongated nodes have their specified weight,
+ all other nodes have a default weight of 1."
+ [node]
+ (if (and (map? node) (= :elongate (:type node)))
+ (:weight node)
+ 1))
+
(defn gcd [a b]
(if (zero? b) a (recur b (mod a b))))
@@ -88,6 +111,11 @@
(= :rate (:type node))
;; Rate doesn't change the cycle count - it just compresses/expands time
;; The parent sees the same cycle count as the child
+ (compute-cycle (:v node))
+
+ (= :elongate (:type node))
+ ;; Elongation doesn't change the cycle count - it just affects time division
+ ;; The parent sees the same cycle count as the child
(compute-cycle (:v node))))
(defn unfold-node [node start end iteration]
@@ -98,14 +126,16 @@
(= :l (:type node))
(let [children (:v node)
- n (count children)
- slice-size (/ duration n)]
- (mapcat (fn [i child]
- (let [child-start (+ start (* i slice-size))
- child-end (+ start (* (inc i) slice-size))]
+ weights (map get-weight children)
+ total-weight (reduce + weights)
+ weight-offsets (reductions + 0 weights)]
+ (mapcat (fn [i child weight]
+ (let [child-start (+ start (* duration (/ (nth weight-offsets i) total-weight)))
+ child-end (+ start (* duration (/ (nth weight-offsets (inc i)) total-weight)))]
(unfold-node child child-start child-end iteration)))
- (range n)
- children))
+ (range (count children))
+ children
+ weights))
(= :f (:type node))
(let [children (:v node)
@@ -133,7 +163,11 @@
(+ start (* i child-cycle-duration))
(+ start (* (inc i) child-cycle-duration))
i))
- (range num-child-cycles)))))))
+ (range num-child-cycles))))
+
+ (= :elongate (:type node))
+ ;; Elongate just wraps a child - unfold the child with the same time bounds
+ (unfold-node (:v node) start end iteration))))
(defn unfold
"Unfolds a pattern tree into concrete time intervals.