void Convolution2_next(Convolution2 *unit, int wrongNumSamples) { float *in1 = IN(0); //float *in2 = IN(1); float curtrig = ZIN0(2); float *out1 = unit->m_inbuf1 + unit->m_pos; // float *out2 = unit->m_inbuf2 + unit->m_pos; int numSamples = unit->mWorld->mFullRate.mBufLength; uint32 insize=unit->m_insize * sizeof(float); // copy input Copy(numSamples, out1, in1); unit->m_pos += numSamples; if (unit->m_prevtrig <= 0.f && curtrig > 0.f){ //float fbufnum = ZIN0(1); //int log2n2 = unit->m_log2n; //uint32 bufnum = (int)fbufnum; //printf("bufnum %i \n", bufnum); //World *world = unit->mWorld; //if (bufnum >= world->mNumSndBufs) bufnum = 0; //SndBuf *buf = world->mSndBufs + bufnum; SndBuf *buf = ConvGetBuffer(unit,(uint32)ZIN0(1), "Convolution2", numSamples); if (!buf) return; LOCK_SNDBUF_SHARED(buf); memcpy(unit->m_fftbuf2, buf->data, insize); memset(unit->m_fftbuf2+unit->m_insize, 0, insize); //rffts(unit->m_fftbuf2, log2n2, 1, cosTable[log2n2]); scfft_dofft(unit->m_scfft2); } if (unit->m_pos & unit->m_insize) { //have collected enough samples to transform next frame unit->m_pos = 0; //reset collection counter // copy to fftbuf //int log2n = unit->m_log2n; memcpy(unit->m_fftbuf1, unit->m_inbuf1, insize); //zero pad second part of buffer to allow for convolution memset(unit->m_fftbuf1+unit->m_insize, 0, insize); //if (unit->m_prevtrig <= 0.f && curtrig > 0.f) scfft_dofft(unit->m_scfft1); //in place transform for now // rffts(unit->m_fftbuf1, log2n, 1, cosTable[log2n]); //complex multiply time int numbins = unit->m_fftsize >> 1; //unit->m_fftsize - 2 >> 1; float * p1= unit->m_fftbuf1; float * p2= unit->m_fftbuf2; p1[0] *= p2[0]; p1[1] *= p2[1]; //complex multiply for (int i=1; i<numbins; ++i) { float real,imag; int realind,imagind; realind= 2*i; imagind= realind+1; real= p1[realind]*p2[realind]- p1[imagind]*p2[imagind]; imag= p1[realind]*p2[imagind]+ p1[imagind]*p2[realind]; p1[realind] = real; //p2->bin[i]; p1[imagind]= imag; } //copy second part from before to overlap memcpy(unit->m_overlapbuf, unit->m_outbuf+unit->m_insize, insize); //inverse fft into outbuf memcpy(unit->m_outbuf, unit->m_fftbuf1, unit->m_fftsize * sizeof(float)); //in place //riffts(unit->m_outbuf, log2n, 1, cosTable[log2n]); scfft_doifft(unit->m_scfftR); // DoWindowing(log2n, unit->m_outbuf, unit->m_fftsize); }
void Convolution2_next(Convolution2 *unit, int wrongNumSamples) { float *in1 = IN(0); float curtrig = ZIN0(2); float *inbuf1writepos = unit->m_inbuf1 + unit->m_pos; int numSamples = unit->mWorld->mFullRate.mBufLength; uint32 framesize = unit->m_framesize; uint32 framesize_f = framesize * sizeof(float); // copy input Copy(numSamples, inbuf1writepos, in1); unit->m_pos += numSamples; if (unit->m_prevtrig <= 0.f && curtrig > 0.f){ SndBuf *kernelbuf = ConvGetBuffer(unit,(uint32)ZIN0(1), "Convolution2", numSamples); if (!kernelbuf) return; LOCK_SNDBUF_SHARED(kernelbuf); // we cannot use a kernel larger than the fft size, so truncate if needed. the kernel may be smaller though. size_t kernelcopysize = sc_min(kernelbuf->frames, framesize); memcpy(unit->m_fftbuf2, kernelbuf->data, kernelcopysize * sizeof(float)); memset(unit->m_fftbuf2 + kernelcopysize, 0, (2 * framesize - kernelcopysize) * sizeof(float)); scfft_dofft(unit->m_scfft2); } if (unit->m_pos >= framesize) { //have collected enough samples to transform next frame unit->m_pos = 0; //reset collection counter // copy to fftbuf memcpy(unit->m_fftbuf1, unit->m_inbuf1, framesize_f); //zero pad second part of buffer to allow for convolution memset(unit->m_fftbuf1+unit->m_framesize, 0, framesize_f); scfft_dofft(unit->m_scfft1); //complex multiply time int numbins = unit->m_fftsize >> 1; float * p1= unit->m_fftbuf1; float * p2= unit->m_fftbuf2; p1[0] *= p2[0]; p1[1] *= p2[1]; //complex multiply for (int i=1; i<numbins; ++i) { float real,imag; int realind,imagind; realind= 2*i; imagind= realind+1; real= p1[realind]*p2[realind]- p1[imagind]*p2[imagind]; imag= p1[realind]*p2[imagind]+ p1[imagind]*p2[realind]; p1[realind] = real; p1[imagind]= imag; } //copy second part from before to overlap memcpy(unit->m_overlapbuf, unit->m_outbuf+unit->m_framesize, framesize_f); //inverse fft into outbuf scfft_doifft(unit->m_scfftR); }
void Convolution2_Ctor(Convolution2 *unit) { //require size N+M-1 to be a power of two unit->m_insize=(int)ZIN0(3); //could be input parameter // printf( "unit->m_insize %i\n", unit->m_insize ); // printf( "unit->mWorld->mFullRate.mBufLength %i\n", unit->mWorld->mFullRate.mBufLength ); //float fbufnum = ZIN0(1); uint32 bufnum = (int)ZIN0(1); //fbufnum; World *world = unit->mWorld; //before added check for LocalBuf //if (bufnum >= world->mNumSndBufs) bufnum = 0; // SndBuf *buf = world->mSndBufs + bufnum; SndBuf *buf = ConvGetBuffer(unit, bufnum, "Convolution2", 1); if(buf) { if ( unit->m_insize <= 0 ) // if smaller than zero, equal to size of buffer unit->m_insize=buf->frames; //could be input parameter unit->m_fftsize=2*(unit->m_insize); //printf("hello %i, %i\n", unit->m_insize, unit->m_fftsize); //just use memory for the input buffers and fft buffers int insize = unit->m_insize * sizeof(float); int fftsize = unit->m_fftsize * sizeof(float); // // unit->m_inbuf1 = (float*)RTAlloc(unit->mWorld, insize); //// unit->m_inbuf2 = (float*)RTAlloc(unit->mWorld, insize); // // unit->m_fftbuf1 = (float*)RTAlloc(unit->mWorld, fftsize); // unit->m_fftbuf2 = (float*)RTAlloc(unit->mWorld, fftsize); unit->m_inbuf1 = (float*)RTAlloc(world, insize); unit->m_fftbuf1 = (float*)RTAlloc(world, fftsize); unit->m_fftbuf2 = (float*)RTAlloc(world, fftsize); unit->m_outbuf = (float*)RTAlloc(world, fftsize); unit->m_overlapbuf = (float*)RTAlloc(world, insize); memset(unit->m_outbuf, 0, fftsize); memset(unit->m_overlapbuf, 0, insize); //unit->m_log2n = LOG2CEIL(unit->m_fftsize); unit->m_pos = 0; memset(unit->m_outbuf, 0, fftsize); memset(unit->m_overlapbuf, 0, insize); SCWorld_Allocator alloc(ft, unit->mWorld); unit->m_scfft1 = scfft_create(unit->m_fftsize, unit->m_fftsize, kRectWindow, unit->m_fftbuf1, unit->m_fftbuf1, kForward, alloc); unit->m_scfft2 = scfft_create(unit->m_fftsize, unit->m_fftsize, kRectWindow, unit->m_fftbuf2, unit->m_fftbuf2, kForward, alloc); unit->m_scfftR = scfft_create(unit->m_fftsize, unit->m_fftsize, kRectWindow, unit->m_fftbuf1, unit->m_outbuf, kBackward, alloc); //calculate fft for kernel straight away memcpy(unit->m_fftbuf2, buf->data, insize); //zero pad second part of buffer to allow for convolution memset(unit->m_fftbuf2+unit->m_insize, 0, insize); scfft_dofft(unit->m_scfft2); unit->m_pos = 0; // unit->m_log2n = LOG2CEIL(unit->m_fftsize); // // int log2n = unit->m_log2n; // // //test for full input buffer // //unit->m_mask = unit->m_insize; // // //in place transform for now // rffts(unit->m_fftbuf2, log2n, 1, cosTable[log2n]); unit->m_prevtrig = 0.f; unit->m_prevtrig = ZIN0(2); // printf( "unit->m_insize %i\n", unit->m_insize ); // printf( "world->mFullRate.mBufLength %i\n", world->mFullRate.mBufLength ); if ( unit->m_insize >= world->mFullRate.mBufLength ) { // printf( "insize bigger than blocksize\n" ); SETCALC(Convolution2_next); } else { printf( "Convolution2 framesize smaller than blocksize \n" ); SETCALC(*ClearUnitOutputs); unit->mDone = true; //SETCALC(Convolution2_next2); } } else { unit->m_scfft2 = unit->m_scfft1 = unit->m_scfftR = NULL; } }
void Convolution2_Ctor(Convolution2 *unit) { //require size N+M-1 to be a power of two unit->m_framesize=(int)ZIN0(3); uint32 kernelbufnum = (int)ZIN0(1); World *world = unit->mWorld; SndBuf *kernelbuf = ConvGetBuffer(unit, kernelbufnum, "Convolution2", 1); if(kernelbuf) { if ( unit->m_framesize <= 0 ){ // if smaller than zero, we use the size of the buffer unit->m_framesize=kernelbuf->frames; } unit->m_fftsize=2*(unit->m_framesize); if(unit->m_fftsize > SC_FFT_ABSOLUTE_MAXSIZE){ printf( "Convolution2: FFT size is larger than SC_FFT_ABSOLUTE_MAXSIZE, cannot run. We suggest PartConv instead.\n" ); SETCALC(*ClearUnitOutputs); } // allocate memory internally for the input buffers and fft buffers int framesize_f = unit->m_framesize * sizeof(float); int fftsize_f = unit->m_fftsize * sizeof(float); unit->m_inbuf1 = (float*)RTAlloc(world, framesize_f); unit->m_fftbuf1 = (float*)RTAlloc(world, fftsize_f); unit->m_fftbuf2 = (float*)RTAlloc(world, fftsize_f); unit->m_outbuf = (float*)RTAlloc(world, fftsize_f); memset(unit->m_outbuf, 0, fftsize_f); unit->m_overlapbuf = (float*)RTAlloc(world, framesize_f); memset(unit->m_overlapbuf, 0, framesize_f); unit->m_pos = 0; SCWorld_Allocator alloc(ft, unit->mWorld); unit->m_scfft1 = scfft_create(unit->m_fftsize, unit->m_fftsize, kRectWindow, unit->m_fftbuf1, unit->m_fftbuf1, kForward, alloc); unit->m_scfft2 = scfft_create(unit->m_fftsize, unit->m_fftsize, kRectWindow, unit->m_fftbuf2, unit->m_fftbuf2, kForward, alloc); unit->m_scfftR = scfft_create(unit->m_fftsize, unit->m_fftsize, kRectWindow, unit->m_fftbuf1, unit->m_outbuf, kBackward, alloc); if(!unit->m_scfft1 || !unit->m_scfft2 || !unit->m_scfftR){ printf( "Could not create scfft.\n" ); SETCALC(*ClearUnitOutputs); unit->mDone = true; return; } //calculate fft for kernel straight away // we cannot use a kernel larger than the fft size, so truncate if needed. the kernel may be smaller though. uint32 framesize = unit->m_framesize; size_t kernelcopysize = sc_min(kernelbuf->frames, framesize); memcpy(unit->m_fftbuf2, kernelbuf->data, kernelcopysize * sizeof(float)); //zero pad second part of buffer to allow for convolution memset(unit->m_fftbuf2 + kernelcopysize, 0, (2 * framesize - kernelcopysize) * sizeof(float)); scfft_dofft(unit->m_scfft2); unit->m_pos = 0; unit->m_prevtrig = 0.f; unit->m_prevtrig = ZIN0(2); if ( unit->m_framesize >= world->mFullRate.mBufLength ) { SETCALC(Convolution2_next); } else { printf( "Convolution2 framesize smaller than blocksize \n" ); SETCALC(*ClearUnitOutputs); unit->mDone = true; } } else { unit->m_scfft2 = unit->m_scfft1 = unit->m_scfftR = NULL; printf("Convolution2_Ctor: can't get kernel buffer, giving up.\n"); SETCALC(*ClearUnitOutputs); } }