aboutsummaryrefslogtreecommitdiff
path: root/src/sfrecord.c
blob: c56adaf1bfc6a73d90e6464e1e1274eab75de4ff (plain)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/* 
 * sfrecord: multichannel soundfile recorder (try [writesf~] instead)
 *
 * (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
sfplay.c - Author: Winfried Ritsch - IEM Graz 10.Mai 99 - 
Modified:
sfrecord.c - hacked from sfplay ::: 2308:forum::für::umläute:1999 @ iem

please mail problems and ideas for improvements to
ritsch@iem.kug.ac.at
zmoelnig@iem.kug.ac.at
*/

/* TODO: deprecate this in favour of [writesf~] */

/* #define DEBUG_ME for debugging messages */

#include "zexy.h"


/* #include "m_imp.h" */

#define DACBLKSIZE 64	/* in m_imp.h, but error if it is included it here*/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>

/* ------------------------ sfrecord ----------------------------- */
#define MAX_CHANS 8		/* channels for soundfiles 1,2,4,8 */

#ifdef __WIN32__
# define BINWRITEMODE "wb"
#else
# include <unistd.h>
# include <sys/mman.h>
# define BINWRITEMODE "w"
#endif

static t_class *sfrecord_class;

typedef struct _sfrecord
{
	t_object x_obj;

	void*   filep;      /* pointer to file data read in mem */
	t_symbol* filename; /* filename */

	/*
		because there is no command queue,
		flags are used instead 
	*/
	t_int write;		/* write: 1, stop: 0 */
	t_int please_stop;	/* can be reset only by stop-state itself */ 
	t_int please_close;	/* can be reset only by close-state */
	t_int x_channels;	/* channels to write */
	t_float x_offset;	/* offset to start writing */
	t_float offset;		/* inlet value offset in secs */
	t_float x_skip;		/* skip bytes because header */
	t_int skip;			/* pending skip if 1 */
	t_float x_speed;	/* write speed, not supported in this version */
	t_int size;			/* size of file (if memory mapped) */
	t_int swap;			/* swap bytes from l->b or b->m */
	FILE *fp;			/* file oper non-NULL of open */
	t_int state;		/* which state is writer in */
	t_int count;		/* count for ticks before next step */

} t_sfrecord;

/* states of statemachine */
#define SFRECORD_WAIT	0		/* wait for open */
#define SFRECORD_OPEN	1
#define SFRECORD_CLOSE	2
#define SFRECORD_SKIP	3
#define SFRECORD_WRITE	4
#define SFRECORD_STOP	5
#define SFRECORD_ERROR	-1

#define SFRECORD_WAITTICKS 10	/* 1 tick of 64 Samples is ca.1.5ms on 441000 */

/* split the os-calls in as many steps as possible 
to split them on different ticks in steps of SFRECORD_WAITTICKS
to avoid peak performance */

/* like the one from garray */
static int sfrecord_am_i_big_endian(void) 
{
	unsigned short s = 1;
	unsigned char c = *(char *) (&s);
#ifdef DEBUG_ME
	post("i am %s-endian", c?"little":"big");
#endif
	return(c==0);
}

static void state_out(t_sfrecord *x, int state)
{	/* outlet the actual state */
	outlet_float(x->x_obj.ob_outlet, state);
}


/* METHOD: "open" file */

/* this dont use memory map, because I dont know about this on NT ? 
Use of the buffered functions fopen, fseek fread fclose instead the 
non buffered ones open read close */

static void sfrecord_open(t_sfrecord *x,t_symbol *filename,t_symbol *endian)
{

	if(x->state != SFRECORD_WAIT)
	{
		post("sfrecord: first close %s before open %s",x->filename->s_name,filename->s_name);
		return;
	}
 
  /* test if big endian else asume little endian 
   * should be 'l' but could be anything
   */
	if(sfrecord_am_i_big_endian())
		x->swap = !(endian->s_name[0] == 'b');
	else
		x->swap = (endian->s_name[0] == 'b');

	/* 
   * skip header after open;; sometimes weŽll have to write a header using the x->skip; so donŽt delete it completely 
   */
  /* x->skip = 1; */

	x->filename = filename;

#ifdef DEBUG_ME
	post("sfrecord: opening %s",x->filename->s_name);
#endif

	if (x->fp != NULL)fclose(x->fp);	/* should not happen */

	if (!(x->fp = fopen(x->filename->s_name,BINWRITEMODE))) 
	{
		error("sfrecord: can't open %s", x->filename->s_name);
	}
}



