| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include <sys/types.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <fcntl.h> |
| 34 |
#include <sys/stat.h> |
| 35 |
#ifndef __APPLE__ |
| 36 |
#include <malloc.h> |
| 37 |
#endif |
| 38 |
#include <ctype.h> |
| 39 |
#include <unistd.h> |
| 40 |
#include <time.h> |
| 41 |
#include <sys/time.h> |
| 42 |
|
| 43 |
#include <m_pd.h> |
| 44 |
|
| 45 |
static char *countund_version = "countund~: count up to a value and then down to zero : author : [email protected]"; |
| 46 |
|
| 47 |
static t_class *countund_class; |
| 48 |
|
| 49 |
typedef struct _countund |
| 50 |
{ |
| 51 |
t_object x_obj; |
| 52 |
t_int x_limit; |
| 53 |
t_int x_value; |
| 54 |
t_int x_up; |
| 55 |
} t_countund; |
| 56 |
|
| 57 |
|
| 58 |
static void countund_free(t_countund *x) |
| 59 |
{ |
| 60 |
} |
| 61 |
|
| 62 |
static void *countund_new(t_float flimit) |
| 63 |
{ |
| 64 |
t_countund *x = (t_countund *)pd_new(countund_class); |
| 65 |
outlet_new(&x->x_obj, &s_float); |
| 66 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("limit")); |
| 67 |
if ( flimit < 0 ) |
| 68 |
{ |
| 69 |
post( "countund~: wrong creation argument" ); |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
x->x_limit = (int) flimit; |
| 73 |
x->x_value = 0; |
| 74 |
x->x_up = 1; |
| 75 |
return(x); |
| 76 |
} |
| 77 |
|
| 78 |
static void *countund_limit(t_countund* x, t_float flimit) |
| 79 |
{ |
| 80 |
if ( flimit < 0 ) |
| 81 |
{ |
| 82 |
post( "countund~: wrong count limit" ); |
| •83 |
return NULL; |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
x->x_limit=(int)flimit; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static void *countund_bang(t_countund *x) |
| 92 |
{ |
| 93 |
|
| 94 |
if ( x->x_up ) |
| 95 |
{ |
| 96 |
x->x_value+=1; |
| 97 |
if (x->x_value>x->x_limit) |
| 98 |
{ |
| 99 |
x->x_value=x->x_limit-1; |
| 100 |
x->x_up=0; |
| 101 |
} |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
x->x_value-=1; |
| 106 |
if (x->x_value<0) |
| 107 |
{ |
| 108 |
x->x_value=1; |
| 109 |
x->x_up=1; |
| 110 |
} |
| 111 |
} |
| 112 |
outlet_float( x->x_obj.ob_outlet, x->x_value ); |
| •113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
void countund_setup(void) |
| 117 |
{ |
| •118 |
logpost(NULL, 4, "%s", countund_version); |
| 119 |
countund_class = class_new(gensym("countund"), (t_newmethod)countund_new, |
| 120 |
(t_method)countund_free, |
| 121 |
sizeof(t_countund), 0, A_DEFFLOAT, 0); |
| 122 |
class_addmethod( countund_class, (t_method)countund_bang, &s_bang, 0); |
| 123 |
class_addmethod( countund_class, (t_method)countund_limit, gensym("limit"), A_FLOAT, 0); |
| 124 |
} |