aboutsummaryrefslogtreecommitdiff
path: root/site/udo/sounddb.udo
blob: 91f6f10740dcc8597918e52fdf69950ce359cb39 (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
#ifndef UDO_SOUNDDB
#define UDO_SOUNDDB ##
/*
	SQL database interface to sound object management.

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

*/

; if XDB extract has been loaded, don't use database
#ifdef XDB_SET
#include "soundxdb.udo"
#else

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

; set max number of files for global array allocation
imaxindex dbscalar gidb, "SELECT MAX(id)+1 FROM file"
gisounddb[][] init imaxindex, 4


/*
	Load file to gisounddb: to be used internally and passed parameters from database
	
	_sounddb_loadfile ifileid, Spath, ichannels, iduration, irmsnorm, isamplerate [, imono=0]

	ifileid			database file ID, corresponds to index of gisounddb
	Spath			path to load sound file from
	ichannels		number of channels
	iduration		sound duration
	irmsnorm		normalisation factor
	isamplerate		sample rate
	imono			whether to load sound as mono
*/
opcode _sounddb_loadfile, 0, iSiiiio
	ifileid, Spath, ichannels, iduration, irmsnorm, isamplerate, imono xin
	if (imono == 1) then
		ichannels = 1
		iloadchan = 1
	else
		iloadchan = 0
	endif
	isize = iduration * isamplerate * ichannels; HACK: grain cannot use deferred time: TODO: pass in samplerate and length to opcode (TODO: database needs samplerate)
	ifn = ftgen(0, 0, isize, 1, Spath, 0, 0, iloadchan)
	gisounddb[ifileid][0] = ifn
	gisounddb[ifileid][1] = ichannels
	gisounddb[ifileid][2] = iduration
	gisounddb[ifileid][3] = irmsnorm
endop


/*
	Get file details for a give file ID

	ifn, ichannels, iduration, irmsnorm sounddb_get ifileid

	ifn			ftable number containing sound
	ichannels	number of channels in file
	iduration	duration of file in seconds
	irmsnorm	RMS normalisation factor
	ifileid		file ID to look up
*/
opcode sounddb_get, iiii, i
	ifileid xin
	xout gisounddb[ifileid][0], gisounddb[ifileid][1], gisounddb[ifileid][2], gisounddb[ifileid][3]
endop


/*
	Load files to gisounddb if not already loaded, to be passed a 2D string array as returned from a database query. Returns the file IDs in an array

	ifileids[] _sounddb_loadobject SqueryResult[][] [, imono=0]

	ifileids[]			database file IDs, which also correspond to indexes in gisounddb
	SqueryResult[][]	query result from database with each row containing file ID, path, channels, duration, RMS normalisation factor and samplerate
	imono				whether to load all sounds as mono
*/
opcode _sounddb_loadobject, i[], S[][]o
	Sres[][], imono xin
	iarraylength = lenarray(Sres)
	idata[] init iarraylength
	index = 0
	while (index < iarraylength) do
		ifileid strtod Sres[index][0] ; fileid
		idata[index] = ifileid

		if (gisounddb[ifileid][0] == 0) then ; load required
			_sounddb_loadfile ifileid, Sres[index][1], strtod(Sres[index][2]), strtod(Sres[index][3]), strtod(Sres[index][4]), strtod(Sres[index][5]), imono
		endif	
		index += 1
	od
	xout idata
endop


/*
	Load a sound to gisounddb if not already loaded, based on a specified query using f_nearestnote.
	Return the file ID and the result of column 6, which is the ratio to the nearest pitch requested.
	Used internally by the sounddb_mel_nearestnote opcodes which select one row

	ifileid, ipitchratio _sounddb_mel_nearestnote_inner Squery

	ifileid			file ID
	ipitchratio		pitch ratio to note requested
	Squery			query to evaluate
*/
opcode _sounddb_mel_nearestnote_inner, ii, S
	Squery xin
	Sres[][] dbarray gidb, Squery
	ifileid strtod Sres[0][0]

	if (gisounddb[ifileid][0] == 0) then ; load required
		_sounddb_loadfile ifileid, Sres[0][1], strtod(Sres[0][2]), strtod(Sres[0][3]), strtod(Sres[0][4]), strtod(Sres[0][5])
	endif	
	xout ifileid, strtod(Sres[0][6])
endop


; nearest note query base
#define SOUNDDB_NNQUERYBASE #SELECT file_id, f_localpath(%d, path), channels, duration, rmsnormal, samplerate, pitchratio FROM f_nearestnote#


/*
	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
	ifileid, ipitchratio _sounddb_mel_nearestnote_inner sprintf("$SOUNDDB_NNQUERYBASE (%f, '%s')", gihost_type, inote, Scollection)
	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 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
	ifileid, ipitchratio _sounddb_mel_nearestnote_inner sprintf("$SOUNDDB_NNQUERYBASE (%f, %d)", gihost_type, inote, icollectionid)
	xout ifileid, ipitchratio
endop


/*
	List file collections

	Sout[][]		list of file collection name and type
*/
opcode sounddb_listcollections, S[][], 0
	Squery = "select fc.name, fct.name from filecollection fc join filecollectiontype fct on fc.type_id = fct.id"
	Sout[][] dbarray gidb, Squery
	xout Sout
endop


/*

*/
opcode sounddb_getbypathpart, i[], S
	SpathPart xin
	Sbase = {{select file_id,  f_localpath(%d, path), channels, duration, rmsnormal, samplerate
		from svw.analysis_basic_collectionnorm
		where path ilike '%s'
	}}
	Squery = sprintf(Sbase, gihost_type, SpathPart)
	Sres[][] dbarray gidb, Squery
	idata[] _sounddb_loadobject Sres
	xout idata
endop


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

	ifileids[], icollectionid sounddb_getcollection Scollection [, imono=0]

	ifileids[]		file IDs in the collection, accessible as indexes to f-tables in gisounddb
	icollectionid	collection ID
	Scollection		collection name
	imono			whether to load all sounds in mono
*/
opcode sounddb_getcollection, i[]i, So
	Scollection, imono xin
	Sbase = {{select file_id, f_localpath(%d, path), channels, duration, rmsnormal, samplerate, fc.id
		from svw.analysis_basic_collectionnorm a
		join filecollection fc on fc.id = a.filecollection_id
		where %s
	}}

	if (strindex(Scollection, ",") > 0) then
		Sclause = "(1=2"
		index = 1
		Stemp = Scollection
		while (index > 0) do
			index strindex Stemp, ","
			if (index > 0) then
				Sclause strcat Sclause, sprintf(" OR fc.name='%s'", strsub(Stemp, 0, index))		
				Stemp strsub Stemp, index+1
			else
				Sclause strcat Sclause, sprintf(" OR fc.name='%s'", Stemp)
			endif
		od
		Sclause strcat Sclause, ")"
	else
		Sclause = sprintf("fc.name = '%s'", Scollection)
	endif

	Squery sprintf Sbase, gihost_type, Sclause
	Sres[][] dbarray gidb, Squery
	idata[] _sounddb_loadobject Sres, imono
	icollectionid = strtod(Sres[0][6])
	xout idata, icollectionid
endop


/*
	Get the ID of a filecollection by name

	icollectionid sounddb_getcollectionid Scollection [, ipreload = 0, imono = 0]

	icollectionid	collection ID
	Scollection		collection name
	ipreload		preload the files to gisounddb
	imono			whether to load all sounds in mono
*/
opcode sounddb_getcollectionid, i, Soo
	Scollection, ipreload, imono xin
	if (ipreload == 0) then
		icollectionid = dbscalar(gidb, sprintf("SELECT id FROM filecollection WHERE name = '%s'", Scollection))
	else
		i_[], icollectionid sounddb_getcollection Scollection, imono
	endif
	xout icollectionid
endop

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

	ifileids[] sounddb_getcollection Scollection [, imono = 0]

	ifileids[]		file IDs in the collection, accessible as indexes to f-tables in gisounddb
	Scollection		collection name
	imono			whether to load all sounds in mono
*/
opcode sounddb_getcollection, i[], So
	Scollection, imono xin
	idata[], icollectionid sounddb_getcollection Scollection, imono
	xout idata
endop

; end of XDB_SET
#end

#end