aboutsummaryrefslogtreecommitdiff
path: root/site/app/twine/twine.csd
blob: 1f31907ee606cf9d15184ba371d43828155348a1 (plain)
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
<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 64
nchnls = 2
0dbfs = 1
seed 0

#define ECP_NORECORDING ##

;#include "scss/elasticlip_sequencer.udo"
#include "/scss/elasticlip.udo"
;#include "scss/mixer/base.udo"
#include "/interop.udo"
#include "/host_platform.udo"
#include "/bussing.udo"
#include "/table_tools.udo"
#include "/lagdetect.udo"

#include "/twigs/twigs.udo"
#include "/twist/twist.udo"



instr twine_automaterun
endin

opcode _twine_playrecurse, i, ikiioj
	iplayfn, ktimes, imaxchannels, iplayfnsize, index, iusedchanfn xin
	if (iusedchanfn == -1) then
		iusedchanfn ftgentmp 0, 0, -imaxchannels, -2, 0
	endif

	iclipindex tab_i index, iplayfn
	istart tab_i index + 1, iplayfn
	iduration tab_i index + 2, iplayfn
	ichannel tab_i index + 3, iplayfn
	istartoffset tab_i index + 4, iplayfn
	tabw_i 1, ichannel, iusedchanfn

	if (ktimes >= istart && ktimes <= istart + iduration) then
		if (iclipindex < 0) then
			aL, aR subinstr sprintf("twinescript%d", -iclipindex), iduration, istartoffset
		else
			aL, aR subinstr "ecp_playback_tst", -1, iclipindex, iduration, istartoffset  ; sort out channels
		endif
		bus_mix sprintf("mxchan%d", ichannel), aL, aR
	endif

	if (index + 5 < iplayfnsize) then
		i_ _twine_playrecurse iplayfn, ktimes, imaxchannels, iplayfnsize, index + 5, iusedchanfn
	endif
	xout iusedchanfn
endop

opcode _twine_channelrecurse, 0, io
	iusedchanfn, index xin
	if (tab_i(index, iusedchanfn) == 1) then
		a_ subinstr sprintf("twine_channel%d", index)
	endif

	if (index + 1 < ftlen(iusedchanfn)) then
		_twine_channelrecurse iusedchanfn, index + 1
	endif
endop

opcode twine_playback, aa, ikii
	iplayfn, ktimes, imaxchannels, iplayfnsize xin
	gitwst_tf_state[0] = 1
	gitwst_tf_state[1] = 1
	iusedchanfn _twine_playrecurse iplayfn, ktimes, imaxchannels, iplayfnsize
	_twine_channelrecurse iusedchanfn
	a_ subinstr "twine_automaterun"
	aL, aR bus_read "twine_master"
	kamp chnget "twine_masteramp"
	aL *= kamp
	aR *= kamp
	xout aL, aR
endop

instr twine_masteroutput
	aL, aR bus_read "twine_master"
	outs aL, aR
endin

instr twine_createtable
	icbid = p4
	ilen = p5
	ifn ftgen 0, 0, -ilen, -2, 0
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"table\":%d}", icbid, ifn))
	turnoff
endin


instr twine_removetable
	icbid = p4
	ifn = p5
	ftfree ifn, 0
	io_sendstring("callback", sprintf("{\"cbid\":%d}", icbid))
	turnoff
endin


instr twine_removeclip
	icbid = p4
	iclipindex = p5
	ecp_removeclip iclipindex
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1}", icbid))
	turnoff
endin


instr twine_render_complete
	icbid = p4
	ifnoutL = p5
	ifnoutR = p6
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1,\"fnL\":%d,\"fnR\":%d}", icbid, ifnoutL, ifnoutR))
	turnoff
endin


instr twine_convertsr_complete
	icbid = p4
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1}", icbid))
	turnoff
endin

