Audacity  3.0.3
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
PaulStretch Class Reference

Class that helps EffectPaulStretch. It does the FFTs and inner loop of the effect. More...

Collaboration diagram for PaulStretch:
[legend]

Public Member Functions

 PaulStretch (float rap_, size_t in_bufsize_, float samplerate_)
 
virtual ~PaulStretch ()
 
void process (float *smps, size_t nsmps)
 
size_t get_nsamples ()
 
size_t get_nsamples_for_fill ()
 

Public Attributes

const size_t out_bufsize
 
const Floats out_buf
 
const size_t poolsize
 

Private Member Functions

void process_spectrum (float *WXUNUSED(freq))
 

Private Attributes

const float samplerate
 
const float rap
 
const size_t in_bufsize
 
const Floats old_out_smp_buf
 
const Floats in_pool
 
double remained_samples
 
const Floats fft_smps
 
const Floats fft_c
 
const Floats fft_s
 
const Floats fft_freq
 
const Floats fft_tmp
 

Detailed Description

Class that helps EffectPaulStretch. It does the FFTs and inner loop of the effect.

Definition at line 46 of file Paulstretch.cpp.

Constructor & Destructor Documentation

◆ PaulStretch()

PaulStretch::PaulStretch ( float  rap_,
size_t  in_bufsize_,
float  samplerate_ 
)

Definition at line 422 of file Paulstretch.cpp.

423  : samplerate { samplerate_ }
424  , rap { std::max(1.0f, rap_) }
425  , in_bufsize { in_bufsize_ }
426  , out_bufsize { std::max(size_t{ 8 }, in_bufsize) }
427  , out_buf { out_bufsize }
428  , old_out_smp_buf { out_bufsize * 2, true }
429  , poolsize { in_bufsize_ * 2 }
430  , in_pool { poolsize, true }
431  , remained_samples { 0.0 }
432  , fft_smps { poolsize, true }
433  , fft_c { poolsize, true }
434  , fft_s { poolsize, true }
435  , fft_freq { poolsize, true }
436  , fft_tmp { poolsize }
437 {
438 }

◆ ~PaulStretch()

PaulStretch::~PaulStretch ( )
virtual

Definition at line 440 of file Paulstretch.cpp.

441 {
442 }

Member Function Documentation

◆ get_nsamples()

size_t PaulStretch::get_nsamples ( )

Definition at line 521 of file Paulstretch.cpp.

522 {
523  double r = out_bufsize / rap;
524  auto ri = (size_t)floor(r);
525  double rf = r - floor(r);
526 
527  remained_samples += rf;
528  if (remained_samples >= 1.0){
529  ri += (size_t)floor(remained_samples);
531  }
532 
533  if (ri > poolsize) {
534  ri = poolsize;
535  }
536 
537  return ri;
538 }

References out_bufsize, poolsize, rap, and remained_samples.

Referenced by EffectPaulstretch::ProcessOne().

Here is the caller graph for this function:

◆ get_nsamples_for_fill()

size_t PaulStretch::get_nsamples_for_fill ( )

Definition at line 540 of file Paulstretch.cpp.

541 {
542  return poolsize;
543 }

References poolsize.

Referenced by EffectPaulstretch::ProcessOne().

Here is the caller graph for this function:

◆ process()

void PaulStretch::process ( float *  smps,
size_t  nsmps 
)

Definition at line 444 of file Paulstretch.cpp.

445 {
446  //add NEW samples to the pool
447  if ((smps != NULL) && (nsmps != 0)) {
448  if (nsmps > poolsize) {
449  nsmps = poolsize;
450  }
451  int nleft = poolsize - nsmps;
452 
453  //move left the samples from the pool to make room for NEW samples
454  for (int i = 0; i < nleft; i++)
455  in_pool[i] = in_pool[i + nsmps];
456 
457  //add NEW samples to the pool
458  for (size_t i = 0; i < nsmps; i++)
459  in_pool[i + nleft] = smps[i];
460  }
461 
462  //get the samples from the pool
463  for (size_t i = 0; i < poolsize; i++)
464  fft_smps[i] = in_pool[i];
466 
467  RealFFT(poolsize, fft_smps.get(), fft_c.get(), fft_s.get());
468 
469  for (size_t i = 0; i < poolsize / 2; i++)
470  fft_freq[i] = sqrt(fft_c[i] * fft_c[i] + fft_s[i] * fft_s[i]);
471  process_spectrum(fft_freq.get());
472 
473 
474  //put randomize phases to frequencies and do a IFFT
475  float inv_2p15_2pi = 1.0 / 16384.0 * (float)M_PI;
476  for (size_t i = 1; i < poolsize / 2; i++) {
477  unsigned int random = (rand()) & 0x7fff;
478  float phase = random * inv_2p15_2pi;
479  float s = fft_freq[i] * sin(phase);
480  float c = fft_freq[i] * cos(phase);
481 
482  fft_c[i] = fft_c[poolsize - i] = c;
483 
484  fft_s[i] = s; fft_s[poolsize - i] = -s;
485  }
486  fft_c[0] = fft_s[0] = 0.0;
487  fft_c[poolsize / 2] = fft_s[poolsize / 2] = 0.0;
488 
489  FFT(poolsize, true, fft_c.get(), fft_s.get(), fft_smps.get(), fft_tmp.get());
490 
491  float max = 0.0, max2 = 0.0;
492  for (size_t i = 0; i < poolsize; i++) {
493  max = std::max(max, fabsf(fft_tmp[i]));
494  max2 = std::max(max2, fabsf(fft_smps[i]));
495  }
496 
497 
498  //make the output buffer
499  float tmp = 1.0 / (float) out_bufsize * M_PI;
500  float hinv_sqrt2 = 0.853553390593f;//(1.0+1.0/sqrt(2))*0.5;
501 
502  float ampfactor = 1.0;
503  if (rap < 1.0)
504  ampfactor = rap * 0.707;
505  else
506  ampfactor = (out_bufsize / (float)poolsize) * 4.0;
507 
508  for (size_t i = 0; i < out_bufsize; i++) {
509  float a = (0.5 + 0.5 * cos(i * tmp));
510  float out = fft_smps[i + out_bufsize] * (1.0 - a) + old_out_smp_buf[i] * a;
511  out_buf[i] =
512  out * (hinv_sqrt2 - (1.0 - hinv_sqrt2) * cos(i * 2.0 * tmp)) *
513  ampfactor;
514  }
515 
516  //copy the current output buffer to old buffer
517  for (size_t i = 0; i < out_bufsize * 2; i++)
518  old_out_smp_buf[i] = fft_smps[i];
519 }

