aboutsummaryrefslogtreecommitdiff
path: root/sonics/soundxdb.udo
blob: 4e0ad61c8d67db2e07c7c2aa1224699f2e2f5f61 (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
#ifndef UDO_SOUNDXDB
#define UDO_SOUNDXDB ##
/*
	SQL database extract interface to sound object management.
	File containing extract definitions must be included before this.
	Slim excerpt for Partial Emergence

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

*/

#ifndef XDB_SET
prints "Database extract not defined; cannot continue.\n\n\n"
exitnow
#end


#ifndef XDB_MINNOTE
#define XDB_MINNOTE #0#
#end





/*
	Get the ID of a filecollection by name

	icollectionid sounddb_getcollectionid Scollection

	icollectionid	collection ID
	Scollection		collection name
*/
opcode sounddb_getcollectionid, i, S
	Scollection xin
	index = 0
	while (index < lenarray(gSxdb_collections)) do
		if (strcmp(gSxdb_collections[index], Scollection) == 0) then
			igoto complete
		endif
		index += 1
	od
	index = -1
complete:
	xout index
endop



/*
	Get the file IDs of a filecollection, also loading each file to gisounddb

	ifileids[] sounddb_getcollection Scollection

	ifileids[]		file IDs in the collection, accessible as indexes to f-tables in gisounddb
	Scollection		collection name
*/
opcode sounddb_getcollection, i[], S
	Scollection xin
	idata[], icollectionid sounddb_getcollection Scollection
	xout idata
endop





/*
	Get the collection ID and file IDs of a filecollection, also loading each file to gisounddb

	ifileids[], icollectionid sounddb_getcollection Scollection

	ifileids[]		file IDs in the collection, accessible as indexes to f-tables in gisounddb
	icollectionid	collection ID
	Scollection		collection name
*/
opcode sounddb_getcollection, i[]i, S
	Scollection xin
	itotalsize = 0
	if (strindex(Scollection, ",") > 0) then
		index = 1
		Stemp = Scollection
		while (index > 0) do
			index strindex Stemp, ","
			if (index > 0) then
				icollectionid = sounddb_getcollectionid(strsub(Stemp, 0, index))
				itotalsize += ftlen(gixdb_collectionsfn[icollectionid])	
				Stemp strsub Stemp, index+1
			else
				icollectionid = sounddb_getcollectionid(Stemp)
				itotalsize += ftlen(gixdb_collectionsfn[icollectionid])	
			endif
		od
		idata[] init itotalsize
		iwriteindex = 0
		index = 1
		Stemp = Scollection
		while (index > 0) do
			index strindex Stemp, ","
			if (index > 0) then
				icollectionid = sounddb_getcollectionid(strsub(Stemp, 0, index))
				ifn = gixdb_collectionsfn[icollectionid]
				ireadindex = 0
				while (ireadindex < ftlen(ifn)) do
					idata[iwriteindex] table ireadindex, ifn
					ireadindex += 1
					iwriteindex += 1
				od
				Stemp strsub Stemp, index+1
			else
				icollectionid = sounddb_getcollectionid(Stemp)
				ifn = gixdb_collectionsfn[icollectionid]
				ireadindex = 0
				while (ireadindex < ftlen(ifn)) do
					idata[iwriteindex] table ireadindex, ifn
					ireadindex += 1
					iwriteindex += 1
				od
			endif
		od

	else
		icollectionid = sounddb_getcollectionid(Scollection)
		idata[] tab2array gixdb_collectionsfn[icollectionid]
		igoto complete
	endif

complete:
	xout idata, icollectionid
endop




/*
	Get the nearest note in a filecollection, return the file ID and the pitch ratio adjustment required to the requested note.

	ifileid, ipitchratio sounddb_mel_nearestnote icollectionid, inote

	ifileid			file ID, corresponding to index in gisounddb
	ipitchratio		pitch ratio adjustment required to make the file match the requested note
	icollectionid	collection ID
	inote			MIDI note number
*/
opcode sounddb_mel_nearestnote, ii, ii
	icollectionid, inote xin
	irefindex = ((inote - $XDB_MINNOTE) + tab_i(icollectionid, gixdb_pitchrefoffset)) * 2
	iselected = round(random(tab_i(irefindex, gixdb_pitchreference), tab_i(irefindex+1, gixdb_pitchreference)))
	ifileid tab_i iselected, gixdb_pitchnotes
	ipitchratio tab_i iselected, gixdb_pitchadjust

	xout ifileid, ipitchratio
endop



/*
	Get the nearest note in a filecollection, return the file ID and the pitch ratio adjustment required to the requested note.

	ifileid, ipitchratio sounddb_mel_nearestnote Scollection, inote

	ifileid			file ID, corresponding to index in gisounddb
	ipitchratio		pitch ratio adjustment required to make the file match the requested note
	Scollection		collection name
	inote			MIDI note number
*/
opcode sounddb_mel_nearestnote, ii, Si
	Scollection, inote xin
	icollectionid = sounddb_getcollectionid(Scollection)
	ifileid, ipitchratio sounddb_mel_nearestnote icollectionid, inote
	xout ifileid, ipitchratio
endop



#end