summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/intern/sfwrite~.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/intern/sfwrite~.c')
-rw-r--r--apps/plugins/pdbox/PDa/intern/sfwrite~.c239
1 files changed, 0 insertions, 239 deletions
diff --git a/apps/plugins/pdbox/PDa/intern/sfwrite~.c b/apps/plugins/pdbox/PDa/intern/sfwrite~.c
index dd3bfc4a09..6b054f17b9 100644
--- a/apps/plugins/pdbox/PDa/intern/sfwrite~.c
+++ b/apps/plugins/pdbox/PDa/intern/sfwrite~.c
@@ -239,242 +239,3 @@ void sfwrite_tilde_setup(void)
239 class_addfloat(sfwrite_class, sfwrite_float); 239 class_addfloat(sfwrite_class, sfwrite_float);
240} 240}
241 241
242#include <unistd.h>
243#include <fcntl.h>
244#include <errno.h>
245#include <stdio.h>
246#include <string.h>
247#include <unistd.h>
248#include <sys/mman.h>
249#include <sys/stat.h>
250
251#include <m_pd.h>
252#include <m_fixed.h>
253#include "g_canvas.h"
254
255
256#define BLOCKTIME 10
257
258#define MAX_CHANS 4
259
260#include "sformat.h"
261
262static t_class *sfwrite_class;
263
264typedef struct _sfwrite
265{
266 t_object x_obj;
267 t_symbol* filename;
268 int x_file;
269
270 t_int rec;
271 t_int x_channels;
272 uint32 size;
273 t_glist * x_glist;
274 t_int x_blocked;
275 t_int x_blockwarn;
276} t_sfwrite;
277
278
279static void sfwrite_wave_setup(t_sfwrite* x,t_wave* w)
280{
281
282 strncpy(w->w_fileid,"RIFF",4); /* chunk id 'RIFF' */
283 w->w_chunksize = x->size + sizeof(t_wave) -8; /* chunk size */
284 strncpy(w->w_waveid,"WAVE",4); /* wave chunk id 'WAVE' */
285 strncpy(w->w_fmtid,"fmt ",4); /* format chunk id 'fmt '*/
286 w->w_fmtchunksize = 16; /* format chunk size */
287 w->w_fmttag = 1; /* format tag, 1 for PCM */
288 w->w_nchannels = x->x_channels; /* number of channels */
289 w->w_samplespersec = 44100; /* sample rate in hz */
290 w->w_navgbytespersec = 44100*x->x_channels*2; /* average bytes per second */
291 w->w_nblockalign = 4; /* number of bytes per sample */
292 w->w_nbitspersample = 16; /* number of bits in a sample */
293 strncpy(w->w_datachunkid,"data",4); /* data chunk id 'data'*/
294 w->w_datachunksize = x->size; /* length of data chunk */
295}
296
297
298
299static void sfwrite_close(t_sfwrite *x)
300{
301 if (x->x_file > 0) {
302 t_wave w;
303 sfwrite_wave_setup(x,&w);
304 lseek(x->x_file,0,SEEK_SET);
305 write(x->x_file,&w,sizeof(w));
306 close(x->x_file);
307 }
308 x->x_file = -1;
309}
310
311
312static void sfwrite_open(t_sfwrite *x,t_symbol *filename)
313{
314 char fname[MAXPDSTRING];
315
316 if (filename == &s_) {
317 post("sfwrite: open without filename");
318 return;
319 }
320
321 canvas_makefilename(glist_getcanvas(x->x_glist), filename->s_name,
322 fname, MAXPDSTRING);
323
324 x->x_blocked = 0;
325 x->filename = filename;
326 post("sfwrite: filename = %s",x->filename->s_name);
327
328 sfwrite_close(x);
329
330 if ((x->x_file = open(fname,O_RDWR | O_CREAT,0664)) < 0)
331 {
332 error("can't create %s",fname);
333 return;
334 }
335
336 /* skip the header */
337
338 lseek(x->x_file,sizeof(t_wave),SEEK_SET);
339 x->size = 0;
340
341
342}
343
344static void sfwrite_block(t_sfwrite *x, t_floatarg f)
345{
346 x->x_blockwarn = f;
347}
348
349
350static void sfwrite_float(t_sfwrite *x, t_floatarg f)
351{
352 int t = f;
353 if (t) {
354 post("sfwrite: start", f);
355 x->rec=1;
356 }
357 else {
358 post("sfwrite: stop", f);
359 x->rec=0;
360 }
361
362}
363
364
365static short out[4*64];
366
367static t_int *sfwrite_perform(t_int *w)
368{
369 t_sfwrite* x = (t_sfwrite*)(w[1]);
370 t_sample * in[4];
371 int c = x->x_channels;
372 int i,num,n;
373 short* tout = out;
374 int ret;
375 int timebefore,timeafter;
376 double late;
377
378 for (i=0;i < c;i++) {
379 in[i] = (t_sample *)(w[2+i]);
380 }
381
382 n = num = (int)(w[2+c]);
383
384 /* loop */
385
386 if (x->rec && x->x_file) {
387
388 while (n--) {
389 for (i=0;i<c;i++) {
390 *tout++ = (*(in[i])++)>>(fix1-16);
391 }
392 }
393
394 timebefore = sys_getrealtime();
395 if ((ret =write(x->x_file,out,sizeof(short)*num*c)) < (signed int)sizeof(short)*num*c) {
396 post("sfwrite: short write %d",ret);
397
398 }
399 timeafter = sys_getrealtime();
400 late = timeafter - timebefore;
401
402#if 0
403 /* OK, we let only 10 ms block here */
404 if (late > BLOCKTIME && x->x_blockwarn) {
405 post("sfwrite blocked %f ms",late*1000);
406 x->x_blocked++;
407 if (x->x_blocked > x->x_blockwarn) {
408 x->rec = 0;
409 post("maximum blockcount %d reached, recording stopped (set blockcount with \"block <num>\"",x->x_blockwarn);
410 }
411 }
412#endif
413 x->size +=64*x->x_channels*sizeof(short) ;
414 }
415
416 return (w+3+c);
417}
418
419
420
421static void sfwrite_dsp(t_sfwrite *x, t_signal **sp)
422{
423 switch (x->x_channels) {
424 case 1:
425 dsp_add(sfwrite_perform, 3, x, sp[0]->s_vec,
426 sp[0]->s_n);
427 break;
428 case 2:
429 dsp_add(sfwrite_perform, 4, x, sp[0]->s_vec,
430 sp[1]->s_vec, sp[0]->s_n);
431 break;
432 case 4:
433 dsp_add(sfwrite_perform, 6, x, sp[0]->s_vec,
434 sp[1]->s_vec,
435 sp[2]->s_vec,
436 sp[3]->s_vec,
437 sp[0]->s_n);
438 break;
439 }
440}
441
442static void sfwrite_free(t_sfwrite* x)
443{
444 sfwrite_close(x);
445}
446
447
448static void *sfwrite_new(t_floatarg chan)
449{
450 t_sfwrite *x = (t_sfwrite *)pd_new(sfwrite_class);
451 t_int c = chan;
452
453 if (c<1 || c > MAX_CHANS) c = 1;
454
455 x->x_glist = (t_glist*) canvas_getcurrent();
456 x->x_channels = c--;
457 x->x_file=0;
458 x->rec = 0;
459 x->size = 0;
460 x->x_blocked = 0;
461 x->x_blockwarn = 10;
462 while (c--) {
463 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
464 }
465
466
467 return (x);
468}
469
470void sfwrite_tilde_setup(void)
471{
472 sfwrite_class = class_new(gensym("sfwrite~"), (t_newmethod)sfwrite_new, (t_method)sfwrite_free,
473 sizeof(t_sfwrite), 0,A_DEFFLOAT,0);
474 class_addmethod(sfwrite_class,nullfn,gensym("signal"), 0);
475 class_addmethod(sfwrite_class, (t_method) sfwrite_dsp, gensym("dsp"), 0);
476 class_addmethod(sfwrite_class, (t_method) sfwrite_open, gensym("open"), A_SYMBOL,A_NULL);
477 class_addmethod(sfwrite_class, (t_method) sfwrite_close, gensym("close"), 0);
478 class_addmethod(sfwrite_class, (t_method)sfwrite_block,gensym("block"),A_DEFFLOAT,0);
479 class_addfloat(sfwrite_class, sfwrite_float);
480}