~ubuntu-branches/ubuntu/natty/pd-zexy/natty

« back to all changes in this revision

Viewing changes to src/sfrecord.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard, IOhannes m zmölnig, Jonas Smedegaard
  • Date: 2010-08-20 12:17:41 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100820121741-4kxozn8b9rhee9fr
Tags: 2.2.3-1
* New upstream version

[ IOhannes m zmölnig ]
* Adopt package, on behalf of Multimedia Team.
  Closes: #546964
* Simply debian/rules with CDBS, and don't unconditionally strip
  binaries.
  Closes: #437763
* Install into /usr/lib/pd/extra/zexy/. Document usage in REAME.Debian
  and warn about change in NEWS.
* git'ify package. Add Vcs-* stanzas to control file.
* Use dpkg source format 3.0 (quilt). Drop build-dependency on quilt.

[ Jonas Smedegaard ]
* Enable CDBS copyright-check routine.
* Add copyright and licensing header to debian/rules.
* Add myself as uploader.
* Rewrite debian/copyright using rev. 135 of draft DEP5 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************
 
2
 *
 
3
 * zexy - implementation file
 
4
 *
 
5
 * copyleft (c) IOhannes m zm�lnig
 
6
 *
 
7
 *   1999:forum::f�r::uml�ute:2004
 
8
 *
 
9
 *   institute of electronic music and acoustics (iem)
 
10
 *
 
11
 ******************************************************
 
12
 *
 
13
 * license: GNU General Public License v.2
 
14
 *
 
15
 ******************************************************/
 
16
 
 
17
/*
 
18
sfplay.c - Author: Winfried Ritsch - IEM Graz 10.Mai 99 - 
 
19
Modified:
 
20
sfrecord.c - hacked from sfplay ::: 2308:forum::f�r::uml�ute:1999 @ iem
 
21
 
 
22
please mail problems and ideas for improvements to
 
23
ritsch@iem.kug.ac.at
 
24
zmoelnig@iem.kug.ac.at
 
25
*/
 
26
 
 
27
/* TODO: deprecate this in favour of [sfplay~] */
 
28
 
 
29
/* #define DEBUG_ME for debugging messages */
 
30
 
 
31
#include "zexy.h"
 
32
 
 
33
 
 
34
/* #include "m_imp.h" */
 
35
 
 
36
#define DACBLKSIZE 64   /* in m_imp.h, but error if it is included it here*/
 
37
 
 
38
#include <stdio.h>
 
39
#include <string.h>
 
40
#include <fcntl.h>
 
41
 
 
42
/* ------------------------ sfrecord ----------------------------- */
 
43
#define MAX_CHANS 8             /* channels for soundfiles 1,2,4,8 */
 
44
 
 
45
#ifdef __WIN32__
 
46
# define BINWRITEMODE "wb"
 
47
#else
 
48
# include <unistd.h>
 
49
# include <sys/mman.h>
 
50
# define BINWRITEMODE "w"
 
51
#endif
 
52
 
 
53
static t_class *sfrecord_class;
 
54
 
 
55
typedef struct _sfrecord
 
56
{
 
57
        t_object x_obj;
 
58
 
 
59
        void*   filep;      /* pointer to file data read in mem */
 
60
        t_symbol* filename; /* filename */
 
61
 
 
62
        /*
 
63
                because there is no command queue,
 
64
                flags are used instead 
 
65
        */
 
66
        t_int write;            /* write: 1, stop: 0 */
 
67
        t_int please_stop;      /* can be reset only by stop-state itself */ 
 
68
        t_int please_close;     /* can be reset only by close-state */
 
69
        t_int x_channels;       /* channels to write */
 
70
        t_float x_offset;       /* offset to start writing */
 
71
        t_float offset;         /* inlet value offset in secs */
 
72
        t_float x_skip;         /* skip bytes because header */
 
73
        t_int skip;                     /* pending skip if 1 */
 
74
        t_float x_speed;        /* write speed, not supported in this version */
 
75
        t_int size;                     /* size of file (if memory mapped) */
 
76
        t_int swap;                     /* swap bytes from l->b or b->m */
 
77
        FILE *fp;                       /* file oper non-NULL of open */
 
78
        t_int state;            /* which state is writer in */
 
79
        t_int count;            /* count for ticks before next step */
 
80
 
 
81
} t_sfrecord;
 
