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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
#ifndef UDO_TWIGS
#define UDO_TWIGS ##
/*
Twigs
Spectral transformer
This file is part of the SONICS UDO collection by Richard Knight 2024, 2025
License: GPL-2.0-or-later
http://1bpm.net
*/
#include "/pvs_fulltabproc.udo"
#include "/host_platform.udo"
#include "/lagdetect.udo"
#include "/interop.udo"
gitwgs_bufferL = -1
gitwgs_bufferR = -1
gitwgs_tpvfHandleL = -1
gitwgs_tpvfHandleR = -1
gitwgs_channels = -1
gitwgs_userstopped = 0
gitwgs_fnbinselection = -1
gitwgs_fnbintimeselection = -1
#include "/twigs/checkpointing.udo"
opcode twgs_getselectionindexes, ii, i
indexbin xin
ibins = ftlen(gitwgs_fnbinselection)
itablen = ftlen(tab_i(2, gitwgs_tpvfHandleL))
iframes = itablen / ibins
istartindex = (round(tab_i(indexbin, gitwgs_fnbintimeselection) * iframes) * ibins) + indexbin
iendindex = (round(tab_i(indexbin + ibins, gitwgs_fnbintimeselection) * iframes) * ibins) + indexbin
if (iendindex > itablen) then
iendindex = itablen
endif
;print tab_i(indexbin, gitwgs_fnbintimeselection)
;print istartindex
;print tab_i(indexbin + ibins, gitwgs_fnbintimeselection)
;print iendindex
xout istartindex, iendindex
endop
opcode twgs_failresponse, S, ij
icbid, istatus xin
xout sprintf("{\"cbid\":%d,\"status\":%d}", icbid, istatus)
endop
opcode twgs_refreshviewresponse, S, i
icbid xin
ifnampL tab_i 2, gitwgs_tpvfHandleL
ifnfreqL tab_i 3, gitwgs_tpvfHandleL
iduration tab_i 4, gitwgs_tpvfHandleL
if (gitwgs_channels == 2) then
ifnampR tab_i 2, gitwgs_tpvfHandleR
ifnfreqR tab_i 3, gitwgs_tpvfHandleR
else
ifnampR = -1
ifnfreqR = -1
endif
xout sprintf("{\"cbid\":%d,\"status\":1,\"tables\":[%d,%d,%d,%d],\"duration\":%f,\"undolevel\":%d}", icbid, ifnampL, ifnfreqL, ifnampR, ifnfreqR, iduration, gitwgs_checkpointstate)
endop
opcode twgs_clearbuffers, 0, pp
ihandles, ibuffers xin
if (ibuffers == 1) then
if (gitwgs_bufferL > 0 && ftexists(gitwgs_bufferL) == 1) then
ftfree gitwgs_bufferL, 0
gitwgs_bufferL = -1
endif
if (gitwgs_bufferR > 0 && ftexists(gitwgs_bufferR) == 1) then
ftfree gitwgs_bufferR, 0
gitwgs_bufferR = -1
endif
endif
if (ihandles == 1) then
if (gitwgs_tpvfHandleL > 0 && ftexists(gitwgs_tpvfHandleL) == 1) then
ftfree gitwgs_tpvfHandleL, 0
gitwgs_tpvfHandleL = -1
endif
if (gitwgs_tpvfHandleR > 0 && ftexists(gitwgs_tpvfHandleR) == 1) then
ftfree gitwgs_tpvfHandleR, 0
gitwgs_tpvfHandleR = -1
endif
gitwgs_channels = -1
endif
endop
opcode twgs_loadfile, ik, Sjj
Spath, ifftsize, ifftdecimation xin
kdone init 0
if (filevalid(Spath) != 1) then
iresponse = -1
goto complete
endif
ifilesr = filesr(Spath)
ifilechannels = filenchnls(Spath)
ilens = filelen(Spath)
ilen = round(ilens * ifilesr)
if (ilen >= gihost_max32bitftlen || ilens * sr >= gihost_max32bitftlen) then ; limitation with WASM Csound build at the moment
iresponse = -2
goto complete
endif
gitwgs_channels = ifilechannels
twgs_clearbuffers()
twgs_checkpoint_clear()
gitwgs_bufferL = ftgen(0, 0, -ilen, 1, Spath, 0, 0, 1)
if (gitwgs_channels == 2) then
gitwgs_bufferR = ftgen(0, 0, -ilen, 1, Spath, 0, 0, 2)
imono = 0
else
imono = 1
endif
if (sr != ifilesr) then ; different sr causes issues in table reading opcodes, convert..
inewlen = ilens * sr
ifnnewL ftgen 0, 0, -inewlen, -2, 0
if (imono == 0) then
ifnnewR ftgen 0, 0, -inewlen, -2, 0
endif
ktimek timeinstk
ikcycles = ilens * kr
if (ktimek == 1) then
kcount = 0
while (kcount < ikcycles) do
aposw linseg 0, ilens, inewlen - 1
aposr linseg 0, ilens, ilen - 1
asig table3 aposr, gitwgs_bufferL
tablew asig, aposw, ifnnewL
if (imono == 0) then
asig table3 aposr, gitwgs_bufferR
tablew asig, aposw, ifnnewR
endif
kcount += 1
od
else
kdone = 1
endif
ftfree gitwgs_bufferL, 1
gitwgs_bufferL = ifnnewL
if (imono == 0) then
ftfree gitwgs_bufferR, 1
gitwgs_bufferR = ifnnewR
endif
else
kdone = 1
endif
iresponse = 1
complete:
xout iresponse, kdone
endop
#include "/twigs/transforms.udo"
instr twgs_undo
icbid = p4
istatus twgs_undo
if (istatus < 0) then
Sresponse = twgs_failresponse(icbid)
else
Sresponse = twgs_refreshviewresponse(icbid)
endif
io_sendstring("callback", Sresponse)
turnoff
endin
instr twgs_stop
gitwgs_userstopped = 1
turnoff2 "twgs_play", 0, 1
turnoff
endin
instr twgs_playcomplete_response
icbid = p4
istatus = 0
if (gitwgs_userstopped == 1) then
istatus = 3
endif
io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":%d}", icbid, istatus))
turnoff
endin
instr twgs_playlag_response
icbid = p4
turnoff2 "twgs_play", 0, 0
io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":-1}", icbid))
turnoff
endin
instr twgs_play
icbid = p4
iplaytype = p5 ; 0 = from analysis, 1 = from buffer
iselectedonly = p6
istart = p7
iend = p8
iresynthtype = p9 ; 0 = pvsynth, 1 = pvsadsyn
gitwgs_userstopped = 0
io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1}", icbid))
ilen = tab_i(4, gitwgs_tpvfHandleL)
p3 = ilen * (iend - istart)
if (iplaytype == 0) then
if (iselectedonly == 1) then
ibinselection = gitwgs_fnbinselection
else
ibinselection = -1
endif
istartframe = tpvf_framecount(gitwgs_tpvfHandleL) * istart
aL, k_ tpvf_resynth gitwgs_tpvfHandleL, ibinselection, iresynthtype, 0, istartframe
aL dcblock aL
if (gitwgs_channels == 2) then
aR, k_ tpvf_resynth gitwgs_tpvfHandleR, ibinselection, iresynthtype, istartframe
aR dcblock aR
else
aR = aL
endif
else
apos lphasor 1
apos += (istart * ftlen(gitwgs_bufferL))
aL table3 apos, gitwgs_bufferL
if (gitwgs_channels == 2) then
aR table3 apos, gitwgs_bufferR
else
aR = aL
endif
endif
kpos linseg istart, p3, iend
chnset kpos, "twgs_playposratio"
klagging lagdetect 1.1
if (klagging == 1) then
schedulek("twgs_playlag_response", 0, 1, icbid)
endif
kreleasing init 0
ktimek timeinstk
iduration = (p3 * kr) ;+ (iextracycles / sr)
krelease release
if (kreleasing == 0 && (krelease == 1 || ktimek >= iduration)) then
kreleasing = 1
schedulek("twgs_playcomplete_response", 0, 1, icbid)
turnoff
endif
outs aL, aR
endin
instr twgs_resynth_response
icbid = p4
io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1}", icbid))
turnoff
endin
instr twgs_resynth
icbid = p4
Snext = strget(p5)
iresynthtype = p6 ; 0 = pvsynth, 1 = pvsadsyn
p3 = 3600
io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":5}", icbid))
twgs_clearbuffers(0, 1)
kdone init 0
gitwgs_bufferL, kdoneL tpvf_resynth_offline gitwgs_tpvfHandleL, iresynthtype
if (gitwgs_channels == 2) then
gitwgs_bufferR, kdoneR tpvf_resynth_offline gitwgs_tpvfHandleR, iresynthtype
else
kdoneR init 1
endif
if (kdoneL == 1 && kdoneR == 1) then
schedulek(Snext, 0, 1, icbid, 1)
turnoff
endif
endin
instr twgs_getbuffers
icbid = p4
if (gitwgs_channels == 2) then
Stables = sprintf("[%d,%d]", gitwgs_bufferL, gitwgs_bufferR)
else
Stables = sprintf("[%d]", gitwgs_bufferL)
endif
io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1,\"tables\":%s}", icbid, Stables))
turnoff
endin
instr twgs_savefile_response
icbid = p4
Spath = p5
io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1,\"path\":\"%s\"}", icbid, Spath))
turnoff
endin
instr twgs_savefile
icbid = p4
Spath = strget(p5)
ktimek timeinstk
idurations = ftlen(gitwgs_bufferL) / sr
ikcycles = idurations * kr
if (ktimek == 1) then
kcount init 0
while (kcount < ikcycles) do
apos lphasor 1
aL table apos, gitwgs_bufferR
if (gitwgs_channels == 1) then
fout Spath, 14, aL
else
aR table3 apos, gitwgs_bufferL
fout Spath, 14, aL, aR
endif
kcount += 1
od
else
schedulek("twgs_savefile_response", 0, 1, icbid, Spath)
turnoff
endif
endin
instr twgs_loadfilecomplete_response
icbid = p4
ifftsize tab_i 0, gitwgs_tpvfHandleL
ifftdecimation tab_i 1, gitwgs_tpvfHandleL
ifnampL tab_i 2, gitwgs_tpvfHandleL
ifnfreqL tab_i 3, gitwgs_tpvfHandleL
iduration tab_i 4, gitwgs_tpvfHandleL
if (gitwgs_channels == 2) then
ifnampR tab_i 2, gitwgs_tpvfHandleR
ifnfreqR tab_i 3, gitwgs_tpvfHandleR
else
ifnampR = -1
ifnfreqR = -1
endif
Sresponse = sprintf("{\"cbid\":%d,\"status\":1,\"channels\":%d,\"tables\":[%d,%d,%d,%d],\"bins\":%d,\"binseltab\":%d,\"bintimeseltab\":%d,\"duration\":%f,\"undolevel\":%d,\"fftdecim\":%d,\"sr\":%d}", icbid, gitwgs_channels, ifnampL, ifnfreqL, ifnampR, ifnfreqR, ifftsize / 2, gitwgs_fnbinselection, gitwgs_fnbintimeselection, iduration, gitwgs_checkpointstate, ifftdecimation, sr)
io_sendstring("callback", Sresponse)
turnoff
endin
instr twgs_loadfile_analysis
icbid = p4
ifftsize = p5
ifftdecimation = p6
itablen = tpvf_tablen(ftlen(gitwgs_bufferL), ifftsize, ifftdecimation)
if (itablen >= gihost_max32bitftlen) then
io_sendstring("callback", twgs_failresponse(icbid, -2))
turnoff
endif
kdoneL, gitwgs_tpvfHandleL tpvf_analyse gitwgs_bufferL, ifftsize, ifftdecimation
ftfree gitwgs_bufferL, 1
if (gitwgs_channels == 2) then
kdoneR, gitwgs_tpvfHandleR tpvf_analyse gitwgs_bufferR, ifftsize, ifftdecimation
ftfree gitwgs_bufferR, 1
else
istatusR = 1
kdoneR init 1
endif
if (gitwgs_fnbinselection > 0) then
ftfree gitwgs_fnbinselection, 0
endif
gitwgs_fnbinselection ftgen 0, 0, -(ifftsize / 2), -2, 0
if (gitwgs_fnbintimeselection > 0) then
ftfree gitwgs_fnbintimeselection, 0
endif
gitwgs_fnbintimeselection ftgen 0, 0, -ifftsize, -2, 0
if (kdoneL == 1 && kdoneR == 1) then
schedule("twgs_loadfilecomplete_response", 0, 1, icbid)
turnoff
endif
endin
instr twgs_loadftable
icbid = p4
ifftsize = p5
ifftdecimation = p6
ifnL = p7
ifnR = p8
iclearaudiobuffers = p9
if (ifnL <= 0 || ftexists(ifnL) == 0 || (ifnR > 0 && ftexists(ifnR) == 0)) then
io_sendstring("callback", twgs_failresponse(icbid, -1))
turnoff
endif
twgs_clearbuffers(1, iclearaudiobuffers)
twgs_checkpoint_clear()
gitwgs_bufferL = ifnL
if (ifnR > 0) then
gitwgs_bufferR = ifnR
gitwgs_channels = 2
else
gitwgs_channels = 1
endif
schedule("twgs_loadfile_analysis", 0, 1, icbid, ifftsize, ifftdecimation)
turnoff
endin
instr twgs_loadfile
icbid = p4
Spath = strget(p5)
ifftsize = p6
ifftdecimation = p7
istatus, kdone twgs_loadfile Spath
if (istatus < 0) then
io_sendstring("callback", twgs_failresponse(icbid, istatus))
turnoff
else
if (kdone == 1) then
schedulek("twgs_loadfile_analysis", 0, 1, icbid, ifftsize, ifftdecimation)
turnoff
endif
endif
endin
#end
|