Audacity 3.2.0
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 37 of file PaulstretchBase.cpp.

Constructor & Destructor Documentation

◆ PaulStretch()

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

Definition at line 373 of file PaulstretchBase.cpp.

374 : samplerate { samplerate_ }
375 , rap { std::max(1.0f, rap_) }
376 , in_bufsize { in_bufsize_ }
377 , out_bufsize { std::max(size_t { 8 }, in_bufsize) }
378 , out_buf { out_bufsize }
379 , old_out_smp_buf { out_bufsize * 2, true }
380 , poolsize { in_bufsize_ * 2 }
381 , in_pool { poolsize, true }
382 , remained_samples { 0.0 }
383 , fft_smps { poolsize, true }
384 , fft_c { poolsize, true }
385 , fft_s { poolsize, true }
386 , fft_freq { poolsize, true }
387 , fft_tmp { poolsize }
388{
389}
const float rap
const Floats in_pool
double remained_samples
const size_t out_bufsize
const Floats old_out_smp_buf
const Floats fft_tmp
const float samplerate
const Floats fft_freq
const Floats fft_smps
const size_t in_bufsize
const size_t poolsize
const Floats out_buf
const Floats fft_s
const Floats fft_c

◆ ~PaulStretch()

PaulStretch::~PaulStretch ( )
virtual

Definition at line 391 of file PaulstretchBase.cpp.

392{
393}

Member Function Documentation

◆ get_nsamples()

size_t PaulStretch::get_nsamples ( )

Definition at line 477 of file PaulstretchBase.cpp.

478{
479 double r = out_bufsize / rap;
480 auto ri = (size_t)floor(r);
481 double rf = r - floor(r);
482
483 remained_samples += rf;
484 if (remained_samples >= 1.0)
485 {
486 ri += (size_t)floor(remained_samples);
488 }
489
490 if (ri > poolsize)
491 {
492 ri = poolsize;
493 }
494
495 return ri;
496}

References out_bufsize, poolsize, rap, and remained_samples.

Referenced by PaulstretchBase::ProcessOne().

Here is the caller graph for this function:

◆ get_nsamples_for_fill()

size_t PaulStretch::get_nsamples_for_fill ( )

Definition at line 498 of file PaulstretchBase.cpp.

499{
500 return poolsize;
501}

References poolsize.

Referenced by PaulstretchBase::ProcessOne().

Here is the caller graph for this function:

◆ process()

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

Definition at line 395 of file PaulstretchBase.cpp.