/* METHOD: close */
static void sfrecord_close(t_sfrecord *x)
{
	x->write = 0;
	x->please_close = 1;

	/* now in state machine 
	if(x->fp != NULL)
	{
		fclose(x->fp);
		x->fp = NULL;
	}
	*/

#ifdef DEBUG_ME
	post("sfrecord: closing ");
#endif
	return;
}

/* for skipping header of soundfile  DonŽt use this for memory map */

static int sfrecord_skip(t_sfrecord *x)
{
	if(!x->skip) return 0;

#ifdef DEBUG_ME
	post("sfrecord:skip to %f",x->x_skip);
#endif

	x->skip = 0;
	return 1;
}

/* Input, method for Start stop */

static void sfrecord_start(t_sfrecord *x)
{
#ifdef DEBUG_ME
	post("sfrecord: start at %d", x->x_offset);
#endif

	state_out(x, 1);
	x->write=1;
}

static void sfrecord_stop(t_sfrecord *x)
{
#ifdef DEBUG_ME
	post("sfrecord: stop");
#endif
	state_out(x, 0);

	x->write=0;
	x->please_stop = 1;
}

static void sfrecord_float(t_sfrecord *x, t_floatarg f)
{
	int t = f;
	if (t) sfrecord_start(x);
	else sfrecord_stop(x);
}

/* say what state weŽre in */
static void sfrecord_bang(t_sfrecord* x)
{
	if (x->state == SFRECORD_WRITE) state_out(x, 1); else state_out(x, 0);
}

/* ******************************************************************************** */
/*                          the work                krow eht                        */
/* ******************************************************************************** */


