aboutsummaryrefslogtreecommitdiff
path: root/site/udo/spectral_sampler.udo
blob: 04f1f4d91a04833ec15ed10641f50da98316eeb2 (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
#ifndef UDO_SPECTRALSAMPLER
#define UDO_SPECTRALSAMPLER ##
/*
	Spectral sampler

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


/*
 ; pvs buffer handle and length storage
gipvsBuffers[] init 8
gipvsBufferLengths[] init lenarray(gipvsBuffers)

; record to a spectral sampling buffer
; ibuffer spectralsamplerecord ain, iduration, ifftsize
opcode spectralsamplerrecord, i, aio
	ain, iduration, ifftsize xin
	if (ifftsize == 0) then
		ifftsize = 1024
	endif
	kamp linseg 0, iduration * 0.01, 1, iduration * 0.98, 1, iduration * 0.01, 0
	ain1 = ain * kamp
	ilength = iduration + (ifftsize / sr)
	fanal pvsanal ain1, ifftsize, ifftsize/4, ifftsize, 1
	ibuffer, ktim pvsbuffer fanal, ilength
	xout ibuffer
endop


; play back from a spectral sampling buffer
; aL, aR spectralsamplerplay ibuffer, ilength, ktime, kpos
opcode spectralsamplerplay, aa, iikk
	ibuffer, ilength, ktime, kpos xin
	kchange changed kpos
	aphasor, asyncout syncphasor a(ktime * ilength), a(kchange)
	kphasor = k(aphasor) + (kpos * ilength)
	fL pvsbufread kphasor, ibuffer
	fR pvsbufread kphasor*0.95, ibuffer
	aL pvsynth fL
	aR pvsynth fR
	xout aL, aR
endop


*/
; ABOVE ARE LEGACY AND TO BE DEPRECATED


/*
	Spectral sampling and playback
	
	Can't sub-opcode the f in the a for some reason, doesn't work - so some duplication here
*/

opcode spectralsampler, f, fkkio
	fanal, ktime, kpos, ilength, icontinuous xin

	ksampling init 1
	if (icontinuous == 1 || ksampling == 1) then
		ibuffer, ktime pvsbuffer fanal, ilength
		if (icontinuous == 0 && timeinsts() >= ilength) then
			ksampling = 0
		endif
	endif

	kchange changed kpos
	aphasor, asyncout syncphasor a(ktime * ilength), a(kchange)
	kphasor = k(aphasor) + (kpos * ilength)
	fread pvsbufread kphasor, ibuffer
	xout fread
endop



opcode spectralsampler, a, akkioo
	ain, ktime, kpos, ilength, ifftsize, icontinuous xin

	ifftsize = (ifftsize == 0) ? 1024 : ifftsize
	ksampling init 1
	if (icontinuous == 1 || ksampling == 1) then
		fanal pvsanal ain, ifftsize, ifftsize/4, ifftsize, 1
		ibuffer, ktime pvsbuffer fanal, ilength
		if (icontinuous == 0 && timeinsts() >= ilength) then
			ksampling = 0
		endif
	endif

	kchange changed kpos
	aphasor, asyncout syncphasor a(ktime * ilength), a(kchange)
	kphasor = k(aphasor) + (kpos * ilength)
	fread pvsbufread kphasor, ibuffer
	aout pvsynth fread
	xout aout
endop



opcode spectralsampler, aa, aakkioo
	ainL, ainR, ktime, kpos, ilength, ifftsize, icontinuous xin

	ifftsize = (ifftsize == 0) ? 1024 : ifftsize
	ksampling init 1
	if (icontinuous == 1 || ksampling == 1) then
		fanalL pvsanal ainL, ifftsize, ifftsize/4, ifftsize, 1
		fanalR pvsanal ainR, ifftsize, ifftsize/4, ifftsize, 1
		ibufferL, ktime pvsbuffer fanalL, ilength
		ibufferR, ktime pvsbuffer fanalR, ilength
		if (icontinuous == 0 && timeinsts() >= ilength) then
			ksampling = 0
		endif
	endif

	kchange changed kpos
	aphasor, asyncout syncphasor a(ktime * ilength), a(kchange)
	kphasor = k(aphasor) + (kpos * ilength)
	freadL pvsbufread kphasor, ibufferL
	freadR pvsbufread kphasor, ibufferR
	aoutL pvsynth freadL
	aoutR pvsynth freadR
	xout aoutL, aoutR
endop

#end