-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatch~.c
105 lines (92 loc) · 2.23 KB
/
latch~.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "MSPd.h"
static t_class *latch_class;
#define OBJECT_NAME "latch~"
#define LATCH_WAITING 0
#define LATCH_RUNNING 1
typedef struct _latch
{
t_object x_obj;
t_float x_f;
double current;
long latch;
long dsamps;
long sr;
double duration;
short status;
short connected;
} t_latch;
void *latch_new(t_float duration);
t_int *latch_perform(t_int *w);
void latch_dsp(t_latch *x, t_signal **sp);
void latch_tilde_setup(void)
{
latch_class = class_new(gensym("latch~"), (t_newmethod)latch_new, 0, sizeof(t_latch), 0,A_FLOAT, 0);
CLASS_MAINSIGNALIN(latch_class, t_latch, x_f);
class_addmethod(latch_class, (t_method)latch_dsp, gensym("dsp"), A_CANT, 0);
potpourri_announce(OBJECT_NAME);
}
void *latch_new(t_float duration)
{
t_latch *x = (t_latch *)pd_new(latch_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
outlet_new(&x->x_obj, gensym("signal"));
x->status = LATCH_WAITING;
x->duration = duration;
x->sr = sys_getsr();
if(x->duration <= 1.0)
x->duration = 1.0;
x->duration *= 0.001;
x->dsamps = x->sr ? x->sr * x->duration : 44100;
return x;
}
t_int *latch_perform(t_int *w)
{
t_latch *x = (t_latch *) (w[1]);
t_float *trigger = (t_float *)(w[2]);
t_float *vec_duration = (t_float *)(w[3]);
t_float *output = (t_float *)(w[4]);
t_int n = w[5];
int next_pointer = 6;
long latch = x->latch;
long dsamps = x->dsamps;
short status = x->status;
double current = x->current;
double duration = x->duration;
long sr = x->sr;
short connected = x->connected;
int i;
for(i = 0; i < n; i++){
if(connected){
duration = vec_duration[i];
if( duration > 0.0 ){ // dummy proof
dsamps = sr * duration * 0.001;
}
}
if(trigger[i]){
latch = 0;
status = LATCH_RUNNING;
current = trigger[i];
} else {
latch++;
if(latch >= dsamps){
status = LATCH_WAITING;
current = 0.0;
}
}
output[i] = current;
}
x->current = current;
x->status = status;
x->latch = latch;
x->dsamps = dsamps;
return w + next_pointer;
}
void latch_dsp(t_latch *x, t_signal **sp)
{
x->connected = 1;
if(x->sr != sp[0]->s_sr){
x->sr = sp[0]->s_sr;
x->dsamps = x->duration * x->sr;
}
dsp_add(latch_perform, 5, x, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}