396{
397 // add NEW samples to the pool
398 if ((smps != NULL) && (nsmps != 0))
399 {
400 if (nsmps > poolsize)
401 {
402 nsmps = poolsize;
403 }
404 int nleft = poolsize - nsmps;
405
406 // move left the samples from the pool to make room for NEW samples
407 for (int i = 0; i < nleft; i++)
408 in_pool[i] = in_pool[i + nsmps];
409
410 // add NEW samples to the pool
411 for (size_t i = 0; i < nsmps; i++)
412 in_pool[i + nleft] = smps[i];
413 }
414
415 // get the samples from the pool
416 for (size_t i = 0; i < poolsize; i++)
417 fft_smps[i] = in_pool[i];
419
420 RealFFT(poolsize, fft_smps.get(), fft_c.get(), fft_s.get());
421
422 for (size_t i = 0; i < poolsize / 2; i++)
423 fft_freq[i] = sqrt(fft_c[i] * fft_c[i] + fft_s[i] * fft_s[i]);
425
426 // put randomize phases to frequencies and do a IFFT
427 float inv_2p15_2pi = 1.0 / 16384.0 * (float)M_PI;
428 for (size_t i = 1; i < poolsize / 2; i++)
429 {
430 unsigned int random = (rand()) & 0x7fff;
431 float phase = random * inv_2p15_2pi;
432 float s = fft_freq[i] * sin(phase);
433 float c = fft_freq[i] * cos(phase);
434
435 fft_c[i] = fft_c[poolsize - i] = c;
436
437 fft_s[i] = s;
438 fft_s[poolsize - i] = -s;
439 }
440 fft_c[0] = fft_s[0] = 0.0;
441 fft_c[poolsize / 2] = fft_s[poolsize / 2] = 0.0;
442
443 FFT(poolsize, true, fft_c.get(), fft_s.get(), fft_smps.get(), fft_tmp.get());
444
445 float max = 0.0, max2 = 0.0;
446 for (size_t i = 0; i < poolsize; i++)
447 {
448 max = std::max(max, fabsf(fft_tmp[i]));
449 max2 = std::max(max2, fabsf(fft_smps[i]));
450 }
451
452 // make the output buffer
453 float tmp = 1.0 / (float)out_bufsize * M_PI;
454 float hinv_sqrt2 = 0.853553390593f; //(1.0+1.0/sqrt(2))*0.5;
455
456 float ampfactor = 1.0;
457 if (rap < 1.0)
458 ampfactor = rap * 0.707;
459 else
460 ampfactor = (out_bufsize / (float)poolsize) * 4.0;
461
462 for (size_t i = 0; i < out_bufsize; i++)
463 {
464 float a = (0.5 + 0.5 * cos(i * tmp));
465 float out =
466 fft_smps[i + out_bufsize] * (1.0 - a) + old_out_smp_buf[i] * a;
467 out_buf[i] = out *
468 (hinv_sqrt2 - (1.0 - hinv_sqrt2) * cos(i * 2.0 * tmp)) *
469 ampfactor;
470 }
471
472 // copy the current output buffer to old buffer
473 for (size_t i = 0; i < out_bufsize * 2; i++)
475}
#define M_PI
Definition: Distortion.cpp:22
void WindowFunc(int whichFunction, size_t NumSamples, float *in)
Definition: FFT.cpp:513
void RealFFT(size_t NumSamples, const float *RealIn, float *RealOut, float *ImagOut)
Definition: FFT.cpp:228
void FFT(size_t NumSamples, bool InverseTransform, const float *RealIn, const float *ImagIn, float *RealOut, float *ImagOut)
Definition: FFT.cpp:129
@ eWinFuncHann
Definition: FFT.h:114
void process_spectrum(float *WXUNUSED(freq))
__finl float_x4 __vecc sqrt(const float_x4 &a)

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(), staffpad::audio::simd::sqrt(), and WindowFunc().

Referenced by PaulstretchBase::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 53 of file PaulstretchBase.cpp.

53{};

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 76 of file PaulstretchBase.cpp.

Referenced by process().

◆ fft_freq

const Floats PaulStretch::fft_freq
private

Definition at line 76 of file PaulstretchBase.cpp.

Referenced by process().

◆ fft_s

const Floats PaulStretch::fft_s
private

Definition at line 76 of file PaulstretchBase.cpp.

Referenced by process().

◆ fft_smps

const Floats PaulStretch::fft_smps
private

Definition at line 76 of file PaulstretchBase.cpp.

Referenced by process().

◆ fft_tmp

const Floats PaulStretch::fft_tmp
private

Definition at line 76 of file PaulstretchBase.cpp.

Referenced by process().

◆ in_bufsize

const size_t PaulStretch::in_bufsize
private

Definition at line 57 of file PaulstretchBase.cpp.

◆ in_pool

const Floats PaulStretch::in_pool
private

Definition at line 72 of file PaulstretchBase.cpp.

Referenced by process().

◆ old_out_smp_buf

const Floats PaulStretch::old_out_smp_buf
private

Definition at line 64 of file PaulstretchBase.cpp.

Referenced by process().

◆ out_buf

const Floats PaulStretch::out_buf

Definition at line 61 of file PaulstretchBase.cpp.

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

◆ out_bufsize

const size_t PaulStretch::out_bufsize

Definition at line 60 of file PaulstretchBase.cpp.

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

◆ poolsize

const size_t PaulStretch::poolsize

◆ rap

const float PaulStretch::rap
private

Definition at line 56 of file PaulstretchBase.cpp.

Referenced by get_nsamples(), and process().

◆ remained_samples

double PaulStretch::remained_samples
private

Definition at line 74 of file PaulstretchBase.cpp.

Referenced by get_nsamples().

◆ samplerate

const float PaulStretch::samplerate
private

Definition at line 55 of file PaulstretchBase.cpp.


The documentation for this class was generated from the following file: