Nsound  0.9.4
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Nsound::Stretcher Class Reference

WSOLA. More...

#include <Nsound/Stretcher.h>

Public Member Functions

 Stretcher (const float64 &sample_rate, const float64 &window_size_seconds=0.08, const float64 &max_delta_window=0.25)
 Default Constructor. More...
 
 Stretcher (const Stretcher &copy)
 Copy constructor. More...
 
virtual ~Stretcher ()
 Destructor. More...
 
Stretcheroperator= (const Stretcher &rhs)
 Assignment. More...
 
AudioStream pitchShift (const AudioStream &x, const float64 &factor)
 
AudioStream pitchShift (const AudioStream &x, const Buffer &factor)
 
Buffer pitchShift (const Buffer &x, const float64 &factor)
 
Buffer pitchShift (const Buffer &x, const Buffer &factor)
 
void showProgress (boolean flag)
 
AudioStream timeShift (const AudioStream &x, const float64 &factor)
 
AudioStream timeShift (const AudioStream &x, const Buffer &factor)
 
Buffer timeShift (const Buffer &x, const float64 &factor)
 
Buffer timeShift (const Buffer &x, const Buffer &factor)
 

Protected Member Functions

void analyize (const Buffer &input, const float64 &factor)
 
void analyize (const Buffer &input, const Buffer &factor)
 
uint32 searchForBestMatch (const Buffer &input, uint32 source_index, uint32 search_index) const
 
Buffer overlapAdd (const Buffer &input) const
 

Protected Attributes

Bufferframes_
 
float64 sample_rate_
 
Bufferwindow_
 
uint32 window_length_
 
uint32 max_delta_
 
boolean show_progress_
 

Detailed Description

WSOLA.

Definition at line 49 of file Stretcher.h.

Constructor & Destructor Documentation

Stretcher::Stretcher ( const float64 sample_rate,
const float64 window_size_seconds = 0.08,
const float64 max_delta_window = 0.25 
)

Default Constructor.

sample_rate: the sample rate window_size: default window size in seconds max_delta_window: percent of window size to search for best fit

Definition at line 54 of file Stretcher.cc.

References Nsound::Generator::drawWindowHanning(), frames_, Nsound::Buffer::getLength(), max_delta_, window_, and window_length_.

58  :
59  frames_(NULL),
60  sample_rate_(sample_rate),
61  window_(NULL),
62  window_length_(0),
63  max_delta_(0),
64  show_progress_(false)
65 {
66  frames_ = new Buffer(1024);
67  window_ = new Buffer(1024);
68 
69  Sine sin(sample_rate);
70  *window_ = sin.drawWindowHanning(window_size_seconds);
72  max_delta_ = uint32(float64(window_length_) * max_delta_window);
73 }
unsigned int uint32
Definition: Nsound.h:153
Buffer * window_
Definition: Stretcher.h:119
double float64
Definition: Nsound.h:146
uint32 window_length_
Definition: Stretcher.h:120
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer * frames_
Definition: Stretcher.h:117
boolean show_progress_
Definition: Stretcher.h:122
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 sample_rate_
Definition: Stretcher.h:118
DOXME.
Definition: Sine.h:43
Stretcher::Stretcher ( const Stretcher copy)

Copy constructor.

Definition at line 77 of file Stretcher.cc.

References frames_, window_, and window_length_.

78  :
79  frames_(NULL),
81  window_(NULL),
83  max_delta_(copy.max_delta_),
85 {
86  frames_ = new Buffer(1024);
88 
89  *this = copy;
90 }
Buffer * window_
Definition: Stretcher.h:119
uint32 window_length_
Definition: Stretcher.h:120
Buffer * frames_
Definition: Stretcher.h:117
boolean show_progress_
Definition: Stretcher.h:122
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 sample_rate_
Definition: Stretcher.h:118
Stretcher::~Stretcher ( )
virtual

