#include #include #if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 401) #error You need at least flext version 0.4.1 #endif #define THRESHOLD 0.0056 //thershold of -30 dB // A flext dsp external ("tilde object") inherits from the class flext_dsp class thresh: public flext_dsp { // Each external that is written in C++ needs to use #defines // from flbase.h // // The define // // FLEXT_HEADER(NEW_CLASS, PARENT_CLASS) // // should be somewhere in your dsp file. // A good place is here: FLEXT_HEADER(thresh, flext_dsp) public: thresh() { // The constructor of your class is responsible for // setting up inlets and outlets and for registering // inlet-methods: // The descriptions of the inlets and outlets are output // via the Max/MSP assist method (when mousing over them in edit mode). // PD will hopefully provide such a feature as well soon AddInSignal("audio in"); // audio in AddOutFloat("true/false out"); // bool out // We're done constructing: post("threshold detector"); max = 0; } // end of constructor protected: // here we declare the virtual DSP function virtual void m_signal(int n, float *const *in, float *const *out); private: float max; }; // end of class declaration // Before we can run our class in PD, the object has to be registered as a // PD object. Otherwise it would be a simple C++-class, and what good would // that be for? Registering is made easy with the FLEXT_NEW_* macros defined // in flext.h. For tilde objects without arguments call: FLEXT_NEW_DSP("thresh~", thresh) // Now we define our DSP function. It gets this arguments: // // int n: length of signal vector. Loop over this for your signal processing. // float *const *in, float *const *out: // These are arrays of the signals in the objects signal inlets rsp. // oulets. We come to that later inside the function. void thresh::m_signal(int n, float *const *in, float *const *out) { //declare local variables const float *ins = in[0]; float current_max = 0; for (int i=0; i current_max) current_max = fabs(ins[i]); } if (current_max > THRESHOLD && current_max > max) { ToOutFloat(0,1); } else ToOutFloat(0, 0); max = current_max; } // end m_signal