aboutsummaryrefslogtreecommitdiff
path: root/site/udo/twist/automation.udo
blob: 39585fb84df6f1e1796f1d284a7ba51f0c7c9cd4 (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
#ifndef TWST_AUTOMATION
#define TWST_AUTOMATION ##
/*
	Twist waveform editor and transformer
	Automation and modulation

	This file is part of the SONICS UDO collection by Richard Knight 2024
		License: GPL-2.0-or-later
		http://1bpm.net
*/


opcode twst_xa_rms, 0, S
	Schannel  xin
	kscale chnget strcat(Schannel, "xrmsscale")
	kporttime chnget strcat(Schannel, "porttime")

	aL, aR, ileft, iright _twst_getcrossinput "xrms"
	if (ileft == 1 && iright == 1) then
		kvalue rms (aL + aR) / 2
	elseif (ileft == 1) then
		kvalue rms aL
	else
		kvalue rms aR
	endif

	kvalue *= kscale
	if (kporttime > 0) then
		kresultvalue portk kvalue, kporttime
	else
		kresultvalue = kvalue
	endif
	chnset kresultvalue, Schannel
endop

opcode twst_xa_pitchamdf, 0, S
	Schannel xin
	kscale chnget strcat(Schannel, "xpitchscale")
	ipitchmin chnget strcat(Schannel, "xpitchmin")
	ipitchmax chnget strcat(Schannel, "xpitchmax")
	kporttime chnget strcat(Schannel, "porttime")
	aL, aR, ileft, iright _twst_getcrossinput "xpitch"

	if (ileft == 1 && iright == 1) then
		kcps, krms pitchamdf (aL + aR) / 2, ipitchmin, ipitchmax
	elseif (ileft == 1) then
		kcps, krms pitchamdf aL, ipitchmin, ipitchmax
	else
		kcps, krms pitchamdf aR, ipitchmin, ipitchmax
	endif

	kvalue = ((ipitchmin + max:k(0, kcps)) / (ipitchmax - ipitchmin)) * kscale
	if (kporttime > 0) then
		kresultvalue portk kvalue, kporttime
	else
		kresultvalue = kvalue
	endif
	chnset kresultvalue, Schannel
endop

opcode twst_xa_pitch1, 0, S
	Schannel xin
	kscale chnget strcat(Schannel, "xpitchscale")
	ihopsize chnget strcat(Schannel, "xpitchhopsize")
	kporttime chnget strcat(Schannel, "porttime")

	ipitchmin = 20
	ipitchmax = sr / 2
	ihopsize = pow(2, ihopsize)

	aL, aR, ileft, iright _twst_getcrossinput "xpitch"
	if (ileft == 1 && iright == 1) then
		kcps, kamp ptrack (aL + aR) / 2, ihopsize
	elseif (ileft == 1) then
		kcps, kamp ptrack aL, ihopsize
	else
		kcps, kamp ptrack aR, ihopsize
	endif

	kvalue = ((ipitchmin + max:k(0, kcps)) / (ipitchmax - ipitchmin)) * kscale
	if (kporttime > 0) then
		kresultvalue portk kvalue, kporttime
	else
		kresultvalue = kvalue
	endif
	chnset kresultvalue, Schannel
endop

opcode twst_xa_pitch2, 0, S
	Schannel xin
	kscale chnget strcat(Schannel, "xpitchscale")
	iperiod chnget strcat(Schannel, "xpitchperiod")
	ipitchmin = octcps(chnget:i(strcat(Schannel, "xpitchmin")))
	ipitchmax = octcps(chnget:i(strcat(Schannel, "xpitchmax")))
	iampthresh chnget strcat(Schannel, "xpitchampthresh")
	kporttime chnget strcat(Schannel, "porttime")

	aL, aR, ileft, iright _twst_getcrossinput "xpitch"
	if (ileft == 1 && iright == 1) then
		koct, kamp pitch (aL + aR) / 2, iperiod, ipitchmin, ipitchmax, iampthresh
	elseif (ileft == 1) then
		koct, kamp pitch aL, iperiod, ipitchmin, ipitchmax, iampthresh
	else
		koct, kamp pitch aR, iperiod, ipitchmin, ipitchmax, iampthresh
	endif

	kcps cpsoct koct
	kvalue = ((ipitchmin + max:k(0, kcps)) / (ipitchmax - ipitchmin)) * kscale
	if (kporttime > 0) then
		kresultvalue portk kvalue, kporttime
	else
		kresultvalue = kvalue
	endif
	chnset kresultvalue, Schannel
endop

opcode twst_xa_centroid, 0, S
	Schannel xin
	kscale chnget strcat(Schannel, "xcentroidscale")
	ifftsize chnget strcat(Schannel, "xcentroidfftsize")
	kperiod chnget strcat(Schannel, "xcentroidperiod")
	kporttime chnget strcat(Schannel, "porttime")
	ipitchmin = 20
	ipitchmax = sr / 2
	ktrig metro 1 / kperiod

	aL, aR, ileft, iright _twst_getcrossinput "xcentroid"
	if (ileft == 1 && iright == 1) then
		kcent centroid (aL + aR) / 2, ktrig, ifftsize
	elseif (ileft == 1) then
		kcent centroid aL, ktrig, ifftsize
	else
		kcent centroid aL, ktrig, ifftsize
	endif

	kvalue = ((ipitchmin + max:k(0, kcent)) / (ipitchmax - ipitchmin)) * kscale
	if (kporttime > 0) then
		kresultvalue portk kvalue, kporttime
	else
		kresultvalue = kvalue
	endif
	chnset kresultvalue, Schannel
endop

opcode twst_mod_jitter, 0, S
	Schannel xin
	kbase chnget strcat(Schannel, "base")
	kamp chnget strcat(Schannel, "amp")
	kfreqmin chnget strcat(Schannel, "freqmin")
	kfreqmax chnget strcat(Schannel, "freqmax")
	imin chnget strcat(Schannel, "min")
	imax chnget strcat(Schannel, "max")
	kval jitter kamp, kfreqmin, kfreqmax
	kval = kbase + (kval * (imax - imin))
	kval = min:k(max:k(kval, imin), imax)
	chnset kval, Schannel
endop

opcode twst_mod_jitter2, 0, S
	Schannel xin
	kbase chnget strcat(Schannel, "base")
	ktotalamp chnget strcat(Schannel, "totalamp")
	kamp1 chnget strcat(Schannel, "amp1")
	kfreq1 chnget strcat(Schannel, "freq1")
	kamp2 chnget strcat(Schannel, "amp2")
	kfreq2 chnget strcat(Schannel, "freq2")
	kamp3 chnget strcat(Schannel, "amp3")
	kfreq3 chnget strcat(Schannel, "freq3")
	imin chnget strcat(Schannel, "min")
	imax chnget strcat(Schannel, "max")
	kval jitter2 ktotalamp, kamp1, kfreq1, kamp2, kfreq2, kamp3, kfreq3, 1
	kval = kbase + (kval * (imax - imin))
	kval = min:k(max:k(kval, imin), imax)
	chnset kval, Schannel
endop

opcode twst_mod_lfo, 0, S
	Schannel xin
	krate chnget strcat(Schannel, "rate")
	kbase chnget strcat(Schannel, "base")
	kgain chnget strcat(Schannel, "gain")
	imin chnget strcat(Schannel, "min")
	imax chnget strcat(Schannel, "max")
	kfn twst_tf_getwaveformk chnget:k(strcat(Schannel, "wave"))
	kval = kbase + (oscilikt:k(kgain, krate, kfn) * (imax - imin))
	kval = min:k(max:k(kval, imin), imax)
	chnset kval, Schannel
endop

opcode twst_mod_line, 0, S
	Schannel xin
	ilatency twst_getlatencyseconds
	ifirst chnget strcat(Schannel, "first")
	ilast chnget strcat(Schannel, "last")
	if (ilatency > 0) then
		ksig linseg ifirst, ilatency, ifirst, p3, ilast
	else
		ksig linseg ifirst, p3, ilast
	endif

	chnset ksig, Schannel
endop

opcode twst_mod_random, 0, S
	Schannel xin
	krate chnget strcat(Schannel, "rate")
	kmin chnget strcat(Schannel, "min")
	kmax chnget strcat(Schannel, "max")
	kporttime chnget strcat(Schannel, "porttime")

	imin chnget strcat(Schannel, "min")
	imax chnget strcat(Schannel, "max")
	initval random imin, imax

	kval init initval
	ktrig metro krate
	if (ktrig == 1) then
		kval = random:k(kmin, kmax)
	endif
	kportval portk kval, kporttime
	chnset kportval, Schannel
endop


instr twst_automationprepare
	icbid = p4
	itemnum = pcount()
	index = 5
	Sinstr = "instr twst_automaterun\n"
	while (index < itemnum + 1) do

		Sinstr = strcat(strcat(Sinstr, strget(p(index))), "\n")
		index += 1
	od
	Sinstr = strcat(Sinstr, "a_ init 0\nout a_\nendin\n")
	ires compilestr Sinstr
	io_sendstring("callback", sprintf("{\"cbid\":%d,\"status\":%d}", icbid, (ires == 0) ? 1 : 0))
endin


#end