Destructor.

Definition at line 94 of file Stretcher.cc.

References frames_, and window_.

95 {
96  delete frames_;
97  delete window_;
98 }
Buffer * window_
Definition: Stretcher.h:119
Buffer * frames_
Definition: Stretcher.h:117

Member Function Documentation

Stretcher & Stretcher::operator= ( const Stretcher rhs)

Assignment.

Definition at line 103 of file Stretcher.cc.

References frames_, max_delta_, sample_rate_, show_progress_, window_, and window_length_.

104 {
105  if(this == & rhs) return *this;
106 
109  max_delta_ = rhs.max_delta_;
110  *frames_ = *rhs.frames_;
111  *window_ = *rhs.window_;
113 
114  return *this;
115 }
Buffer * window_
Definition: Stretcher.h:119
uint32 window_length_
Definition: Stretcher.h:120
Buffer * frames_
Definition: Stretcher.h:117
boolean show_progress_
Definition: Stretcher.h:122
float64 sample_rate_
Definition: Stretcher.h:118
AudioStream Stretcher::pitchShift ( const AudioStream x,
const float64 factor 
)

Definition at line 338 of file Stretcher.cc.

References analyize(), Nsound::AudioStream::getMono(), Nsound::AudioStream::getNChannels(), Nsound::AudioStream::getSampleRate(), and overlapAdd().

Referenced by main().