static t_int *sfrecord_perform(t_int *w)
{
	t_sfrecord* x = (t_sfrecord*)(w[1]);
	short* buf = x->filep;
	short* bufstart = buf;
	int c = x->x_channels;

	int i,j,n, s_n;
	t_float* in[MAX_CHANS];

	short s;
	int swap = x->swap;

	for (i=0;i<c;i++)  
		in[i] = (t_float *)(w[2+i]);

	n = s_n = (int)(w[2+c]);

	/* loop */

	switch(x->state){

		/* just wait */
	case SFRECORD_WAIT:

		if(x->fp != NULL){
#ifdef DEBUG_ME
			post("wait -> open");
#endif
			x->state = SFRECORD_OPEN;
			x->count = SFRECORD_WAITTICKS;
		};
		break;

		/* if in open state, already opened but wait for skip */
	case SFRECORD_OPEN: /* file has opened wait some time */

		if(!(x->count--)){
#ifdef DEBUG_ME
			post("open -> skip");
#endif
			x->state = SFRECORD_SKIP;
			x->count = SFRECORD_WAITTICKS;
		};

		break;

		/* in skipmode wait until ready for stop */
	case SFRECORD_SKIP:

		if(x->count == SFRECORD_WAITTICKS)
		{
			if(!x->fp)
			{
				x->state = SFRECORD_CLOSE;
				x->count=1;
#ifdef DEBUG_ME
				post("skip -> close");
#endif
				break;
			}
			sfrecord_skip(x);
		}
		if(!(x->count--))
		{
#ifdef DEBUG_ME
			post("skip -> stop");
#endif
			x->state = SFRECORD_STOP;
			x->count = SFRECORD_WAITTICKS;
		};
		break;

	case SFRECORD_STOP:		/* in stop state mainly waits for write */

		x->please_stop = 0;

		if(x->please_close)
		{
			x->state = SFRECORD_CLOSE;
			x->count = SFRECORD_WAITTICKS;      
#ifdef DEBUG_ME
			post("stop -> close");
#endif
		}
		else if(x->skip)
		{
			x->state = SFRECORD_SKIP;
			x->count = SFRECORD_WAITTICKS;

#ifdef DEBUG_ME
			post("stop -> skip");
#endif

		}
		else if(x->write)
		{

#ifdef DEBUG_ME
			post("stop -> write");
#endif
			x->state = SFRECORD_WRITE;
			state_out(x, 1);
		}
		break;

	case SFRECORD_WRITE:				/* yes write now */

		if(!x->write || x->please_stop)
		{
			/* if closing dont need to go to stop */
			if(x->please_close)	{
				x->state = SFRECORD_CLOSE;
				x->count = SFRECORD_WAITTICKS;
#ifdef DEBUG_ME
				post("write -> close");
#endif
				state_out(x, 0);

			}
			else	{
				x->state = SFRECORD_STOP;
#ifdef DEBUG_ME
				post("write -> stop");
#endif
			};
			break;
		}

		/* should never happen */
		if(!x->filep){
			x->state = SFRECORD_ERROR;
			error("sfrecord: writing but no buffer ???? write");
			return (w+4+c);
		}

		/* copy float to 16 Bit and swap if neccesairy */ /* LATER treat overflows */
		while (n--) {
			for (i=0;i<c;i++) {
			s = *in[i]++ * 32768.;
			if (swap) s = ((s & 0xFF)<< 8) | ((s& 0xFF00) >> 8);
			*buf++ = s;
			}
		}
		
		/* then write soundfile 16 bit*/		 
		if ( (j = fwrite(bufstart, sizeof(short), c*s_n, x->fp)) < 1) {
		  x->state = SFRECORD_ERROR;
		  x->count = SFRECORD_WAITTICKS;
#ifdef DEBUG_ME
			post("write -> write error\t %xd\t%xd\t%d\t%d", x->filep, buf, c*s_n*sizeof(short), j);
#endif
			break;
		}

#if 0
		if((j=fwrite(buf,sizeof(short),c*n,x->fp)) < (unsigned int) n)
		{
			if(feof(x->fp)){

				while (n--) {
					for (i=0;i<c;i++)	{
						if(--j > 0){
							s = *buf++;
							if(swap) s = ((s & 0xFF)<< 8) | ((s& 0xFF00) >> 8);
							*out[i]++ = s*(1./32768.);
						}
						else
							*out[i]++ = 0;
					}
				}
			}

			x->state = SFRECORD_STOP;
			x->write = 0;
			return(w+c+3);
			}
			/* or error if(ferror()) */
			x->state = SFRECORD_ERROR;
			x->count = SFRECORD_WAITTICKS;
#ifdef DEBUG_ME
			post("write -> write error");
#endif
			break;
		};
#endif /* 0 */
		return (w+c+3); /* writing was fine */


		/* ok :?: write error, please close */
	case SFRECORD_ERROR:

		if(!(x->count--))	{
			x->state = SFRECORD_CLOSE;
			sfrecord_close(x);
#ifdef DEBUG_ME
			post("sfrecord error writing sf: error -> close");
#endif
			x->count = SFRECORD_WAITTICKS;
		}
		break;

		/* in close state go to wait afterwards */
	case SFRECORD_CLOSE:

		x->please_close = 0;

		/* wait until ready for close operation */
		if(!(x->count--)){ 

			x->state = SFRECORD_WAIT;
			x->count = SFRECORD_WAITTICKS;

			/* avoid openfiles */
			if(x->fp){fclose(x->fp);x->fp = NULL;};

#ifdef DEBUG_ME
			post("sfrecord: close -> wait");
#endif
		}
		break;

	}; /*case */

	return(w+c+3);
}




/* ---------------------- Setup junk -------------------------- */

static void sfrecord_dsp(t_sfrecord *x, t_signal **sp)
{

#ifdef DEBUG_ME
	post("sfrecord: dsp");
	post("offset = %f\tspeed = %f\t", x->offset, x->x_speed);
#endif


	switch (x->x_channels) {
	case 1:
		dsp_add(sfrecord_perform, 3, x,
			sp[0]->s_vec, /* in 1 */
			sp[0]->s_n);
		break;
	case 2:
		dsp_add(sfrecord_perform, 4, x, 
			sp[0]->s_vec,
			sp[1]->s_vec,
			sp[0]->s_n);
		break;
	case 4:
		dsp_add(sfrecord_perform, 6, x, 
			sp[0]->s_vec, 
			sp[1]->s_vec,
			sp[2]->s_vec,
			sp[3]->s_vec,
			sp[0]->s_n);
		break;
	case 8:
		dsp_add(sfrecord_perform, 9, x, 
			sp[0]->s_vec, 
			sp[1]->s_vec,
			sp[2]->s_vec,
			sp[3]->s_vec,
			sp[4]->s_vec,
			sp[5]->s_vec,
			sp[6]->s_vec,
			sp[7]->s_vec,
			sp[0]->s_n);
		break;
	}
}


