aboutsummaryrefslogtreecommitdiff
path: root/sonics/frequency_tools.udo
blob: c74739fbfebb0f0481f9a0609a8bfc6978e9e864 (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
#ifndef UDO_FREQUENCYTOOLS
#define UDO_FREQUENCYTOOLS ##
/*
	Frequency tools/effects: shifters, ring modulation, delays, chorus etc
	Slim excerpt for Partial Emergence

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

#include "sonics/wavetables.udo"



/*
	Mono frequency shifter with hilbert transform
	
	aoutput freqshift1 ainput, kfrequency
	
	aoutput		output audio
	ainput		input audio
	kfrequency	shift frequency [MIN(-10000) MAX(10000) DEFAULT(-500)]
*/
opcode freqshift1, a, ak
	ain, kfreq xin
	asin oscili 1, kfreq, gifnSine
	acos oscili 1, kfreq, gifnSine, .25
	areal, aimag hilbert ain
	amod1 = areal * acos
	amod2 = aimag * asin
	ashift = (amod1 - amod2) * 0.7
	xout ashift
endop


/*
	Stereo frequency shifter with hilbert transform
	
	aoutputL, aoutputR freqshift1 ainputL, ainputR, kfrequency

	aoutputL	output audio left
	aoutputR	output audio right
	ainputL		input audio left
	ainputR		input audio right
	kfrequency	shift frequency [MIN(-10000) MAX(10000) DEFAULT(-500)]
*/
opcode freqshift1, aa, aak
	ainL, ainR, kfreq xin
	asin oscili 1, kfreq, gifnSine
	acos oscili 1, kfreq, gifnSine, .25
	arealL, aimagL hilbert ainL
	arealR, aimagR hilbert ainR
	amod1L = arealL * acos
	amod2L = aimagL * asin
	amod1R = arealR * acos
	amod2R = aimagR * asin
	ashiftL = (amod1L - amod2L) * 0.7
	ashiftR = (amod1R - amod2R) * 0.7
	xout ashiftL, ashiftR
endop


/*
	Mono ring modulator with hilbert transform
	
	aoutput ringmod1 ainput, kfrequency

	aoutput		output audio
	ainput		input audio
	kfrequency	modulation frequency [MIN(0) MAX(10000) DEFAULT(440)]
*/
opcode ringmod1, a, ak
	ain, kfreq xin
	asin oscili 1, kfreq, gifnSine
	acos oscili 1, kfreq, gifnSine, .25
	areal, aimag hilbert ain
	amod1 = areal * acos
	amod2 = aimag * asin
	aupshift = (amod1 - amod2) * 0.7
	adownshift = (amod1 + amod2) * 0.7
	xout aupshift+adownshift
endop

/*
	Stereo ring modulator with hilbert transform
	
	aoutputL, aoutputR ringmod1 ainputL, ainputR, kfrequency

	aoutputL	output audio left
	aoutputR	output audio right
	ainputL		input audio left
	ainputR		input audio right
	kfrequency	modulation frequency [MIN(0) MAX(10000) DEFAULT(440)]
*/
opcode ringmod1, aa, aak
	ainL, ainR, kfreq xin
	asin oscili 1, kfreq, gifnSine
	acos oscili 1, kfreq, gifnSine, .25
	arealL, aimagL hilbert ainL
	arealR, aimagR hilbert ainR
	amod1L = arealL * acos
	amod2L = aimagL * asin
	amod1R = arealR * acos
	amod2R = aimagR * asin
	aupshiftL = (amod1L - amod2L) * 0.7
	adownshiftL = (amod1L + amod2L) * 0.7
	aupshiftR = (amod1R - amod2R) * 0.7
	adownshiftR = (amod1R + amod2R) * 0.7
	xout aupshiftL+adownshiftL, aupshiftR+adownshiftR
endop


/*
	Mono frequency shifter with direct modulation
	
	aoutput freqshift2 ainput, kfrequency, [kshiftmode=1]

	aoutput		output audio
	ainput		input audio
	kfrequency	shift frequency [MIN(-10000) MAX(10000) DEFAULT(-500)]
	kshiftmode	shift mode [TYPE(bool) DEFAULT(1)]
*/
opcode freqshift2, a, akP
	ain, kfreq, kshiftmode xin
	isr4 = sr * 0.25

	ko1frq = isr4 - (1 - kshiftmode) * kfreq
	aqo1r oscil 1.0, ko1frq, gifnSine, 0.25 ; cosine
	aqo1i oscil 1.0, ko1frq, gifnSine, 0.0 ; sine

	ko2frq = isr4 + kshiftmode * kfreq
	aqo2r oscil 1.0, ko2frq, gifnSine, 0.25 ; cosine
	aqo2i oscil 1.0, ko2frq, gifnSine, 0.0 ; sine
	awq1r = ain * aqo1r

	awf1r biquad awq1r, 1, 1.6375276435, 1, 1, -0.93027644018, 0.37171017225
	awf2r biquad awf1r, 1, 0.56037176307, 1, 1, -0.40320752514, 0.73736786626
	awf3r biquad awf2r, 1, 0.19165327787, 1, 1, -0.15398586410, 0.94001488557
	aw1fr = awf3r * 0.051532459925
	awq2r = aw1fr * aqo2r

	awq1i = ain * aqo1i
	awf1i biquad awq1i, 1, 1.6375276435, 1, 1, -0.93027644018, 0.37171017225
	awf2i biquad awf1i, 1, 0.56037176307, 1, 1, -0.40320752514, 0.73736786626
	awf3i biquad awf2i, 1, 0.19165327787, 1, 1, -0.15398586410, 0.94001488557

	aw1fi = awf3i * 0.051532459925
	awq2i = aw1fi * aqo2i
	aout = awq2r + awq2i
	xout aout
endop


/*
	Stereo frequency shifter with direct modulation
	
	aoutputL, aoutputR freqshift2 ainputL, ainputR, kfrequency, [kshiftmode=1]

	aoutputL	output audio left
	aoutputR	output audio right
	ainputL		input audio left
	ainputR		input audio right
	kfrequency	shift frequency [MIN(-10000) MAX(10000) DEFAULT(-500)]
	kshiftmode	shift mode [TYPE(bool) DEFAULT(1)]
*/
opcode freqshift2, aa, aakP
	ainL, ainR, kfreq, kshiftmode xin
	isr4 = sr * 0.25

	ko1frq = isr4 - (1 - kshiftmode) * kfreq
	aqo1r oscil 1.0, ko1frq, gifnSine, 0.25 ; cosine
	aqo1i oscil 1.0, ko1frq, gifnSine, 0.0 ; sine
	ko2frq = isr4 + kshiftmode * kfreq
	aqo2r oscil 1.0, ko2frq, gifnSine, 0.25 ; cosine
	aqo2i oscil 1.0, ko2frq, gifnSine, 0.0 ; sine

	awq1rL = ainL * aqo1r
	awq1rR = ainR * aqo1r

	; Left
	awf1rL biquad awq1rL, 1, 1.6375276435, 1, 1, -0.93027644018, 0.37171017225
	awf2rL biquad awf1rL, 1, 0.56037176307, 1, 1, -0.40320752514, 0.73736786626
	awf3rL biquad awf2rL, 1, 0.19165327787, 1, 1, -0.15398586410, 0.94001488557
	aw1frL = awf3rL * 0.051532459925
	awq2rL = aw1frL * aqo2r

	awq1iL = ainL * aqo1i
	awf1iL biquad awq1iL, 1, 1.6375276435, 1, 1, -0.93027644018, 0.37171017225
	awf2iL biquad awf1iL, 1, 0.56037176307, 1, 1, -0.40320752514, 0.73736786626
	awf3iL biquad awf2iL, 1, 0.19165327787, 1, 1, -0.15398586410, 0.94001488557

	aw1fiL = awf3iL * 0.051532459925
	awq2iL = aw1fiL * aqo2i
	aoutL = awq2rL + awq2iL

	; Right
	awf1rR biquad awq1rR, 1, 1.6375276435, 1, 1, -0.93027644018, 0.37171017225
	awf2rR biquad awf1rR, 1, 0.56037176307, 1, 1, -0.40320752514, 0.73736786626
	awf3rR biquad awf2rR, 1, 0.19165327787, 1, 1, -0.15398586410, 0.94001488557
	aw1frR = awf3rR * 0.051532459925
	awq2rR = aw1frR * aqo2r

	awq1iR = ainR * aqo1i
	awf1iR biquad awq1iR, 1, 1.6375276435, 1, 1, -0.93027644018, 0.37171017225
	awf2iR biquad awf1iR, 1, 0.56037176307, 1, 1, -0.40320752514, 0.73736786626
	awf3iR biquad awf2iR, 1, 0.19165327787, 1, 1, -0.15398586410, 0.94001488557

	aw1fiR = awf3iR * 0.051532459925
	awq2iR = aw1fiR * aqo2i
	aoutR = awq2rR + awq2iR

	xout aoutL, aoutR
endop



/*
	Bit depth reducer/crusher
	
	aout bitcrush ain, [krush=16]

	aout	crushed signal
	ain		input signal
	krush	bits to reduce to [TYPE(int) MIN(1) MAX(128) DEFAULT(16)]
	
*/
opcode bitcrush, a, aJ
	a1, krush xin
	krush = (krush == -1) ? 16 : krush
	a1 = round:a(a1 * krush) / krush
	xout a1
endop


/*
	Bit depth reducer/crusher (stereo)
	
	aoutL, aoutR bitcrush ainL, ainR, [krush=16]

	aoutL	crushed signal left
	aoutR	crushed signal right
	ainL	input signal left
	ainR	input signal right
	krush	bits to reduce to [TYPE(int) MIN(1) MAX(128) DEFAULT(16)]
	
*/
opcode bitcrush, aa, aaJ
	aL, aR, krush xin
	krush = (krush == -1) ? 16 : krush
	aL = round:a(aL * krush) / krush
	aR = round:a(aR * krush) / krush
	xout aL, aR
endop



/*
	Resonant delay based tuner

	aout delaytuner ain, kfrequency, kfeedback

	aout		tuned/delayed signal summed with input
	ain			input signal
	kfrequency	cps to tune to [MIN(20) MAX(10000) DEFAULT(440)]
	kfeedback	feedback amount [MIN(0) MAX(1) DEFAULT(0.5)]
*/
opcode delaytuner, a, akk
	ain, kfrequency, kfeedback xin
	adump delayr 1
	adelayed deltap (1/kfrequency)
	delayw ain + (adelayed * kfeedback)
	aout = ain + adelayed
	xout aout
endop


/*
	Resonant delay based tuner (stereo)

	aoutL, aoutR delaytuner ainL, ainR, kfrequency, kfeedback

	aoutL, aoutR	tuned/delayed signal summed with input
	ainL, ainR		input signal
	kfrequency		cps to tune to [MIN(20) MAX(10000) DEFAULT(440)]
	kfeedback		feedback amount [MIN(0) MAX(1) DEFAULT(0.5)]
*/
opcode delaytuner, aa, aakk
	ainL, ainR, kfrequency, kfeedback xin
	aoutL delaytuner ainL, kfrequency, kfeedback
	aoutR delaytuner ainR, kfrequency, kfeedback
	xout aoutL, aoutR
endop


/*
	Resonant delay based tuner with hold control. When held, only outputs effected, not dry
	
	aout glitchtuner ain, kfrequency, ktrig

	aout			output signal
	ain				input signal
	kfrequency		cps to tune to [MIN(20) MAX(10000) DEFAULT(440)]
	khold			apply if 1, bypass if 0
*/
opcode glitchtuner, a, akk
	ain, kfrequency, khold xin
	adump delayr 1
	adelayed deltap (1/kfrequency)
	if (khold >= 1) then
		aout = adelayed
	else
		aout = ain
	endif
	delayw aout
	xout aout
endop


/*
	Resonant delay based tuner with hold control (stereo). When held, only outputs effected, not dry
	
	aout glitchtuner ain, kfrequency, ktrig

	aoutL, aoutR	output signal
	ainL, ainR		input signal
	kfrequency		cps to tune to [MIN(20) MAX(10000) DEFAULT(440)]
	khold			apply if 1, bypass if 0
*/
opcode glitchtuner, aa, aakk
	ainL, ainR, kfrequency, khold xin
	aoutL glitchtuner ainL, kfrequency, khold
	aoutR glitchtuner ainR, kfrequency, khold
	xout aoutL, aoutR
endop



/*
	Simple chorus

	aoutL, aoutR simplechorus ainL, ainR, irateL, irateR

	aoutL, aoutR	output signal
	ainL, ainR		input signal
	irateL			delay rate in Hz left
	irateR			delay rate in Hz right
*/
opcode simplechorus, aa, aaii
	aL, aR, irateL, irateR xin
	alfoL oscil irateL, unirand(1)
	alfoR oscil irateR, unirand(1)
	aL vdelay3 aL, (0.01 + alfoL) * 1000, 1000
	aR vdelay3 aR, (0.01 + alfoR) * 1000, 1000
	xout aL, aR
endop

#end