#ifndef UDO_RUNTIMEDB #define UDO_RUNTIMEDB ## /* Runtime sound database manager This file is part of the SONICS UDO collection by Richard Knight 2021 License: GPL-2.0-or-later http://1bpm.net */ #include "string_tools.udo" /* Runtime sound database array structure: 0 ftable number 1 file id 2 number of channels 3 duration */ gisoundsdb[][] init 99999, 4 gisoundsdbmax init 0 /* Load a sound to the runtime sounds db array, or return the index if already loaded (by file id tracking) For pgdb usage index loadsound ifileid, ichannels, iduration, Spath index sound database index ifileid database file id, for tracking ichannels number of channels in file iduration duration of file Spath path to the file */ opcode _rdb_loadsound, i, iiiS ifileid, ichannels, iduration, Spath xin index = 0 ifn = -1 ;goto loadrequired ; HACK, 32bit fail on hash while (index < gisoundsdbmax) do if (gisoundsdb[index][0] == 0) then ; nothing loaded at all igoto loadrequired ; give up now, don't go through all of array endif if (gisoundsdb[index][1] == ifileid) then igoto complete endif index += 1 od loadrequired: if (ifn == -1) then isize = filelen(Spath) * filesr(Spath) * filenchnls(Spath); 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, 0) index = gisoundsdbmax gisoundsdb[index][0] = ifn gisoundsdb[index][1] = ifileid gisoundsdb[index][2] = ichannels gisoundsdb[index][3] = iduration gisoundsdbmax += 1 endif complete: xout index endop /* Load a sound to the runtime sounds db array, or return the index if already loaded (by file id tracking) For direct FS usage index loadsound Spath index sound database index Spath path to the file */ opcode rdb_loadsound, i, S Spath xin ifileid str_hash Spath ichannels = filenchnls(Spath) iduration = filelen(Spath) print iduration xout _rdb_loadsound(ifileid, ichannels, iduration, Spath) endop /* Load a directory of sounds with suffix .wav and return an array of the runtime db indexes indexes[] rdb_loaddir Spath indexes[] sound database indexes Spath directory path */ opcode rdb_loaddir, i[], S Spath xin Sfiles[] directory Spath, ".wav" isounds[] init lenarray(Sfiles) index = 0 while (index < lenarray(Sfiles)) do isounds[index] rdb_loadsound Sfiles[index] index += 1 od xout isounds endop /* Get sound array from sound database isound[] get_sound index isound[] the runtime sound database entry index sound index as provided by load_sound */ opcode get_sound, i[], i index xin ;xout getrow(gisoundsdb, index) ; 32bit fail! ilen lenarray gisoundsdb, 2 idata[] init ilen index2 = 0 while (index2 < ilen) do idata[index2] = gisoundsdb[index][index2] index2 += 1 od xout idata endop /* Get sound array from sound database ifn, ifileid, inchnls, idur get_sound index ifn ftable of sound ifileid file id inchnls number of channels idur duration in seconds index sound index as provided by load_sound */ opcode get_sound, iiii, i index xin xout gisoundsdb[index][0], gisoundsdb[index][1], gisoundsdb[index][2], gisoundsdb[index][3] endop #end