batchNRT A method for batch-processing audio files in NRT mode


Score.batchNRT(inputpath, outputpath, synthdefname)


If you have a folder full of sound files, and a SynthDef defining the process you want to be applied to the sound files, this method can do it for you.


The method is asynchronous, so it will return quickly but the Routine which is managing the process will carry on in the background. A message is posted (and optionally, a function evaluated) when the processing is complete.


The first three parameters must be specified:


inputpath - Path pattern for input files. For example, "~/data/*.wav" to load all wave files from one folder.

outputpath - Path for the folder in which output files are to be written. This must exist. Output will overwrite existing files if the filenames match.

synthdefname - The name of a SynthDef you have previously created and stored. It must take the following arguments: inbufnum, outbufnum (for the input and output buffer numbers, respectively)


By default, the output buffer is created with the same length as the input buffer, and the synth will be played for exactly that duration. If you want to change this (e.g. make it control rate, or make it slightly longer to account for reverberation, etc) you can supply outputlengthfunc, a function which will deduce the output buffer length (IN FRAMES) from an input file's length in seconds, and/or synthdurfunc, which will do the same for the synth running time (returning a time IN SECONDS). 


The input and output are assumed to be single-channel. You can change this using the inchannels and outchannels arguments. Processing will typically fail if you don't ensure that the number of in/out channels matches those in the SynthDef and the input files.


Other parameters:


synthparams - Additional parameters for the synth

plot - Set to true to automatically plot the results

outputnameadd - Append some text to the output filenames (before the file extension)

maxtimeperfile - Specify a "safety limit", limiting each file's NRT processing time to a maximum number of seconds

extrainitcommands - an array of extra commands you'd like prepending to the list

action - a function to be evaluated once all the processing is complete. It will be passed a list of paths for the files created.

opts - a ServerOptions object (NB options for number of output busses will be overwritten).

really - set this to false to skip the NRT part of the process (in other words, just perform the plotting/action). Useful for debug or rerunning actions.


See also: [Score], [Non-Realtime-Synthesis].




Examples:


The first example is audio-rate, batch-processing a set of sounds to add reverb to them.


// First we'll create a SynthDef for adding a bit of reverb to a sound

(

SynthDef(\help_addalittlereverb, { |inbufnum, outbufnum, gain = 0.2|

var source;

source = PlayBuf.ar(1, inbufnum, BufRateScale.kr(inbufnum));

4.do({ source = AllpassN.ar(source, 0.050, [Rand(0, 0.05), Rand(0,0.05)], 1) } );

RecordBuf.ar([source * gain], outbufnum, loop:0);

Out.ar(0, (source * gain).dup);

}).store;

)

// Now we'll run the SynthDef over a folder full of samples.

// Of course you need a folder with some sound files in,

// and also an output folder. You may need to alter the paths given in this code.

(

Score.batchNRT(

"~/svn/soundsamples/beatboxes/*.wav", // Input path pattern - note the asterisk wildcard

"~/dataoutput/addreverb", // Output path

\help_addalittlereverb,

outputnameadd: "-verbed",

outputlengthfunc: {|indur| (indur + 2) * 44100}, // Allow 2 seconds at end of file for reverberations

synthparams: [\gain, 0.9],

// opts: ServerOptions.new.verbosity_(-1), // Reduces amount of info the server posts

synthdurfunc: {|indur| indur + 2} // Allow 2 seconds at end of file for reverberations

);

)



The second example is control-rate, for extracting a parameter (here the amplitude envelope) of the sounds. It also plots the results.


// First the synthdef

(

SynthDef(\help_param, { |inbufnum, outbufnum|

var source, amp, freq, hasfreq;

source = PlayBuf.ar(1, inbufnum, BufRateScale.kr(inbufnum));

amp = Amplitude.kr(source);

BufWr.kr(amp, outbufnum, Phasor.ar(0, BufRateScale.kr(outbufnum) * ControlRate.ir / SampleRate.ir, 0, BufFrames.kr(outbufnum)), 0);

Out.ar(0, source.dup);

}).store;

)

// Then we process the files

(

Score.batchNRT(

"~/svn/soundsamples/beatboxes/*.wav", // Input path pattern - note the asterisk wildcard

"~/dataoutput/parameters", // Output path

\help_param,

outputnameadd: "-parameters",

outputlengthfunc: {|indur| ((indur) * 44100 / 64).postln}, // Note the way the durations are scaled to the (standard) control rate

plot:true

);

)