summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/intern/vsnapshot~.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/intern/vsnapshot~.c')
-rw-r--r--apps/plugins/pdbox/PDa/intern/vsnapshot~.c176
1 files changed, 176 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/intern/vsnapshot~.c b/apps/plugins/pdbox/PDa/intern/vsnapshot~.c
new file mode 100644
index 0000000000..afa9417248
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/intern/vsnapshot~.c
@@ -0,0 +1,176 @@
1#include <m_pd.h>
2#include <m_fixed.h>
3
4
5static t_class *vsnapshot_tilde_class;
6
7typedef struct _vsnapshot
8{
9 t_object x_obj;
10 int x_n;
11 int x_gotone;
12 t_sample *x_vec;
13 float x_f;
14 float x_sampspermsec;
15 double x_time;
16} t_vsnapshot;
17
18static void *vsnapshot_tilde_new(void)
19{
20 t_vsnapshot *x = (t_vsnapshot *)pd_new(vsnapshot_tilde_class);
21 outlet_new(&x->x_obj, &s_float);
22 x->x_f = 0;
23 x->x_n = 0;
24 x->x_vec = 0;
25 x->x_gotone = 0;
26 return (x);
27}
28
29static t_int *vsnapshot_tilde_perform(t_int *w)
30{
31 t_sample *in = (t_sample *)(w[1]);
32 t_vsnapshot *x = (t_vsnapshot *)(w[2]);
33 t_sample *out = x->x_vec;
34 int n = x->x_n, i;
35 for (i = 0; i < n; i++)
36 out[i] = in[i];
37 x->x_time = clock_getlogicaltime();
38 x->x_gotone = 1;
39 return (w+3);
40}
41
42static void vsnapshot_tilde_dsp(t_vsnapshot *x, t_signal **sp)
43{
44 int n = sp[0]->s_n;
45 if (n != x->x_n)
46 {
47 if (x->x_vec)
48 t_freebytes(x->x_vec, x->x_n * sizeof(t_sample));
49 x->x_vec = (t_sample *)getbytes(n * sizeof(t_sample));
50 x->x_gotone = 0;
51 x->x_n = n;
52 }
53 x->x_sampspermsec = sp[0]->s_sr / 1000;
54 dsp_add(vsnapshot_tilde_perform, 2, sp[0]->s_vec, x);
55}
56
57static void vsnapshot_tilde_bang(t_vsnapshot *x)
58{
59 float val;
60 if (x->x_gotone)
61 {
62 int indx = clock_gettimesince(x->x_time) * x->x_sampspermsec;
63 if (indx < 0)
64 indx = 0;
65 else if (indx >= x->x_n)
66 indx = x->x_n - 1;
67 val = x->x_vec[indx];
68 }
69 else val = 0;
70 outlet_float(x->x_obj.ob_outlet, val);
71}
72
73static void vsnapshot_tilde_ff(t_vsnapshot *x)
74{
75 if (x->x_vec)
76 t_freebytes(x->x_vec, x->x_n * sizeof(t_sample));
77}
78
79void vsnapshot_tilde_setup(void)
80{
81 vsnapshot_tilde_class = class_new(gensym("vsnapshot~"),
82 vsnapshot_tilde_new, (t_method)vsnapshot_tilde_ff,
83 sizeof(t_vsnapshot), 0, 0);
84 CLASS_MAINSIGNALIN(vsnapshot_tilde_class, t_vsnapshot, x_f);
85 class_addmethod(vsnapshot_tilde_class, (t_method)vsnapshot_tilde_dsp, gensym("dsp"), 0);
86 class_addbang(vsnapshot_tilde_class, vsnapshot_tilde_bang);
87}
88
89#include <m_pd.h>
90#include <m_fixed.h>
91
92
93static t_class *vsnapshot_tilde_class;
94
95typedef struct _vsnapshot
96{
97 t_object x_obj;
98 int x_n;
99 int x_gotone;
100 t_sample *x_vec;
101 float x_f;
102 float x_sampspermsec;
103 double x_time;
104} t_vsnapshot;
105
106static void *vsnapshot_tilde_new(void)
107{
108 t_vsnapshot *x = (t_vsnapshot *)pd_new(vsnapshot_tilde_class);
109 outlet_new(&x->x_obj, &s_float);
110 x->x_f = 0;
111 x->x_n = 0;
112 x->x_vec = 0;
113 x->x_gotone = 0;
114 return (x);
115}
116
117static t_int *vsnapshot_tilde_perform(t_int *w)
118{
119 t_sample *in = (t_sample *)(w[1]);
120 t_vsnapshot *x = (t_vsnapshot *)(w[2]);
121 t_sample *out = x->x_vec;
122 int n = x->x_n, i;
123 for (i = 0; i < n; i++)
124 out[i] = in[i];
125 x->x_time = clock_getlogicaltime();
126 x->x_gotone = 1;
127 return (w+3);
128}
129
130static void vsnapshot_tilde_dsp(t_vsnapshot *x, t_signal **sp)
131{
132 int n = sp[0]->s_n;
133 if (n != x->x_n)
134 {
135 if (x->x_vec)
136 t_freebytes(x->x_vec, x->x_n * sizeof(t_sample));
137 x->x_vec = (t_sample *)getbytes(n * sizeof(t_sample));
138 x->x_gotone = 0;
139 x->x_n = n;
140 }
141 x->x_sampspermsec = sp[0]->s_sr / 1000;
142 dsp_add(vsnapshot_tilde_perform, 2, sp[0]->s_vec, x);
143}
144
145static void vsnapshot_tilde_bang(t_vsnapshot *x)
146{
147 float val;
148 if (x->x_gotone)
149 {
150 int indx = clock_gettimesince(x->x_time) * x->x_sampspermsec;
151 if (indx < 0)
152 indx = 0;
153 else if (indx >= x->x_n)
154 indx = x->x_n - 1;
155 val = x->x_vec[indx];
156 }
157 else val = 0;
158 outlet_float(x->x_obj.ob_outlet, val);
159}
160
161static void vsnapshot_tilde_ff(t_vsnapshot *x)
162{
163 if (x->x_vec)
164 t_freebytes(x->x_vec, x->x_n * sizeof(t_sample));
165}
166
167void vsnapshot_tilde_setup(void)
168{
169 vsnapshot_tilde_class = class_new(gensym("vsnapshot~"),
170 vsnapshot_tilde_new, (t_method)vsnapshot_tilde_ff,
171 sizeof(t_vsnapshot), 0, 0);
172 CLASS_MAINSIGNALIN(vsnapshot_tilde_class, t_vsnapshot, x_f);
173 class_addmethod(vsnapshot_tilde_class, (t_method)vsnapshot_tilde_dsp, gensym("dsp"), 0);
174 class_addbang(vsnapshot_tilde_class, vsnapshot_tilde_bang);
175}
176