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
|
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//Partitioned Convolution, Nick Collins mid October 2008
//PartConv(in, fftsize, irbufnum, accumbufnum)
#include "FFT_UGens.h"
struct PartConv : public Unit {
int m_counter;
uint32 m_specbufnumcheck;
float* m_fd_accumulate; //will be exactly fftsize*frames in size
float * m_irspectra;
int m_fd_accum_pos;
int m_partitions; //number of frames impulse response is partitioned into
int m_fullsize;
//sliding window code
int m_fftsize; //must be power of two
//int m_windowsize; //also half fftsize, was partition size, just use nover2 for this
int m_nover2;
//int m_hopsize; //hopsize will be half fftsize
//int m_shuntsize;
int m_pos;
float * m_inputbuf;
float * m_spectrum;
scfft* m_scfft;
float * m_inputbuf2;
float * m_spectrum2;
scfft* m_scifft; //inverse
float * m_transformbuf;
int m_outputpos;
float * m_output;
//amortisation state
int m_blocksize, m_sr;
int m_spareblocks;
int m_numamort; //will relate number of partitions to number of spare blocks
int m_lastamort;
int m_amortcount;
int m_partitionsdone;
};
extern "C" {
void PartConv_next(PartConv *unit, int inNumSamples);
void PartConv_Ctor(PartConv* unit);
void PartConv_Dtor(PartConv* unit);
}
void PartConv_Ctor( PartConv* unit )
{
unit->m_fftsize= (int) ZIN0(1);
unit->m_nover2= unit->m_fftsize>>1;
//unit->m_windowsize= unit->m_fftsize>>1;
//unit->m_hopsize= unit->m_fftsize>>1;
//unit->m_shuntsize== unit->hopsize;
//integer division
unit->m_inputbuf= (float*)RTAlloc(unit->mWorld, unit->m_fftsize * sizeof(float));
unit->m_spectrum= (float*)RTAlloc(unit->mWorld, unit->m_fftsize * sizeof(float));
unit->m_transformbuf = (float*)RTAlloc(unit->mWorld, scfft_trbufsize(unit->m_fftsize));
unit->m_scfft = (scfft*)RTAlloc(unit->mWorld, sizeof(scfft));
scfft_create(unit->m_scfft, unit->m_fftsize, unit->m_fftsize, WINDOW_RECT, unit->m_inputbuf, unit->m_spectrum, unit->m_transformbuf, true);
//inverse
unit->m_inputbuf2= (float*)RTAlloc(unit->mWorld, unit->m_fftsize * sizeof(float));
unit->m_spectrum2= (float*)RTAlloc(unit->mWorld, unit->m_fftsize * sizeof(float));
unit->m_scifft = (scfft*)RTAlloc(unit->mWorld, sizeof(scfft));
//in place this time
scfft_create(unit->m_scifft, unit->m_fftsize, unit->m_fftsize, WINDOW_RECT, unit->m_inputbuf2, unit->m_spectrum2, unit->m_transformbuf, false);
//debug test: changing scale factors in case amplitude summation is a problem
//unit->m_scfft->scalefac=1.0/45.254833995939;
//unit->m_scifft->scalefac=1.0/45.254833995939;
unit->m_output= (float*)RTAlloc(unit->mWorld, unit->m_fftsize * sizeof(float));
unit->m_outputpos=0;
memset(unit->m_output, 0, unit->m_fftsize * sizeof(float));
memset(unit->m_inputbuf, 0, unit->m_fftsize * sizeof(float));
unit->m_pos=0;
//get passed in buffer
unit->m_fd_accumulate=NULL;
uint32 bufnum = (uint32)ZIN0(2);
SndBuf *buf;
if (bufnum >= unit->mWorld->mNumSndBufs) {
int localBufNum = bufnum - unit->mWorld->mNumSndBufs;
Graph *parent = unit->mParent;
if(localBufNum <= parent->localMaxBufNum) {
buf = parent->mLocalSndBufs + localBufNum;
} else {
printf("PartConv Error: Invalid Spectral data bufnum %d \n", bufnum);
SETCALC(*ClearUnitOutputs);
unit->mDone = true;
return;
}
}
buf = unit->mWorld->mSndBufs + bufnum;
unit->m_specbufnumcheck = bufnum;
//buf = unit->mWorld->mSndBufs + bufnum;
if (!buf->data) {
//unit->mDone = true;
printf("PartConv Error: Spectral data buffer not allocated \n");
SETCALC(*ClearUnitOutputs);
unit->mDone = true;
return;
}
unit->m_irspectra = buf->data;
unit->m_fullsize = buf->samples;
unit->m_partitions=buf->samples/(unit->m_fftsize); //should be exact
//printf("another check partitions %d irspecsize %d fftsize %d \n", unit->m_partitions,unit->m_fullsize, unit->m_fftsize);
if((buf->samples % unit->m_fftsize !=0) || (buf->samples==0)) {
printf("PartConv Error: fftsize doesn't divide spectral data buffer size or spectral data buffer size is zero\n");
SETCALC(*ClearUnitOutputs);
unit->mDone = true;
return;
}
//CHECK SAMPLING RATE AND BUFFER SIZE
unit->m_blocksize = unit->mWorld->mFullRate.mBufLength;
//if(unit->m_blocksize!=64) printf("TPV complains: block size not 64, you have %d\n", unit->m_blocksize);
unit->m_sr = unit->mWorld->mSampleRate;
//if(unit->m_sr!=44100) printf("TPV complains: sample rate not 44100, you have %d\n", unit->m_sr);
if(unit->m_nover2 % unit->m_blocksize !=0) {
printf("PartConv Error: block size doesn't divide partition size\n");
SETCALC(*ClearUnitOutputs);
unit->mDone = true;
return;
} else {
//must be exact divisor
int blocksperpartition = unit->m_nover2/unit->m_blocksize;
unit->m_spareblocks = blocksperpartition-1;
if(unit->m_spareblocks<1) {
printf("PartConv Error: no spareblocks, amortisation not possible! \n");
SETCALC(*ClearUnitOutputs);
unit->mDone = true;
return;
}
//won't be exact
unit->m_numamort = (unit->m_partitions-1)/unit->m_spareblocks; //will relate number of partitions to number of spare blocks
unit->m_lastamort= (unit->m_partitions-1)- ((unit->m_spareblocks-1)*(unit->m_numamort)); //allow for error on last one
unit->m_amortcount= -1; //starts as flag to avoid any amortisation before have first fft done
unit->m_partitionsdone=1;
//printf("Amortisation stats partitions %d nover2 %d blocksize %d spareblocks %d numamort %d lastamort %d \n", unit->m_partitions,unit->m_nover2, unit->m_blocksize, unit->m_spareblocks, unit->m_numamort, unit->m_lastamort);
unit->m_fd_accumulate= (float*)RTAlloc(unit->mWorld, unit->m_fullsize * sizeof(float));
memset(unit->m_fd_accumulate, 0, unit->m_fullsize * sizeof(float));
unit->m_fd_accum_pos=0;
// used to be passed in as buffer, simplified interface
// bufnum = (uint32)ZIN0(3);
// if (bufnum >= unit->mWorld->mNumSndBufs) {
// bufnum = 1;
// }
// buf = unit->mWorld->mSndBufs + bufnum;
//
// if (!buf->data) {
// printf("PartConv Error: Accumulation buffer not allocated \n");
// SETCALC(*ClearUnitOutputs);
// unit->mDone = true;
// return;
// }
//
// unit->m_fd_accumulate = buf->data;
SETCALC(PartConv_next);
}
}
void PartConv_Dtor(PartConv *unit)
{
RTFree(unit->mWorld, unit->m_inputbuf);
RTFree(unit->mWorld, unit->m_inputbuf2);
RTFree(unit->mWorld, unit->m_spectrum);
RTFree(unit->mWorld, unit->m_spectrum2);
RTFree(unit->mWorld, unit->m_transformbuf);
RTFree(unit->mWorld, unit->m_output);
if (unit->m_fd_accumulate) RTFree(unit->mWorld, unit->m_fd_accumulate);
if(unit->m_scfft){
scfft_destroy(unit->m_scfft);
RTFree(unit->mWorld, unit->m_scfft);
}
if(unit->m_scifft){
scfft_destroy(unit->m_scifft);
RTFree(unit->mWorld, unit->m_scifft);
}
}
void PartConv_next( PartConv *unit, int inNumSamples )
{
float *in = IN(0);
float *out = OUT(0);
int pos = unit->m_pos;
//safety check
if (!(unit->mWorld->mSndBufs + unit->m_specbufnumcheck)->data) {
//unit->mDone = true;
printf("PartConv Error: Spectral data buffer not allocated \n");
ClearUnitOutputs(unit, inNumSamples);
SETCALC(*ClearUnitOutputs);
unit->mDone = true;
return;
}
float * input= unit->m_inputbuf;
float * output= unit->m_output;
int outputpos= unit->m_outputpos;
//into input buffer
memcpy(input+pos, in, inNumSamples * sizeof(float));
pos+=inNumSamples;
//if ready for new FFT
int nover2= unit->m_nover2;
//assumes that blocksize perfectly divides windowsize
if (pos==nover2) {
//FFT this input, second half of input always zero
//memset(input+unit->m_nover2, 0, sizeof(float)*unit->m_nover2);
scfft_dofft(unit->m_scfft);
//reset pos into input buffer
pos=0;
//reset outputpos
outputpos= 0;
float * spectrum= unit->m_spectrum;
float * spectrum2= unit->m_spectrum2;
//multiply spectra and accumulate for all ir spectra across storage buffer
int fftsize= unit->m_fftsize;
//int frames= unit->m_partitions;
int accumpos= unit->m_fd_accum_pos;
float * accumbuffer= unit->m_fd_accumulate;
float * irspectrum= unit->m_irspectra;
int fullsize= unit->m_fullsize;
//JUST DO FIRST ONE FOR NOW, AMORTISED FOR OTHERS
//frames
for (int i=0; i<1; ++i) {
int irpos= (i*fftsize);
int posnow= (accumpos+irpos)%fullsize;
float * target= accumbuffer+posnow;
float * ir= irspectrum+irpos;
//real multiply for dc and nyquist
target[0] += ir[0]*spectrum[0];
target[1] += ir[1]*spectrum[1];
//complex multiply for frequency bins
for (int j=1; j<nover2; ++j) {
int binposr= 2*j;
int binposi= binposr+1;
//complex multiply
target[binposr] += (ir[binposr]*spectrum[binposr]) - (ir[binposi]*spectrum[binposi]);
target[binposi] += (ir[binposi]*spectrum[binposr]) + (ir[binposr]*spectrum[binposi]);
}
//LESS efficient!
// float * irreal = ir;
// float * irimag = ir+1;
// float * specreal = spectrum;
// float * specimag = spectrum+1;
// float * targetreal= target;
// float * targetimag= target+1;
//
// for (j=1; j<nover2; ++j) {
//
// irreal+=2; irimag+=2;
// specreal+=2; specimag+=2;
// targetreal+=2; targetimag+=2;
//
// //complex multiply
// (*targetreal)+= (*irreal)*(*specreal) - (*irimag)*(*specimag);
// (*targetimag)+= (*irimag)*(*specreal) - (*irreal)*(*specimag);
// }
}
//need separate scfft object with forward= false
//IFFT this partition
float * input2= unit->m_inputbuf2;
memcpy(input2, accumbuffer+accumpos, fftsize * sizeof(float));
scfft_doifft(unit->m_scifft);
//shunt output data down and zero top bit
memcpy(output, output+nover2, nover2 * sizeof(float));
memset(output+nover2, 0, nover2 * sizeof(float));
//surely last entry should be zero?
//printf("sanity check %f \n",spectrum2[0]); //spectrum2[fftsize-1]
//(fftsize-1)
//sum into output
for (int j=0; j<fftsize; ++j)
output[j] += spectrum2[j];
//int testindex2= rgen.irand(fftsize-1);
//printf("outputtest j %d value %f index %d value2 %f \n",testindex2 , output[testindex2], testindex2, *(accumbuffer+accumpos+testindex2));
//zero this partition
memset(accumbuffer+accumpos, 0, fftsize * sizeof(float));
//ONLY DO AT END OF AMORTISATION???? no, amort code has extra -1 in indexing to cope
//update partition counter
accumpos= (accumpos+fftsize)%fullsize;
unit->m_fd_accum_pos= accumpos;
//set up for amortisation (calculate output for other partitions of impulse response)
unit->m_amortcount=0;
unit->m_partitionsdone=1;
}
else {
//amortisation steps:
//complex multiply of this new fft spectrum against existing irspectrums and sum to accumbuffer
if (unit->m_amortcount>=0) {
float * spectrum= unit->m_spectrum;
//multiply spectra and accumulate for all ir spectra across storage buffer
int fftsize= unit->m_fftsize;
int nover2= unit->m_nover2;
//int frames= unit->m_partitions;
int accumpos= unit->m_fd_accum_pos;
float * accumbuffer= unit->m_fd_accumulate;
float * irspectrum= unit->m_irspectra;
int fullsize= unit->m_fullsize;
int starti, stopi;
int number;
if(unit->m_amortcount==(unit->m_spareblocks-1)) {
number= unit->m_lastamort;
}
else
{
number= unit->m_numamort;
}
starti= unit->m_partitionsdone-1;
stopi= starti+number-1;
//printf("amort check count %d starti %d stopi %d number %d framesdone %d \n",unit->m_amortcount, starti, stopi, number, unit->m_partitionsdone);
unit->m_partitionsdone += number;
++unit->m_amortcount;
//JUST DO FIRST ONE FOR NOW
//frames
for (int i=starti; i<=stopi; ++i) {
int posnow= (accumpos+(i*fftsize))%fullsize;
float * target= accumbuffer+posnow;
int irpos= (i*fftsize);
float * ir= irspectrum+irpos;
//real multiply for dc and nyquist
target[0]+= ir[0]*spectrum[0];
target[1]+= ir[1]*spectrum[1];
//complex multiply for frequency bins
for (int j=1; j<nover2; ++j) {
int binposr= 2*j;
int binposi= binposr+1;
//complex multiply
target[binposr]+= (ir[binposr]*spectrum[binposr]) - (ir[binposi]*spectrum[binposi]);
target[binposi]+= (ir[binposi]*spectrum[binposr]) + (ir[binposr]*spectrum[binposi]);
}
//printf("posnow %d i %d dc %f nyquist %f\n", posnow, i, target[400], target[405]);
}
//if(unit->m_amortcount==(unit->m_spareblocks-1)) {
// number= unit->m_numamort;
//}
}
}
//control rate, just one out value
//output max
//for(j=0; j<inNumSamples; ++j) {
//out[j]= outputbuf[outputpos+j];
//}
//do this second!
memcpy(out, output+outputpos, inNumSamples * sizeof(float));
//debugging tests: output values tend to be fftsize too big, probably due to complex multiply and also summation over all partitions
// RGen& rgen = *unit->mParent->mRGen;
// int testindex= rgen.irand(inNumSamples-1);
// printf("inNumSamples %d testindex %d out %f output %f \n",inNumSamples, testindex, out[testindex], *(output+outputpos+testindex));
//
outputpos+=inNumSamples;
unit->m_outputpos= outputpos;
unit->m_pos= pos;
}
//channels not used- should just be mono, num frames= num samples
//buffer preparation
void PreparePartConv(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
{
int frames1 = buf->frames;
//int channels1 = buf->channels;
float *data1 = buf->data;
uint32 frombufnum = msg->geti();
int fftsize = msg->geti();
//output size must be frombuf->frames*2
if (frombufnum >= world->mNumSndBufs) frombufnum = 0;
SndBuf* frombuf = world->mSndBufs + frombufnum;
int frames2 = frombuf->frames;
//int channels2 = frombuf->channels;
float *data2 = frombuf->data;
//int size = buf->samples;
//scfft
int nover2= fftsize>>1;
//int numpartitions = msg->geti();
int numpartitions;
if(frames2%nover2==0)
numpartitions= frames2/nover2;
else
numpartitions= (frames2/nover2)+1;
//check numpartitions*fftsize= frames2
//printf("reality check numpartitions %d fftsize %d product %d numirframes %d numinputframes %d \n", numpartitions, fftsize, numpartitions*fftsize, frames1, frames2);
//integer division
float * inputbuf= (float*)RTAlloc(world, fftsize * sizeof(float));
float * spectrum= (float*)RTAlloc(world, fftsize * sizeof(float));
float * transformbuf = (float*)RTAlloc(world, scfft_trbufsize(fftsize));
scfft* m_scfft = (scfft*)RTAlloc(world, sizeof(scfft));
scfft_create(m_scfft, fftsize, fftsize, -1, inputbuf,spectrum,transformbuf, true);
//for zero padding
memset(inputbuf, 0, sizeof(float)*fftsize);
//run through input data buffer, taking nover2 chunks, zero padding each
for (int i=0; i<numpartitions; ++i) {
int indexnow= nover2*i;
int indexout= fftsize*i;
if(i<(numpartitions-1))
//memset(inputbuf, 0, sizeof(float)*fftsize);
memcpy(inputbuf, data2+indexnow, nover2 * sizeof(float));
else
{
int takenow= frames2%nover2;
if(frames2==nover2)
takenow= nover2;
memcpy(inputbuf, data2+indexnow, takenow * sizeof(float));
if(takenow<nover2)
memset(inputbuf+takenow, 0, (nover2-takenow)*sizeof(float));
}
scfft_dofft(m_scfft);
memcpy(data1+indexout, spectrum, fftsize * sizeof(float));
}
//clean up
RTFree(world, inputbuf);
RTFree(world, spectrum);
RTFree(world, transformbuf);
if(m_scfft){
scfft_destroy(m_scfft);
RTFree(world, m_scfft);
}
}
void initPartConv(InterfaceTable *inTable)
{
ft = inTable;
DefineDtorCantAliasUnit(PartConv);
DefineBufGen("PreparePartConv", PreparePartConv);
}
|