Audacity  2.2.2
Paulstretch.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Paulstretch.cpp
6 
7  Nasca Octavian Paul (Paul Nasca)
8  Some GUI code was taken from the Echo effect
9 
10 *******************************************************************//*******************************************************************/
16 
17 #include "../Audacity.h"
18 #include "Paulstretch.h"
19 
20 #include <algorithm>
21 
22 #include <math.h>
23 #include <float.h>
24 
25 #include <wx/intl.h>
26 #include <wx/valgen.h>
27 
28 #include "../ShuttleGui.h"
29 #include "../FFT.h"
30 #include "../widgets/valnum.h"
31 #include "../widgets/ErrorDialog.h"
32 #include "../Prefs.h"
33 
34 #include "../WaveTrack.h"
35 
36 // Define keys, defaults, minimums, and maximums for the effect parameters
37 //
38 // Name Type Key Def Min Max Scale
39 Param( Amount, float, wxT("Stretch Factor"), 10.0, 1.0, FLT_MAX, 1 );
40 Param( Time, float, wxT("Time Resolution"), 0.25f, 0.00099f, FLT_MAX, 1 );
41 
43 {
44 public:
45  PaulStretch(float rap_, size_t in_bufsize_, float samplerate_);
46  //in_bufsize is also a half of a FFT buffer (in samples)
47  virtual ~PaulStretch();
48 
49  void process(float *smps, size_t nsmps);
50 
51  size_t get_nsamples();//how many samples are required to be added in the pool next time
52  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)
53 
54 private:
55  void process_spectrum(float *WXUNUSED(freq)) {};
56 
57  const float samplerate;
58  const float rap;
59  const size_t in_bufsize;
60 
61 public:
62  const size_t out_bufsize;
63  const Floats out_buf;
64 
65 private:
67 
68 public:
69  const size_t poolsize;//how many samples are inside the input_pool size (need to know how many samples to fill when seeking)
70 
71 private:
72  const Floats in_pool;//de marimea in_bufsize
73 
74  double remained_samples;//how many fraction of samples has remained (0..1)
75 
77 };
78 
79 //
80 // EffectPaulstretch
81 //
82 
83 BEGIN_EVENT_TABLE(EffectPaulstretch, wxEvtHandler)
84  EVT_TEXT(wxID_ANY, EffectPaulstretch::OnText)
86 
88 {
89  mAmount = DEF_Amount;
90  mTime_resolution = DEF_Time;
91 
92  SetLinearEffectFlag(true);
93 }
94 
96 {
97 }
98 
99 // IdentInterface implementation
100 
102 {
104 }
105 
107 {
108  return _("Use Paulstretch only for an extreme time-stretch or \"stasis\" effect");
109 }
110 
112 {
113  return wxT("Paulstretch");
114 }
115 
116 // EffectIdentInterface implementation
117 
119 {
120  return EffectTypeProcess;
121 }
122 
123 // EffectClientInterface implementation
124 
125 bool EffectPaulstretch::GetAutomationParameters(EffectAutomationParameters & parms)
126 {
127  parms.WriteFloat(KEY_Amount, mAmount);
128  parms.WriteFloat(KEY_Time, mTime_resolution);
129 
130  return true;
131 }
132 
133 bool EffectPaulstretch::SetAutomationParameters(EffectAutomationParameters & parms)
134 {
135  ReadAndVerifyFloat(Amount);
136  ReadAndVerifyFloat(Time);
137 
138  mAmount = Amount;
139  mTime_resolution = Time;
140 
141  return true;
142 }
143 
144 // Effect implementation
145 
146 double EffectPaulstretch::CalcPreviewInputLength(double previewLength)
147 {
148  // FIXME: Preview is currently at the project rate, but should really be
149  // at the track rate (bugs 1284 and 852).
150  auto minDuration = GetBufferSize(mProjectRate) * 2 + 1;
151 
152  // Preview playback may need to be trimmed but this is the smallest selection that we can use.
153  double minLength = std::max<double>(minDuration / mProjectRate, previewLength / mAmount);
154 
155  return minLength;
156 }
157 
158 
160 {
161  CopyInputTracks();
163  WaveTrack *track = (WaveTrack *) iter.First();
164  m_t1=mT1;
165  int count=0;
166  while (track) {
167  double trackStart = track->GetStartTime();
168  double trackEnd = track->GetEndTime();
169  double t0 = mT0 < trackStart? trackStart: mT0;
170  double t1 = mT1 > trackEnd? trackEnd: mT1;
171 
172  if (t1 > t0) {
173  if (!ProcessOne(track, t0,t1,count))
174  return false;
175  }
176 
177  track = (WaveTrack *) iter.Next();
178  count++;
179  }
180  mT1=m_t1;
181 
183 
184  return true;
185 }
186 
187 
189 {
190  S.StartMultiColumn(2, wxALIGN_CENTER);
191  {
192  FloatingPointValidator<float> vldAmount(1, &mAmount);
193  vldAmount.SetMin(MIN_Amount);
194 
195  /* i18n-hint: This is how many times longer the sound will be, e.g. applying
196  * the effect to a 1-second sample, with the default Stretch Factor of 10.0
197  * will give an (approximately) 10 second sound
198  */
199  S.AddTextBox(_("Stretch Factor:"), wxT(""), 10)->SetValidator(vldAmount);
200 
201  FloatingPointValidator<float> vldTime(3, &mTime_resolution, NumValidatorStyle::ONE_TRAILING_ZERO);
202  vldTime.SetMin(MIN_Time);
203  S.AddTextBox(_("Time Resolution (seconds):"), wxT(""), 10)->SetValidator(vldTime);
204  }
205  S.EndMultiColumn();
206 };
207 
209 {
210  if (!mUIParent->TransferDataToWindow())
211  {
212  return false;
213  }
214 
215  return true;
216 }
217 
219 {
220  if (!mUIParent->Validate() || !mUIParent->TransferDataFromWindow())
221  {
222  return false;
223  }
224 
225  return true;
226 }
227 
228 // EffectPaulstretch implementation
229 
230 void EffectPaulstretch::OnText(wxCommandEvent & WXUNUSED(evt))
231 {
232  EnableApply(mUIParent->TransferDataFromWindow());
233 }
234 
236 {
237  // Audacity's fft requires a power of 2
238  float tmp = rate * mTime_resolution / 2.0;
239  tmp = log(tmp) / log(2.0);
240  tmp = pow(2.0, floor(tmp + 0.5));
241 
242  auto stmp = size_t(tmp);
243  if (stmp != tmp)
244  // overflow
245  return 0;
246  if (stmp >= 2 * stmp)
247  // overflow
248  return 0;
249 
250  return std::max<size_t>(stmp, 128);
251 }
252 
253 bool EffectPaulstretch::ProcessOne(WaveTrack *track,double t0,double t1,int count)
254 {
255  auto badAllocMessage = _("Requested value exceeds memory capacity.");
256 
257  const auto stretch_buf_size = GetBufferSize(track->GetRate());
258  if (stretch_buf_size == 0) {
259  ::Effect::MessageBox( badAllocMessage );
260  return false;
261  }
262 
263  double amount = this->mAmount;
264 
265  auto start = track->TimeToLongSamples(t0);
266  auto end = track->TimeToLongSamples(t1);
267  auto len = end - start;
268 
269  const auto minDuration = stretch_buf_size * 2 + 1;
270  if (minDuration < stretch_buf_size) {
271  // overflow!
272  ::Effect::MessageBox( badAllocMessage );
273  return false;
274  }
275 
276  if (len < minDuration) { //error because the selection is too short
277 
278  float maxTimeRes = log( len.as_double() ) / log(2.0);
279  maxTimeRes = pow(2.0, floor(maxTimeRes) + 0.5);
280  maxTimeRes = maxTimeRes / track->GetRate();
281 
282  if (this->IsPreviewing()) {
283  double defaultPreviewLen;
284  gPrefs->Read(wxT("/AudioIO/EffectsPreviewLen"), &defaultPreviewLen, 6.0);
285 
286  /* i18n-hint: 'Time Resolution' is the name of a control in the Paulstretch effect.*/
287  if ((minDuration / mProjectRate) < defaultPreviewLen) {
288  ::Effect::MessageBox (wxString::Format(_("Audio selection too short to preview.\n\n"
289  "Try increasing the audio selection to at least %.1f seconds,\n"
290  "or reducing the 'Time Resolution' to less than %.1f seconds."),
291  (minDuration / track->GetRate()) + 0.05, // round up to 1/10 s.
292  floor(maxTimeRes * 10.0) / 10.0),
293  wxOK | wxICON_EXCLAMATION);
294  }
295  else {
296  /* i18n-hint: 'Time Resolution' is the name of a control in the Paulstretch effect.*/
297  ::Effect::MessageBox (wxString::Format(_("Unable to Preview.\n\n"
298  "For the current audio selection, the maximum\n"
299  "'Time Resolution' is %.1f seconds."),
300  floor(maxTimeRes * 10.0) / 10.0),
301  wxOK | wxICON_EXCLAMATION);
302  }
303  }
304  else {
305  /* i18n-hint: 'Time Resolution' is the name of a control in the Paulstretch effect.*/
306  ::Effect::MessageBox (wxString::Format(_("The 'Time Resolution' is too long for the selection.\n\n"
307  "Try increasing the audio selection to at least %.1f seconds,\n"
308  "or reducing the 'Time Resolution' to less than %.1f seconds."),
309  (minDuration / track->GetRate()) + 0.05, // round up to 1/10 s.
310  floor(maxTimeRes * 10.0) / 10.0),
311  wxOK | wxICON_EXCLAMATION);
312  }
313 
314  return false;
315  }
316 
317 
318  auto dlen = len.as_double();
319  double adjust_amount = dlen /
320  (dlen - ((double)stretch_buf_size * 2.0));
321  amount = 1.0 + (amount - 1.0) * adjust_amount;
322 
323  auto outputTrack = mFactory->NewWaveTrack(track->GetSampleFormat(),track->GetRate());
324 
325  try {
326  // This encloses all the allocations of buffers, including those in
327  // the constructor of the PaulStretch object
328 
329  PaulStretch stretch(amount, stretch_buf_size, track->GetRate());
330 
331  auto nget = stretch.get_nsamples_for_fill();
332 
333  auto bufsize = stretch.poolsize;
334  Floats buffer0{ bufsize };
335  float *bufferptr0 = buffer0.get();
336  bool first_time = true;
337 
338  const auto fade_len = std::min<size_t>(100, bufsize / 2 - 1);
339  bool cancelled = false;
340 
341  {
342  Floats fade_track_smps{ fade_len };
343  decltype(len) s=0;
344 
345  while (s < len) {
346  track->Get((samplePtr)bufferptr0, floatSample, start + s, nget);
347  stretch.process(buffer0.get(), nget);
348 
349  if (first_time) {
350  stretch.process(buffer0.get(), 0);
351  };
352 
353  s += nget;
354 
355  if (first_time){//blend the the start of the selection
356  track->Get((samplePtr)fade_track_smps.get(), floatSample, start, fade_len);
357  first_time = false;
358  for (size_t i = 0; i < fade_len; i++){
359  float fi = (float)i / (float)fade_len;
360  stretch.out_buf[i] =
361  stretch.out_buf[i] * fi + (1.0 - fi) * fade_track_smps[i];
362  }
363  }
364  if (s >= len){//blend the end of the selection
365  track->Get((samplePtr)fade_track_smps.get(), floatSample, end - fade_len, fade_len);
366  for (size_t i = 0; i < fade_len; i++){
367  float fi = (float)i / (float)fade_len;
368  auto i2 = bufsize / 2 - 1 - i;
369  stretch.out_buf[i2] =
370  stretch.out_buf[i2] * fi + (1.0 - fi) *
371  fade_track_smps[fade_len - 1 - i];
372  }
373  }
374 
375  outputTrack->Append((samplePtr)stretch.out_buf.get(), floatSample, stretch.out_bufsize);
376 
377  nget = stretch.get_nsamples();
378  if (TrackProgress(count,
379  s.as_double() / len.as_double()
380  )) {
381  cancelled = true;
382  break;
383  }
384  }
385  }
386 
387  if (!cancelled){
388  outputTrack->Flush();
389 
390  track->Clear(t0,t1);
391  track->Paste(t0, outputTrack.get());
392  m_t1 = mT0 + outputTrack->GetEndTime();
393  }
394 
395  return !cancelled;
396  }
397  catch ( const std::bad_alloc& ) {
398  ::Effect::MessageBox( badAllocMessage );
399  return false;
400  }
401 };
402 
403 /*************************************************************/
404 
405 
406 PaulStretch::PaulStretch(float rap_, size_t in_bufsize_, float samplerate_ )
407  : samplerate { samplerate_ }
408  , rap { std::max(1.0f, rap_) }
409  , in_bufsize { in_bufsize_ }
410  , out_bufsize { std::max(size_t{ 8 }, in_bufsize) }
411  , out_buf { out_bufsize }
412  , old_out_smp_buf { out_bufsize * 2, true }
413  , poolsize { in_bufsize_ * 2 }
414  , in_pool { poolsize, true }
415  , remained_samples { 0.0 }
416  , fft_smps { poolsize, true }
417  , fft_s { poolsize, true }
418  , fft_c { poolsize, true }
419  , fft_freq { poolsize, true }
420  , fft_tmp { poolsize }
421 {
422 }
423 
425 {
426 }
427 
428 void PaulStretch::process(float *smps, size_t nsmps)
429 {
430  //add NEW samples to the pool
431  if ((smps != NULL) && (nsmps != 0)) {
432  if (nsmps > poolsize) {
433  nsmps = poolsize;
434  }
435  int nleft = poolsize - nsmps;
436 
437  //move left the samples from the pool to make room for NEW samples
438  for (int i = 0; i < nleft; i++)
439  in_pool[i] = in_pool[i + nsmps];
440 
441  //add NEW samples to the pool
442  for (size_t i = 0; i < nsmps; i++)
443  in_pool[i + nleft] = smps[i];
444  }
445 
446  //get the samples from the pool
447  for (size_t i = 0; i < poolsize; i++)
448  fft_smps[i] = in_pool[i];
449  WindowFunc(eWinFuncHanning, poolsize, fft_smps.get());
450 
451  RealFFT(poolsize, fft_smps.get(), fft_c.get(), fft_s.get());
452 
453  for (size_t i = 0; i < poolsize / 2; i++)
454  fft_freq[i] = sqrt(fft_c[i] * fft_c[i] + fft_s[i] * fft_s[i]);
455  process_spectrum(fft_freq.get());
456 
457 
458  //put randomize phases to frequencies and do a IFFT
459  float inv_2p15_2pi = 1.0 / 16384.0 * (float)M_PI;
460  for (size_t i = 1; i < poolsize / 2; i++) {
461  unsigned int random = (rand()) & 0x7fff;
462  float phase = random * inv_2p15_2pi;
463  float s = fft_freq[i] * sin(phase);
464  float c = fft_freq[i] * cos(phase);
465 
466  fft_c[i] = fft_c[poolsize - i] = c;
467 
468  fft_s[i] = s; fft_s[poolsize - i] = -s;
469  }
470  fft_c[0] = fft_s[0] = 0.0;
471  fft_c[poolsize / 2] = fft_s[poolsize / 2] = 0.0;
472 
473  FFT(poolsize, true, fft_c.get(), fft_s.get(), fft_smps.get(), fft_tmp.get());
474 
475  float max = 0.0, max2 = 0.0;
476  for (size_t i = 0; i < poolsize; i++) {
477  max = std::max(max, fabsf(fft_tmp[i]));
478  max2 = std::max(max2, fabsf(fft_smps[i]));
479  }
480 
481 
482  //make the output buffer
483  float tmp = 1.0 / (float) out_bufsize * M_PI;
484  float hinv_sqrt2 = 0.853553390593f;//(1.0+1.0/sqrt(2))*0.5;
485 
486  float ampfactor = 1.0;
487  if (rap < 1.0)
488  ampfactor = rap * 0.707;
489  else
490  ampfactor = (out_bufsize / (float)poolsize) * 4.0;
491 
492  for (size_t i = 0; i < out_bufsize; i++) {
493  float a = (0.5 + 0.5 * cos(i * tmp));
494  float out = fft_smps[i + out_bufsize] * (1.0 - a) + old_out_smp_buf[i] * a;
495  out_buf[i] =
496  out * (hinv_sqrt2 - (1.0 - hinv_sqrt2) * cos(i * 2.0 * tmp)) *
497  ampfactor;
498  }
499 
500  //copy the current output buffer to old buffer
501  for (size_t i = 0; i < out_bufsize * 2; i++)
502  old_out_smp_buf[i] = fft_smps[i];
503 }
504 
506 {
507  double r = out_bufsize / rap;
508  auto ri = (size_t)floor(r);
509  double rf = r - floor(r);
510 
511  remained_samples += rf;
512  if (remained_samples >= 1.0){
513  ri += (size_t)floor(remained_samples);
515  }
516 
517  if (ri > poolsize) {
518  ri = poolsize;
519  }
520 
521  return ri;
522 }
523 
525 {
526  return poolsize;
527 }
double mT1
Definition: Effect.h:464
int MessageBox(const wxString &message, long style=DefaultMessageBoxStyle, const wxString &titleStr=wxString{})
Definition: Effect.cpp:2665
bool TrackProgress(int whichTrack, double frac, const wxString &=wxEmptyString)
Definition: Effect.cpp:1988
bool TransferDataToWindow() override
const float rap
Definition: Paulstretch.cpp:58
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:366
const size_t poolsize
Definition: Paulstretch.cpp:69
bool Get(samplePtr buffer, sampleFormat format, sampleCount start, size_t len, fillFormat fill=fillZero, bool mayThrow=true) const
Definition: WaveTrack.cpp:1977
bool IsPreviewing()
Definition: Effect.h:364
#define PAULSTRETCH_PLUGIN_SYMBOL
Definition: Paulstretch.h:19
void CopyInputTracks()
Definition: Effect.cpp:2039
#define ReadAndVerifyFloat(name)
Definition: Effect.h:792
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: Effect.cpp:2170
bool Process() override
Param(Amount, float, wxT("Stretch Factor"), 10.0, 1.0, FLT_MAX, 1)
void EndMultiColumn()
PaulStretch(float rap_, size_t in_bufsize_, float samplerate_)
double remained_samples
Definition: Paulstretch.cpp:74
void Clear(double t0, double t1) override
Definition: WaveTrack.cpp:699
EffectType GetType() override
bool ProcessOne(WaveTrack *track, double t0, double t1, int count)
wxString ManualPage() override
virtual ~EffectPaulstretch()
Definition: Paulstretch.cpp:95
const Floats out_buf
Definition: Paulstretch.cpp:63
wxFileConfig * gPrefs
Definition: Prefs.cpp:72
const Floats fft_freq
Definition: Paulstretch.cpp:76
wxTextCtrl * AddTextBox(const wxString &Caption, const wxString &Value, const int nChars)
Definition: ShuttleGui.cpp:493
void FFT(size_t NumSamples, bool InverseTransform, const float *RealIn, const float *ImagIn, float *RealOut, float *ImagOut)
Definition: FFT.cpp:132
virtual ~PaulStretch()
An Extreme Time Stretch and Time Smear effect.
Definition: Paulstretch.h:21
void RealFFT(size_t NumSamples, const float *RealIn, float *RealOut, float *ImagOut)
Definition: FFT.cpp:231
void WindowFunc(int whichFunction, size_t NumSamples, float *in)
Definition: FFT.cpp:507
const Floats fft_smps
Definition: Paulstretch.cpp:76
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
Definition: ShuttleGui.cpp:998
A Track that contains audio waveform data.
Definition: WaveTrack.h:60
void process(float *smps, size_t nsmps)
float mTime_resolution
Definition: Paulstretch.h:60
void Paste(double t0, const Track *src) override
Definition: WaveTrack.cpp:1197
wxString GetDescription() override
void process_spectrum(float *WXUNUSED(freq))
Definition: Paulstretch.cpp:55
const size_t in_bufsize
Definition: Paulstretch.cpp:59
const Floats fft_tmp
Definition: Paulstretch.cpp:76
double CalcPreviewInputLength(double previewLength) override
std::unique_ptr< WaveTrack > NewWaveTrack(sampleFormat format=(sampleFormat) 0, double rate=0)
Definition: WaveTrack.cpp:78
wxWindow * mUIParent
Definition: Effect.h:475
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
double mProjectRate
Definition: Effect.h:456
TrackFactory * mFactory
Definition: Effect.h:460
void PopulateOrExchange(ShuttleGui &S) override
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: WaveTrack.cpp:1849
#define M_PI
Definition: Distortion.cpp:28
sampleFormat GetSampleFormat() const
Definition: WaveTrack.h:146
Memory.h template class for making an array of float, bool, etc.
Definition: MemoryX.h:469
const size_t out_bufsize
Definition: Paulstretch.cpp:62
size_t get_nsamples()
size_t GetBufferSize(double rate)
const Floats fft_c
Definition: Paulstretch.cpp:76
const Floats fft_s
Definition: Paulstretch.cpp:76
wxString GetSymbol() override
bool TransferDataFromWindow() override
END_EVENT_TABLE()
double GetRate() const
Definition: WaveTrack.cpp:424
const Floats in_pool
Definition: Paulstretch.cpp:72
void OnText(wxCommandEvent &evt)
const Floats old_out_smp_buf
Definition: Paulstretch.cpp:66
std::shared_ptr< TrackList > mOutputTracks
Definition: Effect.h:462
const float samplerate
Definition: Paulstretch.cpp:55
bool GetAutomationParameters(EffectAutomationParameters &parms) override
virtual bool EnableApply(bool enable=true)
Definition: Effect.cpp:1889
double mT0
Definition: Effect.h:463
bool SetAutomationParameters(EffectAutomationParameters &parms) override
size_t get_nsamples_for_fill()