~nasser/8fl

8fl/core.fnl -rw-r--r-- 8.0 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;; TODO bad name for file?

(local seq (require :seq))

(require-macros :core-macros)

;; notes

(local note-names [:c- :c# :d- :d# :e- :f- :f# :g- :g# :a- :a# :b-])
(local note-map { :names {} :values {} })

(for [i 0 119]
  (let [note (. note-names (+ 1 (% i (# note-names))))
        octave (math.floor (/ i (# note-names)))
        note-string (string.format "%s%d" note octave)]
    (tset note-map.names note-string i)
    (tset note-map.values i note-string)))

(fn get-note-value [note]
  (match (type note)
    :number note
    :string (. note-map.names note)))

(fn get-note-name [note]
  (match (type note)
    :string note
    :number (. note-map.values note)))

(fn scale [intervals root]
  (let [root (or root :c-4)
        root (string.lower root)
        out (match (type root)
              :number get-note-value
              :string get-note-name)]
    (resumable
     (var root (get-note-value root))
     (coroutine.yield (out root))
     (each [x (seq.cycle intervals)]
       (set root (+ root x))
       (if (< root 128)
         (coroutine.yield (out root))
         (lua "return"))))))

(fn contains [haystack needle]
  (string.find (string.lower haystack) (string.lower needle)))

;; TODO cache
(fn track-lookup-name [name]
  (let [name (if (= 1 (string.find name :-)) (string.sub name 2) name)]
    (each [i track (ipairs (. (renoise.song) :tracks))]
      (if (contains track.name name)
        (lua "return i")))))

(fn track-lookup [idx]
  (match (type idx)
    :number (math.abs idx)
    :string (track-lookup-name idx)))

;; TODO cache
(fn instrument-lookup-name [name]
  (each [i instr (ipairs (. (renoise.song) :instruments))]
    (if (contains instr.name name)
      (lua "return (i-1)"))))

(fn instrument-lookup [idx]
  (match (type idx)
    :number idx
    :string (instrument-lookup-name idx)))

(fn samples [instrument-number fragment]
  (resumable
   (let [song (renoise.song)
         instrument-number (instrument-lookup instrument-number)
         instrument-idx (+ instrument-number 1)
         instrument (. song.instruments instrument-idx)]
     (each [_ sample (pairs instrument.samples)]
       (if (or (not fragment)
               (and fragment (contains sample.name fragment)))
         (let [note (get-note-name (. sample.sample_mapping.note_range 1))]
           (coroutine.yield
            (string.format "%s%02X" note instrument-number))))))))

(tset _G :_CC_VALUES {})

;; TODO cache
(fn maybe-install-device [name]
    (when (= nil (. _G._CC_VALUES name))
      (each [i device (ipairs (renoise.Midi.available_input_devices))]
            (when (contains device name)
              (let [m {}]
                   (tset _G._CC_VALUES name m)
                   (tset _G._CC_VALUES device m)
                   (renoise.Midi.create_input_device
                    device
                    (fn [[s cc value]]
                        (when (= s 176)
                          (status (.. "MIDI CC " device " " cc " " value))
                          (tset m cc value)))))))))


(fn cc [device cc]
    (maybe-install-device device)
    (if cc
        (or (. _G._CC_VALUES device cc) 0)
        #(or (. _G._CC_VALUES device $) 0)))

(fn cc% [device cc]
    (maybe-install-device device)
    (if cc
        (let [v (. _G._CC_VALUES device cc)] (if v (/ v 127) 0))
        #(let [v (. _G._CC_VALUES device $)] (if v (/ v 127) 0))))

(fn cc%1- [device cc]
    (maybe-install-device device)
    (if cc
        (let [v (. _G._CC_VALUES device cc)] (- 1 (if v (/ v 127) 0)))
        #(let [v (. _G._CC_VALUES device $)] (- 1 (if v (/ v 127) 0)))))

(fn cc? [device cc]
    (maybe-install-device device)
    (if cc
        (let [v (. _G._CC_VALUES device cc)] (if v (> v 0) false))
        #(let [v (. _G._CC_VALUES device $)] (if v (> v 0) false))))

(fn or-default [s d]
  (match (# s)
    0 d
    _ s))

(fn str-sub-or-default [in from to default]
  (let [s (string.sub in from to)]
    (match (# s)
        0 default
        _ s)))

(fn sub-or-default [in from to default]
  (match (type in)
    :string (str-sub-or-default in from to default)
    _ (resumable
        (each [i (seq.iter in)]
          (str-sub-or-default i from to default)))))

(fn splice [s i new len]
  (let [new (if len (string.sub new 1 len) new)
        before (string.sub s 1 (- i 1))
        after (string.sub s (+ i (# new)))]
    (string.format "%s%s%s" before new after)))

(fn expand [s with]
  (string.format "%s%s" s (string.sub with (+ 1 (# s)))))

(local blank-line "---............")

(fn splice-line [line i new len]
  (let [filler (string.sub blank-line 1 (math.max (+ i len -1) (# line)))]
    (splice (expand line filler) i new len)))

(fn splice-lines [lines i new len]
  (let [new (seq.cycle new)]
    (resumable
     (each [line (seq.iter lines)]
       (coroutine.yield (splice-line line i (new) len))))))

; (instrument :c-402)   ;; -> 2
; (instrument :c-402 9) ;; -> :c-409
; (instrument seq 9)    ;; -> (... 09 09 09 ...)

(fn note [in new]
  (if new
    (splice-lines in 1 new 3)
    (or-default (string.sub in 1 3) "---")
    ; (sub-or-default in 1 3 "---")
    ))

(fn desugar-instrument [val]
  (match (type val)
    :number (string.format "%02X" val)
    :string (match (instrument-lookup val)
              nil val
              n (string.format "%02X" n))))

(fn desugar-value [val]
  (match (type val)
    :number (string.format "%02X" val)
    :string val))

(fn fxfmt [name val]
  (.. name (desugar-value val)))

(fn instrument [line new]
  (if new
    (splice-lines line 4 (seq.map new desugar-instrument) 2)
    (or-default (string.sub line 4 5) "..")))

(fn volume [line new]
  (if new
    (splice-lines line 6 (seq.map new desugar-value) 2)
    (or-default (string.sub line 6 7) "..")))

(fn pan [line new]
  (if new
    (splice-lines line 8 (seq.map new desugar-value) 2)
    (or-default (string.sub line 8 9) "..")))

(fn delay [line new]
  (if new 
    (splice-lines line 10 (seq.map new desugar-value) 2)
    (or-default (string.sub line 10 11) "..")))

(fn fx [line new]
  (if new
    (splice-lines line 12 new 4)
    (or-default (string.sub line 12 16) "....")))

(fn fxn [line new]
  (if new
    (splice-lines line 12 new 2)
    (or-default (string.sub line 12 13) "..")))

(fn fxa [line new]
  (if new
    (splice-lines line 14 new 2)
    (or-default (string.sub line 14 15) "..")))

(fn rest? [s]
  (match (type s)
    :string (= 1 (s:find "^-*$"))
    _ false))

(fn off? [s]
  (= 1 (: (string.upper s) :find "^OFF")))

(fn note? [s]
  (and (not (off? s)) (not (rest? s))))

(fn balance [columns]
  (let [columns* []]
    ;; init columns*
    ;; one empty array per column in columns
    (while (< (# columns*) (# columns))
      (table.insert columns* []))
    ;; populate and balance columns
    (var balanced false)
    (var longest -1)
    (while (not balanced)
      ;; grow all columns except longest columns
      (each [i column (ipairs columns)]
        (when (not= (# (. columns* i)) longest)
          (each [x (-> column (seq.take (- 511 longest)))]
              (table.insert (. columns* i) x))))
        ;; compute new longest
        (each [_ c (ipairs columns*)]
          (let [len (# c)]
            (when (> len longest)
              (set longest len))))
        ;; balance columns
        (set balanced true)
        (each [_ c (ipairs columns*)]
          (when (not= longest (# c))
            (set balanced false)
            (lua :break))))
    columns*))

;; rle
(fn & [...]
    (let [in (seq.iter [...])]
         (resumable
          (while true
            (var count (in))
            (if (= nil count)
                (lua :break)
                (let [value (in)]
                     (while (> count 0)
                       (coroutine.yield value)
                       (set count (- count 1)))))))))

{ : get-note-value : get-note-name : scale : or-default : str-sub-or-default
  : sub-or-default : splice : expand : splice-line : splice-lines : note
  : instrument : volume : pan : delay : fx : fxn : fxa : rest? : off? : balance
  : fxfmt
  : track-lookup : instrument-lookup : note?
  : desugar-instrument
  : samples
  : cc : cc% : cc%1- : cc?
  : & }