instr twine_convertsr
	icbid = p4
	iclipindex = p5
	isourcesr = p6
	ifndata = giecp_fnclips[iclipindex]
	ifnL tab_i 0, ifndata
	ifnR tab_i 1, ifndata
	if (ifnR > 0) then
		ifnnewL, ifnnewR, kdone tab_samplerateconvert ifnL, ifnR, 1, isourcesr
	else
		ifnnewL, kdone tab_samplerateconvert ifnL, 1, isourcesr
		ifnnewR = 0
	endif
	tabw_i ifnnewL, 0, ifndata
	tabw_i ifnnewR, 1, ifndata
	if (kdone == 1) then
		schedulek("twine_convertsr_complete", 0, 1, icbid)
		turnoff
	endif
endin

instr twine_render
	icbid = p4
	ifn = p5
	imaxchannels = p6
	iduration = p7
	itarget = p8 ; 0 = ftable, 1 = file,
	ifnsize = p9
	ifreeplaybacktable = p11
	if (itarget == 1) then
		Spath = strget(p10)
		ifnoutL = 0
		ifnoutR = 0
	else
		Spath = ""
		ioutlen = iduration * sr
		if (ioutlen >= gihost_max32bitftlen) then ; limitation with WASM Csound build at the moment
			io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":-2}", icbid))
			turnoff
		endif
		ifnoutL ftgen 0, 0, -ioutlen, -2, 0
		ifnoutR ftgen 0, 0, -ioutlen, -2, 0
	endif

	if (ifreeplaybacktable == 1) then
		ftfree ifn, 1
	endif
	p3 = 3600
	
	iblocks = 100
	
	ikcycles = round(iduration * kr)
	
	if (ikcycles < iblocks) then
		ikcyclesperblock = ikcycles
	else
		ikcyclesperblock = round(ikcycles / iblocks)
	endif
	ktotalcount init 0
	klastpercent init 100

	if (ktotalcount < ikcycles) then
		kcount = 0
		while (kcount < ikcyclesperblock) do
			aL, aR twine_playback ifn, ktotalcount / kr, imaxchannels, ifnsize
			if (itarget == 0) then
				apos lphasor 1
				tablew aL, apos, ifnoutL
				tablew aR, apos, ifnoutR
			else
				fout Spath, 14, aL, aR
			endif
			kcount += 1
			ktotalcount += 1
		od
		kpercent = round((100 / ikcycles) * ktotalcount)
		if (kpercent != klastpercent) then
			io_send "percent", kpercent
			klastpercent = kpercent
		endif 
	else
		schedulek("twine_render_complete", 0, 1, icbid, ifnoutL, ifnoutR)
		turnoff
	endif
endin

instr twine_getsr
	icbid = p4
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"sr\":%d}", icbid, sr))
	turnoff
endin

instr twine_playback_complete
	icbid = p4
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":0}", icbid))
	turnoff
endin

instr twine_playback_lag
	icbid = p4
	turnoff2 "twine_playback", 0, 0
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":-1}", icbid))
	turnoff
endin

instr twine_playback
	icbid = p4
	ifn = p5
	imaxchannels = p6
	ifnsize = p7
	ifreeplaybacktable = p8
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":1}", icbid))

	if (ifreeplaybacktable == 1) then
		ftfree ifn, 1
	endif

	ktimes timeinsts
	chnset ktimes, "twine_playpos"
	aL, aR twine_playback ifn, ktimes, imaxchannels, ifnsize
	klevel = rms:k((aL + aR) / 2) * 1.4
	chnset klevel, "twine_mastervu"
	outs aL, aR

	klagging lagdetect 1.1
	if (klagging == 1) then
		schedulek("twine_playback_lag", 0, 1, icbid)
	endif

	kreleasing init 0
	if (lastcycle:k() == 1 || (kreleasing == 0 && release:k() == 1)) then
		kreleasing = 1
		schedulek("twine_playback_complete", 0, 1, icbid)
	endif
endin

instr twine_playback_complete
	icbid = p4
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":0}", icbid))
	turnoff
endin

instr twine_playbackwatchdog
	icbid = p4
	kreleasing init 0
	if (lastcycle:k() == 1 || (kreleasing == 0 && release:k() == 1)) then
		kreleasing = 1
		schedulek("twine_playback_complete", 0, 1, icbid)
	endif
