/* diff.c -- 97/03/24 IF (based on Dale Stammen's diff) ** 98/01/14 for PowerPC only ** This external defines an object similar to the standard "-" Max object. ** The diff object has 2 inlets and 1 outlet. Left inlet accepts bang and integers, ** right inlet accepts integers, and outlet outputs the difference of the 2 inputs. */ #include "ext.h" // Required for all Max external objects void *this_class; // Required. Global pointing to this class typedef struct _diff // Data structure for this object { t_object d_ob; // Must always be the first field; used by Max long d_valleft; // Last value from left outlet long d_valright; // Last value from right outlet long d_valtotal; // Value to be sent to outlet void *d_out; // Pointer to outlet, need one for each outlet } t_diff; // Prototypes for methods: you need a method for each message you want to respond to void *diff_new(long value); // Object creation method void diff_int(t_diff *diff, long value); // Method for message "int" in left inlet void diff_in1(t_diff *diff, long value); // Method for message "int" in right inlet void diff_bang(t_diff *diff); // Method for bang message int main(void) // main receives a copy of the Max function macros table { // set up our class: create a class definition setup((t_messlist **) &this_class, (method)diff_new, 0L, (short)sizeof(t_diff), 0L, A_DEFLONG, 0); addbang((method)diff_bang); // bind method "diff_bang" to the "bang" message addint((method)diff_int); // bind method "diff_int" to int's received in the left inlet addinx((method)diff_in1,1); // bind method "diff_in1" to int's received in the right inlet return (0); } /********************************************************************************** diff_new(long value) inputs: value -- the integer from the typed argument in the object box description: creates a new instance of our class diff. Called once when the external object is loaded. returns: pointer to new instance *************************************************************************************/ void *diff_new(long value) { t_diff *diff; diff = (t_diff *)newobject(this_class); // Create new instance and return a pointer to it diff->d_valright = value; // Initialize the difference value diff->d_valleft = 0; diff->d_valtotal = -value; diff->d_out = intout(diff); // Create our outlet intin(diff, 1); // Create the right inlet return(diff); // Must return a pointer to the new instance } /********************************************************************************** diff_int(t_diff *a, long value) inputs: diff - pointer to t_diff object value - value received in the inlet description: substracts the right value with the incoming value. Stores the new left inlet value as well as the total and outputs the total. returns: nothing ***********************************************************************************/ void diff_int(t_diff *diff, long value) { diff->d_valleft = value; // Store the value received in the left inlet diff->d_valtotal = diff->d_valleft - diff->d_valright; // Subtracts the right inlet // value from the left diff_bang(diff); // Call bang method right away since it's the left inlet } /************************************************************************************* diff_in1(t_diff *diff, long value) inputs: diff - pointer to our object value - value received in the inlet description: stores the new right value, calculates and stores the new difference between the left and right value returns: nothing *************************************************************************************/ void diff_in1(t_diff *diff,long value) { diff->d_valright = value; // Store the value diff->d_valtotal = diff->d_valleft - value; // Update new difference } /************************************************************************************* diff_bang(t_diff *a) inputs: diff - pointer to our object description: method called when bang is received: it outputs the current difference of the left and right values returns: nothing *************************************************************************************/ void diff_bang(t_diff *diff) { outlet_int(diff->d_out, diff->d_valtotal); // put out the current total }