82
 
 
83
/* states of statemachine */
 
84
#define SFRECORD_WAIT   0               /* wait for open */
 
85
#define SFRECORD_OPEN   1
 
86
#define SFRECORD_CLOSE  2
 
87
#define SFRECORD_SKIP   3
 
88
#define SFRECORD_WRITE  4
 
89
#define SFRECORD_STOP   5
 
90
#define SFRECORD_ERROR  -1
 
91
 
 
92
#define SFRECORD_WAITTICKS 10   /* 1 tick of 64 Samples is ca.1.5ms on 441000 */
 
93
 
 
94
/* split the os-calls in as many steps as possible 
 
95
to split them on different ticks in steps of SFRECORD_WAITTICKS
 
96
to avoid peak performance */
 
97
 
 
98
/* like the one from garray */
 
99
static int sfrecord_am_i_big_endian(void) 
 
100
{
 
101
        unsigned short s = 1;
 
102
        unsigned char c = *(char *) (&s);
 
103
#ifdef DEBUG_ME
 
104
        post("i am %s-endian", c?"little":"big");
 
105
#endif
 
106
        return(c==0);
 
107
}
 
108
 
 
109
static void state_out(t_sfrecord *x, int state)
 
110
{       /* outlet the actual state */
 
111
        outlet_float(x->x_obj.ob_outlet, state);
 
112
}
 
113
 
 
114
 
 
115
/* METHOD: "open" file */
 
116
 
 
117
/* this dont use memory map, because I dont know about this on NT ? 
 
118
Use of the buffered functions fopen, fseek fread fclose instead the 
 
119
non buffered ones open read close */
 
120
 
 
121
static void sfrecord_open(t_sfrecord *x,t_symbol *filename,t_symbol *endian)
 
122
{
 
123
 
 
124
        if(x->state != SFRECORD_WAIT)
 
125
        {
 
126
                post("sfrecord: first close %s before open %s",x->filename->s_name,filename->s_name);
 
127
                return;
 
128
        }
 
129
 
 
130
  /* test if big endian else asume little endian 
 
131
   * should be 'l' but could be anything
 
132
   */
 
133
        if(sfrecord_am_i_big_endian())
 
134
                x->swap = !(endian->s_name[0] == 'b');
 
135
        else
 
136
                x->swap = (endian->s_name[0] == 'b');
 
137
 
 
138
        /* 
 
139
   * skip header after open;; sometimes we�ll have to write a header using the x->skip; so don�t delete it completely 
 
140
   */
 
141
  /* x->skip = 1; */
 
142
 
 
143
        x->filename = filename;
 
144
 
 
145
#ifdef DEBUG_ME
 
146
        post("sfrecord: opening %s",x->filename->s_name);
 
147
#endif
 
148
 
 
149
        if (x->fp != NULL)fclose(x->fp);        /* should not happen */
 
150
 
 
151
        if (!(x->fp = fopen(x->filename->s_name,BINWRITEMODE))) 
 
152
        {
 
153
                error("sfrecord: can't open %s", x->filename->s_name);
 
154
        }
 
155
}
 
156
 
 
157
 
 
158
 
 
159
/* METHOD: close */
 
160
static void sfrecord_close(t_sfrecord *x)
 
161
{
 
162
        x->write = 0;
 
163
        x->please_close = 1;
 
164
 
 
165
        /* now in state machine 
 
166
        if(x->fp != NULL)
 
167
        {
 
168
                fclose(x->fp);
 
169
                x->fp = NULL;
 
170
        }
 
171
        */
 
172
 
 
173
#ifdef DEBUG_ME
 
174
        post("sfrecord: closing ");
 
175
#endif
 
176
        return;
 
177
}
 
178
 
 
179
/* for skipping header of soundfile  Don�t use this for memory map */
 
180
 
 
181
static int sfrecord_skip(t_sfrecord *x)
 
