6 #include "../src/m_pd.h"
7 #include <../src/m_fixed.h>
9 static t_class
*tabplay_tilde_class
;
11 typedef struct _tabplay_tilde
19 t_symbol
*x_arrayname
;
23 static void tabplay_tilde_tick(t_tabplay_tilde
*x
);
25 static void *tabplay_tilde_new(t_symbol
*s
)
27 t_tabplay_tilde
*x
= (t_tabplay_tilde
*)pd_new(tabplay_tilde_class
);
28 x
->x_clock
= clock_new(x
, (t_method
)tabplay_tilde_tick
);
29 x
->x_phase
= 0x7fffffff;
32 outlet_new(&x
->x_obj
, &s_signal
);
33 x
->x_bangout
= outlet_new(&x
->x_obj
, &s_bang
);
37 static t_int
*tabplay_tilde_perform(t_int
*w
)
39 t_tabplay_tilde
*x
= (t_tabplay_tilde
*)(w
[1]);
40 t_sample
*out
= (t_sample
*)(w
[2]), *fp
;
41 int n
= (int)(w
[3]), phase
= x
->x_phase
,
42 endphase
= (x
->x_nsampsintab
< x
->x_limit
?
43 x
->x_nsampsintab
: x
->x_limit
), nxfer
, n3
;
44 if (!x
->x_vec
|| phase
>= endphase
)
47 nxfer
= endphase
- phase
;
48 fp
= x
->x_vec
+ phase
;
55 if (phase
>= endphase
)
57 clock_delay(x
->x_clock
, 0);
58 x
->x_phase
= 0x7fffffff;
62 else x
->x_phase
= phase
;
66 while (n
--) *out
++ = 0;
70 void tabplay_tilde_set(t_tabplay_tilde
*x
, t_symbol
*s
)
75 if (!(a
= (t_garray
*)pd_findbyclass(x
->x_arrayname
, garray_class
)))
77 if (*s
->s_name
) pd_error(x
, "tabplay~: %s: no such array",
78 x
->x_arrayname
->s_name
);
81 else if (!garray_getfloatarray(a
, &x
->x_nsampsintab
, &x
->x_vec
))
83 error("%s: bad template for tabplay~", x
->x_arrayname
->s_name
);
86 else garray_usedindsp(a
);
89 static void tabplay_tilde_dsp(t_tabplay_tilde
*x
, t_signal
**sp
)
91 tabplay_tilde_set(x
, x
->x_arrayname
);
92 dsp_add(tabplay_tilde_perform
, 3, x
, sp
[0]->s_vec
, sp
[0]->s_n
);
95 static void tabplay_tilde_list(t_tabplay_tilde
*x
, t_symbol
*s
,
96 int argc
, t_atom
*argv
)
98 long start
= atom_getfloatarg(0, argc
, argv
);
99 long length
= atom_getfloatarg(1, argc
, argv
);
105 if (start
< 0) start
= 0;
107 x
->x_limit
= 0x7fffffff;
109 x
->x_limit
= start
+ length
;
113 static void tabplay_tilde_stop(t_tabplay_tilde
*x
)
115 x
->x_phase
= 0x7fffffff;
118 static void tabplay_tilde_tick(t_tabplay_tilde
*x
)
120 outlet_bang(x
->x_bangout
);
123 static void tabplay_tilde_free(t_tabplay_tilde
*x
)
125 clock_free(x
->x_clock
);
128 void tabplay_tilde_setup(void)
130 tabplay_tilde_class
= class_new(gensym("tabplay~"),
131 (t_newmethod
)tabplay_tilde_new
, (t_method
)tabplay_tilde_free
,
132 sizeof(t_tabplay_tilde
), 0, A_DEFSYM
, 0);
133 class_addmethod(tabplay_tilde_class
, (t_method
)tabplay_tilde_dsp
,
135 class_addmethod(tabplay_tilde_class
, (t_method
)tabplay_tilde_stop
,
137 class_addmethod(tabplay_tilde_class
, (t_method
)tabplay_tilde_set
,
138 gensym("set"), A_DEFSYM
, 0);
139 class_addlist(tabplay_tilde_class
, tabplay_tilde_list
);