Max 5 API Reference
00001 /** 00002 @file 00003 plussz~ - a very simple example of a basic MSP object 00004 00005 updated 3/22/09 ajm: new API 00006 00007 @ingroup examples 00008 */ 00009 00010 #include "ext.h" 00011 #include "ext_obex.h" 00012 #include "z_dsp.h" 00013 00014 void *plussz_class; 00015 00016 typedef struct _plus 00017 { 00018 t_pxobject x_obj; 00019 t_float x_val; 00020 } t_plus; 00021 00022 void *plus_new(double val); 00023 t_int *offset_perform(t_int *w); 00024 t_int *plus2_perform(t_int *w); 00025 void plus_float(t_plus *x, double f); 00026 void plus_int(t_plus *x, long n); 00027 void plus_dsp(t_plus *x, t_signal **sp, short *count); 00028 void plus_assist(t_plus *x, void *b, long m, long a, char *s); 00029 00030 int main(void) 00031 { 00032 t_class *c; 00033 00034 c = class_new("plussz~", (method)plus_new, (method)dsp_free, (short)sizeof(t_plus), 0L, A_DEFFLOAT, 0); 00035 00036 class_addmethod(c, (method)plus_dsp, "dsp", A_CANT, 0); // respond to the dsp message 00037 // (sent to MSP objects when audio is turned on/off) 00038 class_addmethod(c, (method)plus_float, "float", A_FLOAT, 0); 00039 class_addmethod(c, (method)plus_int, "int", A_LONG, 0); 00040 class_addmethod(c, (method)plus_assist,"assist",A_CANT,0); 00041 class_dspinit(c); // must call this function for MSP object classes 00042 00043 class_register(CLASS_BOX, c); 00044 plussz_class = c; 00045 return 0; 00046 } 00047 00048 void plus_assist(t_plus *x, void *b, long m, long a, char *s) 00049 { 00050 if (m == ASSIST_INLET) { 00051 switch (a) { 00052 case 0: 00053 sprintf(s,"(Signal) This + Right Inlet"); 00054 break; 00055 case 1: 00056 sprintf(s,"(Signal) Left Inlet + This"); 00057 break; 00058 } 00059 } else 00060 sprintf(s,"(Signal) Addition Result"); 00061 } 00062 00063 void *plus_new(double val) 00064 { 00065 t_plus *x = object_alloc(plussz_class); 00066 dsp_setup((t_pxobject *)x,2); // set up DSP for the instance and create signal inlets 00067 outlet_new((t_pxobject *)x, "signal"); // signal outlets are created like this 00068 x->x_val = val; 00069 return (x); 00070 } 00071 00072 void plus_float(t_plus *x, double f) // the float and int routines cover both inlets. 00073 { // It doesn't matter which one is involved 00074 x->x_val = f; 00075 } 00076 00077 void plus_int(t_plus *x, long n) 00078 { 00079 x->x_val = (float)n; 00080 } 00081 00082 t_int *offset_perform(t_int *w) // our perform method if one signal inlet is connected 00083 { 00084 t_float *in = (t_float *)(w[1]); 00085 t_float *out = (t_float *)(w[2]); 00086 t_plus *x = (t_plus *)(w[3]); 00087 float val = x->x_val; 00088 int n = (int)(w[4]); 00089 00090 if (x->x_obj.z_disabled) 00091 goto out; 00092 00093 while (--n) *++out = val + *++in; 00094 out: 00095 return (w+5); 00096 } 00097 00098 t_int *plus2_perform(t_int *w) // our perform method if both signal inlets are connected 00099 { 00100 t_float *in1,*in2,*out; 00101 int n; 00102 00103 if (*(long *)(w[1])) // just doing this a different way for some variation 00104 goto out; 00105 00106 in1 = (t_float *)(w[2]); 00107 in2 = (t_float *)(w[3]); 00108 out = (t_float *)(w[4]); 00109 n = (int)(w[5]); 00110 00111 while (--n) *++out = *++in1 + *++in2; 00112 out: 00113 return (w+6); 00114 } 00115 00116 void plus_dsp(t_plus *x, t_signal **sp, short *count) // method called when dsp is turned on 00117 { 00118 if (!count[0]) 00119 dsp_add(offset_perform, 4, sp[1]->s_vec-1, sp[2]->s_vec-1, x, sp[0]->s_n+1); 00120 else if (!count[1]) 00121 dsp_add(offset_perform, 4, sp[0]->s_vec-1, sp[2]->s_vec-1, x, sp[0]->s_n+1); 00122 else 00123 dsp_add(plus2_perform, 5, &x->x_obj.z_disabled, sp[0]->s_vec-1, sp[1]->s_vec-1, sp[2]->s_vec-1, sp[0]->s_n+1); 00124 } 00125
Copyright © 2008, Cycling '74