// // Copyright: Copyright 2003 Craig Stuart Sapp // Programmer: Craig Stuart Sapp // Creation Date: Mon Mar 10 14:39:14 PST 2003 // Last Modified: Tue Mar 11 06:14:27 PST 2003 // Filename: average.c // Web Address: http://peabody.sapp.org/class/dmp2/lab/average/average.c // Syntax: C; Max4/MSP2 External Object; CodeWarrior 6.0 // OS: Mac OS 9; PPC // // Description: An averaging filter. The filter takes the current // sample value and adds it to the last sample value and // then divides by 2. Thus this filter averages the // input signal. // #include "ext.h" #include "z_dsp.h" typedef struct { t_pxobject msp_data; // must always be the first field; used by MSP float lastinput; // the last signal value to come into the filter. float gain; // will be set to 0.5 for averaging. } MyObject; void* object_data; // pointer to data for object (created in setup()); // function declarations: void main (void); void* create_object (void); void MessageDSP (MyObject* mo, t_signal** signal, short* count); void MessageClear (MyObject* mo); t_int* Perform (t_int *parameters); ////////////////////////////////////////////////////////////////////////// // // Initialization functions: // void main(void) { setup((t_messlist**)&object_data, // the pointer for object data (method)create_object, // your function that initializes object (method)dsp_free, // your function that deinitializes object (short)sizeof(MyObject), // size of your object data struct NULL, // your menu function A_NOTHING); // A list of default parameters types // ending with A_NOTHING addmess((method)MessageDSP, "dsp", A_CANT, A_NOTHING); addmess((method)MessageClear, "clear", A_NOTHING); dsp_initclass(); } ////////////////////////////// // // create_object -- object initializing function. This function is // called by the setup() function inside of main(). Place code // here which will be used to initialized your data structure, // as well as the actual creation of the data storage itself. // The setup function will cause the pointer object_data to point // to the created data storage. // void* create_object(void) { MyObject *mo = (MyObject*)newobject(object_data); // create data storage dsp_setup((t_pxobject*)mo, 1); // inlet 1 outlet_new((t_pxobject*)mo, "signal"); // outlet 1 mo->gain = 0.5; // set gain for averaging filter. return mo; // return pointer to data storage } ////////////////////////////////////////////////////////////////////////// // // Behavior functions: // ////////////////////////////// // // MessageDSP -- What to do when a "dsp" message arrives into the object. // void MessageDSP(MyObject* mo, t_signal** signal, short* count) { #pragma unused(count) dsp_add(Perform, 5, 5, mo, signal[0]->s_vec, signal[1]->s_vec, signal[0]->s_n); } ////////////////////////////// // // MessageClear -- clear the memory of the past signal value (set it to 0). // void MessageClear(MyObject *mo) { mo->lastinput = 0.0; } ////////////////////////////// // // Perform -- do the calculations for one sample group. // t_int* Perform(t_int *parameters) { long pcount = (long) (parameters[1]); MyObject *mo = (MyObject*)(parameters[2]); t_float *input = (t_float*) (parameters[3]); t_float *output = (t_float*) (parameters[4]); long count = (long) (parameters[5]); long i; for (i=0; ilastinput) * mo->gain; mo->lastinput = input[i]; } return parameters+pcount+1; }