| 1 |
/*------------------------ samplebox~ ----------------------------------------- */ |
| 2 |
/* */ |
| 3 |
/* samplebox~ : records and plays back a sound */ |
| 4 |
/* Written by Yves Degoyon ( [email protected] ) */ |
| 5 |
/* */ |
| 6 |
/* This program is free software; you can redistribute it and/or */ |
| 7 |
/* modify it under the terms of the GNU General Public License */ |
| 8 |
/* as published by the Free Software Foundation; either version 2 */ |
| 9 |
/* of the License, or (at your option) any later version. */ |
| 10 |
/* */ |
| 11 |
/* See file LICENSE for further informations on licensing terms. */ |
| 12 |
/* */ |
| 13 |
/* This program is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* You should have received a copy of the GNU General Public License */ |
| 19 |
/* along with this program; if not, write to the Free Software */ |
| 20 |
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 21 |
/* */ |
| 22 |
/* Based on PureData by Miller Puckette and others. */ |
| 23 |
/* */ |
| 24 |
/* All i wanted is your time */ |
| 25 |
/* All you gave me was tomorrow */ |
| 26 |
/* But, tomorrow never comes, tomorrow never comes */ |
| 27 |
/* Vini Reilly -- "Tomorrow" */ |
| 28 |
/* ---------------------------------------------------------------------------- */ |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <sys/types.h> |
| 33 |
#include <string.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <errno.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <fcntl.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#ifndef __APPLE__ |
| 40 |
#include <malloc.h> |
| 41 |
#endif |
| 42 |
#include <ctype.h> |
| 43 |
#include <math.h> |
| 44 |
|
| 45 |
#ifdef _MSC_VER |
| 46 |
# define M_PI 3.14159265358979323846 |
| 47 |
#else |
| 48 |
# include <unistd.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#include "m_pd.h" /* standard pd stuff */ |
| 52 |
|
| 53 |
static char *samplebox_version = "samplebox~: stores and plays back a sound version 0.3, written by Yves Degoyon ([email protected])"; |
| 54 |
|
| 55 |
|
| 56 |
static t_class *samplebox_class; |
| 57 |
|
| 58 |
typedef struct _samplebox |
| 59 |
{ |
| 60 |
t_object x_obj; |
| 61 |
|
| 62 |
t_int x_size; /* size of the stored sound ( in blocks~ ) */ |
| 63 |
t_float x_samplerate; /* sample rate */ |
| 64 |
t_int x_blocksize; /* current block size ( might be modified by block~ object ) */ |
| 65 |
t_float x_readpos; /* data's playing position */ |
| 66 |
t_int x_writepos; /* data's recording position */ |
| 67 |
t_int x_readstart; /* data's starting position for reading */ |
| 68 |
t_int x_readend; /* data's ending position for reading */ |
| 69 |
t_int x_modstart; /* data's starting position for modifications */ |
| 70 |
t_int x_modend; /* data's ending position for modifications */ |
| 71 |
t_int x_play; /* playing on/off flag */ |
| 72 |
t_float x_readspeed; /* number of grouped blocks for reading */ |
| 73 |
t_float x_record; /* flag to start recording process */ |
| 74 |
t_float x_allocate; /* flag to avoid reading data during generation */ |
| 75 |
t_float *x_rdata; /* table containing left channel of the sound */ |
| 76 |
t_float *x_idata; /* table containing right channel of the sound */ |
| 77 |
t_float *x_rootsquares; /* sum of the root squares of a block ( energy ) */ |
| 78 |
t_float x_phase; /* phase to apply on output */ |
| 79 |
t_outlet *x_recordend; /* outlet for end of recording */ |
| 80 |
t_outlet *x_playend; /* outlet for end of playing back */ |
| 81 |
t_float x_f; /* float needed for signal input */ |
| 82 |
|
| 83 |
} t_samplebox; |
| 84 |
|
| 85 |
/* clean up */ |
| 86 |
static void samplebox_free(t_samplebox *x) |
| 87 |
{ |
| 88 |
if ( x->x_rdata != NULL ) |
| 89 |
{ |
| 90 |
freebytes(x->x_rdata, x->x_size*x->x_blocksize*sizeof(float) ); |
| 91 |
post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) ); |
| 92 |
x->x_rdata = NULL; |
| 93 |
} |
| 94 |
if ( x->x_idata != NULL ) |
| 95 |
{ |
| 96 |
freebytes(x->x_idata, x->x_size*x->x_blocksize*sizeof(float) ); |
| 97 |
post( "Freed %d bytes", x->x_size*x->x_blocksize*sizeof(float) ); |
| 98 |
x->x_idata = NULL; |
| 99 |
} |
| 100 |
if ( x->x_rootsquares != NULL ) |
| 101 |
{ |
| 102 |
freebytes(x->x_rootsquares, x->x_size*sizeof(float) ); |
| 103 |
post( "Freed %d bytes", x->x_size*sizeof(float) ); |
| 104 |
x->x_rootsquares = NULL; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/* allocate tables for storing sound */ |
| 109 |
static t_int samplebox_allocate(t_samplebox *x) |
| 110 |
{ |
| 111 |
if ( !(x->x_rdata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) ) |
| 112 |
{ |
| 113 |
return -1; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
post( "samplebox~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) ); |
| 118 |
} |
| 119 |
if ( !(x->x_idata = getbytes( x->x_size*x->x_blocksize*sizeof(float) ) ) ) |
| 120 |
{ |
| 121 |
return -1; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
post( "samplebox~ : allocated %d bytes", x->x_size*x->x_blocksize*sizeof(float) ); |
| 126 |
} |
| 127 |
if ( !(x->x_rootsquares = getbytes( x->x_size*sizeof(float) ) ) ) |
| 128 |
{ |
| 129 |
return -1; |
| 130 |
} |
| 131 |
else |
| 132 |
{ |
| 133 |
post( "samplebox~ : allocated %d bytes", x->x_size*sizeof(float) ); |
| 134 |
} |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
/* reallocate tables for storing sound */ |
| 139 |
static t_int samplebox_reallocate(t_samplebox *x, t_int ioldsize, t_int inewsize) |
| 140 |
{ |
| 141 |
t_float *prdata=x->x_rdata, *pidata=x->x_idata, *prootsquares=x->x_rootsquares; |
| 142 |
|
| 143 |
if ( !(x->x_rdata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) ) |
| 144 |
{ |
| 145 |
return -1; |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
post( "samplebox~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) ); |
| 150 |
} |
| 151 |
if ( !(x->x_idata = getbytes( inewsize*x->x_blocksize*sizeof(float) ) ) ) |
| 152 |
{ |
| 153 |
return -1; |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
post( "samplebox~ : allocated %d bytes", inewsize*x->x_blocksize*sizeof(float) ); |
| 158 |
} |
| 159 |
if ( !(x->x_rootsquares = getbytes( inewsize*sizeof(float) ) ) ) |
| 160 |
{ |
| 161 |
return -1; |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
post( "samplebox~ : allocated %d bytes", inewsize*sizeof(float) ); |
| 166 |
} |
| 167 |
if ( prdata != NULL ) |
| 168 |
{ |
| 169 |
freebytes(prdata, ioldsize*x->x_blocksize*sizeof(float) ); |
| 170 |
post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) ); |
| 171 |
} |
| 172 |
if ( pidata != NULL ) |
| 173 |
{ |
| 174 |
freebytes(pidata, ioldsize*x->x_blocksize*sizeof(float) ); |
| 175 |
post( "Freed %d bytes", ioldsize*x->x_blocksize*sizeof(float) ); |
| 176 |
} |
| 177 |
if ( prootsquares != NULL ) |
| 178 |
{ |
| 179 |
freebytes(prootsquares, ioldsize*sizeof(float) ); |
| 180 |
post( "Freed %d bytes", ioldsize*sizeof(float) ); |
| 181 |
} |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
/* records or playback the sonogram */ |
| 186 |
static t_int *samplebox_perform(t_int *w) |
| 187 |
{ |
| 188 |
t_float *rin = (t_float *)(w[1]); |
| 189 |
t_float *iin = (t_float *)(w[2]); |
| 190 |
t_float *rout = (t_float *)(w[3]); |
| 191 |
t_float *iout = (t_float *)(w[4]); |
| 192 |
t_float fspectrum = 0.0; |
| 193 |
t_float fphase = 0.0; |
| 194 |
t_int rpoint; |
| 195 |
t_int n = (int)(w[5]); /* number of samples */ |
| 196 |
t_samplebox *x = (t_samplebox *)(w[6]); |
| 197 |
t_int bi; |
| 198 |
t_float v[4]; |
| 199 |
t_float z[4]; |
| 200 |
|
| 201 |
// reallocate tables if blocksize has been changed |
| 202 |
if ( n != x->x_blocksize ) |
| 203 |
{ |
| 204 |
post( "samplebox~ : reallocating tables" ); |
| 205 |
x->x_allocate = 1; |
| 206 |
samplebox_free(x); |
| 207 |
x->x_blocksize = n; |
| 208 |
samplebox_allocate(x); |
| 209 |
x->x_allocate = 0; |
| 210 |
} |
| 211 |
|
| 212 |
// new block : energy is set to zero |
| 213 |
// if ( x->x_record ) { |
| 214 |
// *(x->x_rootsquares+x->x_writepos) = 0.0; |
| 215 |
// } |
| 216 |
|
| 217 |
if ( x->x_play || x->x_record ) |
| 218 |
{ |
| 219 |
bi = 0; |
| 220 |
while (bi<n) |
| 221 |
{ |
| 222 |
// eventually records input |
| 223 |
if ( !x->x_allocate && x->x_record) |
| 224 |
{ |
| 225 |
*(x->x_rdata+(x->x_writepos*x->x_blocksize)+bi)=*rin; |
| 226 |
*(x->x_idata+(x->x_writepos*x->x_blocksize)+bi)=*iin; |
| 227 |
// *(x->x_rootsquares+x->x_writepos) += sqrt( pow((*rin),2) + pow((*iin),2) ); |
| 228 |
} |
| 229 |
// set outputs |
| 230 |
if ( !x->x_allocate && x->x_play) |
| 231 |
{ |
| 232 |
*rout = 0.; |
| 233 |
*iout = 0.; |
| 234 |
// interpolates 4 points like tabread4 |
| 235 |
rpoint = ((int)x->x_readpos*x->x_blocksize)+bi; |
| 236 |
|
| 237 |
if ( rpoint == 0 ) |
| 238 |
{ |
| 239 |
v[0]=0.0; |
| 240 |
v[1]=*(x->x_rdata+rpoint); |
| 241 |
v[2]=*(x->x_rdata+rpoint+1); |
| 242 |
v[3]=*(x->x_rdata+rpoint+2); |
| 243 |
z[0]=0.0; |
| 244 |
z[1]=*(x->x_idata+rpoint); |
| 245 |
z[2]=*(x->x_idata+rpoint+1); |
| 246 |
z[3]=*(x->x_idata+rpoint+2); |
| 247 |
} |
| 248 |
else if ( rpoint == (x->x_size*x->x_blocksize-1) ) |
| 249 |
{ |
| 250 |
v[0]=*(x->x_rdata+rpoint-1); |
| 251 |
v[1]=*(x->x_rdata+rpoint); |
| 252 |
v[2]=*(x->x_rdata+rpoint+1); |
| 253 |
v[3]=0.0; |
| 254 |
z[0]=*(x->x_idata+rpoint-1); |
| 255 |
z[1]=*(x->x_idata+rpoint); |
| 256 |
z[2]=*(x->x_idata+rpoint+1); |
| 257 |
z[3]=0.0; |
| 258 |
} |
| 259 |
else if ( rpoint == (x->x_size*x->x_blocksize) ) |
| 260 |
{ |
| 261 |
v[0]=*(x->x_rdata+rpoint-1); |
| 262 |
v[1]=*(x->x_rdata+rpoint); |
| 263 |
v[2]=0.0; |
| 264 |
v[3]=0.0; |
| 265 |
z[0]=*(x->x_idata+rpoint-1); |
| 266 |
z[1]=*(x->x_idata+rpoint); |
| 267 |
z[2]=0.0; |
| 268 |
z[3]=0.0; |
| 269 |
} |
| 270 |
else |
| 271 |
{ |
| 272 |
v[0]=*(x->x_rdata+rpoint-1); |
| 273 |
v[1]=*(x->x_rdata+rpoint); |
| 274 |
v[2]=*(x->x_rdata+rpoint+1); |
| 275 |
v[3]=*(x->x_rdata+rpoint+2); |
| 276 |
z[0]=*(x->x_idata+rpoint-1); |
| 277 |
z[1]=*(x->x_idata+rpoint); |
| 278 |
z[2]=*(x->x_idata+rpoint+1); |
| 279 |
z[3]=*(x->x_idata+rpoint+2); |
| 280 |
} |
| 281 |
|
| 282 |
{ |
| 283 |
t_float frac = rpoint-(int)rpoint; |
| 284 |
|
| 285 |
// taken from tabread4_tilde |
| 286 |
*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]))); |
| 287 |
*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]))); |
| 288 |
} |
| 289 |
|
| 290 |
// add phase argument |
| 291 |
fspectrum = sqrt( pow( *rout, 2) + pow( *iout, 2) ); |
| 292 |
fphase = atan2( *iout, *rout ); |
| 293 |
fphase += (x->x_phase/180.0)*(M_PI); |
| 294 |
*rout = fspectrum*cos( fphase ); |
| 295 |
*iout = fspectrum*sin( fphase ); |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
*rout=0.0; |
| 300 |
*iout=0.0; |
| 301 |
} |
| 302 |
rout++; |
| 303 |
iout++; |
| 304 |
rin++; |
| 305 |
iin++; |
| 306 |
bi++; |
| 307 |
} |
| 308 |
// reset playing position until next play |
| 309 |
if ( x->x_play ) |
| 310 |
{ |
| 311 |
x->x_readpos+=x->x_readspeed; |
| 312 |
// post( "xreadpos : %f (added %f)", x->x_readpos, x->x_readspeed ); |
| 313 |
if ( x->x_readpos >= (x->x_readend*x->x_size)/100 ) |
| 314 |
{ |
| 315 |
x->x_play=0; |
| 316 |
x->x_readpos=(x->x_readstart*x->x_size)/100; |
| 317 |
// post( "samplebox~ : stopped playing (readpos=%d)", x->x_readpos ); |
| 318 |
outlet_bang(x->x_playend); |
| 319 |
} |
| 320 |
} |
| 321 |
// reset recording position until next record |
| 322 |
if ( x->x_record ) |
| 323 |
{ |
| 324 |
x->x_writepos++; |
| 325 |
if ( x->x_writepos >= x->x_size ) |
| 326 |
{ |
| 327 |
x->x_record=0; |
| 328 |
x->x_writepos=0; |
| 329 |
outlet_bang(x->x_recordend); |
| 330 |
// post( "samplebox~ : stopped recording" ); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
return (w+7); |
| 335 |
} |
| 336 |
|
| 337 |
static void samplebox_dsp(t_samplebox *x, t_signal **sp) |
| 338 |
{ |
| 339 |
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); |
| 340 |
} |
| 341 |
|
| 342 |
/* record the sonogram */ |
| 343 |
static void samplebox_record(t_samplebox *x) |
| 344 |
{ |
| 345 |
x->x_record=1; |
| 346 |
x->x_writepos=0; |
| 347 |
} |
| 348 |
|
| 349 |
/* play the sonogram */ |
| 350 |
static void samplebox_play(t_samplebox *x) |
| 351 |
{ |
| 352 |
x->x_play=1; |
| 353 |
// reset read position |
| 354 |
x->x_readpos=(x->x_readstart*x->x_size)/100; |
| 355 |
} |
| 356 |
|
| 357 |
/* setting the starting point for reading ( in percent ) */ |
| 358 |
static void samplebox_readstart(t_samplebox *x, t_floatarg fstart) |
| 359 |
{ |
| 360 |
t_float startpoint = fstart; |
| 361 |
|
| 362 |
if (startpoint < 0) startpoint = 0; |
| 363 |
if (startpoint > 100) startpoint = 100; |
| 364 |
if ( startpoint > x->x_readend ) |
| 365 |
{ |
| 366 |
x->x_readstart = x->x_readend; |
| 367 |
post( "samplebox~ : warning : range for reading is null" ); |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
x->x_readstart=startpoint; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
/* setting the starting point for modification ( in percent ) */ |
| 376 |
static void samplebox_modstart(t_samplebox *x, t_floatarg fstart) |
| 377 |
{ |
| 378 |
t_float startpoint = fstart; |
| 379 |
|
| 380 |
if (startpoint < 0) startpoint = 0; |
| 381 |
if (startpoint > 100) startpoint = 100; |
| 382 |
if ( startpoint > x->x_modend ) |
| 383 |
{ |
| 384 |
x->x_modstart = x->x_modend; |
| 385 |
post( "samplebox~ : warning : range for modifications is null" ); |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
x->x_modstart=startpoint; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
/* setting the ending point for reading ( in percent ) */ |
| 394 |
static void samplebox_readend(t_samplebox *x, t_floatarg fend) |
| 395 |
{ |
| 396 |
t_float endpoint = fend; |
| 397 |
|
| 398 |
if (endpoint < 0) endpoint = 0; |
| 399 |
if (endpoint > 100) endpoint = 100; |
| 400 |
if ( endpoint < x->x_readstart ) |
| 401 |
{ |
| 402 |
x->x_readend = x->x_readstart; |
| 403 |
post( "samplebox~ : warning : range for reading is null" ); |
| 404 |
} |
| 405 |
else |
| 406 |
{ |
| 407 |
x->x_readend=endpoint; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/* setting the ending point for modification ( in percent ) */ |
| 412 |
static void samplebox_modend(t_samplebox *x, t_floatarg fend) |
| 413 |
{ |
| 414 |
t_float endpoint = fend; |
| 415 |
|
| 416 |
if (endpoint < 0) endpoint = 0; |
| 417 |
if (endpoint > 100) endpoint = 100; |
| 418 |
if ( endpoint < x->x_modstart ) |
| 419 |
{ |
| 420 |
x->x_modend = x->x_modstart; |
| 421 |
post( "samplebox~ : warning : range for modifications is null" ); |
| 422 |
} |
| 423 |
else |
| 424 |
{ |
| 425 |
x->x_modend=endpoint; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/* sets the reading speed */ |
| 430 |
static void samplebox_readspeed(t_samplebox *x, t_floatarg freadspeed) |
| 431 |
{ |
| 432 |
if ((int)freadspeed < 0 ) |
| 433 |
{ |
| 434 |
post( "samplebox~ : wrong readspeed argument" ); |
| 435 |
} |
| 436 |
x->x_readspeed=freadspeed; |
| 437 |
} |
| 438 |
|
| 439 |
/* resize sonogram */ |
| 440 |
static void samplebox_resize(t_samplebox *x, t_floatarg fnewsize ) |
| 441 |
{ |
| 442 |
if (fnewsize <= 0) |
| 443 |
{ |
| 444 |
post( "samplebox~ : error : wrong size" ); |
| 445 |
return; |
| 446 |
} |
| 447 |
post( "samplebox~ : reallocating tables" ); |
| 448 |
x->x_record = 0; |
| 449 |
x->x_play = 0; |
| 450 |
x->x_allocate = 1; |
| 451 |
samplebox_reallocate(x, x->x_size, fnewsize); |
| 452 |
x->x_size = fnewsize; |
| 453 |
x->x_allocate = 0; |
| 454 |
} |
| 455 |
|
| 456 |
/* flip blocks */ |
| 457 |
static void samplebox_flipblocks(t_samplebox *x) |
| 458 |
{ |
| 459 |
t_int samplestart, sampleend, middlesample, fi, si; |
| 460 |
t_float fvalue; |
| 461 |
|
| 462 |
samplestart=(x->x_modstart*(x->x_size-1))/100; |
| 463 |
sampleend=(x->x_modend*(x->x_size-1))/100; |
| 464 |
middlesample = ( sampleend+samplestart+1 ) / 2; |
| 465 |
post( "flip blocks [%d,%d] and [%d,%d]", samplestart, middlesample, middlesample, sampleend ); |
| 466 |
|
| 467 |
for ( si=samplestart; si<=middlesample; si++ ) |
| 468 |
{ |
| 469 |
for ( fi=0; fi<x->x_blocksize; fi++ ) |
| 470 |
{ |
| 471 |
fvalue = *(x->x_rdata+((si)*x->x_blocksize)+fi); |
| 472 |
*(x->x_rdata+((si)*x->x_blocksize)+fi) = *(x->x_rdata+((sampleend+samplestart-si)*x->x_blocksize)+fi); |
| 473 |
*(x->x_rdata+((sampleend+samplestart-si)*x->x_blocksize)+fi) = fvalue; |
| 474 |
fvalue = *(x->x_idata+((si)*x->x_blocksize)+fi); |
| 475 |
*(x->x_idata+((si)*x->x_blocksize)+fi) = *(x->x_idata+((sampleend+samplestart-si)*x->x_blocksize)+fi); |
| 476 |
*(x->x_idata+((sampleend+samplestart-si)*x->x_blocksize)+fi) = fvalue; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/* change the phase */ |
| 482 |
static void samplebox_phase(t_samplebox *x, t_floatarg fincphase) |
| 483 |
{ |
| 484 |
if (fincphase < 0 || fincphase > 90) |
| 485 |
{ |
| 486 |
post( "samplebox~ : error : wrong phase in phase function : out of [0,90]" ); |
| 487 |
return; |
| 488 |
} |
| 489 |
x->x_phase = fincphase; |
| 490 |
} |
| 491 |
|
| 492 |
/* swap blocks */ |
| 493 |
static void samplebox_swapblocks(t_samplebox *x, t_floatarg fperstart, t_floatarg fperend, t_floatarg fpersize) |
| 494 |
{ |
| 495 |
t_int samplestart, samplestartb, samplesize, sp, sf; |
| 496 |
t_int iperstart, iperend, ipersize; |
| 497 |
t_float s1, s2; |
| 498 |
t_float fvalue; |
| 499 |
|
| 500 |
iperstart = fperstart; |
| 501 |
iperend = fperend; |
| 502 |
ipersize = fpersize; |
| 503 |
|
| 504 |
if (iperstart < 0 || iperstart > iperend || |
| 505 |
iperend <= 0 || iperend+ipersize > 100 || |
| 506 |
ipersize < 0 || fpersize > 100 ) |
| 507 |
{ |
| 508 |
post( "samplebox~ : error : wrong interval [%d%%, %d%%] <-> [%d%%, %d%%]", |
| 509 |
iperstart, iperstart+ipersize, iperend, iperend+ipersize ); |
| 510 |
return; |
| 511 |
} |
| 512 |
samplestart=(x->x_modstart*(x->x_size-1))/100; |
| 513 |
samplestartb=(x->x_modend*(x->x_size-1))/100; |
| 514 |
samplesize=((samplestartb-samplestart)*ipersize)/100; |
| 515 |
samplestart=samplestart+((samplestartb-samplestart)*iperstart)/100; |
| 516 |
samplestartb=samplestart+((samplestartb-samplestart)*iperend)/100; |
| 517 |
|
| 518 |
// post( "swap blocks [%d,%d] and [%d,%d]", samplestart, samplestart+samplesize, samplestartb, samplestartb+samplesize ); |
| 519 |
|
| 520 |
for ( sp=samplesize; sp>=0; sp-- ) |
| 521 |
{ |
| 522 |
for ( sf=0; sf<x->x_blocksize; sf++) |
| 523 |
{ |
| 524 |
fvalue = *(x->x_rdata+((int)(samplestart+sp)*x->x_blocksize)+sf); |
| 525 |
*(x->x_rdata+((int)(samplestart+sp)*x->x_blocksize)+sf) = *(x->x_rdata+((int)(samplestartb+sp)*x->x_blocksize)+sf); |
| 526 |
*(x->x_rdata+((int)(samplestartb+sp)*x->x_blocksize)+sf) = fvalue; |
| 527 |
fvalue = *(x->x_idata+((int)(samplestart+sp)*x->x_blocksize)+sf); |
| 528 |
*(x->x_idata+((int)(samplestart+sp)*x->x_blocksize)+sf) = *(x->x_idata+((int)(samplestartb+sp)*x->x_blocksize)+sf); |
| 529 |
*(x->x_idata+((int)(samplestartb+sp)*x->x_blocksize)+sf) = fvalue; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
static void *samplebox_new(t_floatarg fsize) |
| 535 |
{ |
| 536 |
t_samplebox *x = (t_samplebox *)pd_new(samplebox_class); |
| 537 |
outlet_new(&x->x_obj, &s_signal); |
| 538 |
outlet_new(&x->x_obj, &s_signal); |
| 539 |
x->x_recordend = outlet_new(&x->x_obj, &s_bang ); |
| 540 |
x->x_playend = outlet_new(&x->x_obj, &s_bang ); |
| 541 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal); |
| 542 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readstart")); |
| 543 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("readend")); |
| 544 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("modstart")); |
| 545 |
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("modend")); |
| 546 |
|
| 547 |
if ( fsize <= 0 ) |
| 548 |
{ |
| 549 |
error( "samplebox~ : missing or negative creation arguments" ); |
| 550 |
return NULL; |
| 551 |
} |
| 552 |
|
| 553 |
x->x_size = fsize; |
| 554 |
x->x_blocksize = sys_getblksize(); |
| 555 |
x->x_play = 0; |
| 556 |
x->x_readspeed = 1.; |
| 557 |
x->x_record = 0; |
| 558 |
x->x_readpos = 0.; |
| 559 |
x->x_writepos = 0; |
| 560 |
x->x_modstart = 0; |
| 561 |
x->x_readstart = 0; |
| 562 |
x->x_modend = 100; |
| 563 |
x->x_readend = 100; |
| 564 |
x->x_allocate = 0; |
| 565 |
x->x_rdata = NULL; |
| 566 |
x->x_idata = NULL; |
| 567 |
x->x_rootsquares = NULL; |
| 568 |
x->x_phase = 0.0; |
| 569 |
x->x_samplerate = sys_getsr(); |
| 570 |
if ( samplebox_allocate(x) <0 ) |
| 571 |
{ |
| 572 |
return NULL; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
return(x); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
void samplebox_tilde_setup(void) |
| 581 |
{ |
| 582 |
logpost(NULL, 4, "%s", samplebox_version); |
| 583 |
samplebox_class = class_new(gensym("samplebox~"), (t_newmethod)samplebox_new, (t_method)samplebox_free, |
| 584 |
sizeof(t_samplebox), 0, A_DEFFLOAT, 0); |
| 585 |
CLASS_MAINSIGNALIN( samplebox_class, t_samplebox, x_f ); |
| 586 |
class_addmethod(samplebox_class, (t_method)samplebox_dsp, gensym("dsp"), 0); |
| 587 |
class_addmethod(samplebox_class, (t_method)samplebox_record, gensym("record"), 0); |
| 588 |
class_addmethod(samplebox_class, (t_method)samplebox_resize, gensym("resize"), A_FLOAT, 0); |
| 589 |
class_addmethod(samplebox_class, (t_method)samplebox_swapblocks, gensym("swapblocks"), A_FLOAT, A_FLOAT, A_FLOAT, 0); |
| 590 |
class_addmethod(samplebox_class, (t_method)samplebox_flipblocks, gensym("flipblocks"), 0); |
| 591 |
class_addmethod(samplebox_class, (t_method)samplebox_play, gensym("play"), 0); |
| 592 |
class_addmethod(samplebox_class, (t_method)samplebox_phase, gensym("phase"), A_FLOAT, 0); |
| 593 |
class_addmethod(samplebox_class, (t_method)samplebox_modstart, gensym("modstart"), A_FLOAT, 0); |
| 594 |
class_addmethod(samplebox_class, (t_method)samplebox_modend, gensym("modend"), A_FLOAT, 0); |
| 595 |
class_addmethod(samplebox_class, (t_method)samplebox_readstart, gensym("readstart"), A_FLOAT, 0); |
| 596 |
class_addmethod(samplebox_class, (t_method)samplebox_readend, gensym("readend"), A_FLOAT, 0); |
| 597 |
class_addmethod(samplebox_class, (t_method)samplebox_readspeed, gensym("readspeed"), A_FLOAT, 0); |
| 598 |
} |