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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/* Copyright (c) 2002-2005 krzYszcz and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
/* CHECKME list of indices */
#include <stdio.h>
#include "m_pd.h"
#include "g_canvas.h"
#include "common/loud.h"
#include "hammer/file.h"
#include "sickle/sic.h"
#define CAPTURE_DEFSIZE 4096
#define CAPTURE_DEFPRECISION 4
#define CAPTURE_MAXPRECISION 99 /* format array protection */
#define CAPTURE_MAXINDICES 4096 /* FIXME */
typedef struct _capture
{
t_sic x_sic;
t_glist *x_glist;
char x_mode; /* 'f' for first or 0 for last */
int x_precision;
char x_format[8];
char *x_indices;
int x_szindices; /* size of x_indices array */
int x_nindices; /* number of reported indices */
int x_nblock;
float *x_buffer;
int x_bufsize;
int x_count;
int x_head;
t_hammerfile *x_filehandle;
} t_capture;
static t_class *capture_class;
static void capture_clear(t_capture *x)
{
x->x_count = 0;
x->x_head = 0;
}
static int capture_formatfloat(t_capture *x, float f, char *buf, int col,
int maxcol)
{
char *bp = buf;
int cnt = 0;
if (col > 0)
*bp++ = ' ', cnt++;
if (x->x_precision)
cnt += sprintf(bp, x->x_format, f);
else
cnt += sprintf(bp, "%d", (int)f);
if (col + cnt > maxcol)
buf[0] = '\n', col = cnt - 1; /* assuming col > 0 */
else
col += cnt;
return (col);
}
static int capture_writefloat(t_capture *x, float f, char *buf, int col,
FILE *fp)
{
/* CHECKME linebreaks */
col = capture_formatfloat(x, f, buf, col, 80);
return (fputs(buf, fp) < 0 ? -1 : col);
}
static void capture_dowrite(t_capture *x, t_symbol *fn)
{
FILE *fp = 0;
int count = x->x_count;
char buf[MAXPDSTRING];
canvas_makefilename(glist_getcanvas(x->x_glist),
fn->s_name, buf, MAXPDSTRING);
if (fp = fopen(buf, "w")) /* LATER ask if overwriting, CHECKME */
{
int col = 0;
if (x->x_mode == 'f' || count < x->x_bufsize)
{
float *bp = x->x_buffer;
while (count--)
if ((col = capture_writefloat(x, *bp++, buf, col, fp)) < 0)
goto fail;
}
else
{
float *bp = x->x_buffer + x->x_head;
count = x->x_bufsize - x->x_head;
while (count--)
if ((col = capture_writefloat(x, *bp++, buf, col, fp)) < 0)
goto fail;
bp = x->x_buffer;
count = x->x_head;
while (count--)
if ((col = capture_writefloat(x, *bp++, buf, col, fp)) < 0)
goto fail;
}
if (col) fputc('\n', fp);
fclose(fp);
return;
}
fail:
if (fp) fclose(fp);
loud_syserror((t_pd *)x, 0);
}
static void capture_writehook(t_pd *z, t_symbol *fn, int ac, t_atom *av)
{
capture_dowrite((t_capture *)z, fn);
}
static void capture_write(t_capture *x, t_symbol *s)
{
if (s && s != &s_)
capture_dowrite(x, s);
else
hammerpanel_save(x->x_filehandle, 0, 0);
}
static int capture_appendfloat(t_capture *x, float f, char *buf,
int col, int linebreak)
{
/* CHECKME 80 columns */
col = capture_formatfloat(x, f, buf, col, 80);
hammereditor_append(x->x_filehandle, buf);
if (linebreak)
{
if (col)
{
hammereditor_append(x->x_filehandle, "\n\n");
col = 0;
}
else hammereditor_append(x->x_filehandle, "\n");
}
return (col);
}
/* CHECKED blank line between blocks */
static void capture_open(t_capture *x)
{
int count = x->x_count;
char buf[MAXPDSTRING];
int nindices = (x->x_nindices > 0 ? x->x_nindices : x->x_nblock);
hammereditor_open(x->x_filehandle, "Signal Capture", ""); /* CHECKED */
if (x->x_mode == 'f' || count < x->x_bufsize)
{
float *bp = x->x_buffer;
int col = 0, i;
for (i = 1; i <= count; i++)
col = capture_appendfloat(x, *bp++, buf, col,
((i % nindices) == 0));
}
else
{
float *bp = x->x_buffer + x->x_head;
int col = 0, i = x->x_bufsize;
count = x->x_bufsize - x->x_head;
while (count--)
col = capture_appendfloat(x, *bp++, buf, col,
((--i % nindices) == 0));
bp = x->x_buffer;
count = x->x_head;
while (count--)
col = capture_appendfloat(x, *bp++, buf, col,
((count % nindices) == 0));
}
}
/* CHECKED without asking and storing the changes */
static void capture_wclose(t_capture *x)
{
hammereditor_close(x->x_filehandle, 0);
}
static void capture_click(t_capture *x, t_floatarg xpos, t_floatarg ypos,
t_floatarg shift, t_floatarg ctrl, t_floatarg alt)
{
capture_open(x);
}
static t_int *capture_perform_first(t_int *w)
{
t_capture *x = (t_capture *)(w[1]);
int count = x->x_count;
int bufsize = x->x_bufsize;
if (count < bufsize)
{
t_float *in = (t_float *)(w[2]);
int nblock = (int)(w[3]);
float *bp = x->x_buffer + count;
char *ndxp = x->x_indices;
if (nblock > x->x_szindices)
nblock = x->x_szindices;
while (nblock--)
{
if (*ndxp++)
{
*bp++ = *in++;
if (++count == bufsize)
break;
}
else in++;
}
x->x_count = count;
}
return (w + 4);
}
static t_int *capture_perform_allfirst(t_int *w)
{
t_capture *x = (t_capture *)(w[1]);
int count = x->x_count;
int bufsize = x->x_bufsize;
if (count < bufsize)
{
t_float *in = (t_float *)(w[2]);
int nblock = (int)(w[3]);
float *bp = x->x_buffer + count;
while (nblock--)
{
*bp++ = *in++;
if (++count == bufsize)
break;
}
x->x_count = count;
}
return (w + 4);
}
static t_int *capture_perform_last(t_int *w)
{
t_capture *x = (t_capture *)(w[1]);
t_float *in = (t_float *)(w[2]);
int nblock = (int)(w[3]);
float *buffer = x->x_buffer;
int bufsize = x->x_bufsize;
int count = x->x_count;
int head = x->x_head;
char *ndxp = x->x_indices;
if (nblock > x->x_szindices)
nblock = x->x_szindices;
while (nblock--)
{
if (*ndxp++)
{
buffer[head++] = *in++;
if (head >= bufsize)
head = 0;
if (count < bufsize)
count++;
}
else in++;
}
x->x_count = count;
x->x_head = head;
return (w + 4);
}
static t_int *capture_perform_alllast(t_int *w)
{
t_capture *x = (t_capture *)(w[1]);
t_float *in = (t_float *)(w[2]);
int nblock = (int)(w[3]);
float *buffer = x->x_buffer;
int bufsize = x->x_bufsize;
int count = x->x_count;
int head = x->x_head;
while (nblock--)
{
buffer[head++] = *in++;
if (head >= bufsize)
head = 0;
if (count < bufsize)
count++;
}
x->x_count = count;
x->x_head = head;
return (w + 4);
}
static void capture_dsp(t_capture *x, t_signal **sp)
{
x->x_nblock = sp[0]->s_n;
if (x->x_indices)
dsp_add((x->x_mode == 'f' ?
capture_perform_first : capture_perform_last),
3, x, sp[0]->s_vec, sp[0]->s_n);
else
dsp_add((x->x_mode == 'f' ?
capture_perform_allfirst : capture_perform_alllast),
3, x, sp[0]->s_vec, sp[0]->s_n);
}
static void capture_free(t_capture *x)
{
hammerfile_free(x->x_filehandle);
if (x->x_indices)
freebytes(x->x_indices, x->x_szindices * sizeof(*x->x_indices));
if (x->x_buffer)
freebytes(x->x_buffer, x->x_bufsize * sizeof(*x->x_buffer));
}
static void *capture_new(t_symbol *s, int ac, t_atom *av)
{
t_capture *x = 0;
char mode = 0;
int precision = -1;
float *buffer;
int bufsize = 0;
char *indices = 0;
int szindices = 0, nindices = -1;
if (ac && av->a_type == A_SYMBOL)
{
t_symbol *s = av->a_w.w_symbol;
if (s && *s->s_name == 'f') /* CHECKME */
mode = 'f';
ac--; av++;
}
if (ac && av->a_type == A_FLOAT)
{
bufsize = (int)av->a_w.w_float; /* CHECKME */
ac--; av++;
if (ac && av->a_type == A_FLOAT)
{
int i;
t_atom *ap;
precision = (int)av->a_w.w_float; /* CHECKME */
ac--; av++;
for (i = 0, ap = av; i < ac; i++, ap++)
{
if (ap->a_type == A_FLOAT)
{
int ndx = (int)ap->a_w.w_float;
/* CHECKME noninteger, negative */
ndx++;
if (ndx >= CAPTURE_MAXINDICES)
{
/* CHECKME complaint */
szindices = CAPTURE_MAXINDICES;
break;
}
else if (ndx > szindices)
szindices = ndx;
}
else break; /* CHECKME */
}
if (szindices && (indices = getbytes(szindices * sizeof(*indices))))
{
nindices = 0;
while (i--)
{
int ndx = (int)av++->a_w.w_float;
/* CHECKME noninteger */
if (ndx >= 0 && ndx < szindices)
indices[ndx] = 1, nindices++;
}
}
}
}
if (bufsize <= 0) /* CHECKME */
bufsize = CAPTURE_DEFSIZE;
if (buffer = getbytes(bufsize * sizeof(*buffer)))
{
x = (t_capture *)pd_new(capture_class);
x->x_glist = canvas_getcurrent();
x->x_mode = mode;
if (precision < 0) /* CHECKME */
precision = CAPTURE_DEFPRECISION;
else if (precision > CAPTURE_MAXPRECISION) /* CHECKME */
precision = CAPTURE_MAXPRECISION;
if (x->x_precision = precision)
sprintf(x->x_format, "%%.%dg", precision);
x->x_indices = indices;
x->x_szindices = szindices;
x->x_nindices = nindices;
x->x_nblock = 64; /* redundant */
x->x_buffer = buffer;
x->x_bufsize = bufsize;
x->x_filehandle = hammerfile_new((t_pd *)x, 0, 0, capture_writehook, 0);
capture_clear(x);
}
else if (indices)
freebytes(indices, szindices * sizeof(*indices));
return (x);
}
void capture_tilde_setup(void)
{
capture_class = class_new(gensym("capture~"),
(t_newmethod)capture_new,
(t_method)capture_free,
sizeof(t_capture), 0, A_GIMME, 0);
sic_setup(capture_class, capture_dsp, SIC_FLOATTOSIGNAL);
class_addmethod(capture_class, (t_method)capture_clear,
gensym("clear"), 0);
class_addmethod(capture_class, (t_method)capture_write,
gensym("write"), A_DEFSYM, 0);
class_addmethod(capture_class, (t_method)capture_open,
gensym("open"), 0);
class_addmethod(capture_class, (t_method)capture_wclose,
gensym("wclose"), 0);
class_addmethod(capture_class, (t_method)capture_click,
gensym("click"),
A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
hammerfile_setup(capture_class, 0);
}
|