endin


instr twine_stopplayback
	turnoff2 "ecp_playback", 0, 1
	turnoff3 "ecp_playback"
	turnoff2 "twine_playback", 0, 1
	turnoff2 "twine_playbackwatchdog", 0, 1
	turnoff
endin



instr twine_setbpm
	ibpm = p4
	gkseq_tempo init ibpm
	turnoff
endin


instr twine_createblankclip
	icbid = p4
	istereo = p5
	iduration = p6
	ilen = iduration * sr
	ifnL ftgen 0, 0, -ilen, 2, 0
	if (istereo == 1) then
		ifnR ftgen 0, 0, -ilen, 2, 0
	else
		ifnR = 0
	endif

	iclipindex ecp_addclip "", ifnL, ifnR, 4, 4
	Sresponse = sprintf("{\"cbid\":%d,\"status\":1,\"data\":{\"clipindex\":%d,\"datatable\":%d}}", icbid, iclipindex, giecp_fnclips[iclipindex])
	io_sendstring("callback", Sresponse)
	turnoff
endin

instr twine_importclip
	icbid = p4
	idatafn = p5
	iclipindex ecp_importclip idatafn
	Sresponse = sprintf("{\"cbid\":%d,\"status\":1,\"data\":{\"clipindex\":%d,\"datatable\":%d}}", icbid, iclipindex, idatafn)
	io_sendstring("callback", Sresponse)
	turnoff
endin

instr twine_loadftables
	icbid = p4
	ifnL = p5
	ifnR = p6
	istatus = -10
	Sdata = "{}"

	if (ifnL <= 0) then
		istatus = -1
		goto complete
	endif

	iclipindex ecp_addclip "", ifnL, ifnR, 4, 4 ; name not really required any more, beats contentious, analyse and also set warp mode
	Sdata = sprintf("{\"clipindex\":%d,\"datatable\":%d}", iclipindex, giecp_fnclips[iclipindex])

complete:
	Sresponse = sprintf("{\"cbid\":%d,\"status\":%d,\"data\":%s}", icbid, istatus, Sdata)
	io_sendstring("callback", Sresponse)
	turnoff
endin

instr twine_loadpath
	icbid = p4
	Spath = strget(p5)
	iforcemono = p6
	istatus = -10
	Sdata = "{}"

	if (filevalid(Spath) != 1) then
		istatus = -1
		goto complete
	endif

	ifilesr = filesr(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
		istatus = -2
		goto complete
	endif

	iclipindex ecp_loadsound Spath, 4, iforcemono ; beats contentious, analyse and also set warp mode, also SR conversion
	Sdata = sprintf("{\"clipindex\":%d,\"datatable\":%d}", iclipindex, giecp_fnclips[iclipindex])

	istatus = 1
	
complete:
	Sresponse = sprintf("{\"cbid\":%d,\"status\":%d,\"data\":%s}", icbid, istatus, Sdata)
	io_sendstring("callback", Sresponse)
	turnoff
endin

instr twine_setclipaudiounique
	icbid = p4
	iclipindex = p5
	ecp_setaudiounique iclipindex
	io_sendstring("callback", sprintf("{\"cbid\":%d}", icbid))
	turnoff
endin

instr twine_cloneclip
	icbid = p4
	iclipindex = p5

	inewclipindex ecp_cloneclip iclipindex
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"clipindex\":%d,\"datatable\":%d}", icbid, inewclipindex, giecp_fnclips[inewclipindex]))
	turnoff
endin


instr twine_clipreplacetables
	icbid = p4
	iclipindex = p5
	ifnL = p6
	ifnR = p7
	ecp_replacetables iclipindex, ifnL, ifnR
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"clipindex\":%d,\"datatable\":%d}", icbid, iclipindex, giecp_fnclips[iclipindex]))
	turnoff
endin


</CsInstruments>
<CsScore>
f0 z
</CsScore>
</CsoundSynthesizer>