Next  |  Prev  |  Up  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

Offline Processing of Soundfiles in FAUST

If you have the FAUST distribution and libsndfile installed on your computer, then you can simply say

  > faust2sndfile myprocessor.dsp
  > myprocessor input.wav output.wav
to produce soundfile output.wav from input.wav using myprocessor.dsp. The first line uses the shell script faust2sndfile, normally installed in /usr/local/bin/, to create the binary program myprocessor from FAUST source, and the second line runs myprocessor on input.wav, writing the processed signal to output.wav.

You can also set FAUST parameters (such as sliders and buttons) on the command line, and generate a soundfile with no input file:

  > mysynth -duration 1 -gate 1 -gain 0.5 -freq 440 output.wav
where of course myprocessor.dsp defines these UI elements. Note that the parameter names are made lower case and have spaces and such removed; you can find out what they became using the -help option:
  > myprocessor -h
    *** USAGE: myprocessor input_sfile output_sfile
    ...
    -duration [0..1 ] 
    -gate [0..1 ] 
    -gain [0..1 ] 
    -freq [20..10000 ]
Thus, the name of each parameter is printed along with its range.

Under the hood, the faust2sndfile script uses the FAUST architecture file sndfile.cpp, which in turn uses Erik de Castro Lopo's libsndfile library to process the channels of an input soundfile, producing a single output sound file containing all of the output channel signals.

For example, suppose the file gain-stereo.dsp contains the one-line FAUST program

  process = *(0.5),*(0.5);
Then the following command will compile it to C++:
  > faust -a sndfile.cpp gain-stereo.dsp > gain-stereo.cpp
The file gain-stereo.cpp contains
  #include <sndfile.h>
which is installed with the libsndfile distribution. The main function in gain-stereo.cpp is as follows:
int main(int argc, char *argv[] )
{
  SNDFILE*		in_sf;
  SNDFILE*		out_sf;
  SF_INFO			in_info;
  SF_INFO			out_info;

  CMDUI* interface = new CMDUI(argc, argv);
  DSP.buildUserInterface(interface);
  interface->process_command();

  // open input file
  in_info.format = 0;
  in_sf = sf_open (interface->input_file(), SFM_READ, &in_info);
  if (in_sf == NULL) { sf_perror(in_sf); exit(0); }

  // open output file
  out_info = in_info;
  out_info.format = in_info.format;
  out_info.channels = DSP.getNumOutputs();
  out_sf = sf_open(interface->output_file(), SFM_WRITE, &out_info);
  if (out_sf == NULL) { sf_perror(out_sf); exit(0); }


  // create separator and interleaver
  Separator sep (kFrames, in_info.channels, DSP.getNumInputs());
  Interleaver ilv (kFrames, DSP.getNumOutputs(), DSP.getNumOutputs());

  // init signal processor
  DSP.init(in_info.samplerate);
  //DSP.buildUserInterface(interface);
  interface->process_init();

  // process all samples
  int nbf;
  do {
          nbf = sf_readf_float(in_sf, sep.input(), kFrames);
          sep.separate();
          DSP.compute(nbf, sep.outputs(), ilv.inputs());
          ilv.interleave();
          sf_writef_float(out_sf, ilv.output(), nbf);
          //sf_write_raw(out_sf, ilv.output(), nbf);
  } while (nbf == kFrames);

  // close the input and output files
  sf_close(in_sf);
  sf_close(out_sf);
}

Thus, after opening the input and output soundfiles, there is a loop over time frames (sample times).20For each frame, the interleaved input channels are read from disk by sf_readf_float() and deinterleaved into a set of separate buffers by sep.separate(). The input buffers are processed by DSP.compute to produce output buffers, one for each output signal. The output buffers are then interleaved by ilv.interleave() and written to the output soundfile on disk by sf_writef_float(). After all time frames have been processed, the input and output soundfiles are closed.


Next  |  Prev  |  Up  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

Download aspf.pdf
[Comment on this page via email]

``Audio Signal Processing in Faust'', by Julius O. Smith III
Copyright © 2023-08-16 by Julius O. Smith III
Center for Computer Research in Music and Acoustics (CCRMA),   Stanford University
CCRMA