182
{
 
183
        if(!x->skip) return 0;
 
184
 
 
185
#ifdef DEBUG_ME
 
186
        post("sfrecord:skip to %f",x->x_skip);
 
187
#endif
 
188
 
 
189
        x->skip = 0;
 
190
        return 1;
 
191
}
 
192
 
 
193
/* Input, method for Start stop */
 
194
 
 
195
static void sfrecord_start(t_sfrecord *x)
 
196
{
 
197
#ifdef DEBUG_ME
 
198
        post("sfrecord: start at %d", x->x_offset);
 
199
#endif
 
200
 
 
201
        state_out(x, 1);
 
202
        x->write=1;
 
203
}
 
204
 
 
205
static void sfrecord_stop(t_sfrecord *x)
 
206
{
 
207
#ifdef DEBUG_ME
 
208
        post("sfrecord: stop");
 
209
#endif
 
210
        state_out(x, 0);
 
211
 
 
212
        x->write=0;
 
213
        x->please_stop = 1;
 
214
}
 
215
 
 
216
static void sfrecord_float(t_sfrecord *x, t_floatarg f)
 
217
{
 
218
        int t = f;
 
219
        if (t) sfrecord_start(x);
 
220
        else sfrecord_stop(x);
 
221
}
 
222
 
 
223
/* say what state we�re in */
 
224
static void sfrecord_bang(t_sfrecord* x)
 
225
{
 
226
        if (x->state == SFRECORD_WRITE) state_out(x, 1); else state_out(x, 0);
 
227
}
 
228
 
 
229
/* ******************************************************************************** */
 
230
/*                          the work                krow eht                        */
 
231
/* ******************************************************************************** */
 
232
 
 
233
 
 
234
static t_int *sfrecord_perform(t_int *w)
 
