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
|
/*------------------------ samplebox~ ----------------------------------------- */
/* */
/* samplebox~ : records and plays back a sound */
/* Written by Yves Degoyon ( ydegoyon@free.fr ) */
/* */
/* 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. */
/* */
/* See file LICENSE for further informations on licensing terms. */
/* */
/* 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, write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* */
/* Based on PureData by Miller Puckette and others. */
/* */
/* All i wanted is your time */
/* All you gave me was tomorrow */
/* But, tomorrow never comes, tomorrow never comes */
/* Vini Reilly -- "Tomorrow" */
/* ---------------------------------------------------------------------------- */
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifndef MACOSX
#include <malloc.h>
#endif
#include <ctype.h>
#ifdef UNIX
#include <unistd.h>
#endif
#ifdef NT
#define M_PI 3.14159265358979323846
#endif
#include <math.h>
#include "m_pd.h" /* standard pd stuff */
static char *samplebox_version = "samplebox~: stores and plays back a sound version 0.3, written by Yves Degoyon (ydegoyon@free.fr)";
static t_class *samplebox_class;
typedef struct _samplebox
{
t_object x_obj;
t_int x_size; /* size of the stored sound ( in blocks~ ) */
t_float x_samplerate; /* sample rate */
t_int x_blocksize; /* current block size ( might be modified by block~ object ) */
t_float x_readpos; /* data's playing position */
t_int x_writepos; /* data's recording position */
t_int x_readstart; /* data's starting position for reading */
t_int x_readend; /* data's ending position for reading */
t_int x_modstart; /* data's starting position for modifications */
t_int x_modend; /* data's ending position for modifications */
t_int x_play; /* playing on/off flag */
t_float x_readspeed; /* number of grouped blocks for reading */
t_float x_record; /* flag to start recording process */
t_float x_allocate; /* flag to avoid reading data during generation */
t_float *x_rdata; /* table containing left channel of the sound */
t_float *x_idata; /* table containing right channel of the sound */
t_float *x_rootsquares; /* sum of the root squares of a block ( energy ) */
t_float x_phase; /* phase to apply on output */
t_outlet *x_recordend; /* outlet for end of recording */
t_outlet *x_playend; /* outlet for end of playing back */
t_float x_f; /* float needed for signal input */
} t_samplebox;
/* clean up */
static void samplebox_free(t_samplebox *x)
{
if ( x->x_rdata != NULL ) {
freebytes(x->x_rdata, x->x_size*x->x_blocksize*sizeof(float) );
post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
x->x_rdata = NULL;
}
if ( x->x_idata != NULL ) {
freebytes(x->x_idata, x->x_size*x->x_blocksize*sizeof(float) );
post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
x->x_idata = NULL;
}
if ( x->x_rootsquares != NULL ) {
freebytes(x->x_rootsquares, x->x_size*sizeof(float) );
post( "Freed %d bytes", x->x_size*sizeof(float) );
x->x_rootsquares = NULL;
}
}
/* allocate tables for storing sound */
static t_int samplebox_allocate(t_samplebox *x)
{
if ( !(x->x_rdata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) ) {
return -1;
} else {
post( "samplebox~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
}
if ( !(x->x_idata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) ) {
return -1;
} else {
post( "samplebox~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) );
}
if ( !(x->x_rootsquares = getbytes( x->x_size*sizeof(float) ) ) ) {
return -1;
} else {
post( "samplebox~ : allocated %d bytes", x->x_size*sizeof(float) );
}
return 0;
}
/* reallocate tables for storing sound */
static t_int samplebox_reallocate(t_samplebox *x, t_int ioldsize, t_int inewsize)
{
t_float *prdata=x->x_rdata, *pidata=x->x_idata, *prootsquares=x->x_rootsquares;
if ( !(x->x_rdata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) ) {
return -1;
} else {
post( "samplebox~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) );
}
if ( !(x->x_idata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) ) {
return -1;
} else {
post( "samplebox~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) );
}
if ( !(x->x_rootsquares = getbytes( inewsize*sizeof(float) ) ) ) {
return -1;
} else {
post( "samplebox~ : allocated %d bytes", inewsize*sizeof(float) );
}
if ( prdata != NULL ) {
freebytes(prdata, ioldsize*x->x_blocksize*sizeof(float) );
post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) );
}
if ( pidata != NULL ) {
freebytes(pidata, ioldsize*x->x_blocksize*sizeof(float) );
post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) );
}
if ( prootsquares != NULL ) {
freebytes(prootsquares, ioldsize*sizeof(float) );
post( "Freed %d bytes", ioldsize*sizeof(float) );
}
return 0;
}
/* records or playback the sonogram */
static t_int *samplebox_perform(t_int *w)
{
t_float *rin = (t_float *)(w[1]);
t_float *iin = (t_float *)(w[2]);
t_float *rout = (t_float *)(w[3]);
t_float *iout = (t_float *)(w[4]);
t_float fspectrum = 0.0;
t_float fphase = 0.0;
t_int rpoint;
t_int n = (int)(w[5]); /* number of samples */
t_samplebox *x = (t_samplebox *)(w[6]);
t_int bi;
t_float v[4];
t_float z[4];
// reallocate tables if blocksize has been changed
if ( n != x->x_blocksize ) {
post( "samplebox~ : reallocating tables" );
x->x_allocate = 1;
samplebox_free(x);
x->x_blocksize = n;
samplebox_allocate(x);
x->x_allocate = 0;
}
// new block : energy is set to zero
// if ( x->x_record ) {
// *(x->x_rootsquares+x->x_writepos) = 0.0;
// }
if ( x->x_play || x->x_record ) {
bi = 0;
while (bi<n) {
// eventually records input
if ( !x->x_allocate && x->x_record) {
*(x->x_rdata+(x->x_writepos*x->x_blocksize)+bi)=*rin;
*(x->x_idata+(x->x_writepos*x->x_blocksize)+bi)=*iin;
// *(x->x_rootsquares+x->x_writepos) += sqrt( pow((*rin),2) + pow((*iin),2) );
}
// set outputs
if ( !x->x_allocate && x->x_play) {
*rout = 0.;
*iout = 0.;
// interpolates 4 points like tabread4
rpoint = ((int)x->x_readpos*x->x_blocksize)+bi;
if ( rpoint == 0 ) {
v[0]=0.0;
v[1]=*(x->x_rdata+rpoint);
v[2]=*(x->x_rdata+rpoint+1);
v[3]=*(x->x_rdata+rpoint+2);
z[0]=0.0;
z[1]=*(x->x_idata+rpoint);
z[2]=*(x->x_idata+rpoint+1);
z[3]=*(x->x_idata+rpoint+2);
} else if ( rpoint == (x->x_size*x->x_blocksize-1) ) {
v[0]=*(x->x_rdata+rpoint-1);
v[1]=*(x->x_rdata+rpoint);
v[2]=*(x->x_rdata+rpoint+1);
v[3]=0.0;
z[0]=*(x->x_idata+rpoint-1);
z[1]=*(x->x_idata+rpoint);
z[2]=*(x->x_idata+rpoint+1);
z[3]=0.0;
} else if ( rpoint == (x->x_size*x->x_blocksize) ) {
v[0]=*(x->x_rdata+rpoint-1);
v[1]=*(x->x_rdata+rpoint);
v[2]=0.0;
v[3]=0.0;
z[0]=*(x->x_idata+rpoint-1);
z[1]=*(x->x_idata+rpoint);
z[2]=0.0;
z[3]=0.0;
} else {
v[0]=*(x->x_rdata+rpoint-1);
v[1]=*(x->x_rdata+rpoint);
v[2]=*(x->x_rdata+rpoint+1);
v[3]=*(x->x_rdata+rpoint+2);
z[0]=*(x->x_idata+rpoint-1);
z[1]=*(x->x_idata+rpoint);
z[2]=*(x->x_idata+rpoint+1);
z[3]=*(x->x_idata+rpoint+2);
}
{
t_float frac = rpoint-(int)rpoint;
// taken from tabread4_tilde
*rout = v[1]+frac*((v[2]-v[1])-0.5f*(frac-1.)*((v[0]-v[3]+3.0f*(v[2]-v[1]))*frac+(2.0f*v[1]-v[0]-v[2])));
*iout = z[1]+frac*((z[2]-z[1])-0.5f*(frac-1.)*((z[0]-z[3]+3.0f*(z[2]-z[1]))*frac+(2.0f*z[1]-z[0]-z[2])));
}
// add phase argument
fspectrum = sqrt( pow( *rout, 2) + pow( *iout, 2) );
fphase = atan2( *iout, *rout );
fphase += (x->x_phase/180.0)*(M_PI);
*rout = fspectrum*cos( fphase );
*iout = fspectrum*sin( fphase );
} else {
*rout=0.0;
*iout=0.0;
}
rout++;iout++;
rin++;iin++;
bi++;
}
// reset playing position until next play
if ( x->x_play ) {
x->x_readpos+=x->x_readspeed;
// post( "xreadpos : %f (added %f)", x->x_readpos, x->x_readspeed );
if ( x->x_readpos >= (x->x_readend*x->x_size)/100 ) {
x->x_play=0;
x->x_readpos=(x->x_readstart*x->x_size)/100;
// post( "samplebox~ : stopped playing (readpos=%d)", x->x_readpos );
outlet_bang(x->x_playend);
}
}
// reset recording position until next record
if ( x->x_record ) {
x->x_writepos++;
if ( x->x_writepos >= x->x_size ) {
x->x_record=0;
x->x_writepos=0;
outlet_bang(x->x_recordend);
// post( "samplebox~ : stopped recording" );
}
}
}
return (w+7);
}
static void samplebox_dsp(t_samplebox *x, t_signal **sp)
{
dsp_add(samplebox_perform, 6, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, sp[0]->s_n, x);
}
/* record the sonogram */
static void samplebox_record(t_samplebox *x)
{
x->x_record=1;
x->x_writepos=0;
}
/* play the sonogram */
static void samplebox_play(t_samplebox *x)
{
x->x_play=1;
// reset read position
x->x_readpos=(x->x_readstart*x->x_size)/100;
}
/* setting the starting point for reading ( in percent ) */
static void samplebox_readstart(t_samplebox *x, t_floatarg fstart)
{
t_float startpoint = fstart;
if (startpoint < 0) startpoint = 0;
if (startpoint > 100) startpoint = 100;
if ( startpoint > x->x_readend ) {
x->x_readstart = x->x_readend;
post( "samplebox~ : warning : range for reading is null" );
} else {
x->x_readstart=startpoint;
}
}
/* setting the starting point for modification ( in percent ) */
static void samplebox_modstart(t_samplebox *x, t_floatarg fstart)
{
t_float startpoint = fstart;
if (startpoint < 0) startpoint = 0;
if (startpoint > 100) startpoint = 100;
if ( startpoint > x->x_modend ) {
x->x_modstart = x->x_modend;
post( "samplebox~ : warning : range for modifications is null" );
} else {
x->x_modstart=startpoint;
}
}
/* setting the ending point for reading ( in percent ) */
static void samplebox_readend(t_samplebox *x, t_floatarg fend)
{
t_float endpoint = fend;
if (endpoint < 0) endpoint = 0;
if (endpoint > 100) endpoint = 100;
if ( endpoint < x->x_readstart ) {
x->x_readend = x->x_readstart;
post( "samplebox~ : warning : range for reading is null" );
} else {
x->x_readend=endpoint;
}
}
/* setting the ending point for modification ( in percent ) */
static void samplebox_modend(t_samplebox *x, t_floatarg fend)
{
t_float endpoint = fend;
if (endpoint < 0) endpoint = 0;
if (endpoint > 100) endpoint = 100;
if ( endpoint < x->x_modstart ) {
x->x_modend = x->x_modstart;
post( "samplebox~ : warning : range for modifications is null" );
} else {
x->x_modend=endpoint;
}
}
/* sets the reading speed */
static void samplebox_readspeed(t_samplebox *x, t_floatarg freadspeed)
{
if ((int)freadspeed < 0 ) {
post( "samplebox~ : wrong readspeed argument" );
}
x->x_readspeed=freadspeed;
}
/* resize sonogram */
static void samplebox_resize(t_samplebox *x, t_floatarg fnewsize )
{
if (fnewsize <= 0) {
post( "samplebox~ : error : wrong size" );
return;
}
post( "samplebox~ : reallocating tables" );
x->x_record = 0;
x->x_play = 0;
x->x_allocate = 1;
samplebox_reallocate(x, x->x_size, fnewsize);
x->x_size = fnewsize;
x->x_allocate = 0;
}
/* flip blocks */
static void samplebox_flipblocks(t_samplebox *x)
{
t_int samplestart, sampleend, middlesample, fi, si;
t_float fvalue;
samplestart=(x->x_modstart*(x->x_size-1))/100;
sampleend=(x->x_modend*(x->x_size-1))/100;
middlesample = ( sampleend+samplestart+1 ) / 2;
post( "flip blocks [%d,%d] and [%d,%d]", samplestart, middlesample, middlesample, sampleend );
for ( si=samplestart; si<=middlesample; si++ ) {
for ( fi=0; fi<x->x_blocksize; fi++ ) {
fvalue = *(x->x_rdata+((si)*x->x_blocksize)+fi);
*(x->x_rdata+((si)*x->x_blocksize)+fi) = *(x->x_rdata+((sampleend+samplestart-si)*x->x_blocksize)+fi);
*(x->x_rdata+((sampleend+samplestart-si)*x->x_blocksize)+fi) = fvalue;
fvalue = *(x->x_idata+((si)*x->x_blocksize)+fi);
*(x->x_idata+((si)*x->x_blocksize)+fi) = *(x->x_idata+((sampleend+samplestart-si)*x->x_blocksize)+fi);
*(x->x_idata+((sampleend+samplestart-si)*x->x_blocksize)+fi) = fvalue;
}
}
}
/* change the phase */
static void samplebox_phase(t_samplebox *x, t_floatarg fincphase)
{
if (fincphase < 0 || fincphase > 90) {
post( "samplebox~ : error : wrong phase in phase function : out of [0,90]" );
return;
}
x->x_phase = fincphase;
}
/* swap blocks */
static void samplebox_swapblocks(t_samplebox *x, t_floatarg fperstart, t_floatarg fperend, t_floatarg fpersize)
{
t_int samplestart, samplestartb, samplesize, sp, sf;
t_int iperstart, iperend, ipersize;
t_float s1, s2;
t_float fvalue;
iperstart = fperstart;
iperend = fperend;
ipersize = fpersize;
if (iperstart < 0 || iperstart > iperend ||
iperend <= 0 || iperend+ipersize > 100 ||
ipersize < 0 || fpersize > 100 ) {
post( "samplebox~ : error : wrong interval [%d%%, %d%%] <-> [%d%%, %d%%]",
iperstart, iperstart+ipersize, iperend, iperend+ipersize );
return;
}
samplestart=(x->x_modstart*(x->x_size-1))/100;
samplestartb=(x->x_modend*(x->x_size-1))/100;
samplesize=((samplestartb-samplestart)*ipersize)/100;
samplestart=samplestart+((samplestartb-samplestart)*iperstart)/100;
samplestartb=samplestart+((samplestartb-samplestart)*iperend)/100;
// post( "swap blocks [%d,%d] and [%d,%d]", samplestart, samplestart+samplesize, samplestartb, samplestartb+samplesize );
for ( sp=samplesize; sp>=0; sp-- ) {
for ( sf=0; sf<x->x_blocksize; sf++) {
fvalue = *(x->x_rdata+((int)(samplestart+sp)*x->x_blocksize)+sf);
*(x->x_rdata+((int)(samplestart+sp)*x->x_blocksize)+sf) = *(x->x_rdata+((int)(samplestartb+sp)*x->x_blocksize)+sf);
*(x->x_rdata+((int)(samplestartb+sp)*x->x_blocksize)+sf) = fvalue;
fvalue = *(x->x_idata+((int)(samplestart+sp)*x->x_blocksize)+sf);
*(x->x_idata+((int)(samplestart+sp)*x->x_blocksize)+sf) = *(x->x_idata+((int)(samplestartb+sp)*x->x_blocksize)+sf);
*(x->x_idata+((int)(samplestartb+sp)*x->x_blocksize)+sf) = fvalue;
}
}
}
static void *samplebox_new(t_floatarg fsize)
{
t_samplebox *x = (t_samplebox *)pd_new(samplebox_class);
outlet_new(&x->x_obj, &s_signal);
outlet_new(&x->x_obj, &s_signal);
x->x_recordend = outlet_new(&x->x_obj, &s_bang );
x->x_playend = outlet_new(&x->x_obj, &s_bang );
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readstart"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readend"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("modstart"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("modend"));
if ( fsize <= 0 ) {
error( "samplebox~ : missing or negative creation arguments" );
return NULL;
}
x->x_size = fsize;
x->x_blocksize = sys_getblksize();
x->x_play = 0;
x->x_readspeed = 1.;
x->x_record = 0;
x->x_readpos = 0.;
x->x_writepos = 0;
x->x_modstart = 0;
x->x_readstart = 0;
x->x_modend = 100;
x->x_readend = 100;
x->x_allocate = 0;
x->x_rdata = NULL;
x->x_idata = NULL;
x->x_rootsquares = NULL;
x->x_phase = 0.0;
x->x_samplerate = sys_getsr();
if ( samplebox_allocate(x) <0 ) {
return NULL;
} else {
return(x);
}
}
void samplebox_tilde_setup(void)
{
post(samplebox_version);
samplebox_class = class_new(gensym("samplebox~"), (t_newmethod)samplebox_new, (t_method)samplebox_free,
sizeof(t_samplebox), 0, A_DEFFLOAT, 0);
class_sethelpsymbol( samplebox_class, gensym("help-samplebox~.pd") );
CLASS_MAINSIGNALIN( samplebox_class, t_samplebox, x_f );
class_addmethod(samplebox_class, (t_method)samplebox_dsp, gensym("dsp"), 0);
class_addmethod(samplebox_class, (t_method)samplebox_record, gensym("record"), 0);
class_addmethod(samplebox_class, (t_method)samplebox_resize, gensym("resize"), A_FLOAT, 0);
class_addmethod(samplebox_class, (t_method)samplebox_swapblocks, gensym("swapblocks"), A_FLOAT, A_FLOAT, A_FLOAT, 0);
class_addmethod(samplebox_class, (t_method)samplebox_flipblocks, gensym("flipblocks"), 0);
class_addmethod(samplebox_class, (t_method)samplebox_play, gensym("play"), 0);
class_addmethod(samplebox_class, (t_method)samplebox_phase, gensym("phase"), A_FLOAT, 0);
class_addmethod(samplebox_class, (t_method)samplebox_modstart, gensym("modstart"), A_FLOAT, 0);
class_addmethod(samplebox_class, (t_method)samplebox_modend, gensym("modend"), A_FLOAT, 0);
class_addmethod(samplebox_class, (t_method)samplebox_readstart, gensym("readstart"), A_FLOAT, 0);
class_addmethod(samplebox_class, (t_method)samplebox_readend, gensym("readend"), A_FLOAT, 0);
class_addmethod(samplebox_class, (t_method)samplebox_readspeed, gensym("readspeed"), A_FLOAT, 0);
}
|