コード例 #1
0
ファイル: Stretch.cpp プロジェクト: Apolotary/paulstretch_cpp
REALTYPE Stretch::process(REALTYPE *smps,int nsmps){
	REALTYPE onset=0.0;
	if (bypass){
		for (int i=0;i<bufsize;i++) out_buf[i]=smps[i];
		return 0.0;
	};

	if (smps!=NULL){
		if ((nsmps!=0)&&(nsmps!=bufsize)&&(nsmps!=get_max_bufsize())){
			printf("Warning wrong nsmps on Stretch::process() %d,%d\n",nsmps,bufsize);
			return 0.0;
		};
		if (nsmps!=0){//new data arrived: update the frequency components
			do_analyse_inbuf(smps);		
			if (nsmps==get_max_bufsize()) {
				for (int k=bufsize;k<get_max_bufsize();k+=bufsize) do_analyse_inbuf(smps+k);
			};
			if (onset_detection_sensitivity>1e-3) onset=do_detect_onset();
		};


		//move the buffers	
		if (nsmps!=0){//new data arrived: update the frequency components
			do_next_inbuf_smps(smps);		
			if (nsmps==get_max_bufsize()) {
				for (int k=bufsize;k<get_max_bufsize();k+=bufsize) do_next_inbuf_smps(smps+k);

			};
		};
	
		//construct the input fft
		int start_pos=(int)(floor(remained_samples*bufsize));	
		if (start_pos>=bufsize) start_pos=bufsize-1;
		for (int i=0;i<bufsize-start_pos;i++) fft->smp[i]=very_old_smps[i+start_pos];
		for (int i=0;i<bufsize;i++) fft->smp[i+bufsize-start_pos]=old_smps[i];
		for (int i=0;i<start_pos;i++) fft->smp[i+2*bufsize-start_pos]=new_smps[i];
		//compute the output spectrum
		fft->applywindow(window_type);
		fft->smp2freq();
		for (int i=0;i<bufsize;i++) outfft->freq[i]=fft->freq[i];
	


		//for (int i=0;i<bufsize;i++) outfft->freq[i]=infft->freq[i]*remained_samples+old_freq[i]*(1.0-remained_samples);


		process_spectrum(outfft->freq);

		outfft->freq2smp();

		//make the output buffer
		REALTYPE tmp=1.0/(float) bufsize*M_PI;
		REALTYPE hinv_sqrt2=0.853553390593;//(1.0+1.0/sqrt(2))*0.5;

		REALTYPE ampfactor=2.0;
		
		//remove the resulted unwanted amplitude modulation (caused by the interference of N and N+1 windowed buffer and compute the output buffer
		for (int i=0;i<bufsize;i++) {
			REALTYPE a=(0.5+0.5*cos(i*tmp));
			REALTYPE out=outfft->smp[i+bufsize]*(1.0-a)+old_out_smps[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;i<bufsize*2;i++) old_out_smps[i]=outfft->smp[i];

	};

	if (!freezing){
		long double used_rap=rap*get_stretch_multiplier(c_pos_percents);	

		long double r=1.0/used_rap;
		if (extra_onset_time_credit>0){
			REALTYPE credit_get=0.5*r;//must be smaller than r
			extra_onset_time_credit-=credit_get;
			if (extra_onset_time_credit<0.0) extra_onset_time_credit=0.0;
			r-=credit_get;
		};

		long double old_remained_samples_test=remained_samples;
		remained_samples+=r;
		int result=0;
		if (remained_samples>=1.0){
			skip_samples=(int)(floor(remained_samples-1.0)*bufsize);
			remained_samples=remained_samples-floor(remained_samples);
			require_new_buffer=true;
		}else{
			require_new_buffer=false;
		};
	};
//	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)
	return onset;	
};
コード例 #2
0
ファイル: Paulstretch.cpp プロジェクト: finefin/audacity
void PaulStretch::process(float *smps, size_t nsmps)
{
   //add NEW samples to the pool
   if ((smps != NULL) && (nsmps != 0)) {
      if (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; i < nleft; i++)
         in_pool[i] = in_pool[i + nsmps];

      //add NEW samples to the pool
      for (size_t i = 0; i < nsmps; i++)
         in_pool[i + nleft] = smps[i];
   }

   //get the samples from the pool
   for (size_t i = 0; i < poolsize; i++)
      fft_smps[i] = in_pool[i];
   WindowFunc(eWinFuncHanning, poolsize, fft_smps.get());

   RealFFT(poolsize, fft_smps.get(), fft_c.get(), fft_s.get());

   for (size_t i = 0; i < poolsize / 2; i++)
      fft_freq[i] = sqrt(fft_c[i] * fft_c[i] + fft_s[i] * fft_s[i]);
   process_spectrum(fft_freq.get());


   //put randomize phases to frequencies and do a IFFT
   float inv_2p15_2pi = 1.0 / 16384.0 * (float)M_PI;
   for (size_t i = 1; i < poolsize / 2; i++) {
      unsigned int random = (rand()) & 0x7fff;
      float phase = random * inv_2p15_2pi;
      float s = fft_freq[i] * sin(phase);
      float c = fft_freq[i] * cos(phase);

      fft_c[i] = fft_c[poolsize - i] = c;

      fft_s[i] = s; fft_s[poolsize - i] = -s;
   }
   fft_c[0] = fft_s[0] = 0.0;
   fft_c[poolsize / 2] = fft_s[poolsize / 2] = 0.0;

   FFT(poolsize, true, fft_c.get(), fft_s.get(), fft_smps.get(), fft_tmp.get());

   float max = 0.0, max2 = 0.0;
   for (size_t i = 0; i < poolsize; i++) {
      max = std::max(max, fabsf(fft_tmp[i]));
      max2 = std::max(max2, fabsf(fft_smps[i]));
   }


   //make the output buffer
   float tmp = 1.0 / (float) out_bufsize * M_PI;
   float hinv_sqrt2 = 0.853553390593f;//(1.0+1.0/sqrt(2))*0.5;

   float ampfactor = 1.0;
   if (rap < 1.0)
      ampfactor = rap * 0.707;
   else
      ampfactor = (out_bufsize / (float)poolsize) * 4.0;

   for (size_t i = 0; i < out_bufsize; i++) {
      float a = (0.5 + 0.5 * cos(i * tmp));
      float out = fft_smps[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 (size_t i = 0; i < out_bufsize * 2; i++)
      old_out_smp_buf[i] = fft_smps[i];
}