/* Copyright (C) 2006-2009 Nasca Octavian Paul Author: Nasca Octavian Paul This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. 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 (version 2) for more details. You should have received a copy of the GNU General Public License (version 2) along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "Stretch.h" #include #include unsigned int FFT::start_rand_seed=1; FFT::FFT(int nsamples_){ nsamples=nsamples_; if (nsamples%2!=0) { nsamples+=1; printf("WARNING: Odd sample size on FFT::FFT() (%d)",nsamples); }; smp=new REALTYPE[nsamples];for (int i=0;i>16)&0x7fff; REALTYPE phase=rand*inv_2p15_2pi; #ifdef KISSFFT datac[i].r=freq[i]*cos(phase); datac[i].i=freq[i]*sin(phase); #else data[i]=freq[i]*cos(phase); data[nsamples-i]=freq[i]*sin(phase); #endif }; #ifdef KISSFFT datac[0].r=datac[0].i=0.0; kiss_fftri(plankifft,datac,datar); for (int i=0;i=1.0){//stretch out_bufsize=in_bufsize; }else{ //shorten out_bufsize=(int)(in_bufsize*rap); }; if (out_bufsize<8) out_bufsize=8; if (bypass) out_bufsize=in_bufsize; out_buf=new REALTYPE[out_bufsize]; old_out_smp_buf=new REALTYPE[out_bufsize*2];for (int i=0;i=1.0)&&(newrap>=1.0)) rap=newrap; }; void Stretch::process(REALTYPE *smps,int nsmps){ if (bypass){ for (int i=0;ipoolsize){ printf("Warning nsmps> inbufsize on Stretch::process() %d>%d\n",nsmps,poolsize); nsmps=poolsize; }; int nleft=poolsize-nsmps; //move left the samples from the pool to make room for new samples for (int i=0;ismp[i]=in_pool[i]; infft->applywindow(window_type); infft->smp2freq(); if (out_bufsize==in_bufsize){//output is the same as the input (as usual) for (int i=0;ifreq[i]=infft->freq[i]; } else { if (out_bufsize>in_bufsize){//output is longer REALTYPE rap=(REALTYPE)in_bufsize/(REALTYPE)out_bufsize; for (int i=0;ifreq[i]=infft->freq[poshi]*(1.0-poslo)+infft->freq[poshi+1]*poslo; }; }else{//output is shorter for (int i=0;ifreq[i]=0.0; REALTYPE rap=(REALTYPE)out_bufsize/(REALTYPE)in_bufsize; for (int i=0;ifreq[poshi]+=infft->freq[i]; }; }; }; process_spectrum(outfft->freq); outfft->freq2smp(); //make the output buffer REALTYPE tmp=1.0/(float) out_bufsize*M_PI; REALTYPE hinv_sqrt2=0.853553390593;//(1.0+1.0/sqrt(2))*0.5; REALTYPE ampfactor=1.0; if (rap<1.0) ampfactor=rap*0.707; else ampfactor=(out_bufsize/(float)poolsize)*4.0; for (int i=0;ismp[i+out_bufsize]*(1.0-a)+old_out_smp_buf[i]*a; out_buf[i]=out*(hinv_sqrt2-(1.0-hinv_sqrt2)*cos(i*2.0*tmp))*ampfactor; }; //copy the current output buffer to old buffer for (int i=0;ismp[i]; //post-process the output //process_output(out_buf,out_bufsize); }; int Stretch::get_nsamples(REALTYPE current_pos_percents){ if (bypass) return out_bufsize; if (rap<1.0) return poolsize/2;//pentru shorten long double used_rap=rap*get_stretch_multiplier(current_pos_percents); long double r=out_bufsize/used_rap; int ri=(int)floor(r); long double rf=r-floor(r); long double old_remained_samples_test=remained_samples; remained_samples+=rf; if (remained_samples>=1.0){ ri+=(int)floor(remained_samples); remained_samples=remained_samples-floor(remained_samples); }; long double rf_test=remained_samples-old_remained_samples_test;//this value should be almost like "rf" (for most of the time with the exception of changing the "ri" value) for extremely long stretches (otherwise the shown stretch value is not accurate) //for stretch up to 10^18x "long double" must have at least 64 bits in the fraction part (true for gcc compiler on x86 and macosx) // long double zzz=1.0;//quick test by adding a "largish" number and substracting it again // rf_test+=zzz; // rf_test-=zzz; // printf("remained_samples=%.20Lg rf=%.20Lg rf_test=%.20Lg\n",remained_samples,rf,rf_test); // printf("rf=%g rf_test=%g\n",(double)rf,(double)(rf_test)); if (ri>poolsize){ ri=poolsize; }; return ri; }; int Stretch::get_nsamples_for_fill(){ return poolsize; }; REALTYPE Stretch::get_stretch_multiplier(REALTYPE pos_percents){ return 1.0; };