aboutsummaryrefslogtreecommitdiff
path: root/site/udo/fx_autoglitch.udo
blob: 163110cfbb4b07cf3fefa22102c756cf854ef9cb (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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#ifndef UDO_FXAUTOGLITCH
#define UDO_FXAUTOGLITCH ##
/*
	Autoglitch effects

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

#include "pvs_tabproc.udo"
#include "wavetables.udo"

#ifndef AUTOGLITCH_WINSIZE
#define AUTOGLITCH_WINSIZE #4410#
#end

#ifndef AUTOGLITCH_WINRAND
#define AUTOGLITCH_WINRAND #441#
#end


/*
	Internal autoglitch UDO for actual audio processing
	
	aout _fx_autoglitch_audio ain, awritepos, areadpos, ibuflensamps, kdo_distortion, kdist, kporttime, kdo_ampchange, kamp, kreadmode

	aout			audio output
	ain				audio input
	awritepos		write position for sampler in samples
	areadpos		read position in samples
	ibuflensamps	length of buffer to be created in samples
	kdo_distortion	apply distortion
	kdist			distortion amount
	kporttime		portamento time
	kdo_ampchange	apply amplitude change
	kamp			amplitude
	kreadmode		read mode: 0 = direct table; 1 = sndwarp; 2 = mincer
*/
opcode _fx_autoglitch_audio, a, aaaikkkkkk
	ain, awritepos, areadpos, ibuflensamps, kdo_distortion, kdist, kporttime, kdo_ampchange, kamp, kreadmode xin
	ibuffer = ftgentmp(0, 0, -ibuflensamps, -2, 0)

	tablew ain, awritepos, ibuffer

	if (kreadmode == 0) then
		asig table areadpos, ibuffer
	elseif (kreadmode == 1) then
		asig sndwarp 1, areadpos / sr, 1, ibuffer, 0, $AUTOGLITCH_WINSIZE, $AUTOGLITCH_WINRAND, 4, gifnHalfSine, 1
	elseif (kreadmode == 2) then
		asig mincer areadpos / sr, 1, 1, ibuffer, 1
	endif

	asig butterhp dcblock(asig), 70

	if (kdo_distortion == 1 && kdist > 1) then
		asigd distort asig, portk(kdist, kporttime), gifnSquare
		asig balance asigd, asig
	endif

	if (kdo_ampchange == 1) then
		asig *= portk(kamp, kporttime)
	endif

	asig butterhp dcblock(asig), 100
	xout asig
endop


/*
	Internal autoglitch UDO for control

	awritepos, areadpos, ibuflensamps, kamp, kdist _fx_autoglitch_control kminratio, kchangerate, kchangechance, kporttime, ibuflens

	awritepos		write position for sampler in samples
	areadpos		read position in samples
	ibuflensamps	length of buffer to be created in samples
	kamp 			amplitude
	kdist			distortion amount
	kminratio		minimum ratio of sound length to use (0 to 1)
	kchangerate		rate of change in Hz
	kchangechance	chance of changing at change rate (0 to 1)
	kporttime		portamento time in seconds
	ibuflens		required buffer length in seconds
	
*/
opcode _fx_autoglitch_control, aaikk, kkkki
	kminratio, kchangerate, kchangechance, kporttime, ibuflens xin

	kminratio = (kminratio == -1) ? 0.2 : kminratio
	kchangerate = (kchangerate == -1) ? 0.1 : kchangerate
	kchangechance = (kchangechance == -1) ? 0.8 : kchangechance
	kporttime = (kporttime == -1) ? 0.2 : kporttime
	ibuflens = (ibuflens == -1) ? 2 : ibuflens

	ibuflensamps = sr * ibuflens	
	awritepos lphasor 1, 0, ibuflensamps, 1 ; ,3

	kdist init 0
	klen init random(256, ibuflensamps * 0.5)
	kstart init random(0, ibuflensamps * 0.5)
	kreverse init 0
	kamp init 0

	kcps = ibuflensamps / klen
	if (kreverse == 1) then
		kcps = 0 - kcps
	endif

	areadindex phasor kcps
	areadpos = ((areadindex * ibuflensamps) + portk(kstart, kporttime))

	kchanger metro kchangerate
	if (kchanger == 1 && random:k(0, 1) < kchangechance) then
		klen = random:k(50, ibuflensamps * kminratio)
		kstart = random:k(0, ibuflensamps - klen)
	endif

	if (kchanger == 1 && random:k(0, 1) < kchangechance) then
		kreverse = round:k(random:k(0, 1))
	endif

	if (kchanger == 1 && random:k(0, 1) < kchangechance) then
		kdist = random:k(0, 2)
	endif

	if (kchanger == 1 && random:k(0, 1) < kchangechance) then
		kamp = round:k(random:k(0, 1))
	endif

	xout awritepos, areadpos, ibuflensamps, kamp, kdist
endop



/*
	Mono in, mono out autoglitch
	
	aout fx_autoglitch ain, kminratio, kchangerate, kchangechance, kporttime, kdo_distortion, kdo_ampchange, ibuflens, kreadmode 
	
	aout			audio output
	ain				audio input
	kminratio		minimum ratio of sound length to use (0 to 1)
	kchangerate		rate of change in Hz
	kchangechance	chance of changing at change rate (0 to 1)
	kporttime		portamento time in seconds
	kdo_distortion	apply distortion
	kdo_ampchange	apply amplitude change
	ibuflens		required buffer length in seconds
	kreadmode		read mode: 0 = direct table; 1 = sndwarp; 2 = mincer
	
*/
opcode fx_autoglitch, a, aJJJJOOjO
	ain, kminratio, kchangerate, kchangechance, kporttime, kdo_distortion, kdo_ampchange, ibuflens, kreadmode xin
	
	awritepos, areadpos, ibuflensamps, kamp, kdist _fx_autoglitch_control kminratio, kchangerate, kchangechance, kporttime, ibuflens
	aout _fx_autoglitch_audio ain, awritepos, areadpos, ibuflensamps, kdo_distortion, kdist, kporttime, kdo_ampchange, kamp, kreadmode
	xout aout
endop


/*
	Mono in, stereo out autoglitch
	
	aoutL, aoutR fx_autoglitch ain, kminratio, kchangerate, kchangechance, kporttime, kdo_distortion, kdo_ampchange, ibuflens, kreadmode 
	
	aoutL			left audio output
	aoutR			right audio output
	ain				audio input
	kminratio		minimum ratio of sound length to use (0 to 1)
	kchangerate		rate of change in Hz
	kchangechance	chance of changing at change rate (0 to 1)
	kporttime		portamento time in seconds
	kdo_distortion	apply distortion
	kdo_ampchange	apply amplitude change
	ibuflens		required buffer length in seconds
	kreadmode		read mode: 0 = direct table; 1 = sndwarp; 2 = mincer
	
*/
opcode fx_autoglitch, aa, aJJJJOOjO
	ain, kminratio, kchangerate, kchangechance, kporttime, kdo_distortion, kdo_ampchange, ibuflens, kreadmode xin
	
	awriteposL, areadposL, ibuflensamps, kampL, kdistL _fx_autoglitch_control kminratio, kchangerate, kchangechance, kporttime, ibuflens
	awriteposR, areadposR, ibuflensamps, kampR, kdistR _fx_autoglitch_control kminratio, kchangerate, kchangechance, kporttime, ibuflens
	aL _fx_autoglitch_audio ain, awriteposL, areadposL, ibuflensamps, kdo_distortion, kdistL, kporttime, kdo_ampchange, kampL, kreadmode
	aR _fx_autoglitch_audio ain, awriteposR, areadposR, ibuflensamps, kdo_distortion, kdistR, kporttime, kdo_ampchange, kampR, kreadmode
	xout aL, aR
endop


/*
	Stereo in, stereo out autoglitch
	
	aoutL, aoutR fx_autoglitch ainL, ainR, kminratio, kchangerate, kchangechance, kporttime, kdo_distortion, kdo_ampchange, ibuflens, istereounique, kreadmode 
	
	aoutL			left audio output
	aoutR			right audio output
	ainL			left audio input
	ainR			right audio input
	kminratio		minimum ratio of sound length to use (0 to 1)
	kchangerate		rate of change in Hz
	kchangechance	chance of changing at change rate (0 to 1)
	kporttime		portamento time in seconds
	kdo_distortion	apply distortion
	kdo_ampchange	apply amplitude change
	ibuflens		required buffer length in seconds
	istereounique	stereo mode: 0 = left and right read the same positions from buffers; 1 = left and right are unique
	kreadmode		read mode: 0 = direct table; 1 = sndwarp; 2 = mincer
	
*/
opcode fx_autoglitch, aa, aaJJJJOOjjO
	aL, aR, kminratio, kchangerate, kchangechance, kporttime, kdo_distortion, kdo_ampchange, ibuflens, istereounique, kreadmode xin

	if (istereounique == 1) then
		awriteposL, areadposL, ibuflensamps, kampL, kdistL _fx_autoglitch_control kminratio, kchangerate, kchangechance, kporttime, ibuflens
		awriteposR, areadposR, ibuflensamps, kampR, kdistR _fx_autoglitch_control kminratio, kchangerate, kchangechance, kporttime, ibuflens
		aL _fx_autoglitch_audio aL, awriteposL, areadposL, ibuflensamps, kdo_distortion, kdistL, kporttime, kdo_ampchange, kampL, kreadmode
		aR _fx_autoglitch_audio aR, awriteposR, areadposR, ibuflensamps, kdo_distortion, kdistR, kporttime, kdo_ampchange, kampR, kreadmode
	else
		awritepos, areadpos, ibuflensamps, kamp, kdist _fx_autoglitch_control kminratio, kchangerate, kchangechance, kporttime, ibuflens
		aL _fx_autoglitch_audio aL, awritepos, areadpos, ibuflensamps, kdo_distortion, kdist, kporttime, kdo_ampchange, kamp, kreadmode
		aR _fx_autoglitch_audio aR, awritepos, areadpos, ibuflensamps, kdo_distortion, kdist, kporttime, kdo_ampchange, kamp, kreadmode
	endif
	
	xout aL, aR
endop



/*
	Spectral processing autoglitch

	aout fx_spectralautoglitch ain, kchangerate, kchangechance, kdo_pitchalter, kporttime

	aout			audio output
	ain				audio input
	kchangerate		rate of change in Hz
	kchangechance	chance of changing at change rate (0 to 1)
	kdo_pitchalter	apply pitch alterations
	kporttime		portamento time in seconds
	ifftsize		fft size
	
*/
opcode fx_spectralautoglitch, a, akkkkj
	ainput, kchangerate, kchangechance, kdo_pitchalter, kporttime, ifftsize xin

	kdo_freeze init 0
	kdo_average init 0
	kdo_scramble init 0
	kdo_scale init 0
	kdo_shift init 0
	kdo_blur init 0
	kdo_delay init 0
	kdo_bubble init 0
	kdo_wrap init 0
	
	kbubblechance init 0
	kbubblestereo init 0
	kwrapamp init 0
	kwrapfreq init 0
	kfreezetime init 0
	kaveragetime init 0
	kscale init 1
	kshift init 0
	kblur init 0
	kdelaytime init 0
	ktime init 0
	kpos init 0

	ilength = random(1, 4)
	ir = (ifftsize == -1) ? 512 : ifftsize

	finput pvsanal ainput, ir, ir/4, ir, 1

	ibuffer, ktime pvsbuffer finput, ilength
	kchange changed kpos
	aphasor, asyncout syncphasor a(portk(ktime, kporttime) * ilength), a(kchange)
	kphasor = k(aphasor) + (portk(kpos, kporttime) * ilength)
	floop pvsbufread kphasor, ibuffer

	
	kchanger metro kchangerate
	if (kchanger == 1) then
		if (random:k(0, 1) < kchangechance) then
			ktime = random:k(0, 1)
		endif

		if (random:k(0, 1) < kchangechance) then
			kpos = random:k(0, 1)
		endif

		if (random:k(0, 1) < kchangechance) then
			kfreezetime = round:k(random:k(0, 20))
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_freeze = 1 - kdo_freeze
		endif

		if (random:k(0, 1) < kchangechance) then
			kaveragetime = random:k(0, 10)
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_average = 1 - kdo_average
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_scramble = 1 - kdo_scramble
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_bubble = 1 - kdo_bubble
		endif

		if (random:k(0, 1) < kchangechance) then
			kbubblechance = random:k(0, 1)
		endif

		if (random:k(0, 1) < kchangechance) then
			kbubblestereo = round:k(random:k(0, 1))
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_wrap = 1 - kdo_wrap
		endif

		if (random:k(0, 1) < kchangechance) then
			kwrapamp = round:k(random:k(0, ir / 2))
		endif

		if (random:k(0, 1) < kchangechance) then
			kwrapfreq = round:k(random:k(0, ir / 2))
		endif

		if (random:k(0, 1) < kchangechance) then
			kscale = random:k(0.5, 2)
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_scale = 1 - kdo_scale
		endif

		if (random:k(0, 1) < kchangechance) then
			kshift = random:k(-500, 100)
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_shift = 1 - kdo_shift
		endif

		if (random:k(0, 1) < kchangechance) then
			kblur = random:k(0, 1)
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_blur = 1 - kdo_blur
		endif

		if (random:k(0, 1) < kchangechance) then
			kdelaytime = random:k(2, 1000)
		endif

		if (random:k(0, 1) < kchangechance) then
			kdo_delay = 1 - kdo_delay
		endif

	endif
	
	kready, itpv tpv_anal floop
	
	if (kdo_scramble == 1) then
		tpv_scramble kready, itpv, 4
	endif

	if (kdo_freeze == 1) then
		tpv_freeze1 kready, itpv, portk(kfreezetime, kporttime)
	endif

	if (kdo_average == 1) then
		tpv_average kready, itpv, portk(kaveragetime, kporttime)
	endif

	if (kdo_bubble == 1) then
		tpv_bubble kready, itpv, kbubblechance, kbubblestereo
	endif

	if (kdo_wrap == 1) then
		tpv_wrap kready, itpv, kwrapamp, kwrapfreq
	endif

	floop tpv_resynth itpv, floop

	if (kdo_blur == 1) then
		;floop pvsblur floop, portk(kblur, kporttime), 1
	endif

	if (kdo_pitchalter == 1 && kdo_scale == 1) then
		floop pvscale floop, portk(kscale, kporttime)
	endif

	if (kdo_pitchalter == 1 && kdo_shift == 1) then
		floop pvshift floop, portk(kshift, kporttime), 150
	endif

	aout pvsynth floop

	if (kdo_delay == 1) then
		kdtime = (kdo_pitchalter == 1) ? portk(kdelaytime, kporttime) : kdelaytime
		aout vdelay aout, kdtime, 1000
	endif

	xout aout * 2.5 ; is quieter
endop



/*
	Retrigger glitcher

	aout fx_retrigglitch ifn, ktriglen, areadpos[, kpitchratio=1, kapplywindowing=1, kwinfn=gifnHanning]

	aout				audio output
	ifn					input audio ftable (mono)
	ktriglen			retrigger length in seconds
	areadpos			read position in seconds; -1 is set randomly upon each retrigger
	kpitchratio			resample ratio; 1 = normal pitch
	kapplywindowing		amplitude enveloping: 0 = none; 1 = apply Hanning window to output
	kwinfn				window function table
*/
opcode fx_retrigglitch, a, ikaPPJ
	ifn, ktriglen, areadpos, kpitchratio, kapplywindowing, kwinfn xin
	ifnsr = ftsr(ifn)
	ifnlen = ftlen(ifn)
	ifnlens = ifnlen / ifnsr
	
	kfreq = 1 / ktriglen
	ktrig metro kfreq

	if (ktrig == 1 && k(areadpos) == -1) then
		kstart random 0, ifnlens - ktriglen
		areadpos = a(kstart)
	endif

	areadpos = areadpos / ifnlens
	irate = 1 / ifnlens
	areadpos samphold areadpos, ktrig
	async upsamp ktrig
	apos, a_ syncphasor irate * kpitchratio, async
	apos += areadpos
	apos *= ifnlen

	aout tablei apos, ifn
	;aout, a_ mincer (apos / ifnlen) * ifnlens, 1, kpitchratio, ifn, 1

	if (kapplywindowing == 1) then
		if (kwinfn == -1) then
			kwinfn = gifnHanning
		endif
		aenv oscilikt 1, kfreq, kwinfn
		aout *= aenv
	endif

	aout dcblock aout
	xout aout
endop


#end