DSPRelated.com
Free Books

A Look at the Generated C++ code

Running Faust with no architecture file, e.g.,

  > faust cpgrir.dsp
causes the C++ signal-processing code to be printed on the standard output, as shown for this example in Fig.K.6. Notice how the constant subexpressions are computed only once per instance in the instanceInit member function. Also, even in instanceInit, which is only called once, repeated computation of $ R^2$ is avoided by use of the temporary variable fConst1, and division by 2 is converted to multiplication by $ 0.5$.

Figure: C++ code emitted by the shell command
faust cpgrir.dsp (and reformatted slightly).

 
  class mydsp : public dsp {
  private:
    float fConst0;    float fConst1;
    int   iVec0[2];   float fConst2;
    int   iVec1[3];   float fConst3;   float fRec0[3];
  public:
    virtual int getNumInputs()    { return 0; }
    virtual int getNumOutputs()   { return 1; }
    static void classInit(int samplingFreq) { }
    virtual void instanceInit(int samplingFreq) {
      fSamplingFreq = samplingFreq;
      fConst0 = expf((0 - (314.159271f / fSamplingFreq)));
      fConst1 = (fConst0 * fConst0);
      for (int i=0; i<2; i++) iVec0[i] = 0;
      fConst2 = ((2 * fConst0) * cosf((2764.601562f
                               / fSamplingFreq)));
      for (int i=0; i<3; i++) iVec1[i] = 0;
      fConst3 = (0.500000f * (1 - fConst1));
      for (int i=0; i<3; i++) fRec0[i] = 0; }
    virtual void init(int samplingFreq) {
      classInit(samplingFreq);
      instanceInit(samplingFreq); }
    virtual void buildUserInterface(UI* interface) {
      interface->openVerticalBox("faust");
      interface->closeBox(); }
    virtual void compute (int count, float** input,
                                     float** output) {
      float* output0 = output[0];
      for (int i=0; i<count; i++) {
        iVec0[0] = 1;
        int iTemp0 = iVec0[1];
        iVec1[0] = (1 - iTemp0);
        fRec0[0] = (((fConst3 * (1 - (iTemp0 + iVec1[2])))
                                   + (fConst2 * fRec0[1]))
                                   - (fConst1 * fRec0[2]));
        output0[i] = fRec0[0];
        // post processing
        fRec0[2] = fRec0[1]; fRec0[1] = fRec0[0];
        iVec1[2] = iVec1[1]; iVec1[1] = iVec1[0];
        iVec0[1] = iVec0[0];
      }
    }
  };


Next Section:
Generating a Pure Data (PD) Plugin
Previous Section:
Testing a Faust Filter Section