~nasser/8fl

8fl/patterns.fnl -rw-r--r-- 5.7 KiB
2934f9e8Ramsey Nasser Use negative track numbers for fx columns a month ago
                                                                                
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
(local core (require :core))
(local seq (require :seq))

(require-macros :core-macros)

(fn rests [n]
  "A vector of n rests"
  (-> :--- (seq.repeat n) seq.vec))

(fn or-rests [in]
  (match (type in)
    :number (rests in)
    _ in))

(fn fill-steps [in n xs]
  (resumable
    (let [in (or-rests in)
          xs (seq.iter xs)]
      (var k n)
      (each [i (seq.iter in)]
        (if (= k 1)
          (do 
            (set k n)
            (coroutine.yield (xs)))
          (do
            (set k (- k 1))
            (coroutine.yield i)))))))

(fn fill-notes [in xs]
  "Fill `in` sequence with notes from `xs`
  
* `in` -- sequence
* `xs` -- cycle to populate
* length -- same as `in`"
  (let [xs (seq.cycle xs)]
    (resumable
     (let [in (or-rests in)]
       (each [i (seq.iter in)]
         (if (core.rest? i)
           (coroutine.yield i)
           (let [x (xs)]
             (if (= :function (type x))
               (coroutine.yield (x i))
               (coroutine.yield x)))))))))

(fn fill-rests [in xs]
  (resumable
    (let [in (or-rests in)
          xs (seq.iter xs)]
      (each [i (seq.iter in)]
        (if (core.rest? i)
          (let [x (xs)]
             (if (= :function (type x))
               (coroutine.yield (x i))
               (coroutine.yield x)))
          (coroutine.yield i))))))

(fn fill-random [in density xs]
  (resumable
    (let [in (or-rests in)
          xs (seq.cycle xs)]
      (each [i (seq.iter in)]
        (if (< (math.random) density)
          (coroutine.yield (xs))
          (coroutine.yield i))))
    ))

(fn filter-notes [xs]
  (resumable
    (each [x (seq.iter xs)]
      (if (and (not (core.off? x)) (not (core.rest? x)))
        (coroutine.yield x)))))

;; TODO make this work without vec, if possible
(fn invert [xs]
  (resumable
    (let [xs (seq.vec xs)
          notes (seq.cycle (filter-notes xs))]
      (each [x (seq.iter xs)]
        (if (core.rest? x)
          (coroutine.yield (notes))
          (coroutine.yield :---))))))

;; https://codepen.io/Dafuseder/pen/WEqOVw
;; TODO falls into loop when xs < #pulses
(fn fill-euclid [xs in pulses]
  ""
  (let [xs (seq.cycle xs)]
    (resumable
      (let [in (seq.vec (or-rests in))
            steps (# in)
            in (seq.iter in)] 
        (var prev nil)
        (for [i 0 (- steps 1)]
          (let [x (math.floor (* i (/ pulses steps)))]
            (coroutine.yield (if (= x prev) (in) (xs)))
            (set prev x)))))))

(fn make-euclid [in pulses]
  (let [xs (seq.cycle :x)]
    (resumable
      (let [in (seq.vec (or-rests in))
            steps (# in)
            in (seq.iter in)] 
        (var prev nil)
        (for [i 0 (- steps 1)]
          (let [x (math.floor (* i (/ pulses steps)))]
            (coroutine.yield (if (= x prev) (in) (xs)))
            (set prev x)))))))

;; TODO move to seq
(fn rotate [in rotation]
  (resumable
    (let [_seq (seq.vec in)]
      (for [_ 1 rotation]
        (table.insert _seq 1 (table.remove _seq (# _seq))))
      (each [s (seq.iter _seq)]
        (coroutine.yield s)))))

(fn product [xs rhs]
  (let [xs (seq.cycle xs)]
    (resumable
      (each [r (seq.iter (seq.iter rhs))]
        (if (core.rest? r)
          (coroutine.yield r)
          (coroutine.yield (xs)))))))

;; (fn product [a b]
;;   (let [b (seq.iter b)]
;;     (resumable
;;       (each [x (seq.iter a)]
;;         (if (core.rest? x)
;;           (coroutine.yield x)
;;           (coroutine.yield (b x)))))))

;; tidal (amwaj)

;; TODO rename in to xs
(fn fit [in n]
  (let [in (seq.vec (or-rests in))
        in-count (# in)
        m (math.max 1 (math.floor (/ n in-count)))
        rem (- n (* m in-count))
        m-1 (- m 1)]
    (resumable
      (var n n)
      (each [line (seq.iter in)]
        (match (type line)
               :function (do (coroutine.yield (line)) (for [i 1 (math.min n m-1)] (coroutine.yield :---)))
               :table    (each [x (seq.iter (fit line m))] (coroutine.yield x))
               _         (do (coroutine.yield line) (for [i 1 (math.min n m-1)] (coroutine.yield :---))))
        (set n (- n m))
        (if (<= n 0) (lua :break)))
      (for [i 1 rem] (coroutine.yield :---))
      )))

(fn choice [xs]
  (. xs (math.random (# xs))))

(fn choice-of [xs] "choose a random element from xs" #(choice (seq.vec xs)))

(fn sum [...]
  (local coros (-> [...] (seq.map seq.iter) seq.vec))
  (resumable
   (while true
     (var val nil)
     (each [_ c (ipairs coros)]
       (match (c)
         (where x (= nil x)) (lua :return)
         (where x (= nil val) (core.rest? x)) (set val x)
         (where x (not (core.rest? x))) (set val x)))
     (coroutine.yield val))))

(fn follow [in xs]
  (let [xs (seq.cycle xs)
        in (seq.iter in)]
    (resumable
     (var was-note false)
     (each [i in]
       (if was-note
         (do (coroutine.yield (xs))
             (set was-note false))
         (do (coroutine.yield i)
             (when (core.note? i)
               (set was-note true))))))))

(fn before [in xs]
  (let [xs (seq.cycle xs)
        in (seq.iter in)]
    (resumable
     (var last nil)
     (each [i in]
       (if last
         (if (core.note? i)
           (coroutine.yield (xs))
           (coroutine.yield last)))
       (set last i))
     (coroutine.yield last))))

(fn melody [xs pat]
  (let [pat (if (not= (type pat) :table) [pat] pat)
        max (math.max (unpack pat))
        vs (-> (seq.cycle xs) (seq.take max) seq.vec)]
    (resumable
     (each [p (seq.iter pat)]
      (coroutine.yield (seq.nth vs (math.fmod p (+ 1 (# vs)))))))))

;; TODO get rid
(fn ins! [in new]
  (seq.map in #(if (core.rest? $) $ (core.instrument $ new))))

{ : rests : or-rests : fill-steps : fill-notes : fill-rests : fill-random
  : filter-notes : invert : fill-euclid : make-euclid : rotate : product : fit : choice
  : choice-of : ins! : sum : follow : before : melody }