/* create sfrecord with args <channels> <skip> */
static void *sfrecord_new(t_floatarg chan)
{
	t_sfrecord *x = (t_sfrecord *)pd_new(sfrecord_class);
	t_int c = chan;

	switch(c){
		/* ok */
	case 1: case 2: case 4: case 8: break;
		/* try it, good luck ... */
	case 3: c = 2; break;     
	case 5: case 6: case 7: c=7; break;
	default: c=1; break;
	}

	outlet_new(&x->x_obj, &s_float);

	x->x_channels = c;
	x->x_skip = x->x_offset = 0;
	x->skip = 1;
	x->offset = 0.;
	x->x_speed = 1.0;
	x->write = 0;
	x->please_stop = 0;
	x->please_close = 0;
	x->state = SFRECORD_WAIT;
	x->count = 0;
	x->filename = NULL;
	x->fp = NULL;
	x->swap = 1;

	c--;

	while (c--) {
#ifdef DEBUG_ME
		post("create extra channel #%d", c);
#endif
		inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); /* channels inlet */
	}

	x->filep = t_getbytes(DACBLKSIZE*sizeof(short)*x->x_channels);

#ifdef DEBUG_ME
	post("get_bytes DACBLKSIZE*%d*%d->%ld",sizeof(short),x->x_channels,x->filep);
	post("sfrecord: x_channels = %d, x_speed = %f, x_skip = %f",x->x_channels,x->x_speed,x->x_skip);
#endif

	return (x);
}


static void sfrecord_helper(void)
{
	post("\nsfplay :: a raw-data soundfile-recorder");
	post("\ncreation :: sfrecord <channels>\t: channels set the number of channels");
	post("\nopen [<path>]<filename> [<endianity>]\t:: open b(ig) or l(ittle) endian file"
		"\nclose\t\t\t:: close file (aka eject)"
		"\nstart\t\t\t:: start playing"
		"\nstop\t\t\t:: stop playing"
		"\nbang\t\t\t:: outputs the current state (1_recording, 0_not-recording)");
		
	post("\n\nyou can also start recording with a Ž1Ž, and stop with a Ž0Ž");
}


static void sfrecord_free(t_sfrecord *x)
{
	freebytes(x->filep, DACBLKSIZE*sizeof(short)*x->x_channels);
}

void sfrecord_setup(void)
{
	sfrecord_class = class_new(gensym("sfrecord"), (t_newmethod)sfrecord_new, (t_method)sfrecord_free,
		sizeof(t_sfrecord), 0, A_DEFFLOAT, A_DEFFLOAT,0);
	class_addmethod(sfrecord_class, nullfn, gensym("signal"), 0);
	class_addmethod(sfrecord_class, (t_method)sfrecord_dsp, gensym("dsp"), 0);

	/* method open with filename */
	class_addmethod(sfrecord_class, (t_method)sfrecord_open, gensym("open"), A_SYMBOL,A_SYMBOL,A_NULL);
	class_addmethod(sfrecord_class, (t_method)sfrecord_close, gensym("close"), A_NULL);
	
	class_addmethod(sfrecord_class, (t_method)sfrecord_start, gensym("start"), A_NULL);
	class_addmethod(sfrecord_class, (t_method)sfrecord_stop,  gensym("stop"), A_NULL);

	/* start/stop with 0/1 */
	class_addfloat(sfrecord_class, sfrecord_float);

	/* bang out the current-state to the outlet*/
	class_addbang(sfrecord_class,sfrecord_bang);

	/* some help */
	class_addmethod(sfrecord_class, (t_method)sfrecord_helper,  gensym("help"), A_NULL);
	class_sethelpsymbol(sfrecord_class, gensym("sf-play_record"));
  zexy_register("sfrecord");
}