1
0
mirror of https://github.com/cookiengineer/audacity synced 2026-03-19 04:36:35 +01:00

Remove naked new[] in: builtin effects

This commit is contained in:
Paul Licameli
2016-04-14 12:35:15 -04:00
parent f858d97352
commit 6ca89c28ff
28 changed files with 558 additions and 774 deletions

View File

@@ -45,7 +45,7 @@ public:
//in_bufsize is also a half of a FFT buffer (in samples)
virtual ~PaulStretch();
void process(float *smps,int nsmps);
void process(float *smps, size_t nsmps);
size_t get_nsamples();//how many samples are required to be added in the pool next time
size_t get_nsamples_for_fill();//how many samples are required to be added for a complete buffer refill (at start of the song or after seek)
@@ -59,24 +59,20 @@ private:
public:
const size_t out_bufsize;
float *const out_buf;
const Floats out_buf;
private:
float *const old_out_smp_buf;
const Floats old_out_smp_buf;
public:
const size_t poolsize;//how many samples are inside the input_pool size (need to know how many samples to fill when seeking)
private:
float *const in_pool;//de marimea in_bufsize
const Floats in_pool;//de marimea in_bufsize
double remained_samples;//how many fraction of samples has remained (0..1)
float *const fft_smps;
float *const fft_c;
float *const fft_s;
float *const fft_freq;
float *const fft_tmp;
const Floats fft_smps, fft_c, fft_s, fft_freq, fft_tmp;
};
//
@@ -329,58 +325,59 @@ bool EffectPaulstretch::ProcessOne(WaveTrack *track,double t0,double t1,int coun
auto nget = stretch.get_nsamples_for_fill();
auto bufsize = stretch.poolsize;
float *buffer0 = new float[bufsize];
float *bufferptr0 = buffer0;
Floats buffer0{ bufsize };
float *bufferptr0 = buffer0.get();
bool first_time = true;
const auto fade_len = std::min<size_t>(100, bufsize / 2 - 1);
float *fade_track_smps = new float[fade_len];
decltype(len) s = 0;
bool cancelled = false;
while (s < len) {
track->Get((samplePtr)bufferptr0, floatSample, start + s, nget);
stretch.process(buffer0, nget);
{
Floats fade_track_smps{ fade_len };
decltype(len) s=0;
if (first_time) {
stretch.process(buffer0, 0);
};
while (s < len) {
track->Get((samplePtr)bufferptr0, floatSample, start + s, nget);
stretch.process(buffer0.get(), nget);
s += nget;
if (first_time) {//blend the the start of the selection
track->Get((samplePtr)fade_track_smps, floatSample, start, fade_len);
first_time = false;
for (int i = 0; i < fade_len; i++){
float fi = (float)i / (float)fade_len;
stretch.out_buf[i] =
stretch.out_buf[i] * fi + (1.0 - fi) * fade_track_smps[i];
if (first_time) {
stretch.process(buffer0.get(), 0);
};
};
if (s >= len) {//blend the end of the selection
track->Get((samplePtr)fade_track_smps,floatSample,end-fade_len,fade_len);
for (int i = 0; i < fade_len; i++){
float fi = (float)i / (float)fade_len;
auto i2 = bufsize / 2 - 1 - i;
stretch.out_buf[i2] =
stretch.out_buf[i2] * fi + (1.0 - fi) *
fade_track_smps[fade_len - 1 - i];
};
};
outputTrack->Append((samplePtr)stretch.out_buf, floatSample,
stretch.out_bufsize);
s += nget;
nget = stretch.get_nsamples();
if (TrackProgress(count,
s.as_double() / len.as_double()
)) {
cancelled=true;
break;
if (first_time){//blend the the start of the selection
track->Get((samplePtr)fade_track_smps.get(), floatSample, start, fade_len);
first_time = false;
for (size_t i = 0; i < fade_len; i++){
float fi = (float)i / (float)fade_len;
stretch.out_buf[i] =
stretch.out_buf[i] * fi + (1.0 - fi) * fade_track_smps[i];
}
}
if (s >= len){//blend the end of the selection
track->Get((samplePtr)fade_track_smps.get(), floatSample, end - fade_len, fade_len);
for (size_t i = 0; i < fade_len; i++){
float fi = (float)i / (float)fade_len;
auto i2 = bufsize / 2 - 1 - i;
stretch.out_buf[i2] =
stretch.out_buf[i2] * fi + (1.0 - fi) *
fade_track_smps[fade_len - 1 - i];
}
}
outputTrack->Append((samplePtr)stretch.out_buf.get(), floatSample, stretch.out_bufsize);
nget = stretch.get_nsamples();
if (TrackProgress(count,
s.as_double() / len.as_double()
)) {
cancelled = true;
break;
}
}
}
delete [] fade_track_smps;
outputTrack->Flush();
track->Clear(t0,t1);
@@ -389,8 +386,6 @@ bool EffectPaulstretch::ProcessOne(WaveTrack *track,double t0,double t1,int coun
m_t1 = mT0 + outputTrack->GetEndTime();
}
delete []buffer0;
return !cancelled;
}
catch ( const std::bad_alloc& ) {
@@ -407,32 +402,24 @@ PaulStretch::PaulStretch(float rap_, size_t in_bufsize_, float samplerate_ )
, rap { std::max(1.0f, rap_) }
, in_bufsize { in_bufsize_ }
, out_bufsize { std::max(size_t{ 8 }, in_bufsize) }
, out_buf { new float[out_bufsize] }
, old_out_smp_buf { new float[out_bufsize * 2] { 0.0f } }
, out_buf { out_bufsize }
, old_out_smp_buf { out_bufsize * 2, true }
, poolsize { in_bufsize_ * 2 }
, in_pool { new float[poolsize] { 0.0f } }
, in_pool { poolsize, true }
, remained_samples { 0.0 }
, fft_smps { new float[poolsize] { 0.0f } }
, fft_s { new float[poolsize] { 0.0f } }
, fft_c { new float[poolsize] { 0.0f } }
, fft_freq { new float[poolsize] { 0.0f } }
, fft_tmp { new float[poolsize] }
, fft_smps { poolsize, true }
, fft_s { poolsize, true }
, fft_c { poolsize, true }
, fft_freq { poolsize, true }
, fft_tmp { poolsize }
{
}
PaulStretch::~PaulStretch()
{
delete [] out_buf;
delete [] old_out_smp_buf;
delete [] in_pool;
delete [] fft_smps;
delete [] fft_c;
delete [] fft_s;
delete [] fft_freq;
delete [] fft_tmp;
}
void PaulStretch::process(float *smps,int nsmps)
void PaulStretch::process(float *smps, size_t nsmps)
{
//add NEW samples to the pool
if ((smps != NULL) && (nsmps != 0)) {
@@ -446,20 +433,20 @@ void PaulStretch::process(float *smps,int nsmps)
in_pool[i] = in_pool[i + nsmps];
//add NEW samples to the pool
for (int i = 0; i < nsmps; i++)
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);
WindowFunc(eWinFuncHanning, poolsize, fft_smps.get());
RealFFT(poolsize, fft_smps, fft_c, fft_s);
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);
process_spectrum(fft_freq.get());
//put randomize phases to frequencies and do a IFFT
@@ -477,7 +464,7 @@ void PaulStretch::process(float *smps,int nsmps)
fft_c[0] = fft_s[0] = 0.0;
fft_c[poolsize / 2] = fft_s[poolsize / 2] = 0.0;
FFT(poolsize, true, fft_c, fft_s, fft_smps, fft_tmp);
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++) {