; 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