aboutsummaryrefslogtreecommitdiff
path: root/signal/sfwrite~.c
blob: e254137a19957a2eeb08ac8c1de6fd3551c8cfcd (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
/* (C) Guenter Geiger <geiger@epy.co.at> */


#include <m_pd.h>
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

#include <stdio.h>
#include <string.h>
#ifndef NT
#include <unistd.h>
#include <sys/mman.h>
#else
#include <io.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>


#ifdef NT
#define BINREADMODE "rb"
#define OPENPARAMS O_WRONLY | O_CREAT | O_TRUNC 
#else
#define BINREADMODE "r"
#define OPENPARAMS O_WRONLY | O_CREAT | O_NONBLOCK | O_TRUNC 
#endif
#define MAX_CHANS 4


/*
 * ------------------------------------------- sfwrite~ -------------------------------
 */

#define BLOCKTIME 0.01
#define uint32 unsigned int
#define uint16 unsigned short

static t_class *sfwrite_class;

typedef struct _sfwrite
{
     t_object x_obj;
     t_symbol* filename;
     int x_file;

     t_int rec;
     t_int x_channels;
     uint32  size;
     t_glist * x_glist;
     t_int x_blocked;
     t_int x_blockwarn;
} t_sfwrite;

typedef struct _wave
{
    char  w_fileid[4];	    	    /* chunk id 'RIFF'            */
    uint32 w_chunksize;     	    /* chunk size                 */
    char  w_waveid[4];	    	    /* wave chunk id 'WAVE'       */
    char  w_fmtid[4];	    	    /* format chunk id 'fmt '     */
    uint32 w_fmtchunksize;   	    /* format chunk size          */
    uint16  w_fmttag;	    	    /* format tag, 1 for PCM      */
    uint16  w_nchannels;    	    /* number of channels         */
    uint32 w_samplespersec;  	    /* sample rate in hz          */
    uint32 w_navgbytespersec; 	    /* average bytes per second   */
    uint16  w_nblockalign;    	    /* number of bytes per sample */
    uint16  w_nbitspersample; 	    /* number of bits in a sample */
    char  w_datachunkid[4]; 	    /* data chunk id 'data'       */
    uint32 w_datachunksize;         /* length of data chunk       */
} t_wave;


static void sfwrite_wave_setup(t_sfwrite* x,t_wave* w) 
{
     
    strncpy(w->w_fileid,"RIFF",4);	    	    /* chunk id 'RIFF'     */
    w->w_chunksize = x->size + sizeof(t_wave) -8;     	    /* chunk size  */
    strncpy(w->w_waveid,"WAVE",4);	    	    /* wave chunk id 'WAVE'  */
    strncpy(w->w_fmtid,"fmt ",4);	    	    /* format chunk id 'fmt '*/
    w->w_fmtchunksize = 16;   	    /* format chunk size          */
    w->w_fmttag = 1;	    	    /* format tag, 1 for PCM      */
    w->w_nchannels = x->x_channels;    	    /* number of channels         */
    w->w_samplespersec = 44100;  	    /* sample rate in hz          */
    w->w_navgbytespersec = 44100*x->x_channels*2; 	    /* average bytes per second   */
    w->w_nblockalign = 4;    	    /* number of bytes per sample */
    w->w_nbitspersample = 16; 	    /* number of bits in a sample */
    strncpy(w->w_datachunkid,"data",4); 	    /* data chunk id 'data'*/
    w->w_datachunksize = x->size;         /* length of data chunk       */
}



static void sfwrite_close(t_sfwrite *x)
{
     if (x->x_file > 0) {
	  t_wave w;
	  sfwrite_wave_setup(x,&w);
	  lseek(x->x_file,0,SEEK_SET);
	  write(x->x_file,&w,sizeof(w));
	  close(x->x_file);
     }
     x->x_file = -1;
}


static void sfwrite_open(t_sfwrite *x,t_symbol *filename)
{
     char fname[MAXPDSTRING];

     if (filename == &s_) {
	  post("sfwrite: open without filename");
	  return;
     }

     canvas_makefilename(glist_getcanvas(x->x_glist), filename->s_name,
			 fname, MAXPDSTRING);

     x->x_blocked = 0;
     x->filename = filename;
     post("sfwrite: filename = %s",x->filename->s_name);

     sfwrite_close(x);

     if ((x->x_file = open(fname,OPENPARAMS,0664)) < 0)
     {
	  error("can't create %s",fname);
	  return;
     }

     /* skip the header */

     lseek(x->x_file,sizeof(t_wave),SEEK_SET);
     x->size = 0;


}

static void sfwrite_block(t_sfwrite *x, t_floatarg f)
{
     x->x_blockwarn = f;
}


static void sfwrite_float(t_sfwrite *x, t_floatarg f)
{
  int t = f;
  if (t) {
       post("sfwrite: start", f); 
       x->rec=1;
  }
  else {
       post("sfwrite: stop", f); 
       x->rec=0;
  }

}


static short out[4*64];

static t_int *sfwrite_perform(t_int *w)
{
     t_sfwrite* x = (t_sfwrite*)(w[1]);
     t_float * in[4];
     int c = x->x_channels;
     int i,num,n;
     short* tout = out;
     int ret;
     double timebefore,timeafter;
     double late;

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

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

     /* loop */

     if (x->rec && x->x_file) {

	  while (n--) {
	       for (i=0;i<c;i++)  {
		    *tout++ = *(in[i])++ * 32768.;
	       }
	  }
	  
	  timebefore = sys_getrealtime();
	  if ((ret =write(x->x_file,out,sizeof(short)*num*c)) < (signed int)sizeof(short)*num*c) {
	       post("sfwrite: short write %d",ret);

	       }
	  timeafter = sys_getrealtime();
	  late = timeafter - timebefore;

	  /* OK, we let only 10 ms block here */
	  if (late > BLOCKTIME && x->x_blockwarn) { 
	       post("sfwrite blocked %f ms",late*1000);
	       x->x_blocked++;
	       if (x->x_blocked > x->x_blockwarn) {
		    x->rec = 0;
		    post("maximum blockcount %d reached, recording stopped (set blockcount with \"block <num>\"",x->x_blockwarn);
	       }
	  }
	  x->size +=64*x->x_channels*sizeof(short) ;
     }

     return (w+3+c);
}



static void sfwrite_dsp(t_sfwrite *x, t_signal **sp)
{
     switch (x->x_channels) {
     case 1:
	  dsp_add(sfwrite_perform, 3, x, sp[0]->s_vec, 
		   sp[0]->s_n);
	  break;
     case 2:
	  dsp_add(sfwrite_perform, 4, x, sp[0]->s_vec, 
		  sp[1]->s_vec, sp[0]->s_n);
	  break;
     case 4:
	  dsp_add(sfwrite_perform, 6, x, sp[0]->s_vec, 
		  sp[1]->s_vec,
		  sp[2]->s_vec,
		  sp[3]->s_vec,
		  sp[0]->s_n);
	  break;
     }
}

static void sfwrite_free(t_sfwrite* x)
{
     sfwrite_close(x);
}


static void *sfwrite_new(t_floatarg chan)
{
    t_sfwrite *x = (t_sfwrite *)pd_new(sfwrite_class);
    t_int c = chan;

    if (c<1 || c > MAX_CHANS) c = 1;

    x->x_glist = (t_glist*) canvas_getcurrent();
    x->x_channels = c--;
    x->x_file=0;
    x->rec = 0;
    x->size = 0;
    x->x_blocked = 0;
    x->x_blockwarn = 10;
    while (c--) {
	 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
    }


    return (x);
}

void sfwrite_tilde_setup(void)
{
     sfwrite_class = class_new(gensym("sfwrite~"), (t_newmethod)sfwrite_new, (t_method)sfwrite_free,
    	sizeof(t_sfwrite), 0,A_DEFFLOAT,0);
     class_addmethod(sfwrite_class,nullfn,gensym("signal"), 0);
     class_addmethod(sfwrite_class, (t_method) sfwrite_dsp, gensym("dsp"), 0);
     class_addmethod(sfwrite_class, (t_method) sfwrite_open, gensym("open"), A_SYMBOL,A_NULL);
     class_addmethod(sfwrite_class, (t_method) sfwrite_close, gensym("close"), 0);
    class_addmethod(sfwrite_class, (t_method)sfwrite_block,gensym("block"),A_DEFFLOAT,0);
     class_addfloat(sfwrite_class, sfwrite_float);
     
}