339 {
340  // Prepare for time shift.
341  analyize(x.getMono()[0], factor);
342 
344 
345  for(uint32 i = 0; i < x.getNChannels(); ++i)
346  {
347  y[i] = overlapAdd(x[i]);
348  }
349 
350  return y.getResample(1.0/factor);
351 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:265
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
AudioStream Stretcher::pitchShift ( const AudioStream x,
const Buffer factor 
)

Definition at line 355 of file Stretcher.cc.

References analyize(), Nsound::AudioStream::getMono(), Nsound::AudioStream::getNChannels(), Nsound::AudioStream::getSampleRate(), and overlapAdd().

356 {
357  // Prepare for time shift.
358  analyize(x.getMono()[0], factor);
359 
361 
362  for(uint32 i = 0; i < x.getNChannels(); ++i)
363  {
364  y[i] = overlapAdd(x[i]);
365  }
366 
367  return y.getSpeedUp(factor);
368 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:265
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
Buffer Stretcher::pitchShift ( const Buffer x,
const float64 factor 
)

Definition at line 372 of file Stretcher.cc.

References analyize(), Nsound::Buffer::getResample(), and overlapAdd().

373 {
374  // Prepare for time shift.
375  analyize(x, factor);
376 
377  Buffer stretched = overlapAdd(x);
378 
379  return stretched.getResample(1.0/factor);
380 }
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer getResample(float64 factor, const uint32 N=10, float64 beta=5.0) const
Resamples a copy of this Buffer using discrete-time resampling.
Definition: Buffer.cc:1607
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
Buffer Stretcher::pitchShift ( const Buffer x,
const Buffer factor 
)

Definition at line 384 of file Stretcher.cc.

References analyize(), Nsound::Buffer::getSpeedUp(), and overlapAdd().

385 {
386  // Prepare for time shift.
387  analyize(x, factor);
388 
389  Buffer stretched = overlapAdd(x);
390 
391  return stretched.getSpeedUp(factor);
392 }
Buffer getSpeedUp(float64 step_size) const
Resamples a copy of this Buffer by the step_size, no interpolation.
Definition: Buffer.h:1707
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
A Buffer for storing audio samples.
Definition: Buffer.h:60
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
void Nsound::Stretcher::showProgress ( boolean  flag)
inline

Definition at line 86 of file Stretcher.h.

References show_progress_.

Referenced by main(), and stretch_to().

86 {show_progress_ = flag;};
boolean show_progress_
Definition: Stretcher.h:122
AudioStream Stretcher::timeShift ( const AudioStream x,
const float64 factor 
)

Definition at line 396 of file Stretcher.cc.

References analyize(), Nsound::AudioStream::getMono(), Nsound::AudioStream::getNChannels(), Nsound::AudioStream::getSampleRate(), and overlapAdd().

Referenced by main().

397 {
398  // Prepare for time shift.
399  analyize(x.getMono()[0], factor);
400 
402 
403  for(uint32 i = 0; i < x.getNChannels(); ++i)
404  {
405  y[i] = overlapAdd(x[i]);
406  }
407 
408  return y;
409 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:265
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
AudioStream Stretcher::timeShift ( const AudioStream x,
const Buffer factor 
)

Definition at line 413 of file Stretcher.cc.

References analyize(), Nsound::AudioStream::getMono(), Nsound::AudioStream::getNChannels(), Nsound::AudioStream::getSampleRate(), and overlapAdd().

414 {
415  // Prepare for time shift.
416  analyize(x.getMono()[0], factor);
417 
419 
420  for(uint32 i = 0; i < x.getNChannels(); ++i)
421  {
422  y[i] = overlapAdd(x[i]);
423  }
424 
425  return y;
426 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:265
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
Buffer Stretcher::timeShift ( const Buffer x,
const float64 factor 
)

Definition at line 430 of file Stretcher.cc.

References analyize(), and overlapAdd().

431 {
432  // Prepare for time shift.
433  analyize(x, factor);
434 
435  Buffer stretched = overlapAdd(x);
436 
437  return stretched;
438 }
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
A Buffer for storing audio samples.
Definition: Buffer.h:60
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
Buffer Stretcher::timeShift ( const Buffer x,
const Buffer factor 
)

Definition at line 442 of file Stretcher.cc.

References analyize(), and overlapAdd().

443 {
444  // Prepare for time shift.
445  analyize(x, factor);
446 
447  Buffer stretched = overlapAdd(x);
448 
449  return stretched;
450 }
Buffer overlapAdd(const Buffer &input) const
Definition: Stretcher.cc:281
A Buffer for storing audio samples.
Definition: Buffer.h:60
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
void Stretcher::analyize ( const Buffer input,
const float64 factor 
)
protected

Definition at line 119 of file Stretcher.cc.

Referenced by pitchShift(), and timeShift().

120 {
121  Buffer factor(1);
122  factor << f;
123 
124  analyize(input, factor);
125 }
A Buffer for storing audio samples.
Definition: Buffer.h:60
void analyize(const Buffer &input, const float64 &factor)
Definition: Stretcher.cc:119
void Stretcher::analyize ( const Buffer input,
const Buffer factor 
)
protected

Definition at line 129 of file Stretcher.cc.

References Nsound::Buffer::cbegin(), frames_, Nsound::Buffer::getLength(), searchForBestMatch(), show_progress_, and window_length_.

130 {
131  // Always calculate time shift.
132 
133  Buffer::const_circular_iterator ifactor = factor.cbegin();
134 
135  uint32 tau = static_cast<uint32>(window_length_ / 2.0 / ::fabs(*ifactor));
136 
137  uint32 input_length = input.getLength();
138 
139  *frames_ = Buffer(256);
140 
141  uint32 i = 0;
142  while(i < input_length - window_length_)
143  {
144  *frames_ << i;
145  i += tau;
146 
147  ifactor += tau;
148 
149  tau = static_cast<uint32>(window_length_ / 2.0 / ::fabs(*ifactor));
150  }
151 
152  uint32 n_frames = frames_->getLength();
153 
154  uint32 mod_frames = n_frames / 100;
155 
156  if(n_frames < 100) mod_frames = n_frames;
157 
158  if(show_progress_)
159  {
160  #ifdef NSOUND_CUDA
161 
162  printf("Using CUDA\n");
163 
164  #else
165 
166  #ifdef NSOUND_OPENMP
167  #pragma omp parallel
168  {
169  if(0 == omp_get_thread_num())
170  {
171  printf("Using OpenMP with %d threads\n",
172  omp_get_num_threads());
173  fflush(stdout);
174  }
175  }
176  #endif
177 
178  #endif
179 
180  printf("Analyizing %3.0f%%", 0.0);
181  fflush(stdout);
182  }
183 
184  for(uint32 j = 0; j < n_frames-1; ++j)
185  {
186  if(show_progress_ && j % mod_frames == 0)
187  {
188  printf("\b\b\b\b");
189  printf("%3.0f%%", 100.0 * float64(j) / float64(n_frames));
190  fflush(stdout);
191  }
192 
193  (*frames_)[j] = searchForBestMatch(
194  input,
195  uint32((*frames_)[j]),
196  uint32((*frames_)[j+1]));
197  }
198 
199  if(show_progress_)
200  {
201  printf("\b\b\b\b");
202  printf("%3.0f%%\n", 100.0);
203  fflush(stdout);
204  }
205 }
unsigned int uint32
Definition: Nsound.h:153
uint32 searchForBestMatch(const Buffer &input, uint32 source_index, uint32 search_index) const
Definition: Stretcher.cc:209
double float64
Definition: Nsound.h:146
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
uint32 window_length_
Definition: Stretcher.h:120
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer * frames_
Definition: Stretcher.h:117
boolean show_progress_
Definition: Stretcher.h:122
A Buffer for storing audio samples.
Definition: Buffer.h:60
uint32 Stretcher::searchForBestMatch ( const Buffer input,
uint32  source_index,
uint32  search_index 
) const
protected

Definition at line 209 of file Stretcher.cc.

References Nsound::FFTransform::fft(), Nsound::Buffer::getSum(), max_delta_, Nsound::Buffer::subbuffer(), and window_length_.

Referenced by analyize().

213 {
214  // Nick's original score function
215  #if 1
216  Buffer b_source = input.subbuffer(source_index, window_length_);
217 
218  uint32 t = 0;
219  float64 min_score = 1.0e100;
220 
221  #ifdef NSOUND_OPENMP
222  #pragma omp parallel for shared(t, min_score)
223  #endif
224  for(uint32 i = 0; i < max_delta_; ++i)
225  {
226  Buffer b_test = input.subbuffer(search_index + i, window_length_)
227  - b_source;
228 
229  b_test *= b_test;
230 
231  float64 score = b_test.getSum();
232 
233  if(score < min_score)
234  {
235  min_score = score;
236  t = i;
237  }
238  }
239  #endif
240 
241  #if 0
242  // Maximizing fft phase information.
243 
244  FFTransform transform(100.0); // Sample rate here won't be used.
245 
246  Buffer b_source = input.subbuffer(source_index, window_length_);
247 
248  FFTChunkVector vec = transform.fft(b_source, window_length_);
249 
250  Buffer source_fft = vec[0].getPhase();
251 
252  uint32 t = 0;
253  float64 min_score = 1.0e100;
254  for(uint32 i = 0; i < max_delta_; ++i)
255  {
256  Buffer b_test = input.subbuffer(search_index + i, window_length_);
257 
258  FFTChunkVector vec = transform.fft(b_test, window_length_);
259 
260  Buffer test_fft = vec[0].getPhase();
261 
262  Buffer delta = source_fft - test_fft;
263 
264  delta *= delta;
265 
266  float64 score = delta.getSum();
267 
268  if(score < min_score)
269  {
270  min_score = score;
271  t = i;
272  }
273  }
274  #endif
275 
276  return t + search_index;
277 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 window_length_
Definition: Stretcher.h:120
A Class that performes the Fast Fouier Transfrom on a Buffer.
Definition: FFTransform.h:57
float64 getSum() const
Returns the sum of all samples.
Definition: Buffer.cc:1118
A Buffer for storing audio samples.
Definition: Buffer.h:60
std::vector< FFTChunk > FFTChunkVector
Definition: FFTChunk.h:119
Buffer Stretcher::overlapAdd ( const Buffer input) const
protected

Definition at line 281 of file Stretcher.cc.

References Nsound::Buffer::add(), Nsound::Generator::drawLine(), frames_, Nsound::Buffer::getLength(), Nsound::Buffer::getReverse(), sample_rate_, show_progress_, Nsound::Buffer::subbuffer(), window_, and window_length_.

Referenced by pitchShift(), and timeShift().

282 {
283  Sine sin(sample_rate_);
284 
285  float64 half_window_seconds = (window_length_ / 2) / sample_rate_;
286 
287  uint32 half_length = window_length_ / 2;
288 
289  Buffer window_start = sin.drawLine(half_window_seconds, 1.0, 1.0)
290  << window_->subbuffer(half_length, half_length);
291 
292  Buffer window_finish = window_start.getReverse();
293 
294  Buffer output = input.subbuffer(0, window_length_) * (*window_);
295 
296  uint32 n_frames = frames_->getLength();
297 
298  uint32 mod_frames = n_frames / 100;
299 
300  if(n_frames < 100) mod_frames = n_frames;
301 
302  if(show_progress_)
303  {
304  printf("Creating output %3.0f%%", 0.0);
305  fflush(stdout);
306  }
307 
308  for(uint32 i = 1; i < n_frames; ++i)
309  {
310  if(show_progress_ && i % mod_frames == 0)
311  {
312  printf("\b\b\b\b");
313  printf("%3.0f%%", 100.0 * float64(i) / float64(n_frames));
314  fflush(stdout);
315  }
316 
317  uint32 frame = uint32((*frames_)[i]);
318 
319  Buffer next_window = input.subbuffer(frame, window_length_)
320  * (*window_);
321 
322  // overlap & add
323  output.add(next_window, i * half_length);
324  }
325 
326  if(show_progress_)
327  {
328  printf("\b\b\b\b");
329  printf("%3.0f%%\n", 100.0);
330  fflush(stdout);
331  }
332 
333  return output;
334 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
Buffer * window_
Definition: Stretcher.h:119
double float64
Definition: Nsound.h:146
uint32 window_length_
Definition: Stretcher.h:120
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer * frames_
Definition: Stretcher.h:117
boolean show_progress_
Definition: Stretcher.h:122
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 sample_rate_
Definition: Stretcher.h:118
void add(const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
This method adds buffer to *this.
Definition: Buffer.cc:225
Buffer getReverse() const
Reverses the samples in a copy of this Buffer.
Definition: Buffer.h:1558
DOXME.
Definition: Sine.h:43

Member Data Documentation

Buffer* Nsound::Stretcher::frames_
protected

Definition at line 117 of file Stretcher.h.

Referenced by analyize(), operator=(), overlapAdd(), Stretcher(), and ~Stretcher().

float64 Nsound::Stretcher::sample_rate_
protected

Definition at line 118 of file Stretcher.h.

Referenced by operator=(), and overlapAdd().

Buffer* Nsound::Stretcher::window_
protected

Definition at line 119 of file Stretcher.h.

Referenced by operator=(), overlapAdd(), Stretcher(), and ~Stretcher().

uint32 Nsound::Stretcher::window_length_
protected

Definition at line 120 of file Stretcher.h.

Referenced by analyize(), operator=(), overlapAdd(), searchForBestMatch(), and Stretcher().

uint32 Nsound::Stretcher::max_delta_
protected

Definition at line 121 of file Stretcher.h.

Referenced by operator=(), searchForBestMatch(), and Stretcher().

boolean Nsound::Stretcher::show_progress_
protected

Definition at line 122 of file Stretcher.h.

Referenced by analyize(), operator=(), overlapAdd(), and showProgress().


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