Skip to content

Commit

Permalink
Fix some bugs with frequency calculations, as well as buffer clearing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremysalwen committed Jun 5, 2011
1 parent aebab42 commit 16409a3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
42 changes: 27 additions & 15 deletions kn0ck0ut6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ AKnockout::AKnockout(double rate) : Plugin<AKnockout>(p_n_ports)
gfftSize=8192;
goverlap=8;
AllocateNewBuffers(gfftSize);

clearBuffers();
}

//-----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -71,9 +71,7 @@ void AKnockout::AllocateNewBuffers(unsigned int fftSize) {
backwards=fftwf_plan_dft_c2r_1d(fftSize, gFFTworksp, FFTRealBuffer,
FFTW_ESTIMATE);

makelookup(fftSize);

clearBuffers (); // flush buffer
makelookup(fftSize);
}
void AKnockout::FreeOldBuffers() {
delete[] gInFIFO;
Expand Down Expand Up @@ -104,6 +102,12 @@ void AKnockout::clearBuffers()
memset(gFFTworksp2, 0, fftSize2*sizeof(fftwf_complex));
memset(gAnaMagn2, 0, fftSize2*sizeof(float));
memset(gDecay,0,fftSize2*sizeof(float));

long stepSize=fftSize/goverlap;
gRover=0;
samples_needed_in_buffer=stepSize;
outAccumIndex=stepSize;
copiesremaining=0;
}
//assumes desired and fftsize are multiples of 4.
int calcOsampFromFFTSize(unsigned long desired, unsigned long fftsize) {
Expand Down Expand Up @@ -160,15 +164,23 @@ void AKnockout::run(uint32_t sampleFrames)
}
CLAMP_PORT(long,windowsize,p_windowsize)
unsigned long newwin=findBestFFTSize((unsigned long)windowsize);
bool resetbuffers=false;
if(newwin!=gfftSize) {
gfftSize=newwin;
FreeOldBuffers();
AllocateNewBuffers(newwin);
resetbuffers=true;
}
unsigned int iOsamp = 4*((unsigned int)(*p(p_overlapf)));

goverlap=calcOsampFromFFTSize(iOsamp,gfftSize);

iOsamp=calcOsampFromFFTSize(iOsamp,gfftSize);
if(iOsamp!=goverlap) {
goverlap=iOsamp;
resetbuffers=true;
}
if(resetbuffers) {
clearBuffers();
}
CLAMP_PORT(int,iBlur,p_blur)
CLAMP_PORT(float,fDecay,p_decay)
// arguments are number of samples to process, fft window size, sample overlap (4-32), input buffer, output buffer, init flag, gain, R input gain, decay, l cut, hi cut
Expand Down Expand Up @@ -234,19 +246,13 @@ void AKnockout::do_rebuild(long numSampsToProcess, long fftFrameSize, long osamp
/* set up some handy variables */
long fftFrameSize2 = fftFrameSize/2;
long stepSize = fftFrameSize/osamp;
double dOversampbytwopi = (double)osamp/PI/2;
double dOversampbytwopi = (double)osamp/(PI*2);
double freqPerBin = sampleRate/(double)fftFrameSize;
double dFreqfactor = PI/(double)osamp/freqPerBin*2;
double dOutfactor = (double)fftFrameSize2*(double)osamp;
fDecayRate=(fDecayRate>0)*(4.00001-(fDecayRate*fDecayRate*4));

double expct = 2.*PI*(double)stepSize/(double)fftFrameSize;


static long gRover=0;
static long samples_needed_in_buffer=stepSize;
static long outAccumIndex=stepSize;
static long copiesremaining=0;
{
int numpro=copiesremaining;
if(numSampsToProcess<copiesremaining) {
Expand Down Expand Up @@ -283,9 +289,15 @@ void AKnockout::do_rebuild(long numSampsToProcess, long fftFrameSize, long osamp
tAnaMagn[k] = 2.*sqrt(real*real + imag*imag);
double phase = atan2(imag,real);

double tmp = phase-(double)k*expct;
double tmp = phase-(double)k*expct;
tmp+=PI;
//now bring into range [0, 2*pi)
long qpd = tmp/(2*PI);
tmp -= 2*PI*(double)qpd;
tmp -= 2*PI*(double)qpd;
tmp+=2*PI*(tmp<0);
//return to [-pi,pi)
tmp-=PI;
//Multiply by factor of Oversampbytwopi
tmp *= dOversampbytwopi;

/* store frequency in analysis array */
Expand Down
4 changes: 4 additions & 0 deletions kn0ck0ut6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class AKnockout:public Plugin<AKnockout> {
float* __restrict gDecay;
float* __restrict window;

long gRover;
long samples_needed_in_buffer;
long outAccumIndex;
long copiesremaining;

fftwf_complex * gFFTworksp2;
fftwf_complex * gFFTworksp;
Expand Down

0 comments on commit 16409a3

Please sign in to comment.