References eWinFuncHann, FFT(), fft_c, fft_freq, fft_s, fft_smps, fft_tmp, in_pool, M_PI, old_out_smp_buf, out_buf, out_bufsize, poolsize, process_spectrum(), rap, RealFFT(), and WindowFunc().

Referenced by EffectPaulstretch::ProcessOne().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_spectrum()

void PaulStretch::process_spectrum ( float *  WXUNUSEDfreq)
inlineprivate

Definition at line 59 of file Paulstretch.cpp.

59 {};

Referenced by process().

Here is the caller graph for this function:

Member Data Documentation

◆ fft_c

const Floats PaulStretch::fft_c
private

Definition at line 80 of file Paulstretch.cpp.

Referenced by process().

◆ fft_freq

const Floats PaulStretch::fft_freq
private

Definition at line 80 of file Paulstretch.cpp.

Referenced by process().

◆ fft_s

const Floats PaulStretch::fft_s
private

Definition at line 80 of file Paulstretch.cpp.

Referenced by process().

◆ fft_smps

const Floats PaulStretch::fft_smps
private

Definition at line 80 of file Paulstretch.cpp.

Referenced by process().

◆ fft_tmp

const Floats PaulStretch::fft_tmp
private

Definition at line 80 of file Paulstretch.cpp.

Referenced by process().

◆ in_bufsize

const size_t PaulStretch::in_bufsize
private

Definition at line 63 of file Paulstretch.cpp.

◆ in_pool

const Floats PaulStretch::in_pool
private

Definition at line 76 of file Paulstretch.cpp.

Referenced by process().

◆ old_out_smp_buf

const Floats PaulStretch::old_out_smp_buf
private

Definition at line 70 of file Paulstretch.cpp.

Referenced by process().

◆ out_buf

const Floats PaulStretch::out_buf

Definition at line 67 of file Paulstretch.cpp.

Referenced by process(), and EffectPaulstretch::ProcessOne().

◆ out_bufsize

const size_t PaulStretch::out_bufsize

Definition at line 66 of file Paulstretch.cpp.

Referenced by get_nsamples(), process(), and EffectPaulstretch::ProcessOne().

◆ poolsize

const size_t PaulStretch::poolsize

◆ rap

const float PaulStretch::rap
private

Definition at line 62 of file Paulstretch.cpp.

Referenced by get_nsamples(), and process().

◆ remained_samples

double PaulStretch::remained_samples
private

Definition at line 78 of file Paulstretch.cpp.

Referenced by get_nsamples().

◆ samplerate

const float PaulStretch::samplerate
private

Definition at line 61 of file Paulstretch.cpp.


The documentation for this class was generated from the following file:
WindowFunc
void WindowFunc(int whichFunction, size_t NumSamples, float *in)
Definition: FFT.cpp:515
PaulStretch::in_bufsize
const size_t in_bufsize
Definition: Paulstretch.cpp:63
PaulStretch::samplerate
const float samplerate
Definition: Paulstretch.cpp:59
PaulStretch::rap
const float rap
Definition: Paulstretch.cpp:62
PaulStretch::fft_tmp
const Floats fft_tmp
Definition: Paulstretch.cpp:80
PaulStretch::out_buf
const Floats out_buf
Definition: Paulstretch.cpp:67
PaulStretch::fft_c
const Floats fft_c
Definition: Paulstretch.cpp:80
RealFFT
void RealFFT(size_t NumSamples, const float *RealIn, float *RealOut, float *ImagOut)
Definition: FFT.cpp:230
PaulStretch::out_bufsize
const size_t out_bufsize
Definition: Paulstretch.cpp:66
PaulStretch::process_spectrum
void process_spectrum(float *WXUNUSED(freq))
Definition: Paulstretch.cpp:59
FFT
void FFT(size_t NumSamples, bool InverseTransform, const float *RealIn, const float *ImagIn, float *RealOut, float *ImagOut)
Definition: FFT.cpp:131
PaulStretch::old_out_smp_buf
const Floats old_out_smp_buf
Definition: Paulstretch.cpp:70
PaulStretch::fft_smps
const Floats fft_smps
Definition: Paulstretch.cpp:80
eWinFuncHann
@ eWinFuncHann
Definition: FFT.h:114
PaulStretch::remained_samples
double remained_samples
Definition: Paulstretch.cpp:78
PaulStretch::fft_freq
const Floats fft_freq
Definition: Paulstretch.cpp:80
M_PI
#define M_PI
Definition: Distortion.cpp:29
PaulStretch::poolsize
const size_t poolsize
Definition: Paulstretch.cpp:73
PaulStretch::fft_s
const Floats fft_s
Definition: Paulstretch.cpp:80
PaulStretch::in_pool
const Floats in_pool
Definition: Paulstretch.cpp:76