235
{
 
236
        t_sfrecord* x = (t_sfrecord*)(w[1]);
 
237
        short* buf = x->filep;
 
238
        short* bufstart = buf;
 
239
        int c = x->x_channels;
 
240
 
 
241
        int i,j,n, s_n;
 
242
        t_float* in[MAX_CHANS];
 
243
 
 
244
        short s;
 
245
        int swap = x->swap;
 
246
 
 
247
        for (i=0;i<c;i++)  
 
248
                in[i] = (t_float *)(w[2+i]);
 
249
 
 
250
        n = s_n = (int)(w[2+c]);
 
251
 
 
252
        /* loop */
 
253
 
 
254
        switch(x->state){
 
255
 
 
256
                /* just wait */
 
257
        case SFRECORD_WAIT:
 
258
 
 
259
                if(x->fp != NULL){
 
260
#ifdef DEBUG_ME
 
261
                        post("wait -> open");
 
262
#endif
 
263
                        x->state = SFRECORD_OPEN;
 
264
                        x->count = SFRECORD_WAITTICKS;
 
265
                };
 
266
                break;
 
267
 
 
268
                /* if in open state, already opened but wait for skip */
 
269
        case SFRECORD_OPEN: /* file has opened wait some time */
 
270
 
 
271
                if(!(x->count--)){
 
272
#ifdef DEBUG_ME
 
273
                        post("open -> skip");
 
274
#endif
 
275
                        x->state = SFRECORD_SKIP;
 
276
                        x->count = SFRECORD_WAITTICKS;
 
277
                };
 
278
 
 
279
                break;
 
280
 
 
281
                /* in skipmode wait until ready for stop */
 
282
        case SFRECORD_SKIP:
 
283
 
 
284
                if(x->count == SFRECORD_WAITTICKS)
 
285
                {
 
286
                        if(!x->fp)
 
287
                        {
 
288
                                x->state = SFRECORD_CLOSE;
 
289
                                x->count=1;
 
290
#ifdef DEBUG_ME
 
291
                                post("skip -> close");
 
292
#endif
 
293
                                break;
 
294
                        }
 
295
                        sfrecord_skip(x);
 
296
                }
 
297
                if(!(x->count--))
 
298
                {
 
299
#ifdef DEBUG_ME
 
300
                        post("skip -> stop");
 
301
#endif
 
302
                        x->state = SFRECORD_STOP;
 
303
                        x->count = SFRECORD_WAITTICKS;
 
304
                };
 
305
                break;
 
306
 
 
307
        case SFRECORD_STOP:             /* in stop state mainly waits for write */
 
308
 
 
309
                x->please_stop = 0;
 
310
 
 
311
                if(x->please_close)
 
312
                {
 
313
                        x->state = SFRECORD_CLOSE;
 
314
                        x->count = SFRECORD_WAITTICKS;      
 
315
#ifdef DEBUG_ME
 
316
                        post("stop -> close");
 
317
#endif
 
318
                }
 
319
                else if(x->skip)
 
320
                {
 
321
                        x->state = SFRECORD_SKIP;
 
322
                        x->count = SFRECORD_WAITTICKS;
 
323
 
 
324
#ifdef DEBUG_ME
 
325
                        post("stop -> skip");
 
326
#endif
 
327
 
 
328
                }
 
329
                else if(x->write)
 
330
                {
 
331
 
 
332
#ifdef DEBUG_ME
 
333
                        post("stop -> write");
 
334
#endif
 
335
                        x->state = SFRECORD_WRITE;
 
336
                        state_out(x, 1);
 
337
                }
 
338
                break;
 
339
 
 
340
        case SFRECORD_WRITE:                            /* yes write now */
 
341
 
 
342
                if(!x->write || x->please_stop)
 
343
                {
 
344
                        /* if closing dont need to go to stop */
 
345
                        if(x->please_close)     {
 
346
                                x->state = SFRECORD_CLOSE;
 
347
                                x->count = SFRECORD_WAITTICKS;
 
348
#ifdef DEBUG_ME
 
349
                                post("write -> close");
 
350
#endif
 
351
                                state_out(x, 0);
 
352
 
 
353
                        }
 
354
                        else    {
 
355
                                x->state = SFRECORD_STOP;
 
356
#ifdef DEBUG_ME
 
357
                                post("write -> stop");
 
358
#endif
 
359
                        };
 
360
                        break;
 
361
                }
 
362
 
 
363
                /* should never happen */
 
364
                if(!x->filep){
 
365
                        x->state = SFRECORD_ERROR;
 
366
                        error("sfrecord: writing but no buffer ???? write");
 
367
                        return (w+4+c);
 
368
                }
 
369
 
 
370
                /* copy float to 16 Bit and swap if neccesairy */ /* LATER treat overflows */
 
371
                while (n--) {
 
372
                        for (i=0;i<c;i++) {
 
373
                        s = *in[i]++ * 32768.;
 
374
                        if (swap) s = ((s & 0xFF)<< 8) | ((s& 0xFF00) >> 8);
 
375
                        *buf++ = s;
 
376
                        }
 
377
                }
 
378
                
 
379
                /* then write soundfile 16 bit*/                 
 
380
                if ( (j = fwrite(bufstart, sizeof(short), c*s_n, x->fp)) < 1) {
 
381
                  x->state = SFRECORD_ERROR;
 
382
                  x->count = SFRECORD_WAITTICKS;
 
383
#ifdef DEBUG_ME
 
384
                        post("write -> write error\t %xd\t%xd\t%d\t%d", x->filep, buf, c*s_n*sizeof(short), j);
 
385
#endif
 
386
                        break;
 
387
                }
 
388
 
 
389
#if 0
 
390
                if((j=fwrite(buf,sizeof(short),c*n,x->fp)) < (unsigned int) n)
 
391
                {
 
392
                        if(feof(x->fp)){
 
393
 
 
394
                                while (n--) {
 
395
                                        for (i=0;i<c;i++)       {
 
396
                                                if(--j > 0){
 
397
                                                        s = *buf++;
 
398
                                                        if(swap) s = ((s & 0xFF)<< 8) | ((s& 0xFF00) >> 8);
 
399
                                                        *out[i]++ = s*(1./32768.);
 
400
                                                }
 
401
                                                else
 
402
                                                        *out[i]++ = 0;
 
403
                                        }
 
404
                                }
 
405
                        }
 
406
 
 
407
                        x->state = SFRECORD_STOP;
 
408
                        x->write = 0;
 
409
                        return(w+c+3);
 
410
                        }
 
411
                        /* or error if(ferror()) */
 
412
                        x->state = SFRECORD_ERROR;
 
413
                        x->count = SFRECORD_WAITTICKS;
 
414
#ifdef DEBUG_ME
 
415
                        post("write -> write error");
 
416
#endif
 
417
                        break;
 
418
                };
 
419
#endif /* 0 */
 
420
                return (w+c+3); /* writing was fine */
 
421
 
 
422
 
 
423
                /* ok :?: write error, please close */
 
424
        case SFRECORD_ERROR:
 
425
 
 
426
                if(!(x->count--))       {
 
427
                        x->state = SFRECORD_CLOSE;
 
428
                        sfrecord_close(x);
 
429
#ifdef DEBUG_ME
 
430
                        post("sfrecord error writing sf: error -> close");
 
431
#endif
 
432
                        x->count = SFRECORD_WAITTICKS;
 
433
                }
 
434
                break;
 
435
 
 
436
                /* in close state go to wait afterwards */
 
437
        case SFRECORD_CLOSE:
 
438
 
 
439
                x->please_close = 0;
 
440
 
 
441
                /* wait until ready for close operation */
 
442
                if(!(x->count--)){ 
 
443
 
 
444
                        x->state = SFRECORD_WAIT;
 
445
                        x->count = SFRECORD_WAITTICKS;
 
446
 
 
447
                        /* avoid openfiles */
 
448
                        if(x->fp){fclose(x->fp);x->fp = NULL;};
 
449
 
 
450
#ifdef DEBUG_ME
 
451
                        post("sfrecord: close -> wait");
 
452
#endif
 
453
                }
 
454
                break;
 
455
 
 
456
        }; /*case */
 
