forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaulstretch.cpp
More file actions
543 lines (424 loc) · 15.1 KB
/
Copy pathPaulstretch.cpp
File metadata and controls
543 lines (424 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**********************************************************************
Audacity: A Digital Audio Editor
Paulstretch.cpp
Nasca Octavian Paul (Paul Nasca)
Some GUI code was taken from the Echo effect
*******************************************************************//**
\class EffectPaulstretch
\brief An Extreme Time Stretch and Time Smear effect
*//*******************************************************************/
#include "../Audacity.h"
#include "Paulstretch.h"
#include "LoadEffects.h"
#include <algorithm>
#include <math.h>
#include <float.h>
#include <wx/intl.h>
#include <wx/valgen.h>
#include "../Shuttle.h"
#include "../ShuttleGui.h"
#include "../FFT.h"
#include "../widgets/valnum.h"
#include "../widgets/AudacityMessageBox.h"
#include "../Prefs.h"
#include "../WaveTrack.h"
// Define keys, defaults, minimums, and maximums for the effect parameters
//
// Name Type Key Def Min Max Scale
Param( Amount, float, wxT("Stretch Factor"), 10.0, 1.0, FLT_MAX, 1 );
Param( Time, float, wxT("Time Resolution"), 0.25f, 0.00099f, FLT_MAX, 1 );
/// \brief Class that helps EffectPaulStretch. It does the FFTs and inner loop
/// of the effect.
class PaulStretch
{
public:
PaulStretch(float rap_, size_t in_bufsize_, float samplerate_);
//in_bufsize is also a half of a FFT buffer (in samples)
virtual ~PaulStretch();
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)
private:
void process_spectrum(float *WXUNUSED(freq)) {};
const float samplerate;
const float rap;
const size_t in_bufsize;
public:
const size_t out_bufsize;
const Floats out_buf;
private:
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:
const Floats in_pool;//de marimea in_bufsize
double remained_samples;//how many fraction of samples has remained (0..1)
const Floats fft_smps, fft_c, fft_s, fft_freq, fft_tmp;
};
//
// EffectPaulstretch
//
const ComponentInterfaceSymbol EffectPaulstretch::Symbol
{ XO("Paulstretch") };
namespace{ BuiltinEffectsModule::Registration< EffectPaulstretch > reg; }
BEGIN_EVENT_TABLE(EffectPaulstretch, wxEvtHandler)
EVT_TEXT(wxID_ANY, EffectPaulstretch::OnText)
END_EVENT_TABLE()
EffectPaulstretch::EffectPaulstretch()
{
mAmount = DEF_Amount;
mTime_resolution = DEF_Time;
SetLinearEffectFlag(true);
}
EffectPaulstretch::~EffectPaulstretch()
{
}
// ComponentInterface implementation
ComponentInterfaceSymbol EffectPaulstretch::GetSymbol()
{
return Symbol;
}
TranslatableString EffectPaulstretch::GetDescription()
{
return XO("Paulstretch is only for an extreme time-stretch or \"stasis\" effect");
}
wxString EffectPaulstretch::ManualPage()
{
return wxT("Paulstretch");
}
// EffectDefinitionInterface implementation
EffectType EffectPaulstretch::GetType()
{
return EffectTypeProcess;
}
// EffectClientInterface implementation
bool EffectPaulstretch::DefineParams( ShuttleParams & S ){
S.SHUTTLE_PARAM( mAmount, Amount );
S.SHUTTLE_PARAM( mTime_resolution, Time );
return true;
}
bool EffectPaulstretch::GetAutomationParameters(CommandParameters & parms)
{
parms.WriteFloat(KEY_Amount, mAmount);
parms.WriteFloat(KEY_Time, mTime_resolution);
return true;
}
bool EffectPaulstretch::SetAutomationParameters(CommandParameters & parms)
{
ReadAndVerifyFloat(Amount);
ReadAndVerifyFloat(Time);
mAmount = Amount;
mTime_resolution = Time;
return true;
}
// Effect implementation
double EffectPaulstretch::CalcPreviewInputLength(double previewLength)
{
// FIXME: Preview is currently at the project rate, but should really be
// at the track rate (bugs 1284 and 852).
auto minDuration = GetBufferSize(mProjectRate) * 2 + 1;
// Preview playback may need to be trimmed but this is the smallest selection that we can use.
double minLength = std::max<double>(minDuration / mProjectRate, previewLength / mAmount);
return minLength;
}
bool EffectPaulstretch::Process()
{
CopyInputTracks();
m_t1=mT1;
int count=0;
for( auto track : mOutputTracks->Selected< WaveTrack >() ) {
double trackStart = track->GetStartTime();
double trackEnd = track->GetEndTime();
double t0 = mT0 < trackStart? trackStart: mT0;
double t1 = mT1 > trackEnd? trackEnd: mT1;
if (t1 > t0) {
if (!ProcessOne(track, t0,t1,count))
return false;
}
count++;
}
mT1=m_t1;
ReplaceProcessedTracks(true);
return true;
}
void EffectPaulstretch::PopulateOrExchange(ShuttleGui & S)
{
S.StartMultiColumn(2, wxALIGN_CENTER);
{
S.Validator<FloatingPointValidator<float>>(
1, &mAmount, NumValidatorStyle::DEFAULT, MIN_Amount)
/* i18n-hint: This is how many times longer the sound will be, e.g. applying
* the effect to a 1-second sample, with the default Stretch Factor of 10.0
* will give an (approximately) 10 second sound
*/
.AddTextBox(XXO("&Stretch Factor:"), wxT(""), 10);
S.Validator<FloatingPointValidator<float>>(
3, &mTime_resolution, NumValidatorStyle::ONE_TRAILING_ZERO, MIN_Time)
.AddTextBox(XXO("&Time Resolution (seconds):"), wxT(""), 10);
}
S.EndMultiColumn();
};
bool EffectPaulstretch::TransferDataToWindow()
{
if (!mUIParent->TransferDataToWindow())
{
return false;
}
return true;
}
bool EffectPaulstretch::TransferDataFromWindow()
{
if (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow())
{
return false;
}
return true;
}
// EffectPaulstretch implementation
void EffectPaulstretch::OnText(wxCommandEvent & WXUNUSED(evt))
{
EnableApply(mUIParent->TransferDataFromWindow());
}
size_t EffectPaulstretch::GetBufferSize(double rate)
{
// Audacity's fft requires a power of 2
float tmp = rate * mTime_resolution / 2.0;
tmp = log(tmp) / log(2.0);
tmp = pow(2.0, floor(tmp + 0.5));
auto stmp = size_t(tmp);
if (stmp != tmp)
// overflow
return 0;
if (stmp >= 2 * stmp)
// overflow
return 0;
return std::max<size_t>(stmp, 128);
}
bool EffectPaulstretch::ProcessOne(WaveTrack *track,double t0,double t1,int count)
{
const auto badAllocMessage =
XO("Requested value exceeds memory capacity.");
const auto stretch_buf_size = GetBufferSize(track->GetRate());
if (stretch_buf_size == 0) {
::Effect::MessageBox( badAllocMessage );
return false;
}
double amount = this->mAmount;
auto start = track->TimeToLongSamples(t0);
auto end = track->TimeToLongSamples(t1);
auto len = end - start;
const auto minDuration = stretch_buf_size * 2 + 1;
if (minDuration < stretch_buf_size) {
// overflow!
::Effect::MessageBox( badAllocMessage );
return false;
}
if (len < minDuration) { //error because the selection is too short
float maxTimeRes = log( len.as_double() ) / log(2.0);
maxTimeRes = pow(2.0, floor(maxTimeRes) + 0.5);
maxTimeRes = maxTimeRes / track->GetRate();
if (this->IsPreviewing()) {
double defaultPreviewLen;
gPrefs->Read(wxT("/AudioIO/EffectsPreviewLen"), &defaultPreviewLen, 6.0);
if ((minDuration / mProjectRate) < defaultPreviewLen) {
::Effect::MessageBox(
/* i18n-hint: 'Time Resolution' is the name of a control in the Paulstretch effect.*/
XO("Audio selection too short to preview.\n\n"
"Try increasing the audio selection to at least %.1f seconds,\n"
"or reducing the 'Time Resolution' to less than %.1f seconds.")
.Format(
(minDuration / track->GetRate()) + 0.05, // round up to 1/10 s.
floor(maxTimeRes * 10.0) / 10.0),
wxOK | wxICON_EXCLAMATION );
}
else {
::Effect::MessageBox(
/* i18n-hint: 'Time Resolution' is the name of a control in the Paulstretch effect.*/
XO("Unable to Preview.\n\n"
"For the current audio selection, the maximum\n"
"'Time Resolution' is %.1f seconds.")
.Format( floor(maxTimeRes * 10.0) / 10.0 ),
wxOK | wxICON_EXCLAMATION );
}
}
else {
::Effect::MessageBox(
/* i18n-hint: 'Time Resolution' is the name of a control in the Paulstretch effect.*/
XO("The 'Time Resolution' is too long for the selection.\n\n"
"Try increasing the audio selection to at least %.1f seconds,\n"
"or reducing the 'Time Resolution' to less than %.1f seconds.")
.Format(
(minDuration / track->GetRate()) + 0.05, // round up to 1/10 s.
floor(maxTimeRes * 10.0) / 10.0),
wxOK | wxICON_EXCLAMATION );
}
return false;
}
auto dlen = len.as_double();
double adjust_amount = dlen /
(dlen - ((double)stretch_buf_size * 2.0));
amount = 1.0 + (amount - 1.0) * adjust_amount;
auto outputTrack = track->EmptyCopy();
try {
// This encloses all the allocations of buffers, including those in
// the constructor of the PaulStretch object
PaulStretch stretch(amount, stretch_buf_size, track->GetRate());
auto nget = stretch.get_nsamples_for_fill();
auto bufsize = stretch.poolsize;
Floats buffer0{ bufsize };
float *bufferptr0 = buffer0.get();
bool first_time = true;
const auto fade_len = std::min<size_t>(100, bufsize / 2 - 1);
bool cancelled = false;
{
Floats fade_track_smps{ fade_len };
decltype(len) s=0;
while (s < len) {
track->Get((samplePtr)bufferptr0, floatSample, start + s, nget);
stretch.process(buffer0.get(), nget);
if (first_time) {
stretch.process(buffer0.get(), 0);
};
s += nget;
if (first_time){//blend 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;
}
}
}
if (!cancelled){
outputTrack->Flush();
track->Clear(t0,t1);
track->Paste(t0, outputTrack.get());
m_t1 = mT0 + outputTrack->GetEndTime();
}
return !cancelled;
}
catch ( const std::bad_alloc& ) {
::Effect::MessageBox( badAllocMessage );
return false;
}
};
/*************************************************************/
PaulStretch::PaulStretch(float rap_, size_t in_bufsize_, float samplerate_ )
: samplerate { samplerate_ }
, rap { std::max(1.0f, rap_) }
, in_bufsize { in_bufsize_ }
, out_bufsize { std::max(size_t{ 8 }, in_bufsize) }
, out_buf { out_bufsize }
, old_out_smp_buf { out_bufsize * 2, true }
, poolsize { in_bufsize_ * 2 }
, in_pool { poolsize, true }
, remained_samples { 0.0 }
, fft_smps { poolsize, true }
, fft_c { poolsize, true }
, fft_s { poolsize, true }
, fft_freq { poolsize, true }
, fft_tmp { poolsize }
{
}
PaulStretch::~PaulStretch()
{
}
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(eWinFuncHann, 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];
}
size_t PaulStretch::get_nsamples()
{
double r = out_bufsize / rap;
auto ri = (size_t)floor(r);
double rf = r - floor(r);
remained_samples += rf;
if (remained_samples >= 1.0){
ri += (size_t)floor(remained_samples);
remained_samples = remained_samples - floor(remained_samples);
}
if (ri > poolsize) {
ri = poolsize;
}
return ri;
}
size_t PaulStretch::get_nsamples_for_fill()
{
return poolsize;
}