Max External Object Example: mydiff.c




This is the diff external object from Ich's Max/MSP Externals Tutorial, version 2.41, pages 7-8.

mydiff.c

#include "ext.h"

typedef struct {
   t_object maxData;     // Max/MSP data, MUST come first in struct
   long     leftval;     // first input value
   long     rightval;    // second input value
   void*    output;      // output bang function pointer
} MyObject;

void* objectdata = NULL;

void*  createObject (long value);
void   InputLeft   (MyObject* mo, long value);
void   InputRight  (MyObject* mo, long value);
void   Bang        (MyObject* mo);

void main(void) {
   setup((t_messlist**)&objectdata, (method)createObject, NULL, 
      sizeof(MyObject), NULL, A_DEFLONG, A_NOTHING);
   addbang((method)Bang);
   addint ((method)InputLeft);
   addint ((method)InputRight, 1);
}

void* createObject(long value) {
   MyObject *mo;
   mo = (MyObject*)newobject(objectdata);
   mo->leftval  = 0;
   mo->rightval = value; 
   mo->output   = intout(mo);
   intin(mo, 1);
   return mo;
}

void InputLeft(MyObject* mo, long value) {
   mo->leftval = value;
   Bang(mo);
}

void InputRight(MyObject* mo, long value) {
   mo->rightval = value;
}

void Bang(MyObject* mo) {
   outlet_int(mo->output, mo->leftval - mo->rightval);
}

Max functions of intrest in this object

setup
The setup function takes many inputs:
  1. t_messlist** data -- a pointer to the data struct which contains the t_object information used to communicate with the Max program.
  2. method creatingFunction -- a function used to inialize the data for the object.
  3. method destroyingFunction -- a function used to cleanup an object after Max is finished using it (not used in this example).
  4. short dataSize -- the number of bytes the object data struct.
  5. method menuFunction -- a function to handle menu/help information (not used in this example).
  6. ... -- Next comes a list of inputs.
  7. int A_NOTHING -- equal to the number 0. This indicates the end of the list of inputs.






Exercises

  1. Compile the mydiff object and test it in Max.

  2. Make an external object called myadd which adds two integer inputs instead of subtracts them.

  3. Make an external object called myadd3 which adds three integer inputs instead of two inputs.