aboutsummaryrefslogtreecommitdiff
path: root/site/udo/DEPRECATE_SORT_opcodes.udo
blob: a942a3d30d9cdfd3c2d46585d003e0eb49e0d1ee (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
; knearest nearest iarray, kvalue
; get the nearest value to kvalue in the one-dimensional array iarray




; aL, aR randpan asource
; pan randomly (mono input)
opcode randpan, aa, a
	asource xin
	ipan randfloat, 0, 1
	xout asource * ipan, asource * (1 - ipan)
endop


; aL, aR randpan aL, aR
; pan randomly (stereo input)
opcode randpan, aa, aa
	aL, aR xin
	ipan randfloat, 0, 1
	xout aL * ipan, aR * (1 - ipan)
endop





; Sfiltered[] directoryfiles Sdirectory, Sextension
; get an array of files from Sdirectory with given extension Sextension (extension given without ".", and case insensitive)
; does the same as csound's directory opcode but without false positives (ie, ".wav" would return ".wav.asd"
opcode directoryfiles, S[], SS
	Sdirectory, Sextension xin
	Sextension strlower Sextension
	Sfiles[] directory Sdirectory
	Sfiltered[] init 1
	index = 0
	while (index < lenarray(Sfiles)) do
		Sfile = Sfiles[index]
		Sfileext fileextension Sfile
		Sfileext strlower Sfileext
		isame strcmp Sfileext, Sextension
		if (isame == 0) then
			if (index == 0) then
				Sfiltered[index] = Sfile
			else
				Sfiltered[] arrayappend Sfiltered, Sfile
			endif
		endif
		index += 1
	od
	xout Sfiltered
endop


; Selected randarrayext Sfiles[], Sext
; get a random file from Sfiles[] where the extension is Sext
; directory opcode does not do this correctly (would select file.wav.asd when ".wav" is supplied as extension)
opcode randarrayext, S, S[]S
	Sfiles[], Sext xin
	Selected = "default"
	ifindex = 0
	while (ifindex < lenarray(Sfiles)) do
		index random 0, lenarray(Sfiles) - 1
		Sfile = Sfiles[index]
		Sfileext fileextension Sfile
		isame strcmp Sfileext, Sext
		if (isame == 0) then
			Selected = Sfile
			goto return
		endif
		ifindex += 1
	od
return:
	xout Selected
endop


; Spath randfile Spath, Sextension
opcode randfile, S, SS
	Sdirectory, Sextension xin
	Sfiles[] directory Sdirectory
	Spath  randarrayext Sfiles, Sextension
	xout Spath
endop


; aL, aR sndplay Spath, ipitch, ioffset=0
; play a sound file with mono/stereo detection using pitch and optional offset
opcode sndplay, aa, Sio
	Spath, ipitch, ioffset xin
	ichn filenchnls Spath
	if (ichn == 1) then
		aL diskin2 Spath, ipitch, ioffset
		aR = aL
	else
		aL, aR diskin2 Spath, ipitch, ioffset
	endif
	xout aL, aR
endop


; aL, aR randplay Sbasepath, ipitch
; play a random wav file from Sbasepath with pitch control and mono/stereo detection
opcode randplay, aa, Si
	Sbase, ipitch xin 
	Spath randfile Sbase, "wav"
	aL, aR sndplay Spath, ipitch
	xout aL, aR
endop


; ifn filetotable Spath, ichannel=0
; read a sound file to a GEN01 table and return the ftable. global tracking of ftables so the same sound is not created twice
; 	optional channel to read from (defaults to 0 = all channels)
gisoundfiletablemax = 2048 	; max number of files that can be loaded to ftables
gisoundfiletableindex = 0	; using long variable names as they're global to reduce chance of a user collision
gSsoundfiletables[] init gisoundfiletablemax  ; maybe replace with arrayappend depending on performance
opcode filetotable, i, So
	Spath, ichannel xin
	iftincrement = 20  ; ftables will start being created at this ftable number
	ifn = 0
	index = 0

	; check if the sound exists in a table already, if so return that f number
	iarraymax = lenarray(gSsoundfiletables)
	while (index < iarraymax) do
		isame strcmp gSsoundfiletables[index], Spath
		if (isame == 0) then
			ifn = index + iftincrement
			goto return
		endif
		index += 1
	od
	
	; get file information
	iseconds filelen Spath
	isr filesr Spath
	ibits filebit Spath  ; just assuming 16 (4), maybe use this later if required
	isize = round(iseconds * isr) - 1
	
	; create the GEN01 ftable
	ifn = gisoundfiletableindex + iftincrement
	ix ftgen ifn, 0, isize, 1, Spath, 0, 4, ichannel
	gSsoundfiletables[gisoundfiletableindex] = Spath
	gisoundfiletableindex += 1
	goto return

return:
	xout ifn
endop


; isamples[], itimes[] loadallwavtotables Sfiles[], ichannel=0
; load all wav files in Sfiles array to ftables, providing the ftable numbers in isamples[] and the file length in itimes[]
; default channel=0, ie read all channels
opcode filestotables, i[]i[], S[]o
	Sfiles[], ichannel xin
	ilen = lenarray(Sfiles)
	isamples[] init lenarray(Sfiles)
	itimes[] init lenarray(Sfiles)
	index = 0
	while (index < lenarray(Sfiles)) do
		ifn filetotable Sfiles[index], ichannel
		isamples[index] = ifn
		ifilesr filesr Sfiles[index] 
		if (ichannel != 0) then
			itimemul = 1
		else 
			itimemul = 0.5
		endif
		itimes[index] = (ftlen(ifn) / ifilesr) * itimemul ; sr
		index += 1
	od
	xout isamples, itimes
endop


; isamples[], itimes[] loadallwavtotables Spath, ichannel=0
; load all wav files in Spath to ftables, providing the ftable numbers in isamples[] and the file length in itimes[]
; default channel=0, ie read all channels
opcode loadallwavtotables, i[]i[], So
	Spath, ichannel xin
	Sfiles[] directoryfiles Spath, "wav"
	isamples[], itimes[] filestotables Sfiles, ichannel
	xout isamples, itimes
endop


; aL, aR rndpanlpfplay iamp, ifn, iduration, kpitch, [ilpfmin=300], [ilpfmax=12000]
; play a sound in ftable number ifn for duration iduraction with amp iamp and pitch kpitch
; 	apply a random panning amount, and a random low pass filter constrained to within optional parameters ilpfmin and ilpfmax
opcode rndpanlpfplay, aa, iiikjj
	iamp, ifn, iduration, kpitch, ilpfmin, ilpfmax xin
	if (ilpfmin == -1) then
		ilpfmin = 300
	endif

	if (ilpfmax == -1) then
		ilpfmax = 12000
	endif
	
	ain loscil iamp, kpitch, ifn, 1
	ilpffreq random ilpfmin, ilpfmax
	afilt butterlp ain, ilpffreq
	kamp linseg 1, iduration * 0.95, 1, iduration * 0.05, 0
	aout = afilt * kamp
	aL, aR randpan aout
	xout aL, aR
endop