457
 
 
458
        return(w+c+3);
 
459
}
 
460
 
 
461
 
 
462
 
 
463
 
 
464
/* ---------------------- Setup junk -------------------------- */
 
465
 
 
466
static void sfrecord_dsp(t_sfrecord *x, t_signal **sp)
 
467
{
 
468
 
 
469
#ifdef DEBUG_ME
 
470
        post("sfrecord: dsp");
 
471
        post("offset = %f\tspeed = %f\t", x->offset, x->x_speed);
 
472
#endif
 
473
 
 
474
 
 
475
        switch (x->x_channels) {
 
476
        case 1:
 
477
                dsp_add(sfrecord_perform, 3, x,
 
478
                        sp[0]->s_vec, /* in 1 */
 
479
                        sp[0]->s_n);
 
480
                break;
 
481
        case 2:
 
482
                dsp_add(sfrecord_perform, 4, x, 
 
483
                        sp[0]->s_vec,
 
484
                        sp[1]->s_vec,
 
485
                        sp[0]->s_n);
 
486
                break;
 
487
        case 4:
 
488
                dsp_add(sfrecord_perform, 6, x, 
 
489
                        sp[0]->s_vec, 
 
490
                        sp[1]->s_vec,
 
491
                        sp[2]->s_vec,
 
492
                        sp[3]->s_vec,
 
493
                        sp[0]->s_n);
 
494
                break;
 
495
        case 8:
 
496
                dsp_add(sfrecord_perform, 9, x, 
 
497
                        sp[0]->s_vec, 
 
498
                        sp[1]->s_vec,
 
499
                        sp[2]->s_vec,
 
500
                        sp[3]->s_vec,
 
501
                        sp[4]->s_vec,
 
502
                        sp[5]->s_vec,
 
503
                        sp[6]->s_vec,
 
504
                        sp[7]->s_vec,
 
505
                        sp[0]->s_n);
 
506
                break;
 
507
        }
 
508
}
 
509
 
 
510
 
 
511
/* create sfrecord with args <channels> <skip> */
 
512
static void *sfrecord_new(t_floatarg chan)
 
513
{
 
514
        t_sfrecord *x = (t_sfrecord *)pd_new(sfrecord_class);
 
515
        t_int c = chan;
 
516
 
 
517
        switch(c){
 
518
                /* ok */
 
519
        case 1: case 2: case 4: case 8: break;
 
520
                /* try it, good luck ... */
 
521
        case 3: c = 2; break;     
 
522
        case 5: case 6: case 7: c=7; break;
 
523
        default: c=1; break;
 
524
        }
 
525
 
 
526
        outlet_new(&x->x_obj, &s_float);
 
527
 
 
528
        x->x_channels = c;
 
529
        x->x_skip = x->x_offset = 0;
 
530
        x->skip = 1;
 
531
        x->offset = 0.;
 
532
        x->x_speed = 1.0;
 
533
        x->write = 0;
 
534
        x->please_stop = 0;
 
535
        x->please_close = 0;
 
536
        x->state = SFRECORD_WAIT;
 
537
        x->count = 0;
 
538
        x->filename = NULL;
 
539
        x->fp = NULL;
 
540
        x->swap = 1;
 
541
 
 
542
        c--;
 
543
 
 
544
        while (c--) {
 
545
#ifdef DEBUG_ME
 
546
                post("create extra channel #%d", c);
 
547
#endif
 
548
                inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); /* channels inlet */
 
549
        }
 
550
 
 
551
        x->filep = t_getbytes(DACBLKSIZE*sizeof(short)*x->x_channels);
 
552
 
 
553
#ifdef DEBUG_ME
 
554
        post("get_bytes DACBLKSIZE*%d*%d->%ld",sizeof(short),x->x_channels,x->filep);
 
555
        post("sfrecord: x_channels = %d, x_speed = %f, x_skip = %f",x->x_channels,x->x_speed,x->x_skip);
 
556
#endif
 
557
 
 
558
        return (x);
 
559
}
 
560
 
 
561
 
 
562
static void sfrecord_helper(void)
 
563
{
 
564
        post("\nsfplay :: a raw-data soundfile-recorder");
 
565
        post("\ncreation :: sfrecord <channels>\t: channels set the number of channels");
 
566
        post("\nopen [<path>]<filename> [<endianity>]\t:: open b(ig) or l(ittle) endian file"
 
567
                "\nclose\t\t\t:: close file (aka eject)"
 
568
                "\nstart\t\t\t:: start playing"
 
569
                "\nstop\t\t\t:: stop playing"
 
570
                "\nbang\t\t\t:: outputs the current state (1_recording, 0_not-recording)");
 
571
                
 
572
        post("\n\nyou can also start recording with a �1�, and stop with a �0�");
 
573
}
 
574
 
 
575
 
 
576
static void sfrecord_free(t_sfrecord *x)
 
577
{
 
578
        freebytes(x->filep, DACBLKSIZE*sizeof(short)*x->x_channels);
 
579
}
 
580
 
 
581
void sfrecord_setup(void)
 
582
{
 
583
        sfrecord_class = class_new(gensym("sfrecord"), (t_newmethod)sfrecord_new, (t_method)sfrecord_free,
 
584
                sizeof(t_sfrecord), 0, A_DEFFLOAT, A_DEFFLOAT,0);
 
585
        class_addmethod(sfrecord_class, nullfn, gensym("signal"), 0);
 
586
        class_addmethod(sfrecord_class, (t_method)sfrecord_dsp, gensym("dsp"), 0);
 
587
 
 
588
        /* method open with filename */
 
589
        class_addmethod(sfrecord_class, (t_method)sfrecord_open, gensym("open"), A_SYMBOL,A_SYMBOL,A_NULL);
 
590
        class_addmethod(sfrecord_class, (t_method)sfrecord_close, gensym("close"), A_NULL);
 
591
        
 
592
        class_addmethod(sfrecord_class, (t_method)sfrecord_start, gensym("start"), A_NULL);
 
593
        class_addmethod(sfrecord_class, (t_method)sfrecord_stop,  gensym("stop"), A_NULL);
 
594
 
 
595
        /* start/stop with 0/1 */
 
596
        class_addfloat(sfrecord_class, sfrecord_float);
 
597
 
 
598
        /* bang out the current-state to the outlet*/
 
599
        class_addbang(sfrecord_class,sfrecord_bang);
 
600
 
 
601
        /* some help */
 
602
        class_addmethod(sfrecord_class, (t_method)sfrecord_helper,  gensym("help"), A_NULL);
 
603
        class_sethelpsymbol(sfrecord_class, gensym("sf-play_record"));
 
604
  zexy_register("sfrecord");
 
605
}