aboutsummaryrefslogtreecommitdiff
path: root/site/udo/convolutiondb.udo
blob: 55f0aef005386efb033bfcf40a6bc039a8365f58 (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
#ifndef UDO_CONVOLUTIONDB
#define UDO_CONVOLUTIONDB ##
/*
	SQL database interface to convolution impulse usage

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

*/


#include "pgdb.udo"
#include "host_tools.udo"

gSconvdbPaths[] init 1
gSconvdbNames[] init 1
giconvdbChannels[] init 1

opcode convdb_getimpulses, 0, 0
	Squery = {{
		select f.id, f_localpath(%d, path), replace(basename(f.path), '.wav', '') as name, s.channels 
		from file f 
		join sound s on f.id = s.file_id 
		join filecollectionrelation fcr on fcr.file_id = f.id 
		join filecollection fc on fc.id = fcr.filecollection_id 
		where fc.name = 'Impulses' 
		order by f.path;
	}}
	Sres[][] dbarray gidb, sprintf(Squery, gihost_type)
	ilen = lenarray(Sres)
	gSconvdbPaths[] init ilen
	gSconvdbNames[] init ilen
	giconvdbChannels[] init ilen
	index = 0
	while (index < ilen) do
		gSconvdbPaths[index] = Sres[index][1]
		gSconvdbNames[index] = Sres[index][2]
		giconvdbChannels[index] = strtod(Sres[index][3])
		index += 1
	od
endop

opcode convdb_getnames, S[], 0
	xout gSconvdbNames
endop

opcode convdb_getindexbyname, i, S
	Sname xin
	index = 0
	while (index < lenarray(gSconvdbNames)) do
		if (strcmp(gSconvdbNames[index], Sname) == 0) then
			goto complete
		endif
		index += 1
	od

complete:
	xout index
endop

opcode convdb_randomimpulseindex, i, 0
	xout round(random(0, lenarray(gSconvdbPaths)))
endop

opcode convdb_convolve, aa, ai
	ain, iimpulseindex xin
	SimpulsePath = gSconvdbPaths[iimpulseindex]
	aL pconvolve ain, SimpulsePath, -1, 1
	if (giconvdbChannels[iimpulseindex] == 2) then
		aR pconvolve ain, SimpulsePath, -1, 2
	else
		aR = aL
	endif
	xout aL, aR
endop


opcode convdb_convolve, aa, aai
	ainL, ainR, iimpulseindex xin
	SimpulsePath = gSconvdbPaths[iimpulseindex]
	aL pconvolve ainL, SimpulsePath, -1, 1
	if (giconvdbChannels[iimpulseindex] == 2) then
		aR pconvolve ainR, SimpulsePath, -1, 2
	else
		aL pconvolve ainR, SimpulsePath, -1, 1
	endif
	xout aL, aR
endop


opcode convdb_convolve, aa, aS
	ain, Sname xin
	impulseindex convdb_getindexbyname Sname
	aL, aR convdb_convolve ain, impulseindex
	xout aL, aR
endop

opcode convdb_convolve, aa, aaS
	ainL, ainR, Sname xin
	impulseindex convdb_getindexbyname Sname
	aL, aR convdb_convolve ainL, ainR, impulseindex
	xout aL, aR
endop


convdb_getimpulses()


#end