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 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
|
/*
syncgrain.c: Synchronous granular synthesis
Copyright (c) Victor Lazzarini, 2004
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "stdopcod.h"
#include "syncgrain.h"
#include "soundio.h"
#include "interlocks.h"
/*
#ifdef HAVE_VALUES_H
#include <values.h>
#endif
#ifndef MAXINT
#include <limits.h>
#define MAXINT INT_MAX
#endif
*/
static int32_t syncgrain_init(CSOUND *csound, syncgrain *p)
{
int32_t size;
p->efunc = csound->FTnp2Finde(csound, p->ifn2);
if (UNLIKELY(p->efunc == NULL))
return NOTOK;
p->sfunc = csound->FTnp2Finde(csound, p->ifn1);
if (UNLIKELY(p->sfunc == NULL))
return NOTOK;
p->olaps = (int32_t) *p->ols+2;
if (UNLIKELY(p->olaps < 2))
p->olaps = 2;
size = (p->olaps) * sizeof(double);
csound->AuxAlloc(csound, size, &p->index);
csound->AuxAlloc(csound, size, &p->envindex);
csound->AuxAlloc(csound, size, &p->envincr);
size = (p->olaps) * sizeof(int32_t);
csound->AuxAlloc(csound, size, &p->streamon);
p->count = 0; /* sampling period counter */
p->numstreams = 0; /* curr num of streams */
p->firststream = 0; /* streams index (first stream) */
p->datasize = p->sfunc->flen;
p->envtablesize = p->efunc->flen; /* size of envtable */
p->start = 0.0f;
p->frac = 0.0f;
return OK;
}
static int32_t syncgrain_process(CSOUND *csound, syncgrain *p)
{
MYFLT sig, pitch, amp, grsize, envincr, period, fperiod, prate;
MYFLT *output = p->output;
MYFLT *datap = p->sfunc->ftable;
MYFLT *ftable = p->efunc->ftable;
float start = p->start, frac = p->frac;
double *index = (double *) p->index.auxp;
double *envindex = (double *) p->envindex.auxp;
double *envincrn = (double *) p->envincr.auxp;
int32_t *streamon = (int32_t *) p->streamon.auxp;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t vecpos, vecsize=CS_KSMPS;
int32_t firststream = p->firststream;
int32_t numstreams = p->numstreams, olaps = p->olaps;
int32_t count = p->count, j, newstream;
int32_t datasize = p->datasize, envtablesize = p->envtablesize;
MYFLT pscale = p->sfunc->gen01args.sample_rate/CS_ESR;
pitch = *p->pitch * pscale;
fperiod = FABS(p->sfunc->gen01args.sample_rate/(*p->fr));
//if (UNLIKELY(fperiod < 0)) fperiod = -fperiod;
amp = *p->amp;
grsize = p->sfunc->gen01args.sample_rate * *p->grsize;
if (UNLIKELY(grsize<1)) goto err1;
envincr = envtablesize/grsize;
prate = *p->prate * pscale;
if (UNLIKELY(offset)) memset(output, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
vecsize -= early;
memset(&output[vecsize], '\0', early*sizeof(MYFLT));
}
for (vecpos = offset; vecpos < vecsize; vecpos++) {
sig = FL(0.0);
/* if a grain has finished, clean up */
if (UNLIKELY((!streamon[firststream]) && (numstreams) )) {
//numstreams--; /* decrease the no of streams */
firststream=(firststream+1)%olaps; /* first stream is the next */
}
/* if a fund period has elapsed */
/* start a new grain */
period = fperiod - frac;
if (count == 0 || count >= period) {
if (count) frac = count - period; /* frac part to be accummulated */
newstream =(firststream+numstreams)%olaps;
streamon[newstream] = 1; /* turn the stream on */
envindex[newstream] = 0.0;
envincrn[newstream] = envtablesize/grsize;
index[newstream] = start;
numstreams++; /* increase the stream count */
count = 0;
start += prate*grsize;
while (UNLIKELY(start >= datasize)) start-=datasize;
while (UNLIKELY(start < 0)) start+=datasize;
}
//for (i=numstreams,
// j=firststream; i; i--, j=(j+1)%olaps) {
for(j=0; j < olaps; j++){
if(!streamon[j]) continue;
/* modulus */
while (UNLIKELY(index[j] >= datasize))
index[j] -= datasize;
while (UNLIKELY(index[j] < 0))
index[j] += datasize;
if (UNLIKELY(envindex[j] < envtablesize)){
/* sum all the grain streams */
sig += ((datap[(int32_t)index[j]] +
(index[j] - (int32_t)index[j])*
(datap[(int32_t)index[j]+1] - datap[(int32_t)index[j]])
) *
(ftable[(int32_t)envindex[j]] +
(envindex[j] - (int32_t)envindex[j])*
(ftable[(int32_t)envindex[j]+1] - ftable[(int32_t)envindex[j]])
)
);
}
/* increment the indexes */
/* for each grain */
index[j] += pitch;
envindex[j] += envincr;
/* if the envelope is finished */
/* the grain is also finished */
if (UNLIKELY(envindex[j] >= envtablesize)) {
streamon[j] = 0;
numstreams--;
}
}
/* increment the period counter */
count++;
/* scale the output */
output[vecpos] = sig*amp;
}
p->firststream = firststream;
p->numstreams = numstreams;
p->count = count;
p->start = start;
p->frac = frac;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("grain size smaller than 1 sample\n"));
}
static int32_t syncgrainloop_init(CSOUND *csound, syncgrainloop *p)
{
p->efunc = csound->FTnp2Finde(csound, p->ifn2);
if (UNLIKELY(p->efunc == NULL))
return NOTOK;
p->sfunc = csound->FTnp2Finde(csound, p->ifn1);
if (UNLIKELY(p->sfunc == NULL))
return NOTOK;
p->datasize = p->sfunc->flen;
p->envtablesize = p->efunc->flen; /* size of envtable */
p->olaps = (int32_t) *p->ols+1;
if (UNLIKELY(p->olaps <2))
p->olaps = 2;
if (*p->iskip == 0) {
int32_t size = (p->olaps) * sizeof(double);
if (p->index.auxp == NULL || p->index.size < (uint32_t)size)
csound->AuxAlloc(csound, size, &p->index);
if (p->envindex.auxp == NULL || p->envindex.size < (uint32_t)size)
csound->AuxAlloc(csound, size, &p->envindex);
size = (p->olaps) * sizeof(int32_t);
if (p->streamon.auxp == NULL || p->streamon.size < (uint32_t)size)
csound->AuxAlloc(csound, size, &p->streamon);
p->count = 0; /* sampling period counter */
p->numstreams = 0; /* curr num of streams */
p->firststream = 0; /* streams index (first stream) */
p->start = *p->startpos*(p->sfunc->gen01args.sample_rate);
p->frac = 0.0f;
p->firsttime = 1;
}
return OK;
}
static int32_t syncgrainloop_process(CSOUND *csound, syncgrainloop *p)
{
MYFLT sig, pitch, amp, grsize, envincr, period, fperiod, prate;
MYFLT *output = p->output;
MYFLT *datap = p->sfunc->ftable;
MYFLT *ftable = p->efunc->ftable;
int32_t *streamon = (int32_t *) p->streamon.auxp;
float start = p->start, frac = p->frac;
double *index = (double *) p->index.auxp;
double *envindex = (double *) p->envindex.auxp;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t vecpos, vecsize=CS_KSMPS;
int32_t firststream = p->firststream;
int32_t numstreams = p->numstreams, olaps = p->olaps;
int32_t count = p->count, i,j, newstream;
int32_t datasize = p->datasize, envtablesize = p->envtablesize;
int32_t loop_start;
int32_t loop_end;
int32_t loopsize;
int32_t firsttime = p->firsttime;
MYFLT sr = p->sfunc->gen01args.sample_rate;
MYFLT pscale = sr/CS_ESR;
/* loop points & checks */
loop_start = (int32_t) (*p->loop_start*sr);
loop_end = (int32_t) (*p->loop_end*sr);
if (UNLIKELY(loop_start < 0)) loop_start = 0;
if (UNLIKELY(loop_start >= datasize)) loop_start = datasize-1;
loop_end = (loop_start > loop_end ? loop_start : loop_end);
loopsize = loop_end - loop_start;
/*csound->Message(csound, "st:%d, end:%d, loopsize=%d\n",
loop_start, loop_end, loopsize); */
pitch = *p->pitch * pscale;
fperiod = FABS(sr/(*p->fr));
//if (UNLIKELY(fperiod < 0)) fperiod = -fperiod;
amp = *p->amp;
grsize = sr * *p->grsize;
if (UNLIKELY(grsize<1)) goto err1;
if (loopsize <= 0) loopsize = grsize;
envincr = envtablesize/grsize;
prate = *p->prate * pscale;
if (UNLIKELY(offset)) memset(output, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
vecsize -= early;
memset(&output[vecsize], '\0', early*sizeof(MYFLT));
}
for (vecpos = offset; vecpos < vecsize; vecpos++) {
sig = FL(0.0);
/* if a grain has finished, clean up */
if (UNLIKELY((!streamon[firststream]) && (numstreams) )) {
numstreams--; /* decrease the no of streams */
firststream=(firststream+1)%olaps; /* first stream is the next */
}
/* if a fund period has elapsed */
/* start a new grain */
period = fperiod - frac;
if (UNLIKELY(count == 0 || count >= period)) {
if (count) frac = count - period; /* frac part to be accummulated */
newstream =(firststream+numstreams)%olaps;
streamon[newstream] = 1; /* turn the stream on */
envindex[newstream] = 0.0;
index[newstream] = start;
numstreams++; /* increase the stream count */
count = 0;
start += prate*grsize;
/* this will keep syncgrain looping within the
loop boundaries */
while (UNLIKELY(start >= loop_end)) {
firsttime = 0;
start -= loopsize;
/*csound->Message(csound, "st:%d, end:%d, loopsize=%d\n",
loop_start, loop_end, loopsize); */
}
while (UNLIKELY(start < loop_start && !firsttime))
start += loopsize;
}
/* depending on pitch transpsition a
grain can extend beyond the loop points.
it will be wrapped up at the ends of the
table.
*/
for (i=numstreams,
j=firststream; i; i--, j=(j+1)%olaps) {
/* modulus */
while (index[j] >= datasize)
index[j] -= datasize;
while(index[j] < 0)
index[j] += datasize;
if (UNLIKELY(envindex[j] < envtablesize)){
/* sum all the grain streams */
sig += ((datap[(int32_t)index[j]] +
(index[j] - (int32_t)index[j])*
(datap[(int32_t)index[j]+1] - datap[(int32_t)index[j]])
) *
(ftable[(int32_t)envindex[j]] +
(envindex[j] - (int32_t)envindex[j])*
(ftable[(int32_t)envindex[j]+1] - ftable[(int32_t)envindex[j]])
)
);
/* increment the indexes */
/* for each grain */
index[j] += pitch;
envindex[j] += envincr;
}
/* if the envelope is finished */
/* the grain is also finished */
if (UNLIKELY(envindex[j] > envtablesize))
streamon[j] = 0;
}
/* increment the period counter */
count++;
/* scale the output */
output[vecpos] = sig*amp;
}
p->firststream = firststream;
p->numstreams = numstreams;
p->count = count;
p->start = start;
p->frac = frac;
p->firsttime = firsttime;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("grain size smaller than 1 sample\n"));
}
#define DGRAIN_MAXCHAN 4
#define DGRAIN_OUTTYPES "mmmm"
typedef struct _filegrain {
OPDS h;
MYFLT *output[DGRAIN_MAXCHAN];
STRINGDAT *fname;
MYFLT *amp;
MYFLT *fr;
MYFLT *pitch;
MYFLT *grsize;
MYFLT *prate;
MYFLT *ifn2;
MYFLT *ols;
MYFLT *max;
MYFLT *ioff;
FUNC *efunc;
SNDFILE *sf;
AUXCH buffer;
int32_t count, numstreams, firststream;
int32_t dataframes, envtablesize, olaps;
AUXCH streamon;
AUXCH index;
AUXCH envindex;
float start,frac;
int32_t read1,read2;
uint32 pos;
float trigger;
int32_t nChannels;
int32 flen;
MYFLT pscale;
MYFLT sr;
} filegrain;
#define MINFBUFSIZE 88200
static int32_t filegrain_init(CSOUND *csound, filegrain *p)
{
int32_t size;
void *fd;
MYFLT *buffer;
SF_INFO sfinfo;
char *fname = p->fname->data;
p->nChannels = (int32_t) (p->OUTOCOUNT);
if (UNLIKELY(p->nChannels < 1 || p->nChannels > DGRAIN_MAXCHAN)) {
return csound->InitError(csound,
Str("diskgrain: invalid number of channels"));
}
p->efunc = csound->FTnp2Finde(csound, p->ifn2);
if (UNLIKELY(p->efunc == NULL))
return NOTOK;
p->olaps = (int32_t) *p->ols + 1;
p->dataframes = (int32_t)(*p->max*CS_ESR*4);
if (p->dataframes < MINFBUFSIZE)
p->dataframes = MINFBUFSIZE;
if (UNLIKELY(p->olaps < 2))
p->olaps = 2;
size = (p->olaps) * sizeof(double);
if (p->index.auxp == NULL || p->index.size < (uint32_t)size)
csound->AuxAlloc(csound, size, &p->index);
if (p->envindex.auxp == NULL || p->envindex.size < (uint32_t)size)
csound->AuxAlloc(csound, size, &p->envindex);
size = (p->olaps) * sizeof(int32_t);
if (p->streamon.auxp == NULL || p->streamon.size < (uint32_t)size)
csound->AuxAlloc(csound, size, &p->streamon);
if (p->buffer.auxp == NULL ||
p->buffer.size < (p->dataframes+1)*sizeof(MYFLT)*p->nChannels)
csound->AuxAlloc(csound,
(p->dataframes+1)*sizeof(MYFLT)*p->nChannels, &p->buffer);
buffer = (MYFLT *) p->buffer.auxp;
memset(&sfinfo, '\0', sizeof(sfinfo)); /* for Valgrind */
/* open file and read the first block using *p->ioff */
fd = csound->FileOpen2(csound, &(p->sf), CSFILE_SND_R, fname, &sfinfo,
"SFDIR;SSDIR", CSFTYPE_UNKNOWN_AUDIO, 0);
memset(buffer, 0,p->buffer.size);
if (UNLIKELY(fd == NULL)) {
return csound->InitError(csound, Str("diskgrain: could not open file: %s\n"),
Str(sf_strerror(NULL)));
}
if (UNLIKELY(sfinfo.channels != p->nChannels)) {
return
csound->InitError(csound, Str("diskgrain: soundfile channel numbers "
"do not match the number of outputs\n"));
}
p->sr = sfinfo.samplerate;
p->pscale = p->sr/CS_ESR;
if (*p->ioff >= 0)
sf_seek(p->sf,*p->ioff * p->sr, SEEK_SET);
if (LIKELY(sf_read_MYFLT(p->sf,buffer,p->dataframes*p->nChannels/2) != 0)) {
p->read1 = 1;
p->read2 = 0;
}
else {
return csound->InitError(csound, Str("diskgrain: could not read file\n"));
}
/* -===- */
p->count = 0; /* sampling period counter */
p->numstreams = 0; /* curr num of streams */
p->firststream = 0; /* streams index (first stream) */
p->envtablesize = p->efunc->flen; /* size of envtable */
p->start = 0.0f;
p->frac = 0.0f;
p->pos = *p->ioff*p->sr;
p->trigger = 0.0f;
p->flen = sfinfo.frames;
return OK;
}
static int32_t filegrain_process(CSOUND *csound, filegrain *p)
{
MYFLT sig[DGRAIN_MAXCHAN], pitch, amp, grsize, envincr, period,
fperiod, prate;
MYFLT **output = p->output;
MYFLT *datap = (MYFLT *) p->buffer.auxp;
MYFLT *ftable = p->efunc->ftable;
int32_t *streamon = (int32_t *) p->streamon.auxp;
float start = p->start, frac = p->frac, jump;
double *index = (double *) p->index.auxp;
double *envindex = (double *) p->envindex.auxp;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t vecpos, vecsize=CS_KSMPS;
int32_t firststream = p->firststream;
int32_t numstreams = p->numstreams, olaps = p->olaps;
int32_t count = p->count, i,j, newstream;
int32_t datasize, hdatasize, envtablesize = p->envtablesize;
int32_t dataframes = p->dataframes, hdataframes = p->dataframes/2;
int32_t read1 = p->read1, read2 = p->read2;
int32_t items, chans = p->nChannels, tndx,endx,n;
uint32 pos = p->pos;
int32 negpos, flen = p->flen;
float trigger = p->trigger, incr;
memset(sig, 0, DGRAIN_MAXCHAN*sizeof(MYFLT));
datasize = dataframes*chans;
hdatasize = hdataframes*chans;
pitch = *p->pitch * p->pscale;
fperiod = FABS(p->sr/(*p->fr));
//if (UNLIKELY(fperiod < FL(0.0))) fperiod = -fperiod;
amp = *p->amp;
grsize = p->sr * *p->grsize;
if (UNLIKELY(grsize<1)) goto err1;
if (grsize > hdataframes) grsize = hdataframes;
envincr = envtablesize/grsize;
prate = *p->prate * p->pscale;
if (UNLIKELY(offset)) memset(output, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
vecsize -= early;
memset(&output[vecsize], '\0', early*sizeof(MYFLT));
}
for (vecpos = offset; vecpos < vecsize; vecpos++) {
/* sig = (MYFLT) 0; */
/* if a grain has finished, clean up */
if (UNLIKELY((!streamon[firststream]) && (numstreams) )) {
numstreams--; /* decrease the no of streams */
firststream=(firststream+1)%olaps; /* first stream is the next */
}
/* if a fund period has elapsed */
/* start a new grain */
period = fperiod - frac;
if (count ==0 || count >= period) {
if (count) frac = count - period;
newstream =(firststream+numstreams)%olaps;
streamon[newstream] = 1;
envindex[newstream] = 0.0;
index[newstream] = start;
numstreams++;
count = 0;
incr = prate*grsize;
start += (incr);
trigger += (incr);
jump = grsize*(pitch > 0 ? pitch : -pitch);
if (incr >= 0) {
if (trigger >= (dataframes - jump)) {
trigger -= (dataframes);
if (!read1) {
pos += hdataframes;
sf_seek(p->sf,pos,SEEK_SET);
items = sf_read_MYFLT(p->sf,datap,hdatasize);
if (items < hdatasize) {
sf_seek(p->sf, 0, 0);
sf_read_MYFLT(p->sf,datap+items, hdatasize-items);
}
for (n=0; n < chans; n++)
datap[hdatasize+n] = datap[hdatasize-chans+n];
read1 = 1;
read2 = 0;
}
}
else if (trigger >= (hdataframes - jump)) {
if (!read2) {
pos += hdataframes;
sf_seek(p->sf,pos,SEEK_SET);
items = sf_read_MYFLT(p->sf,datap+hdatasize, hdatasize);
if (items < hdatasize) {
sf_seek(p->sf, 0, SEEK_SET);
sf_read_MYFLT(p->sf,datap+items+hdatasize, hdatasize-items);
}
for (n=0; n < chans; n++)
datap[datasize+n] = datap[datasize-chans+n];
read2 = 1;
read1 = 0;
}
}
}
else {
if (trigger < jump) {
trigger += (dataframes);
if (!read1) {
/*this roundabout code is to
allow us to use an uint64_t
to hold the file position
whilst allowing for pos to go negative
*/
negpos = pos;
negpos -= hdataframes;
if (negpos < 0) {
while(negpos < 0) negpos += flen;
pos = negpos;
}
else pos -= hdataframes;
/*
pos -= hdataframes;
if (pos < 0) pos += flen;
*/
sf_seek(p->sf,pos,SEEK_SET);
items = sf_read_MYFLT(p->sf,datap+hdatasize,hdatasize);
if (items < hdatasize) {
sf_seek(p->sf,items-hdatasize,SEEK_END);
sf_read_MYFLT(p->sf,datap+hdatasize+items, hdatasize-items);
}
for (n=0; n < chans; n++)
datap[datasize+n] = datap[datasize-chans+n];
read1 = 1;
read2 = 0;
}
}
else if (trigger <= (hdataframes + jump)) {
if (!read2) {
negpos = pos;
negpos -= hdataframes;
if (negpos < 0) {
while(negpos < 0) negpos += flen;
pos = negpos;
}
else pos -= hdataframes;
/*
pos -= hdataframes;
if (pos < 0) pos += flen;
*/
sf_seek(p->sf,pos,SEEK_SET);
items = sf_read_MYFLT(p->sf,datap,hdatasize);
if (items < hdatasize) {
sf_seek(p->sf,items-hdatasize,SEEK_END);
(void) sf_read_MYFLT(p->sf,datap+items,hdatasize-items);
}
for (n=0; n < chans; n++)
datap[hdatasize+n] = datap[hdatasize-chans+n];
read2 = 1;
read1 = 0;
}
}
}
if (start >= dataframes) start -= dataframes;
if (start < 0) start += dataframes;
}
for (i=numstreams,
j=firststream; i; i--, j=(j+1)%olaps) {
/* modulus */
if (index[j] >= dataframes)
index[j] -= dataframes;
if (index[j] < 0)
index[j] += dataframes;
/* sum all the grain streams */
tndx = (int32_t)index[j]*chans;
endx = (int32_t) envindex[j];
/* sig[0] = sig[1] = sig[2] = sig[3] = 0.0; */
for (n=0; n < chans; n++) {
sig[n] += ((datap[tndx+n] +
(index[j] - (int32_t)index[j])*
(datap[tndx+n+chans] - datap[tndx+n])
) *
(ftable[endx] +
(envindex[j] - endx)*
(ftable[endx+1] - ftable[endx])
)
);
}
/* increment the indexes */
/* for each grain */
index[j] += (pitch);
envindex[j] += envincr;
/* if the envelope is finished */
/* the grain is also finished */
if (envindex[j] > envtablesize)
streamon[j] = 0;
}
/* increment the period counter */
count++;
/* scale the output */
for (n=0; n < chans; n++) {
output[n][vecpos] = sig[n]*amp;
sig[n] = 0;
}
}
p->firststream = firststream;
p->numstreams = numstreams;
p->count = count;
p->start = start;
p->frac = frac;
p->trigger = trigger;
p->read1 = read1;
p->read2 = read2;
p->pos = pos;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("grain size smaller than 1 sample\n"));
}
static OENTRY localops[] =
{
{"syncgrain", sizeof(syncgrain), TR, 3, "a", "kkkkkiii",
(SUBR)syncgrain_init,(SUBR)syncgrain_process },
{"syncloop", sizeof(syncgrainloop), TR, 3, "a", "kkkkkkkiiioo",
(SUBR)syncgrainloop_init,(SUBR)syncgrainloop_process },
{"diskgrain", sizeof(filegrain), TR, 3, DGRAIN_OUTTYPES, "Skkkkkiipo",
(SUBR)filegrain_init,(SUBR)filegrain_process }
};
int32_t syncgrain_init_(CSOUND *csound)
{
return csound->AppendOpcodes(csound, &(localops[0]),
(int32_t
) (sizeof(localops) / sizeof(OENTRY)));
}
|