Free Books

Generating a MIDI Synthesizer for PD

The faust2pd script (introduced in §K.5 above) also has a mode for generating MIDI synthesizer plugins for pd. This mode is triggered by use of the -n option (``number of voices''). For this mode, the Faust program should be written to synthesize one voice using the following three parameters (which are driven from MIDI data in the pd plugin):

  • freq - frequency of the played note (Hz)
  • gain - amplitude of the played note (0 to 1)
  • gate - 1 while ``key is down'', 0 after ``key up''
The parameters freq and gain are set according to MIDI note-number and velocity, respectively, while the gate parameter is set to 1 on a MIDI ``note-on'' and back to zero upon ``note-off''. The faust2pd script handles instantiation of up to 8 instances of the synth patch, and provides the abstraction midi-in.pd for receiving and decoding MIDI data in pd.

Let's make a simple 8-voiced MIDI synthesizer based on the example Faust program cpgrs.dsp (``Constant-Peak-Gain Resonator Synth'') listed in Fig.K.15. In addition to converting the frequency and gain parameters to the standard names, we have added a classic ADSR envelope generator (defined in Faust's music.lib file) which uses the new gate parameter, and which adds the four new envelope parameters attack, decay, sustain, and release.

Compiling the example is the same as for a pd plugin, except that the -n option is used (8 voices is the maximum):

  > faust -xml -a puredata.cpp -o cpgrs-pd.cpp cpgrs.dsp
  > g++ -DPD -Wall -g -shared -Dmydsp=cpgrs \
        -o cpgrs~.pd_linux cpgrs-pd.cpp
  > faust2pd -n 8 -s -o cpgrs.pd cpgrs.dsp.xml

Figure K.15: Listing of cpgrs.dsp--a Faust program specifying a simple synth patch consisting of white noise through a constant-peak-gain resonator.

  declare name "Constant-Peak-Gain Resonator Synth";
  declare author "Julius Smith";
  declare version "1.0";
  declare license "GPL";

  /* Standard synth controls supported by faust2pd */
  freq = nentry("freq", 440, 20, 20000, 1);	// Hz
  gain = nentry("gain", 0.1, 0, 1, 0.01);	// frac
  gate = button("gate");			// 0/1

  /* User Controls */
  bw = hslider("bandwidth (Hz)", 100, 20, 20000, 10);

  import("music.lib"); // define noise, adsr, PI, SR, et al.

  /* ADSR envelope parameters */
  attack  = hslider("attack", 0.01,0, 1, 0.001); // sec
  decay	  = hslider("decay",  0.3, 0, 1, 0.001); // sec
  sustain = hslider("sustain",0.5, 0, 1, 0.01);	 // frac
  release = hslider("release",0.2, 0, 1, 0.001); // sec

  /* Synth */
  process = noise * env * gain : filter
  with {
    env = gate :
                 adsr(attack, decay, sustain, release));
    filter = vgroup("2-filter", (firpart : + ~ feedback));
    R = exp(0-PI*bw/SR); // pole radius [0 required]
    A = 2*PI*freq/SR;      // pole angle (radians)
    RR = R*R;
    firpart(x) = (x - x'') * ((1-RR)/2);
    // time-domain coeffs ASSUMING ONE PIPELINE DELAY:
    feedback(v) = 0 + 2*R*cos(A)*v - RR*v';

Next Section:
MIDI Synthesizer Test Patch
Previous Section:
Generating a VST Plugin via Faust