-odac
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
f0 z