Max 5 API Reference
00001 /** 00002 @file 00003 dspstress~ - very simple msp object that does nothing except eat up a specified % of processor time 00004 00005 updated 6/5/09 rbs: initial 00006 00007 @ingroup examples 00008 */ 00009 00010 #include "ext.h" 00011 #include "ext_obex.h" 00012 #include "z_dsp.h" 00013 00014 void *dspstress_class; 00015 00016 typedef struct _dspstress 00017 { 00018 t_pxobject x_obj; 00019 double x_cpuusagetarget; // how much cpu to burn (0 - 100) 00020 double x_sr; 00021 double x_svs; 00022 double x_svtime_ms; // how long one signal vector takes in ms 00023 } t_dspstress; 00024 00025 void *dspstress_new(double val); 00026 t_int *dspstress_perform(t_int *w); 00027 void dspstress_float(t_dspstress *x, double f); 00028 void dspstress_int(t_dspstress *x, long n); 00029 void dspstress_dsp(t_dspstress *x, t_signal **sp, short *count); 00030 void dspstress_assist(t_dspstress *x, void *b, long m, long a, char *s); 00031 00032 int main(void) 00033 { 00034 t_class *c; 00035 00036 c = class_new("dspstress~", (method)dspstress_new, (method)dsp_free, (short)sizeof(t_dspstress), 0L, A_DEFFLOAT, 0); 00037 00038 class_addmethod(c, (method)dspstress_dsp, "dsp", A_CANT, 0); // respond to the dsp message 00039 // (sent to MSP objects when audio is turned on/off) 00040 class_addmethod(c, (method)dspstress_float, "float", A_FLOAT, 0); 00041 class_addmethod(c, (method)dspstress_int, "int", A_LONG, 0); 00042 class_addmethod(c, (method)dspstress_assist,"assist",A_CANT,0); 00043 class_dspinit(c); // must call this function for MSP object classes 00044 00045 class_register(CLASS_BOX, c); 00046 dspstress_class = c; 00047 return 0; 00048 } 00049 00050 void dspstress_assist(t_dspstress *x, void *b, long m, long a, char *s) 00051 { 00052 if (m == ASSIST_INLET) { 00053 switch (a) { 00054 case 0: 00055 strcpy(s,"Specify cpu usage % here"); 00056 break; 00057 } 00058 } 00059 } 00060 00061 void *dspstress_new(double val) 00062 { 00063 t_dspstress *x = object_alloc(dspstress_class); 00064 dsp_setup((t_pxobject *)x,1); // set up DSP for the instance and create signal inlet 00065 x->x_sr = 0; 00066 x->x_svs = 0; 00067 dspstress_float(x, val); 00068 return x; 00069 } 00070 00071 void dspstress_float(t_dspstress *x, double f) // the float and int routines cover both inlets. 00072 { // It doesn't matter which one is involved 00073 x->x_cpuusagetarget = f; 00074 } 00075 00076 void dspstress_int(t_dspstress *x, long n) 00077 { 00078 dspstress_float(x, (double) n); 00079 } 00080 00081 t_int *dspstress_perform(t_int *w) // our perform method 00082 { 00083 t_dspstress *x = (t_dspstress *)(w[1]); 00084 float spintime; 00085 double intime; 00086 double outtime; 00087 unsigned long spincounter = 0; 00088 00089 if (x->x_obj.z_disabled) 00090 goto out; 00091 00092 spintime = x->x_svtime_ms * x->x_cpuusagetarget / 100.; 00093 00094 intime = systimer_gettime(); 00095 outtime = intime + spintime; 00096 while (systimer_gettime() < outtime) 00097 { 00098 // tra la la 00099 spincounter++; // how high can we count? 00100 } 00101 00102 out: 00103 return (w+2); 00104 } 00105 00106 void dspstress_dsp(t_dspstress *x, t_signal **sp, short *count) // method called when dsp is turned on 00107 { 00108 dsp_add(dspstress_perform, 1, x); 00109 x->x_sr = sp[0]->s_sr; 00110 x->x_svs = sp[0]->s_n; 00111 x->x_svtime_ms = x->x_svs / x->x_sr * 1000.; 00112 } 00113
Copyright © 2008, Cycling '74