-
Notifications
You must be signed in to change notification settings - Fork 15
/
pat.orc
340 lines (250 loc) · 6.65 KB
/
pat.orc
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
;; internal pat format
;; header: beat-len
;; cmd args
;; 0 group len
;; 1 play: start dur val
;; 2 rest: start dur _
;;
;; 0 4, 0 3, 1 0 0 , 1 1 0
;; 0 3 (new group, len 3)
;; 0 play
;; 1 rest
opcode grow_array, i[], i[]
in_array[] xin
ilen = lenarray(in_array)
iout_array[] init ilen * 2
indx = 0
while (indx < ilen) do
iout_array[indx] = in_array[indx]
indx += 1
od
xout iout_array
endop
opcode pat_len, i, S
Spat xin
indx = 0
icount = 0
imode = 0
ibrackets = 0
ialts = 0
while (indx < strlen(Spat)) do
Stmp = strsub(Spat, indx, indx + 1)
if (strcmp(Stmp, "[") == 0) then
if((ialts + ibrackets) == 0 && imode == 1) then
icount += 1
endif
ibrackets += 1
imode = 0
elseif (strcmp(Stmp, "]") == 0) then
ibrackets -= 1
if((ialts + ibrackets) == 0) then
icount += 1
endif
imode = 0
elseif (strcmp(Stmp, "<") == 0) then
if((ialts + ibrackets) == 0 && imode == 1) then
icount += 1
endif
ialts += 1
imode = 0
elseif (strcmp(Stmp, ">") == 0) then
ialts -= 1
if((ialts + ibrackets) == 0) then
icount += 1
endif
imode = 0
elseif (strcmp(Stmp, " ") == 0) then
if((ialts + ibrackets) == 0 && imode == 1) then
icount += 1
endif
imode = 0
else
imode = 1
endif
indx += 1
od
icount += imode ;; in case was reading number value at end
xout icount
endop
opcode pat_subpat, S, Si
Spat, indx xin
istart = indx
indx += 1
Sstart = strsub(Spat, istart, istart + 1)
ibrackets = strcmp(Sstart, "[") == 0 ? 1 : 0
ialts = strcmp(Sstart, "<") == 0 ? 1 : 0
istrlen = strlen(Spat)
while ((ibrackets + ialts) > 0 && indx < istrlen) do
Stmp = strsub(Spat, indx, indx + 1)
if (strcmp(Stmp, "[") == 0) then
ibrackets += 1
elseif (strcmp(Stmp, "]") == 0) then
ibrackets -= 1
elseif (strcmp(Stmp, "<") == 0) then
ialts += 1
elseif (strcmp(Stmp, ">") == 0) then
ialts -= 1
endif
indx += 1
od
Sout = (ibrackets > 0) ? "error" : strsub(Spat, istart + 1, indx - 1)
xout Sout
endop
opcode pat_compile, i[], S
Spat xin
ipat[] init 64
icount = pat_len(Spat)
istrlen = strlen(Spat)
;print icount
;; header
ipat[0] = icount
ibeatindx = 1
ipatindx = 1 + icount
indx = 0
istart = 0
imode = 0
igrpcount = 0
while (indx <= istrlen) do
Stmp = strsub(Spat, indx, indx + 1)
if (imode == 0) then
if(strcmp(Stmp, " ") == 0) then
;; ignore
elseif(strcmp(Stmp, "[") == 0) then
ilen = pat_len(pat_subpat(Spat, indx))
;print ilen
if(ipatindx + 2 > lenarray(ipat)) then
ipat = grow_array(ipat)
endif
;; record top-level beat to speed up performance
if(igrpcount == 0) then
ipat[ibeatindx] = ipatindx
ibeatindx += 1
endif
ipat[ipatindx] = 0 ;; cmd: new group
ipat[ipatindx + 1] = ilen ;; cmd: new group
ipatindx += 2
igrpcount += 1
elseif (strcmp(Stmp, "]") == 0) then
igrpcount -= 1
else
istart = indx
imode = 1
endif
elseif (imode == 1) then
if (strcmp(Stmp, " ") == 0 || strcmp(Stmp, "[") == 0 ||
strcmp(Stmp, "]") == 0 || indx == istrlen) then
Sval = strsub(Spat, istart, indx)
if(ipatindx + 3 > lenarray(ipat)) then
ipat = grow_array(ipat)
endif
;; record top-level beat to speed up performance
if(igrpcount == 0) then
ipat[ibeatindx] = ipatindx
ibeatindx += 1
endif
if(strcmp(Sval, "~") == 0) then
ipat[ipatindx] = 2 ;; cmd: rest
ipat[ipatindx + 1] = 1
ipat[ipatindx + 2] = 0
ipatindx += 3
else
ival = strtod(Sval)
ipat[ipatindx] = 1 ;; cmd: play
ipat[ipatindx + 1] = 1
ipat[ipatindx + 2] = ival
ipatindx += 3
endif
indx -= 1
imode = 0
endif
endif
indx += 1
od
xout ipat
endop
opcode pat_perf, 0, i[]Siooo
ipat[], Sinstr, iamp, ioct, ibase, itick xin
if(itick <= 0) then
itick = now_tick()
endif
if(itick % 4 == 0) then
ibeat = itick / 4
/*print ibeat*/
ibeat = ibeat % ipat[0]
/*print ipat[0]*/
ipatindx = ipat[1 + ibeat]
/*print ipatindx*/
igrpcount[] init 16
igrpcount[0] = 0
idurs[] init 16
idurs[0] = 1
igrpindx = 0
istart = 0
while (istart < 1 && ipatindx < lenarray(ipat)) do
icmd = ipat[ipatindx]
if(icmd == 0) then
ipatdur = idurs[igrpindx]
igrpindx += 1
igrplen = ipat[ipatindx + 1]
idurs[igrpindx] = ipatdur / igrplen
igrpcount[igrpindx] = igrplen
ipatindx += 2
elseif (icmd == 1 || icmd == 2) then
idur = idurs[igrpindx] * ipat[ipatindx + 1]
;;print istart, idur
if(icmd == 1) then
schedule(Sinstr, beats(istart), beats(idur),
in_scale(ioct, ibase + ipat[ipatindx + 2]), iamp)
endif
ipatindx += 3
istart += idur
if(igrpindx > 0) then
icount = igrpcount[igrpindx] - 1
if(icount == 0) then
igrpindx -= 1
else
igrpcount[igrpindx] = icount
endif
endif
else
prints("Error: Unknown command %d\n", icmd)
ipatindx += 3
endif
od
endif
endop
opcode pat_perf, 0, SSiooo
Spat, Sinstr, iamp, ioct, ibase, itick xin
ipat[] pat_compile Spat
pat_perf(ipat, Sinstr, iamp, ioct, ibase, itick)
endop
/*
print pat_len("1 0 2")
print pat_len("[ 1 0 ] [3 4 5]")
printarray(pat_compile("[ 1 0 ] 3 2 [3 4 5]"))
printarray(pat_compile("0 1 2"))
print(pat_len("[ 1 0 ] 3 2 [3 4 5]"))
prints pat_subpat("[ 1 0 ] [3 4 5] ", 9)
prints pat_subpat("[ 1 0 ] <3 [4 5] 5> ", 9)
print pat_len("[ 1 <0 4>] <3 [4 5 ] 5> 4")
prints strsub("Test", 0,1)
prints "TEST"
*/
/*prints(strsub("[ 1 0 ] [3 4 5] ", 10, 15))*/
start("ReverbMixer")
gipat1[] = pat_compile("0 0 [~ 5 ~ 5] [4 4] ")
instr P1
/*Spat = "0 0 [_ 5 _ 5] [4 4]"*/
/*pat_perf(Spat, "Bass", ampdbfs(-12), -1, 0)*/
/*xchnset("Bass.pan", random:i(0, 1))*/
/*xchnset("Bass.pan", 0.5)*/
pat_perf(gipat1, "Bass", ampdbfs(-12), -1, 0)
Spat = "[4 4] [~ ~ 4 ~] [4 3 7 4] ~"
pat_perf(Spat, "Bass", ampdbfs(-12), ((p4 << 2) # p4) % 3)
Spat = "8 ~ 8 ~ [9 9 ] ~ 8 ~"
pat_perf(Spat, "Bass", ampdbfs(-12), -1 + ((p4 << 1) # p4) % 3)
hexdirt("2", "808:1", ampdbfs(-21))
hexdirt("8", "bd", ampdbfs(-9))
endin
printarray(pat_compile("0 [~ ~ ~ 5] [4 4] 2"))
;clear_instr("P1")