Nsound  0.9.4
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
Nsound::Buffer Class Reference

A Buffer for storing audio samples. More...

#include <Nsound/Buffer.h>

Public Types

typedef Nsound::circular_iterator circular_iterator
 
typedef
Nsound::const_circular_iterator 
const_circular_iterator
 
typedef FloatVector::iterator iterator
 
typedef FloatVector::const_iterator const_iterator
 

Public Member Functions

 Buffer ()
 Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples. More...
 
 Buffer (uint32 chunk_size)
 
 Buffer (const FloatVector &list)
 Creates a Buffer and fills it with the contents of list. More...
 
 Buffer (const std::string &filename, uint32 chunk_size=4096)
 Creates an Buffer from a Wavefile, also specifies the chunk size. More...
 
 ~Buffer ()
 Destroys the Buffer. More...
 
 Buffer (const Buffer &rhs)
 Copy constructor, must be used in Python to copy Buffers. More...
 
 Buffer (Buffer &&move)
 
void abs ()
 Modifies the Buffer by making any negative value positive. More...
 
Buffer getAbs () const
 Modifies a copy of the Buffer by making any negative value positive. More...
 
void add (const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
 This method adds buffer to *this. More...
 
uint32 argmax () const
 Retruns the index of the maximum value in the Buffer. More...
 
uint32 argmin () const
 Retruns the index of the minimum value in the Buffer. More...
 
iterator begin ()
 Retruns the itreator at the start of the Buffer. More...
 
const_iterator begin () const
 Retruns the itreator at the start of the Buffer. More...
 
circular_iterator cbegin ()
 Retruns the itreator at the start of the Buffer. More...
 
const_circular_iterator cbegin () const
 Retruns the itreator at the start of the Buffer. More...
 
iterator end ()
 Retruns the itreator at the end of the Buffer. More...
 
const_iterator end () const
 Retruns the itreator at the end of the Buffer. More...
 
uint32 getNBytes () const
 Retruns the number of bytes held in the Buffer array. More...
 
void exp ()
 Each sample in the Buffer becomes the power e^x. More...
 
Buffer getExp () const
 Each sample in the Buffer becomes the power e^x. More...
 
void convolve (const Buffer &H)
 Convolves the Buffer with another Buffer. More...
 
Buffer getConvolve (const Buffer &H) const
 Convolves a copy of the Buffer with another Buffer. More...
 
void dB ()
 Modifies the Buffer so each sample is converted to dB, 20 * log10(sample). More...
 
Buffer getdB () const
 Returns the Buffer in dB. More...
 
void derivative (uint32 n)
 Calculates the nth derivative of the Buffer. More...
 
Buffer getDerivative (uint32 n) const
 Returns the nth derivative of the Buffer. More...
 
void downSample (uint32 n)
 Downsample this Buffer by a integral factor. N must be > 1. More...
 
Buffer getDownSample (uint32 n) const
 Returns a copy of this Buffer downsampled by a integral factor. N must be > 1. More...
 
Uint32Vector findPeaks (uint32 window_size=0, float64 min_height=0.0) const
 Find the peaks in the Buffer and return a vector of indicies. More...
 
uint32 getLength () const
 Returns the number of samples in the Buffer. More...
 
void limit (float64 min, float64 max)
 Limits the Buffer to min and max. More...
 
void limit (const Buffer &min, const Buffer &max)
 Limits the Buffer to min and max. More...
 
Buffer getLimit (float64 min, float64 max) const
 Returns a copy of the Buffer with min and max limits. More...
 
Buffer getLimit (const Buffer &min, const Buffer &max) const
 Returns a copy of the Buffer with min and max limits. More...
 
void log ()
 Sets each sample in the Buffer to the natural log. More...
 
Buffer getLog () const
 Returns the the Buffer where each sample is the natural log. More...
 
void log10 ()
 Sets each sample in the Buffer to log base 10. More...
 
Buffer getLog10 () const
 Returns the the Buffer where each sample is the natural log. More...
 
float64 getMax () const
 Returns the maximum sample value in the Buffer. More...
 
float64 getMaxMagnitude () const
 Returns the maximum magnitude value in the Buffer, i.e. max(abs(samples)). More...
 
float64 getMean () const
 Returns the mean sample value in the Buffer. More...
 
float64 getMin () const
 Returns the minimum sample value in the Buffer. More...
 
void mul (const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
 This method multiplies a Buffer to *this over a sub range. More...
 
void normalize ()
 Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0. More...
 
Buffer getNormalize () const
 Returns a copy of the Buffer normalized. More...
 
Buffer getSignalEnergy (uint32 window_size) const
 Returns the signal energy: E = 1 / N * sum(|x(i)|) over the window of N samples. More...
 
float64 getStd () const
 Returns the standard deviation of the Buffer. More...
 
float64 getSum () const
 Returns the sum of all samples. More...
 
void zNorm ()
 Normalized the Buffer using Z score normalizing. More...
 
Buffer getZNorm () const
 Returns a Z score normalized copy of the Buffer. More...
 
BufferSelection operator() (const BooleanVector &bv)
 Returns a BufferSelection object used for manipulation of a selected region of samples. More...
 
Bufferoperator= (const Buffer &rhs)
 The assignment operator. C++ only, for Python, use the copy constructor. More...
 
boolean operator== (const Buffer &rhs) const
 Tests of equality. More...
 
boolean operator!= (const Buffer &rhs) const
 Tests of inequality. More...
 
float64operator[] (uint32 index)
 Retruns a reference into the Buffer. More...
 
const float64operator[] (uint32 index) const
 Returns a const reference into the Buffer. More...
 
Bufferoperator<< (const AudioStream &rhs)
 Concatenates Buffers and AudioStreams together. More...
 
Bufferoperator<< (const Buffer &rhs)
 Concatenates Buffers together. More...
 
Bufferoperator<< (float64 d)
 Concatenates samples to the Buffer. More...
 
Bufferoperator+= (const Buffer &rhs)
 Adds each sample from the right hand side (rhs) Buffer with this Buffer. More...
 
Bufferoperator-= (const Buffer &rhs)
 Subracts each sample from the right hand side (rhs) Buffer from this Buffer. More...
 
Bufferoperator*= (const Buffer &rhs)
 Multiplies each sample from the right hand side (rhs) with this Buffer. More...
 
Bufferoperator/= (const Buffer &rhs)
 Divides each sample in this Buffer with the right hand side (rhs) Buffer. More...
 
Bufferoperator^= (const Buffer &powers)
 Each sample in the Buffer becomes the power x^n. More...
 
Bufferoperator+= (float64 d)
 Adds the sample d to each sample in this Buffer. More...
 
Bufferoperator-= (float64 d)
 Subtracts the sample d from each sample in this Buffer. More...
 
Bufferoperator*= (float64 d)
 Multiplies the sample d with each sample in this Buffer. More...
 
Bufferoperator/= (float64 d)
 Divides each sample in this Buffer by d. More...
 
Bufferoperator^= (float64 power)
 Each sample in the Buffer becomes the power x^n. More...
 
BooleanVector operator> (float64 rhs)
 Creates a BooleanVector where each value is true iff Buffer[n] > rhs. More...
 
BooleanVector operator>= (float64 rhs)
 Creates a BooleanVector where each value is true iff Buffer[n] >= rhs. More...
 
BooleanVector operator< (float64 rhs)
 Creates a BooleanVector where each value is true iff Buffer[n] < rhs. More...
 
BooleanVector operator<= (float64 rhs)
 Creates a BooleanVector where each value is true iff Buffer[n] <= rhs. More...
 
BooleanVector operator== (float64 rhs)
 Creates a BooleanVector where each value is true iff Buffer[n] == rhs. More...
 
BooleanVector operator!= (float64 rhs)
 Creates a BooleanVector where each value is true iff Buffer[n] != rhs. More...
 
void plot (const std::string &title="Buffer") const
 Requires matplotlib. Creates a plot of this Buffer. More...
 
const float64getPointer () const
 Returns the raw point to the first sample in the Buffer. More...
 
float64getPointer ()
 Requires matplotlib. Creates a plot of this Buffer. More...
 
void preallocate (uint32 n)
 Preallocates memory to hold an additional n samples. More...
 
void readWavefile (const char *filename)
 Sets this Buffer to the contents of the wavefile on the disk. More...
 
void resample (float64 factor)
 Resamples this Buffer using discrete-time resampling. More...
 
void resample (const Buffer &factor)
 Resamples this Buffer using discrete-time resampling. More...
 
Buffer getResample (float64 factor, const uint32 N=10, float64 beta=5.0) const
 Resamples a copy of this Buffer using discrete-time resampling. More...
 
Buffer getResample (const Buffer &factor, const uint32 N=10, float64 beta=5.0) const
 Resamples a copy of this Buffer using discrete-time resampling. More...
 
Buffer getResample (const uint32 L, const uint32 M, const uint32 N=10, float64 beta=5.0) const
 Resamples a copy of this Buffer using discrete-time resampling. More...
 
void reverse ()
 Reverses the samples in this Buffer. More...
 
Buffer getReverse () const
 Reverses the samples in a copy of this Buffer. More...
 
void round ()
 Rounds the samples in this Buffer to the nearest integer value. More...
 
Buffer getRound () const
 Rounds the samples in a copy of this Buffer to the nearest integer value. More...
 
BufferSelection select (const uint32 start_index, const uint32 stop_index)
 Returns a BufferSelection for the range of indicies. More...
 
std::ostream & write (std::ostream &out) const
 Serializes the Buffer to output stream, no endian checks. More...
 
std::string write () const
 
std::istream & read (std::istream &stream_in)
 Constructs a Buffer from seralized data in the inputstream. More...
 
void read (const std::string &string_in)
 
void smooth (uint32 n_passes, uint32 n_samples_per_average)
 Applies a moving average filter to smooth this Buffer. More...
 
Buffer getSmooth (uint32 n_passes, uint32 n_samples_per_average) const
 Applies a moving average filter to smooth a copy of this Buffer. More...
 
void speedUp (float64 step_size)
 Resamples this Buffer by the step_size, no interpolation. More...
 
Buffer getSpeedUp (float64 step_size) const
 Resamples a copy of this Buffer by the step_size, no interpolation. More...
 
void speedUp (const Buffer &step_size)
 Resamples this Buffer on a sample by sample basis, no interpolation. More...
 
Buffer getSpeedUp (const Buffer &step_size) const
 Resamples a copy of this Buffer on a sample by sample basis, no interpolation. More...
 
void sqrt ()
 Takes the square root of each sample in this Buffer. More...
 
Buffer getSqrt () const
 Taks the square root of each sample in a copy of this Buffer. More...
 
Buffer subbuffer (uint32 start_index, uint32 n_samples=0) const
 Slice the Buffer. More...
 
void upSample (uint32 n)
 Upsample this Buffer by a integral factor. N must be > 1. More...
 
Buffer getUpSample (uint32 n) const
 Upsample a copy of this Buffer by a integral factor. N must be > 1. More...
 
void writeWavefile (const char *filename) const
 Writes the Buffer to a Wavefile. More...
 
void _set_at_index (int32 index, float64 x)
 SWIG helper function function to shadow. More...
 
void _swig_shadow ()
 SWIG helper function function to shadow. More...
 

Static Public Member Functions

static Buffer ones (const uint32 n_samples)
 Returns a Buffer full of ones of length n_samples. More...
 
static Buffer rand (const uint32 n_samples)
 Returns a Buffer full of random values of length n_samples. More...
 
static Buffer zeros (const uint32 n_samples)
 Returns a Buffer full of zeros of length n_samples. More...
 

Protected Member Functions

Buffer _get_resample (const uint32 L, const uint32 M, const uint32 N, float64 beta) const
 

Protected Attributes

FloatVector data_
 

Static Protected Attributes

static const uint32 bytes_per_sample_ = sizeof(float64)
 

Friends

std::ostream & operator<< (std::ostream &out, const Buffer &rhs)
 Sends the contents of the Buffer to the output stream. More...
 

Detailed Description

A Buffer for storing audio samples.

This is the workhourse of Nsound. This class dynamically allocates memory and frees the user of the class from worrying about buffer sizes. This class does not have a sample rate, samples are indexed by an integer index.

Definition at line 60 of file Buffer.h.

Member Typedef Documentation

Definition at line 66 of file Buffer.h.

Definition at line 67 of file Buffer.h.

typedef FloatVector::iterator Nsound::Buffer::iterator

Definition at line 69 of file Buffer.h.

typedef FloatVector::const_iterator Nsound::Buffer::const_iterator

Definition at line 70 of file Buffer.h.

Constructor & Destructor Documentation

Nsound::Buffer::Buffer ( )

Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples.

Parameters
chunk_sizethe number of samples to preallocate.
Returns
void
Example:
// C++
Buffer b(1024);
// Python
b = Buffer(1024)

Definition at line 62 of file Buffer.cc.

References data_.

63  :
64  data_()
65 {
66  data_.reserve(4096);
67 }
FloatVector data_
Definition: Buffer.h:1874
Nsound::Buffer::Buffer ( uint32  chunk_size)
explicit

Definition at line 70 of file Buffer.cc.

References data_.

71  :
72  data_()
73 {
74  data_.reserve(chunk_size);
75 }
FloatVector data_
Definition: Buffer.h:1874
Nsound::Buffer::Buffer ( const FloatVector list)
explicit

Creates a Buffer and fills it with the contents of list.

Parameters
lista FloatVector holding samples
Returns
Buffer
Example:
// C++
fv.push_back(1.0);
fv.push_back(2.0);
fv.push_back(3.0);
fv.push_back(4.0);
fv.push_back(5.0);
Buffer b(fv);
// Python
b = Buffer([1,2,3,4,5])

Definition at line 78 of file Buffer.cc.

79  :
80  data_(list) // Copies contents
81 {
82 }
FloatVector data_
Definition: Buffer.h:1874
Nsound::Buffer::Buffer ( const std::string &  filename,
uint32  chunk_size = 4096 
)

Creates an Buffer from a Wavefile, also specifies the chunk size.

Parameters
filenamea string with a wave filename to read.
chunk_sizeoptional, sets the chunk size for the Buffer.
Example:
// C++
Buffer b("california.wav");
// Python
b = Buffer("california.wav")

Definition at line 85 of file Buffer.cc.

References data_.

86  :
87  data_()
88 {
89  data_.reserve(chunk_size);
90 
91  // This function is defined in Wavefile.h.
92  *this << filename.c_str();
93 }
FloatVector data_
Definition: Buffer.h:1874
Nsound::Buffer::~Buffer ( )

Destroys the Buffer.

Definition at line 112 of file Buffer.cc.

113 {
114 }
Nsound::Buffer::Buffer ( const Buffer rhs)

Copy constructor, must be used in Python to copy Buffers.

Example:
// C++
Buffer b1("california.wav");
Buffer b2(b1);
// Python
b1 = Buffer("california.wav")
b2 = Buffer(b1) // Copies memory, Python does not have
// an operator=

Definition at line 96 of file Buffer.cc.

97  :
98  data_(copy.data_)
99 {
100 }
FloatVector data_
Definition: Buffer.h:1874
Nsound::Buffer::Buffer ( Buffer &&  move)

Definition at line 103 of file Buffer.cc.

References data_.

104  :
105  data_()
106 {
107  std::swap(data_, move.data_);
108 }
FloatVector data_
Definition: Buffer.h:1874

Member Function Documentation

void Nsound::Buffer::abs ( )

Modifies the Buffer by making any negative value positive.

Example:
// C++
Buffer b1("california.wav");
b1.abs();
// Python
b1 = Buffer("california.wav")
b1.abs()

Definition at line 119 of file Buffer.cc.

References data_.

Referenced by Buffer_UnitTest(), DelayLine_UnitTest(), FFTransform_UnitTest(), FilterCombLowPassFeedback_UnitTest(), FilterLeastSquaresFIR_UnitTest(), FilterParametricEqualizer_UnitTest(), Generator_UnitTest(), getAbs(), main(), Sine_UnitTest(), and Triangle_UnitTest().

120 {
121  for(uint32 i = 0; i < data_.size(); ++i)
122  {
123  data_[i] = ::fabs(data_[i]);
124  }
125 }
unsigned int uint32
Definition: Nsound.h:153
FloatVector data_
Definition: Buffer.h:1874
Buffer Nsound::Buffer::getAbs ( ) const
inline

Modifies a copy of the Buffer by making any negative value positive.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getAbs();
// Python
b1 = Buffer("california.wav")
b2 = b1.getAbs();

Definition at line 174 of file Buffer.h.

References abs().

Referenced by Buffer_UnitTest(), BufferResample_UnitTest(), FFTransform_UnitTest(), FilterLeastSquaresFIR_UnitTest(), and main().

175  { Buffer temp(*this); temp.abs(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::add ( const Buffer buffer,
uint32  offset = 0,
uint32  n_samples = 0 
)

This method adds buffer to *this.

If the length of 'buffer' is longer than *this, *this will be extended. If n_samples is 0, the whole length of buffer will get added, otherwise, only n_samples will get added.

For example:

a = sample in *this
b = sample in buffer
c = a + b
this = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa]
     + |--offset--|[bbbbbbbbb buffer bbbbbbbbbbbbbbbbbb]
     = [aaaaaaaaaaacccccccccccccccccbbbbbbbbbbbbbbbbbbb]

Another exampple:

a = sample in *this
b = sample in buffer
c = a + b
this = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa]
     + |--offset--|[bbbbbbbbb buffer bbbbbbbbbbbbbbbbbb]
                   |--------n_samples------|
     = [aaaaaaaaaaaccccccccccccccccccbbbbbbb]
Parameters
offset- The index into this Buffer to start adding
buffer- The Buffer to add data from
n_samples- The number of samples from buffer to add
Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
Buffer b1.add(0,b2);
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1.add(0,b2);

Definition at line 225 of file Buffer.cc.

References data_, and getLength().

Referenced by Nsound::Granulator::generate(), main(), Nsound::Stretcher::overlapAdd(), and testBufferAdd().

228 {
229  uint32 a_length = getLength();
230  uint32 b_length = b.getLength();
231 
232  // Case 1: n_samples == 0
233  // && a_length < offset
234  //
235  // [aaaaaaa]
236  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
237  // result = |aaaaaaaa---bbbbbbbbbbbbbbbbbbbbbbb|
238  if(n_samples == 0 && a_length < offset)
239  {
240  // Append zeros
241  for(uint32 i = a_length; i < offset; ++i)
242  {
243  *this << 0.0;
244  }
245 
246  // Append b
247  *this << b;
248  }
249 
250  // Case 2: n_samples != 0
251  // && a_length < offset
252  // && n_samples <= b_length
253  //
254  // [aaaaaaa]
255  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
256  // |---n_samples---|
257  // result = |aaaaaaaa---bbbbbbbbbbbbbbbb|
258  else if(n_samples != 0
259  && a_length < offset
260  && n_samples <= b_length)
261  {
262  // Append zeros
263  for(uint32 i = a_length; i < offset; ++i)
264  {
265  *this << 0.0;
266  }
267 
268  for(uint32 i = 0; i < n_samples; ++i)
269  {
270  *this << b[i];
271  }
272  }
273 
274  // Case 3: n_samples != 0
275  // && a_length < offset
276  // && n_samples > b_length
277  //
278  // [aaaaaaa]
279  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
280  // |---n_samples----------------|
281  // result = |aaaaaaaa---bbbbbbbbbbbbbbbbbbbbbbbb-----|
282  else if(n_samples != 0
283  && a_length < offset
284  && n_samples > b_length)
285  {
286  // Append zeros
287  for(uint32 i = a_length; i < offset; ++i)
288  {
289  *this << 0.0;
290  }
291 
292  // Append b samples
293 
294  *this << b;
295 
296  // Append zeros
297 
298  for(uint32 i = b_length; i < n_samples; ++i)
299  {
300  *this << 0.0;
301  }
302  }
303 
304  // Case 4: n_samples == 0
305  // && a_length >= offset
306  // && a_length < offset + b_length
307  //
308  // [aaaaaaaaaaaaaaaaaaaaaaaaaaaa]
309  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
310  // result = |aaaaaaaaaaaccccccccccccccccccbbbbb|
311  else if(n_samples == 0
312  && a_length >= offset
313  && a_length < offset + b_length)
314  {
315  uint32 b_index = 0;
316  for(uint32 i = offset; i < a_length; ++i)
317  {
318  data_[i] += b[b_index++];
319  }
320 
321  for(uint32 i = b_index; i < b_length; ++i)
322  {
323  *this << b[i];
324  }
325  }
326 
327  // Case 5: n_samples != 0
328  // && a_length >= offset
329  // && n_samples <= b_length
330  // && a_length <= offset + n_samples
331  //
332  // [aaaaaaaaaaaaaaaaaaaa]
333  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
334  // |----n_samples----|
335  // result = |aaaaaaaaaacccccccccccbbbbbbb|
336  else if(n_samples != 0
337  && a_length >= offset
338  && a_length <= offset + n_samples
339  && n_samples <= b_length)
340  {
341  uint32 b_index = 0;
342  for(uint32 i = offset; i < a_length; ++i)
343  {
344  data_[i] += b[b_index++];
345  }
346 
347  for(uint32 i = b_index; i < n_samples; ++i)
348  {
349  *this << b[i];
350  }
351  }
352  // Case 6: n_samples != 0
353  // && a_length >= offset
354  // && n_samples < b_length
355  // && a_length > offset + n_samples
356  //
357  // [aaaaaaaaaaaaaaaaaaaaaaaaaaaa]
358  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
359  // |--n_samples--|
360  // result = |aaaaaaaaaacccccccccccccccaaa|
361  else if(n_samples != 0
362  && a_length >= offset
363  && a_length > offset + n_samples
364  && n_samples < b_length)
365  {
366  uint32 b_index = 0;
367  for(uint32 i = offset; i < offset + n_samples; ++i)
368  {
369  data_[i] += b[b_index++];
370  }
371  }
372 
373  // Case 7: a_length >= offset
374  // && n_samples > b_length
375  // && a_length <= offset + b_length
376  //
377  // [aaaaaaaaaaaaaaaaaaaa]
378  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
379  // |----n_samples---------------|
380  // result = |aaaaaaaaaaaccccccccccbbbbbbbbbbbbbb----|
381  else if(a_length >= offset
382  && a_length <= offset + b_length
383  && n_samples > b_length)
384  {
385  uint32 b_index = 0;
386  for(uint32 i = offset; i < a_length; ++i)
387  {
388  data_[i] += b[b_index++];
389  }
390 
391  for(uint32 i = b_index; i < b_length; ++i)
392  {
393  *this << b[i];
394  }
395 
396  for(uint32 i = b_length; i < n_samples; ++i)
397  {
398  *this << 0.0;
399  }
400  }
401  // Case 8: n_samples == 0
402  // && a_length >= offset + b_length
403  //
404  // [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
405  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbbb]
406  // result = |aaaaaaaaaaaccccccccccccccccccccccccaaaaaa|
407  else if(n_samples == 0
408  && a_length >= offset + b_length)
409  {
410  uint32 b_index = 0;
411  for(uint32 i = offset; i < offset + b_length; ++i)
412  {
413  data_[i] += b[b_index++];
414  }
415  }
416 
417  // Case 9: a_length >= offset + n_samples
418  // && n_samples >= b_length
419  //
420  // result = [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
421  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbb]
422  // |----n_samples------------|
423  // = |aaaaaaaaaaaacccccccccccccccccccccaaaaaaaa|
424  else if( a_length >= offset + n_samples
425  && n_samples >= b_length)
426  {
427  uint32 b_index = 0;
428  for(uint32 i = offset; i < offset + b_length; ++i)
429  {
430  data_[i] += b[b_index++];
431  }
432  }
433  // Case 10: n_samples != 0
434  // && a_length > offset + b_length
435  // && a_length < offset + n_samples
436  //
437  // result = [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
438  // + |--offset--|[bbbbbbbbbbbbbbbbbbbbb]
439  // |----n_samples----------------------|
440  // = |aaaaaaaaaaaccccccccccccccccccccccaaaaaaa------|
441  else if(n_samples != 0
442  && a_length > offset + b_length
443  && a_length < offset + n_samples)
444  {
445  uint32 b_index = 0;
446  for(uint32 i = offset; i < offset + b_length; ++i)
447  {
448  data_[i] += b[b_index++];
449  }
450 
451  for(uint32 i = a_length; i < offset + n_samples; ++i)
452  {
453  *this << 0.0;
454  }
455  }
456 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
uint32 Nsound::Buffer::argmax ( ) const

Retruns the index of the maximum value in the Buffer.

Example:
// C++
Buffer b1("california.wav");
uint32 max_index = b1.argmax();
// Python
b1 = Buffer("california.wav")
max_index = b1.argmax()

Definition at line 460 of file Buffer.cc.

References begin(), and end().

Referenced by my_main().

461 {
462  uint32 index = 0;
463 
464  Buffer::const_iterator itor = begin();
465  Buffer::const_iterator tend = end();
466 
467  float64 max = *itor;
468 
469  uint32 i = 0;
470  while(itor != tend)
471  {
472  if(*itor > max)
473  {
474  max = *itor;
475  index = i;
476  }
477  ++i;
478  ++itor;
479  }
480  return index;
481 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
uint32 Nsound::Buffer::argmin ( ) const

Retruns the index of the minimum value in the Buffer.

Example:
// C++
Buffer b1("california.wav");
uint32 min_index = b1.argmin();
// Python
b1 = Buffer("california.wav")
min_index = b1.argmin()

Definition at line 485 of file Buffer.cc.

References begin(), and end().

486 {
487  uint32 index = 0;
488 
489  Buffer::const_iterator itor = begin();
490  Buffer::const_iterator tend = end();
491 
492  float64 min = *itor;
493 
494  uint32 i = 0;
495  while(itor != tend)
496  {
497  if(*itor < min)
498  {
499  min = *itor;
500  index = i;
501  }
502  ++i;
503  ++itor;
504  }
505  return index;
506 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
iterator Nsound::Buffer::begin ( )
inline

Retruns the itreator at the start of the Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer::iterator itor = b1.begin();
// Python, you can already iterate over the samples
b1 = Buffer("california.wav");
for sample in b1:
// do something

Definition at line 285 of file Buffer.h.

References data_.

Referenced by _get_resample(), argmax(), argmin(), Nsound::FilterHighPassFIR::filter(), Nsound::FilterLowPassFIR::filter(), Nsound::Filter::filter(), Nsound::FilterStageIIR::filter(), Nsound::FilterBandRejectFIR::filter(), Nsound::FFTransform::ifft(), limit(), operator!=(), operator<(), operator<=(), operator==(), operator>(), operator>=(), round(), Nsound::EnvelopeAdsr::shape(), smooth(), and sqrt().

286  { return data_.begin(); }
FloatVector data_
Definition: Buffer.h:1874
const_iterator Nsound::Buffer::begin ( ) const
inline

Retruns the itreator at the start of the Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer::cons_iterator itor = b1.begin();
// Python, you can iterate over the samples
b1 = Buffer("california.wav");
for sample in b1:
// do something

Definition at line 303 of file Buffer.h.

References data_.

304  { return data_.begin(); }
FloatVector data_
Definition: Buffer.h:1874
circular_iterator Nsound::Buffer::cbegin ( )
inline
const_circular_iterator Nsound::Buffer::cbegin ( ) const
inline

Retruns the itreator at the start of the Buffer.

Example:
// C++
const Buffer b1("california.wav");
Buffer::circular_iterator ctor = b1.begin();
// Python, currently not supported

Definition at line 333 of file Buffer.h.

References data_.

334  { return const_circular_iterator(data_); }
Nsound::const_circular_iterator const_circular_iterator
Definition: Buffer.h:67
FloatVector data_
Definition: Buffer.h:1874
iterator Nsound::Buffer::end ( )
inline

Retruns the itreator at the end of the Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer::iterator end = b1.end();
// Python, currently not supported

Definition at line 348 of file Buffer.h.

References data_.

Referenced by _get_resample(), argmax(), argmin(), Nsound::FilterHighPassFIR::filter(), Nsound::FilterLowPassFIR::filter(), Nsound::Filter::filter(), Nsound::FilterStageIIR::filter(), Nsound::FilterBandRejectFIR::filter(), limit(), operator!=(), operator<(), operator<<(), operator<=(), operator==(), operator>(), operator>=(), round(), Nsound::EnvelopeAdsr::shape(), smooth(), and sqrt().

349  { return data_.end(); }
FloatVector data_
Definition: Buffer.h:1874
const_iterator Nsound::Buffer::end ( ) const
inline

Retruns the itreator at the end of the Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer::iterator end = b1.end();
// Python, currently not supported

Definition at line 363 of file Buffer.h.

References data_.

364  { return data_.end(); }
FloatVector data_
Definition: Buffer.h:1874
uint32 Nsound::Buffer::getNBytes ( ) const
inline

Retruns the number of bytes held in the Buffer array.

Example:
// C++
Buffer b1("california.wav");
uint32 n_bytes = b1.getNBytes();
// Python
b1 = Buffer("california.wav")
n_bytes = b1.getNBytes()

Definition at line 380 of file Buffer.h.

References bytes_per_sample_, and data_.

381  { return static_cast<uint32>(bytes_per_sample_ * data_.size()); }
unsigned int uint32
Definition: Nsound.h:153
static const uint32 bytes_per_sample_
Definition: Buffer.h:1876
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::exp ( )

Each sample in the Buffer becomes the power e^x.

Each sample in the Buffer becomes the power e^x, where x is the Buffer sample.

Example:
// C++
Buffer b1("california.wav");
b1.exp();
// Python
b1 = Buffer("california.wav")
b1.exp()

Definition at line 510 of file Buffer.cc.

References data_, and getLength().

Referenced by Nsound::Generator::drawDecay(), Nsound::Generator::drawGaussian(), and getExp().

511 {
512  for(uint32 i = 0; i < getLength(); ++i)
513  {
514  data_[i] = ::exp(data_[i]);
515  }
516 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void exp()
Each sample in the Buffer becomes the power e^x.
Definition: Buffer.cc:510
FloatVector data_
Definition: Buffer.h:1874
Buffer Nsound::Buffer::getExp ( ) const
inline

Each sample in the Buffer becomes the power e^x.

Each sample in the Buffer becomes the power e^x, where x is the Buffer sample.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getExp();
// Python
b1 = Buffer("california.wav")
b2 = b1.getExp()

Definition at line 417 of file Buffer.h.

References exp().

418  { Buffer temp(*this); temp.exp(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::convolve ( const Buffer H)

Convolves the Buffer with another Buffer.

Parameters
Hanother Buffer to convole with.
Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
b1.convolve(b2);
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1.convolve(b2)

Definition at line 520 of file Buffer.cc.

References getConvolve().

521 {
522  *this = getConvolve(H);
523 }
Buffer getConvolve(const Buffer &H) const
Convolves a copy of the Buffer with another Buffer.
Definition: Buffer.cc:527
Buffer Nsound::Buffer::getConvolve ( const Buffer H) const

Convolves a copy of the Buffer with another Buffer.

Parameters
Hanother Buffer to convole with.
Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
Buffer b3 = b1.getConvolve(b2);
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b3 = b1.getConvolve(b2)

Definition at line 527 of file Buffer.cc.

References getLength().

Referenced by convolve().

528 {
529  // Alocate the output Buffer will all zeros.
530  Buffer y = *this * 0.0;
531 
532  // Pad y with H.getLength() samples.
533  for(uint32 i = 0; i < H.getLength(); ++i)
534  {
535  y << 0.0;
536  }
537 
538 //~ uint32 N = getLength() / 1000;
539 
540 //~ printf("%6.2f", 0.0);
541 
542  // For each sample in this Buffer.
543  for(uint32 i = 0; i < getLength(); ++i)
544  {
545 //~ // DEBUG
546 //~ if(i % N == 0)
547 //~ {
548 //~ float64 p_done = 100.0 * i / float64(getLength());
549 //~ printf("\b\b\b\b\b\b%6.2f", p_done);
550 //~ fflush(stdout);
551 //~ }
552 
553  // For each sample in H.
554  for(uint32 j = 0; j < H.getLength(); ++j)
555  {
556  y[i + j] += (*this)[i] * H[j];
557  }
558  }
559 
560 //~ printf("\b\b\b\b\b\b%6.2f\n", 100.0);
561 
562  return y;
563 }
unsigned int uint32
Definition: Nsound.h:153
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void Nsound::Buffer::dB ( )

Modifies the Buffer so each sample is converted to dB, 20 * log10(sample).

Example:
// C++
Buffer b1("california.wav");
b1.dB();
// Python
b1 = Buffer("california.wav")
b1.dB()

Definition at line 567 of file Buffer.cc.

References log10().

Referenced by getdB(), and Nsound::FFTChunk::plot().

568 {
569 //~ FloatVector::iterator itor = data_.begin();
570 //~ FloatVector::iterator end = data_.end();
571 
572 //~ while(itor != end)
573 //~ {
574 //~ if(*itor == 0.0)
575 //~ {
576 //~ *itor = -150.0;
577 //~ }
578 //~ else
579 //~ {
580 //~ *itor = 20.0 * ::log10(*itor);
581 //~ }
582 
583 //~ ++itor;
584 //~ }
585 
586  log10();
587  *this *= 20.0;
588 }
void log10()
Sets each sample in the Buffer to log base 10.
Definition: Buffer.cc:936
Buffer Nsound::Buffer::getdB ( ) const
inline

Returns the Buffer in dB.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getdB();
// Python
b1 = Buffer("california.wav")
b2 = b1.getdB()

Definition at line 487 of file Buffer.h.

References dB().

Referenced by FilterLeastSquaresFIR_UnitTest(), and Nsound::Filter::plot().

488  { Buffer temp(*this); temp.dB(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::derivative ( uint32  n)

Calculates the nth derivative of the Buffer.

Example:
// C++
Buffer b1("california.wav");
b1.derivative(1);
// Python
b1 = Buffer("california.wav")

Definition at line 592 of file Buffer.cc.

References data_.

Referenced by getDerivative().

593 {
594  for(uint32 pass = 0; pass < n; ++pass)
595  {
596  for(uint32 i = 1; i < data_.size(); ++i)
597  {
598  data_[i - 1] = (data_[i] - data_[i - 1]);
599  }
600  }
601 }
unsigned int uint32
Definition: Nsound.h:153
FloatVector data_
Definition: Buffer.h:1874
Buffer Nsound::Buffer::getDerivative ( uint32  n) const
inline

Returns the nth derivative of the Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getDerivative(1);
// Python
b1 = Buffer("california.wav")
b2 = b1.getDerivative(1)

Definition at line 518 of file Buffer.h.

References derivative().

Referenced by Nsound::FilterLeastSquaresFIR::makeKernel().

519  { Buffer temp(*this); temp.derivative(n); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::downSample ( uint32  n)

Downsample this Buffer by a integral factor. N must be > 1.

Example:
// C++
Buffer b1("california.wav");
b1.downSample(2);
// Python
b1 = Buffer("california.wav")

Definition at line 605 of file Buffer.cc.

References getDownSample().

606 {
607  *this = getDownSample(n);
608 }
Buffer getDownSample(uint32 n) const
Returns a copy of this Buffer downsampled by a integral factor. N must be > 1.
Definition: Buffer.cc:615
Buffer Nsound::Buffer::getDownSample ( uint32  n) const

Returns a copy of this Buffer downsampled by a integral factor. N must be > 1.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getDownSample(2);
// Python
b1 = Buffer("california.wav")
b2 = b1.getDownSample(2)

Definition at line 615 of file Buffer.cc.

References getResample(), and M_ASSERT_VALUE.

Referenced by downSample().

616 {
617  M_ASSERT_VALUE(M, >, 0);
618  return getResample(static_cast<uint32>(1),M);
619 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
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
Uint32Vector Nsound::Buffer::findPeaks ( uint32  window_size = 0,
float64  min_height = 0.0 
) const

Find the peaks in the Buffer and return a vector of indicies.

Example:
// C++
Buffer b1("california.wav");
Uint32Vector v = b1.findPeaks(1000, 0.0);
// Python
b1 = Buffer("california.wav")
peaks = b1.findPeaks(1000, 0.0)

Definition at line 623 of file Buffer.cc.

References data_, and getLength().

626 {
627  Uint32Vector peaks;
628  peaks.reserve(128);
629 
630  uint32 h_window_size = window_size / 2 + 1; // half window size
631 
632  uint32 n_samples = getLength();
633 
634  for(uint32 i = 1; i < n_samples - 1; ++i)
635  {
636  boolean is_peak = false;
637 
638  float64 sample = data_[i];
639 
640  if(window_size <= 2)
641  {
642  if( sample > data_[i - 1] &&
643  sample > data_[i + 1])
644  {
645  is_peak = true;
646  }
647  }
648  else
649  {
650  // Assume it's a peak.
651 
652  is_peak = true;
653 
654  for(uint32 j = 1; j < h_window_size; ++j)
655  {
656  if(j < i)
657  {
658  if(data_[i - j] > sample)
659  {
660  is_peak = false;
661  break;
662  }
663  }
664 
665  if(i + j < n_samples)
666  {
667  if(data_[i + j] > sample)
668  {
669  is_peak = false;
670  break;
671  }
672  }
673  }
674  }
675 
676  if(is_peak && sample > min_height)
677  {
678  peaks.push_back(i);
679  }
680  }
681 
682  return peaks;
683 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
std::vector< uint32 > Uint32Vector
FloatVector data_
Definition: Buffer.h:1874
uint32 Nsound::Buffer::getLength ( ) const
inline

Returns the number of samples in the Buffer.

Example:
// C++
Buffer b1("california.wav");
uint32 length = b1.getLength();
// Python
b1 = Buffer("california.wav")
length = b1.getLength()

Definition at line 587 of file Buffer.h.

References data_.

Referenced by _get_resample(), _set_at_index(), add(), Nsound::Stretcher::analyize(), Buffer_UnitTest(), BufferResample_UnitTest(), Nsound::Spectrogram::computeMagnitude(), cosinewindow(), Nsound::Generator::ctor(), Nsound::FilterIIR::designKernel(), Nsound::Generator::drawWindowKaiser(), exp(), Nsound::FFTransform::fft(), FFTransform_UnitTest(), Nsound::FilterDC::filter(), Nsound::Vocoder::filter(), Nsound::FilterLowPassFIR::filter(), Nsound::Filter::filter(), Nsound::FilterSlinky::filter(), Nsound::ReverberationRoom::filter(), Nsound::FilterBandPassVocoder::filter(), Nsound::FilterFlanger::filter(), Nsound::FilterStageIIR::filter(), Nsound::FilterBandRejectFIR::filter(), Nsound::FilterBandRejectIIR::filter(), Nsound::FilterParametricEqualizer::filter(), findPeaks(), Nsound::Granulator::generate(), Nsound::Generator::Generator(), getConvolve(), Nsound::FFTChunk::getFrequencyAxis(), Nsound::FFTChunk::getImaginary(), Nsound::FFTChunk::getMagnitude(), getMean(), Nsound::BufferWindowSearch::getNextWindow(), Nsound::FFTChunk::getPhase(), Nsound::Filter::getPhaseResponse(), Nsound::FFTChunk::getReal(), getResample(), Nsound::FilterIIR::getRMS(), Nsound::BufferWindowSearch::getSamplesLeft(), getSignalEnergy(), getStd(), Nsound::Granulator::Granulator(), Nsound::FFTransform::ifft(), Nsound::Plotter::imagesc(), log(), log10(), main(), Nsound::FilterLeastSquaresFIR::makeKernel(), mul(), my_main(), operator*=(), operator+=(), operator-=(), Nsound::operator/(), operator/=(), operator<<(), Nsound::operator^(), operator^=(), Nsound::Stretcher::overlapAdd(), Nsound::AudioStream::pan(), Nsound::Clarinet::play(), Nsound::FluteSlide::play(), play_int(), Nsound::FFTChunk::plot(), Nsound::Plotter::plot(), Nsound::CircularBuffer::read(), reverse(), Nsound::FilterIIR::savePlot(), select(), Nsound::Kernel::setA(), Nsound::Kernel::setB(), Nsound::FilterLeastSquaresFIR::setKernel(), Nsound::EnvelopeAdsr::shape(), Nsound::Spectrogram::Spectrogram(), speedUp(), Nsound::Stretcher::Stretcher(), Nsound::Mesh2D::strike(), subbuffer(), testAutioStream(), testBufferAdd(), Nsound::FFTChunk::toCartesian(), Nsound::FFTChunk::toPolar(), Nsound::Triangle::Triangle(), Nsound::Vocoder::Vocoder(), Nsound::CircularBuffer::write(), Nsound::Wavefile::write(), write(), and zNorm().

588  { return static_cast<uint32>(data_.size()); }
unsigned int uint32
Definition: Nsound.h:153
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::limit ( float64  min,
float64  max 
)

Limits the Buffer to min and max.

Example:
// C++
Buffer b1("california.wav");
b1.limit(-1.0, 1.0);
// alternate expressions
b1(b1 > 1.0) = 1.0
b1(b1 < -1.0) = -1.0
# Python
b1 = Buffer("california.wav")
b1.limit(-1.0, 1.0);
# alternalte expressions
b1(b1 > 1.0).set(1.0)
b1(b1 < -1.0).set(-1.0)

Definition at line 876 of file Buffer.cc.

References begin(), and end().

Referenced by getLimit(), and Nsound::DrumBD01::play().

877 {
878  Buffer::iterator itor = this->begin();
879  Buffer::iterator end = this->end();
880 
881  while(itor != end)
882  {
883  float64 s = *itor;
884 
885  if(s > max) s = max;
886  if(s < min) s = min;
887 
888  *itor = s;
889 
890  ++itor;
891  }
892 }
double float64
Definition: Nsound.h:146
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
FloatVector::iterator iterator
Definition: Buffer.h:69
void Nsound::Buffer::limit ( const Buffer min,
const Buffer max 
)

Limits the Buffer to min and max.

Example:
// C++
Sine gen(100.0);
Buffer b1("california.wav");
Buffer b2 = gen.generate(5.0, 2.0); // 5 seconds at 2 Hz
b1.limit(b2, b2 + 1.0);
// Python
gen = Sine(100.0)
b1 = Buffer("california.wav")
b2 = Buffer(100) // sets chunk size to 100
b2 << gen.generate(5.0, 2.0) // 5 seconds at 2 Hz
b1.limit(b2, b2 + 1.0)

Definition at line 896 of file Buffer.cc.

References begin(), cbegin(), and end().

897 {
898  Buffer::const_circular_iterator imin = min.cbegin();
899  Buffer::const_circular_iterator imax = max.cbegin();
900 
901  Buffer::iterator itor = this->begin();
902  Buffer::iterator end = this->end();
903 
904  while(itor != end)
905  {
906  float64 s = *itor;
907 
908  if(s > *imax) s = *imax;
909  if(s < *imin) s = *imin;
910 
911  *itor = s;
912 
913  ++itor;
914  ++imax;
915  ++imin;
916  }
917 }
double float64
Definition: Nsound.h:146
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
Nsound::const_circular_iterator const_circular_iterator
Definition: Buffer.h:67
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
FloatVector::iterator iterator
Definition: Buffer.h:69
Buffer Nsound::Buffer::getLimit ( float64  min,
float64  max 
) const
inline

Returns a copy of the Buffer with min and max limits.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getLimit(-1.0, 1.0);
// Python
b1 = Buffer("california.wav")
b2 = b1.getLimit(-1.0, 1.0);

Definition at line 649 of file Buffer.h.

References limit().

650  { Buffer temp(*this); temp.limit(min, max); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
Buffer Nsound::Buffer::getLimit ( const Buffer min,
const Buffer max 
) const
inline

Returns a copy of the Buffer with min and max limits.

Example:
// C++
Sine gen(100.0);
Buffer b1("california.wav");
Buffer b2 = gen.generate(5.0, 2.0); // 5 seconds at 2 Hz
Buffer b3 = b1.getLimit(b2, b2 + 1.0);
// Python
gen = Sine(100.0)
b1 = Buffer("california.wav")
b2 = Buffer(100) // sets chunk size to 100
b2 << gen.generate(5.0, 2.0) // 5 seconds at 2 Hz
b3 = b1.getLimit(b2, b2 + 1.0)

Definition at line 670 of file Buffer.h.

References limit().

671  { Buffer temp(*this); temp.limit(min, max); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::log ( )

Sets each sample in the Buffer to the natural log.

Example:
// C++
Buffer b1("california.wav");
b1.log();
// Python
b1 = Buffer("california.wav")
b1.log()

Definition at line 923 of file Buffer.cc.

References data_, getLength(), and LM_MAX.

Referenced by getLog().

924 {
925  for(uint32 i = 0; i < getLength(); ++i)
926  {
927  // Avoid taking log of really tiny numbers.
928  float64 t = LM_MAX(data_[i], 1e-9);
929 
930  data_[i] = ::log(t);
931  }
932 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#define LM_MAX(a, b)
Definition: Buffer.cc:919
FloatVector data_
Definition: Buffer.h:1874
void log()
Sets each sample in the Buffer to the natural log.
Definition: Buffer.cc:923
Buffer Nsound::Buffer::getLog ( ) const
inline

Returns the the Buffer where each sample is the natural log.

Example:
// C++
Buffer b1("california.wav");
Buffer b1_log = b1.getLog();
// Python
b1 = Buffer("california.wav")
l = b1.getLog()

Definition at line 701 of file Buffer.h.

References log().

702  { Buffer temp(*this); temp.log(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::log10 ( )

Sets each sample in the Buffer to log base 10.

Example:
// C++
Buffer b1("california.wav");
b1.log10();
// Python
b1 = Buffer("california.wav")
b1.log10()

Definition at line 936 of file Buffer.cc.

References data_, getLength(), and LM_MAX.

Referenced by dB(), and getLog10().

937 {
938  for(uint32 i = 0; i < getLength(); ++i)
939  {
940  // Avoid taking log of really tiny numbers.
941  float64 t = LM_MAX(data_[i], 1e-9);
942 
943  data_[i] = ::log10(t);
944  }
945 }
unsigned int uint32
Definition: Nsound.h:153
void log10()
Sets each sample in the Buffer to log base 10.
Definition: Buffer.cc:936
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#define LM_MAX(a, b)
Definition: Buffer.cc:919
FloatVector data_
Definition: Buffer.h:1874
Buffer Nsound::Buffer::getLog10 ( ) const
inline

Returns the the Buffer where each sample is the natural log.

Example:
// C++
Buffer b1("california.wav");
Buffer b1_log = b1.getLog();
// Python
b1 = Buffer("california.wav")
l = b1.getLog()

Definition at line 732 of file Buffer.h.

References log10().

733  { Buffer temp(*this); temp.log10(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
float64 Nsound::Buffer::getMax ( ) const

Returns the maximum sample value in the Buffer.

Example:
// C++
Buffer b1("california.wav");
float64 max = b1.getMax();
// Python
b1 = Buffer("california.wav")
m = b1.getMax()

Definition at line 951 of file Buffer.cc.

References data_, and M_ASSERT_VALUE.

Referenced by Buffer_UnitTest(), BufferResample_UnitTest(), Nsound::Generator::buzz(), DelayLine_UnitTest(), FFTransform_UnitTest(), Nsound::FilterBandPassIIR::FilterBandPassIIR(), FilterCombLowPassFeedback_UnitTest(), FilterLeastSquaresFIR_UnitTest(), FilterParametricEqualizer_UnitTest(), Generator_UnitTest(), main(), Nsound::Filter::plot(), Nsound::FilterIIR::savePlot(), Sine_UnitTest(), and Triangle_UnitTest().

952 {
953  M_ASSERT_VALUE(data_.size(), >=, 1);
954 
955  float64 max = data_[0];
956 
957  for(const_iterator itor = data_.begin();
958  itor != data_.end();
959  ++itor)
960  {
961  float64 t = *itor;
962  if(t > max)
963  {
964  max = t;
965  }
966  }
967 
968  return max;
969 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
FloatVector data_
Definition: Buffer.h:1874
float64 Nsound::Buffer::getMaxMagnitude ( ) const

Returns the maximum magnitude value in the Buffer, i.e. max(abs(samples)).

Example:
// C++
Buffer b1("california.wav");
float64 max = b1.getMaxMagnitude();
// Python
b1 = Buffer("california.wav")

Definition at line 973 of file Buffer.cc.

References data_, and M_ASSERT_VALUE.

Referenced by normalize().

974 {
975  M_ASSERT_VALUE(data_.size(), >=, 1);
976 
977  float64 max = data_[0];
978 
979  for(const_iterator itor = data_.begin();
980  itor != data_.end();
981  ++itor)
982  {
983  float64 t = std::fabs(*itor);
984  if( t > max)
985  {
986  max = t;
987  }
988  }
989 
990  return max;
991 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
FloatVector data_
Definition: Buffer.h:1874
float64 Nsound::Buffer::getMean ( ) const

Returns the mean sample value in the Buffer.

Example:
// C++
Buffer b1("california.wav");
float64 mean = b1.getMean();
// Python
b1 = Buffer("california.wav")
m = b1.getMean()

Definition at line 995 of file Buffer.cc.

References getLength(), and getSum().

Referenced by getResample(), getStd(), and zNorm().

996 {
997  float64 sum = getSum();
998 
999  return sum / static_cast<float64>(getLength());
1000 }
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
float64 getSum() const
Returns the sum of all samples.
Definition: Buffer.cc:1118
float64 Nsound::Buffer::getMin ( ) const

Returns the minimum sample value in the Buffer.

Example:
// C++
Buffer b1("california.wav");
float64 min = b1.getMin();
// Python
b1 = Buffer("california.wav")
m = b1.getMin()

Definition at line 1004 of file Buffer.cc.

References data_, and M_ASSERT_VALUE.

Referenced by Buffer_UnitTest(), main(), and Nsound::FilterLeastSquaresFIR::makeKernel().

1005 {
1006  M_ASSERT_VALUE(data_.size(), >=, 1);
1007 
1008  float64 min = data_[0];
1009 
1010  for(const_iterator itor = data_.begin();
1011  itor != data_.end();
1012  ++itor)
1013  {
1014  if(*itor < min)
1015  {
1016  min = *itor;
1017  }
1018  }
1019 
1020  return min;
1021 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::mul ( const Buffer buffer,
uint32  offset = 0,
uint32  n_samples = 0 
)

This method multiplies a Buffer to *this over a sub range.

If the length of 'buffer' is longer than *this, *this will be extended. If n_samples is 0, the whole length of buffer will get added, otherwise, only n_samples will get added.

Examples:

a = sample in *this
b = sample in buffer
c = a * b
this = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa]
     * |  offset  |[bbbbbbbbb buffer bbbbbbbbbbbbbbbbbb]
     = [aaaaaaaaaaaccccccccccccccccc]
a = sample in *this
b = sample in buffer
c = a * b
this = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa]
     + |  offset  |[bbbbbbbbb buffer bbbbbbbbbbbbbbbbbb]
                   |-n_samples---|
     = [aaaaaaaaaaacccccccccccccccaa]
Parameters
offset- The index into this Buffer to start multiplying
buffer- The Buffer to multiply data from
n_samples- The number of samples from buffer to multiply
Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
Buffer b1.mul(0, b2);
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1.mul(0, b2);

Definition at line 1025 of file Buffer.cc.

References getLength(), and M_ASSERT_VALUE.

1026 {
1027  M_ASSERT_VALUE(offset, >=, 0);
1028  M_ASSERT_VALUE(offset, <, getLength());
1029 
1030  uint32 b0 = offset;
1031  uint32 b1 = 0;
1032 
1033  uint32 n = n_samples;
1034 
1035  if(n_samples == 0)
1036  {
1037  n = buffer.getLength();
1038  }
1039 
1040  b1 = b0 + n;
1041 
1042  if(b1 > getLength())
1043  {
1044  b1 = getLength();
1045  n = b1 - b0;
1046  }
1047 
1048  // Limit
1049  if(n > buffer.getLength())
1050  {
1051  b1 = b0 + buffer.getLength();
1052  }
1053 
1054  uint32 k = 0;
1055  for(uint32 i = b0; i < b1; ++i)
1056  {
1057  (*this)[i] *= buffer[k];
1058  ++k;
1059  }
1060 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void Nsound::Buffer::normalize ( )

Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.

Example:
// C++
Buffer b1("california.wav");
b1.normalize();
// Python
b1 = Buffer("california.wav")

Definition at line 1064 of file Buffer.cc.

References getMaxMagnitude().

Referenced by Nsound::Generator::drawGaussian(), FilterCombLowPassFeedback_UnitTest(), Nsound::Pluck::generate(), getNormalize(), main(), my_main(), and Nsound::DrumBD01::play().

1065 {
1066  float64 peak = getMaxMagnitude();
1067 
1068  float64 scale = 1.0;
1069 
1070  if(peak != 0.0) scale = 1.0 / peak;
1071 
1072  *this *= scale;
1073 }
double float64
Definition: Nsound.h:146
float64 getMaxMagnitude() const
Returns the maximum magnitude value in the Buffer, i.e. max(abs(samples)).
Definition: Buffer.cc:973
Buffer Nsound::Buffer::getNormalize ( ) const
inline

Returns a copy of the Buffer normalized.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getNormalize();
// Python
b1 = Buffer("california.wav")
b2 = b1.getNormalize()

Definition at line 885 of file Buffer.h.

References normalize().

886  { Buffer temp(*this); temp.normalize(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
Buffer Nsound::Buffer::getSignalEnergy ( uint32  window_size) const

Returns the signal energy: E = 1 / N * sum(|x(i)|) over the window of N samples.

Example:
// C++
Buffer b1("california.wav");
float64 energy = b1.getSignalEnergy();
// Python
b1 = Buffer("california.wav")
energy = b1.getSignalEnergy()

Definition at line 1077 of file Buffer.cc.

References data_, and getLength().

1078 {
1079  float64 n = static_cast<float64>(N);
1080 
1081  Buffer y;
1082 
1083  uint32 length = getLength();
1084 
1085  for(uint32 i = 0; i < length; ++i)
1086  {
1087  float64 sum = 0.0;
1088 
1089  for(uint32 j = 0; j < N; ++j)
1090  {
1091  if(i + j < length - 1)
1092  {
1093  sum += ::fabs(data_[i + j]);
1094  }
1095  }
1096  sum /= n;
1097 
1098  y << sum;
1099 
1100  }
1101 
1102  return y;
1103 }
unsigned int uint32
Definition: Nsound.h:153
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
float64 Nsound::Buffer::getStd ( ) const

Returns the standard deviation of the Buffer.

Example:
// C++
Buffer b1("california.wav");
float64 std = b1.getStd();
// Python
b1 = Buffer("california.wav")
std = b1.getStd()

Definition at line 1107 of file Buffer.cc.

References getLength(), getMean(), and getSum().

1108 {
1109  Buffer diff = *this - getMean();
1110 
1111  diff ^= 2.0;
1112 
1113  return ::sqrt(diff.getSum() / static_cast<float64>(getLength()));
1114 }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
float64 getMean() const
Returns the mean sample value in the Buffer.
Definition: Buffer.cc:995
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
float64 Nsound::Buffer::getSum ( ) const

Returns the sum of all samples.

Example:
// C++
Buffer b1("california.wav");
float64 sum = b1.getSum();
// Python
b1 = Buffer("california.wav")
sum = b1.getSum()

Definition at line 1118 of file Buffer.cc.

References data_.

Referenced by getMean(), Nsound::FilterIIR::getRMS(), getStd(), Nsound::Stretcher::searchForBestMatch(), and zNorm().

1119 {
1120  float64 sum = 0.0;
1121 
1122  for(const_iterator itor = data_.begin();
1123  itor != data_.end();
1124  ++itor)
1125  {
1126  sum += *itor;
1127  }
1128 
1129  return sum;
1130 }
double float64
Definition: Nsound.h:146
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::zNorm ( )

Normalized the Buffer using Z score normalizing.

z = (x - mean(x)) / std(x)

Example:
// C++
Buffer b1("california.wav");
b1.zNorm();
// Python
b1 = Buffer("california.wav")
b1.zNorm()

Definition at line 1134 of file Buffer.cc.

References getLength(), getMean(), getSum(), and sqrt().

Referenced by getZNorm().

1135 {
1136  // z = (x - mean(x)) / std(x)
1137 
1138  // We won't call getStd() to avoid calculatig the diff and mean twice.
1139 
1140  // z = (x - mean(x)) / sqrt( sum((x - mean(x))^2) / N)
1141 
1142  float64 mean = getMean();
1143 
1144  Buffer diff = *this - mean;
1145 
1146  float64 std = (diff*diff).getSum() / static_cast<float64>(getLength());
1147 
1148  std = ::sqrt(std);
1149 
1150  *this = diff / (std + 1e-20);
1151 }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
float64 getMean() const
Returns the mean sample value in the Buffer.
Definition: Buffer.cc:995
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
float64 getSum() const
Returns the sum of all samples.
Definition: Buffer.cc:1118
void sqrt()
Takes the square root of each sample in this Buffer.
Definition: Buffer.cc:2051
Buffer Nsound::Buffer::getZNorm ( ) const
inline

Returns a Z score normalized copy of the Buffer.

z = (x - mean(x)) / std(x)

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getNorm();
// Python
b1 = Buffer("california.wav")
b2 = b1.getZNorm()

Definition at line 965 of file Buffer.h.

References zNorm().

966  { Buffer temp(*this); temp.zNorm(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
BufferSelection Nsound::Buffer::operator() ( const BooleanVector bv)

Returns a BufferSelection object used for manipulation of a selected region of samples.

Example:
// C++
Buffer b1("california.wav");
BufferSelection bs = b1(b1 > 0.5);
// Python
b1 = Buffer("california.wav")
bs = b1(b1 > 0.5)

Definition at line 1170 of file Buffer.cc.

1171 {
1172  BufferSelection bs(*this, bv);
1173 
1174  return bs;
1175 }
Buffer & Nsound::Buffer::operator= ( const Buffer rhs)

The assignment operator. C++ only, for Python, use the copy constructor.

Definition at line 1156 of file Buffer.cc.

References data_.

1157 {
1158  if(this == &rhs_buffer) // If it is the same object, return it.
1159  {
1160  return *this;
1161  }
1162 
1163  data_ = rhs_buffer.data_;
1164 
1165  return *this;
1166 }
FloatVector data_
Definition: Buffer.h:1874
boolean Nsound::Buffer::operator== ( const Buffer rhs) const

Tests of equality.

Definition at line 1179 of file Buffer.cc.

References data_.

1180 {
1181  if(data_.size() != rhs_buffer.data_.size())
1182  {
1183  return false;
1184  }
1185 
1186  for(uint32 i = 0; i < data_.size(); ++i)
1187  {
1188  if(data_[i] != rhs_buffer.data_[i])
1189  {
1190  return false;
1191  }
1192  }
1193 
1194  return true;
1195 }
unsigned int uint32
Definition: Nsound.h:153
FloatVector data_
Definition: Buffer.h:1874
boolean Nsound::Buffer::operator!= ( const Buffer rhs) const

Tests of inequality.

Definition at line 1199 of file Buffer.cc.

References data_.

1200 {
1201  if(data_.size() != rhs_buffer.data_.size())
1202  {
1203  return true;
1204  }
1205 
1206  for(uint32 i = 0; i < data_.size(); ++i)
1207  {
1208  if(data_[i] != rhs_buffer.data_[i])
1209  {
1210  return true;
1211  }
1212  }
1213 
1214  return false;
1215 }
unsigned int uint32
Definition: Nsound.h:153
FloatVector data_
Definition: Buffer.h:1874
float64& Nsound::Buffer::operator[] ( uint32  index)
inline

Retruns a reference into the Buffer.

Definition at line 998 of file Buffer.h.

References data_.

999  { return data_[index]; }
FloatVector data_
Definition: Buffer.h:1874
const float64& Nsound::Buffer::operator[] ( uint32  index) const
inline

Returns a const reference into the Buffer.

Definition at line 1004 of file Buffer.h.

References data_.

1005  { return data_[index]; }
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator<< ( const AudioStream rhs)

Concatenates Buffers and AudioStreams together.

Example:
// C++
Buffer b1("california.wav");
AudioStream a2("walle.wav");
b1 << a2;
// Python
b1 = Buffer("california.wav")
a2 = AudioStream("walle.wav")
b1 << a2

Definition at line 1219 of file Buffer.cc.

References Nsound::AudioStream::getMono().

1220 {
1221  return (*this) << rhs.getMono()[0];
1222 }
Buffer & Nsound::Buffer::operator<< ( const Buffer rhs)

Concatenates Buffers together.

Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
b1 << b2;
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1 << b2

Definition at line 1226 of file Buffer.cc.

References data_, end(), and getLength().

1227 {
1228  if(this == & rhs_buffer)
1229  {
1230  uint32 size = getLength();
1231 
1232  for(uint32 i = 0; i < size; ++i)
1233  {
1234  data_.push_back(data_[i]);
1235  }
1236 
1237  return *this;
1238  }
1239 
1240  std::vector<float64>::const_iterator itor = rhs_buffer.data_.begin();
1241  std::vector<float64>::const_iterator end = rhs_buffer.data_.end();
1242 
1243  while(itor != end)
1244  {
1245  data_.push_back(*itor);
1246  ++itor;
1247  }
1248 
1249  return *this;
1250 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector data_
Definition: Buffer.h:1874
Buffer& Nsound::Buffer::operator<< ( float64  d)
inline

Concatenates samples to the Buffer.

Definition at line 1044 of file Buffer.h.

References data_.

1045  { data_.push_back(d); return *this; }
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator+= ( const Buffer rhs)

Adds each sample from the right hand side (rhs) Buffer with this Buffer.

All arithmatic operations only operate for N samples where N is min(b1.getLength(), b2.getLength()).

Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
b1 += b2;
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1 += b2

Definition at line 1254 of file Buffer.cc.

References data_, and getLength().

1255 {
1256  std::size_t N = std::min(getLength(), rhs.getLength());
1257  for(std::size_t i = 0; i < N; ++i) data_[i] += rhs.data_[i];
1258  return *this;
1259 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator-= ( const Buffer rhs)

Subracts each sample from the right hand side (rhs) Buffer from this Buffer.

All arithmatic operations only operate for N samples where N is min(b1.getLength(), b2.getLength()).

Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
b1 -= b2;
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1 -= b2

Definition at line 1263 of file Buffer.cc.

References data_, and getLength().

1264 {
1265  std::size_t N = std::min(getLength(), rhs.getLength());
1266  for(std::size_t i = 0; i < N; ++i) data_[i] -= rhs.data_[i];
1267  return *this;
1268 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator*= ( const Buffer rhs)

Multiplies each sample from the right hand side (rhs) with this Buffer.

All arithmatic operations only operate for N samples where N is min(b1.getLength(), b2.getLength()).

Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
b1 *= b2;
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1 *= b2

Definition at line 1272 of file Buffer.cc.

References data_, and getLength().

1273 {
1274  std::size_t N = std::min(getLength(), rhs.getLength());
1275  for(std::size_t i = 0; i < N; ++i) data_[i] *= rhs.data_[i];
1276  return *this;
1277 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator/= ( const Buffer rhs)

Divides each sample in this Buffer with the right hand side (rhs) Buffer.

All arithmatic operations only operate for N samples where N is min(b1.getLength(), b2.getLength()).

Example:
// C++
Buffer b1("california.wav");
Buffer b2("walle.wav");
b1 /= b2;
// Python
b1 = Buffer("california.wav")
b2 = Buffer("walle.wav")
b1 /= b2

Definition at line 1281 of file Buffer.cc.

References data_, and getLength().

1282 {
1283  std::size_t N = std::min(getLength(), rhs.getLength());
1284  for(std::size_t i = 0; i < N; ++i) data_[i] /= rhs.data_[i];
1285  return *this;
1286 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator^= ( const Buffer powers)

Each sample in the Buffer becomes the power x^n.

Each sample in the Buffer becomes the power x^n, where x is the Buffer sample and N is the power.

Example:
// C++
Buffer b1("california.wav");
Buffer b2("california.wav");
b1 ^= b2;
// Python
b1 = Buffer("california.wav")
b2 = Buffer("california.wav")
b1 **= b2

Definition at line 1290 of file Buffer.cc.

References data_, and getLength().

1291 {
1292  std::size_t N = std::min(getLength(), rhs.getLength());
1293  for(std::size_t i = 0; i < N; ++i) data_[i] = std::pow(data_[i], rhs.data_[i]);
1294  return *this;
1295 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator+= ( float64  d)

Adds the sample d to each sample in this Buffer.

Example:
// C++
Buffer b1("california.wav");
b1 += 0.26;
// Python
b1 = Buffer("california.wav")
b1 += 0.26

Definition at line 1299 of file Buffer.cc.

References data_.

1300 {
1301  for(auto & x : data_) x += d;
1302  return *this;
1303 }
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator-= ( float64  d)

Subtracts the sample d from each sample in this Buffer.

Example:
// C++
Buffer b1("california.wav");
b1 += 0.26;
// Python
b1 = Buffer("california.wav")
b1 += 0.26

Definition at line 1307 of file Buffer.cc.

References data_.

1308 {
1309  for(auto & x : data_) x -= d;
1310  return *this;
1311 }
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator*= ( float64  d)

Multiplies the sample d with each sample in this Buffer.

Example:
// C++
Buffer b1("california.wav");
b1 *= 0.26;
// Python
b1 = Buffer("california.wav")
b1 *= 0.26

Definition at line 1315 of file Buffer.cc.

References data_.

1316 {
1317  for(auto & x : data_) x *= d;
1318  return *this;
1319 }
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator/= ( float64  d)

Divides each sample in this Buffer by d.

Example:
// C++
Buffer b1("california.wav");
b1 /= 0.26;
// Python
b1 = Buffer("california.wav")
b1 /= 0.26

Definition at line 1323 of file Buffer.cc.

References data_.

1324 {
1325  for(auto & x : data_) x /= d;
1326  return *this;
1327 }
FloatVector data_
Definition: Buffer.h:1874
Buffer & Nsound::Buffer::operator^= ( float64  power)

Each sample in the Buffer becomes the power x^n.

Each sample in the Buffer becomes the power x^n, where x is the Buffer sample and N is the power.

Example:
// C++
Buffer b1("california.wav");
b1 ^= 2.0;
// Python
b1 = Buffer("california.wav")
b1 **= 2.0
WARNING: sometimes one may want to do this:
// C++
mag = real ^ 2 + imag ^ 2;
But C++'s default operator precedence will actually compile this:
mag = (real ^ (2 + imag)) ^ 2;
because operator+ has high presedence. To avoid this, in C++ you must use parentheses.
mag = (real ^ 2) + (imag ^ 2);

Definition at line 1331 of file Buffer.cc.

References data_.

1332 {
1333  for(auto & x : data_) x = std::pow(x, d);
1334  return *this;
1335 }
FloatVector data_
Definition: Buffer.h:1874
BooleanVector Nsound::Buffer::operator> ( float64  rhs)

Creates a BooleanVector where each value is true iff Buffer[n] > rhs.

Example:
// C++
Buffer b1("california.wav");
BufferSelection bs = b1 > 0.26;
// Python
b1 = Buffer("california.wav")
bs = b1 > 0.26

Definition at line 2127 of file Buffer.cc.

References begin(), and end().

2128 {
2129  BooleanVector bv;
2130 
2131  Buffer::const_iterator itor = this->begin();
2132  Buffer::const_iterator end = this->end();
2133 
2134  while(itor != end)
2135  {
2136  if(*itor > rhs) bv.push_back(true);
2137  else bv.push_back(false);
2138  ++itor;
2139  }
2140 
2141  return bv;
2142 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
std::vector< boolean > BooleanVector
BooleanVector Nsound::Buffer::operator>= ( float64  rhs)

Creates a BooleanVector where each value is true iff Buffer[n] >= rhs.

Example:
// C++
Buffer b1("california.wav");
BufferSelection bs = b1 >= 0.26;
// Python
b1 = Buffer("california.wav")
bs = b1 >= 0.26

Definition at line 2146 of file Buffer.cc.

References begin(), and end().

2147 {
2148  BooleanVector bv;
2149 
2150  Buffer::const_iterator itor = this->begin();
2151  Buffer::const_iterator end = this->end();
2152 
2153  while(itor != end)
2154  {
2155  if(*itor >= rhs) bv.push_back(true);
2156  else bv.push_back(false);
2157  ++itor;
2158  }
2159 
2160  return bv;
2161 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
std::vector< boolean > BooleanVector
BooleanVector Nsound::Buffer::operator< ( float64  rhs)

Creates a BooleanVector where each value is true iff Buffer[n] < rhs.

Example:
// C++
Buffer b1("california.wav");
BufferSelection bs = b1 < 0.26;
// Python
b1 = Buffer("california.wav")
bs = b1 < 0.26

Definition at line 2165 of file Buffer.cc.

References begin(), and end().

2166 {
2167  BooleanVector bv;
2168 
2169  Buffer::const_iterator itor = this->begin();
2170  Buffer::const_iterator end = this->end();
2171 
2172  while(itor != end)
2173  {
2174  if(*itor < rhs) bv.push_back(true);
2175  else bv.push_back(false);
2176  ++itor;
2177  }
2178 
2179  return bv;
2180 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
std::vector< boolean > BooleanVector
BooleanVector Nsound::Buffer::operator<= ( float64  rhs)

Creates a BooleanVector where each value is true iff Buffer[n] <= rhs.

Example:
// C++
Buffer b1("california.wav");
BufferSelection bs = b1 <= 0.26;
// Python
b1 = Buffer("california.wav")
bs = b1 <= 0.26

Definition at line 2184 of file Buffer.cc.

References begin(), and end().

2185 {
2186  BooleanVector bv;
2187 
2188  Buffer::const_iterator itor = this->begin();
2189  Buffer::const_iterator end = this->end();
2190 
2191  while(itor != end)
2192  {
2193  if(*itor <= rhs) bv.push_back(true);
2194  else bv.push_back(false);
2195  ++itor;
2196  }
2197 
2198  return bv;
2199 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
std::vector< boolean > BooleanVector
BooleanVector Nsound::Buffer::operator== ( float64  rhs)

Creates a BooleanVector where each value is true iff Buffer[n] == rhs.

Example:
// C++
Buffer b1("california.wav");
BufferSelection bs = b1 == 0.26;
// Python
b1 = Buffer("california.wav")
bs = b1 == 0.26

Definition at line 2203 of file Buffer.cc.

References begin(), and end().

2204 {
2205  BooleanVector bv;
2206 
2207  Buffer::const_iterator itor = this->begin();
2208  Buffer::const_iterator end = this->end();
2209 
2210  while(itor != end)
2211  {
2212  bv.push_back(*itor == rhs);
2213  ++itor;
2214  }
2215 
2216  return bv;
2217 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
std::vector< boolean > BooleanVector
BooleanVector Nsound::Buffer::operator!= ( float64  rhs)

Creates a BooleanVector where each value is true iff Buffer[n] != rhs.

Example:
// C++
Buffer b1("california.wav");
BufferSelection bs = b1 != 0.26;
// Python
b1 = Buffer("california.wav")
bs = b1 != 0.26

Definition at line 2221 of file Buffer.cc.

References begin(), and end().

2222 {
2223  BooleanVector bv;
2224 
2225  Buffer::const_iterator itor = this->begin();
2226  Buffer::const_iterator end = this->end();
2227 
2228  while(itor != end)
2229  {
2230  bv.push_back(*itor != rhs);
2231  ++itor;
2232  }
2233 
2234  return bv;
2235 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
std::vector< boolean > BooleanVector
void Nsound::Buffer::plot ( const std::string &  title = "Buffer") const

Requires matplotlib. Creates a plot of this Buffer.

Example:
// C++
Buffer b1("california.wav");
b1.plot("This is california.wav");
Plotter::show(); // This will render any plots created.
// Python
b1 = Buffer("california.wav")
b1.plot("This is california.wav")
Plotter.show()

Definition at line 1551 of file Buffer.cc.

References Nsound::Plotter::figure(), Nsound::Plotter::plot(), and Nsound::Plotter::title().

Referenced by Buffer_UnitTest(), BufferResample_UnitTest(), DelayLine_UnitTest(), FFTransform_UnitTest(), FilterCombLowPassFeedback_UnitTest(), FilterLeastSquaresFIR_UnitTest(), FilterParametricEqualizer_UnitTest(), Generator_UnitTest(), main(), Sine_UnitTest(), testBufferAdd(), and Triangle_UnitTest().

1552 {
1553  Plotter pylab;
1554 
1555  pylab.figure();
1556  pylab.plot(*this);
1557  pylab.title(title);
1558 }
const float64* Nsound::Buffer::getPointer ( ) const
inline

Returns the raw point to the first sample in the Buffer.

Example:
// C++
Buffer b1("california.wav");
const flaot64 * ptr = b1.getPointer();
// Python, not supported or needed.

Definition at line 1382 of file Buffer.h.

References data_.

Referenced by Nsound::FilterLeastSquaresFIR::FilterLeastSquaresFIR(), Nsound::FilterLeastSquaresFIR::makeKernel(), Nsound::FilterLeastSquaresFIR::setKernel(), and Nsound::FilterLeastSquaresFIR::setWindow().

1383  { return data_.data(); }
FloatVector data_
Definition: Buffer.h:1874
float64* Nsound::Buffer::getPointer ( )
inline

Requires matplotlib. Creates a plot of this Buffer.

Example:
// C++
Buffer b1("california.wav");
b1.plot("This is california.wav");
Plotter::show(); // This will render any plots created.
// Python, not supported or needed.

Definition at line 1397 of file Buffer.h.

References data_.

1398  { return data_.data(); }
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::preallocate ( uint32  n)

Preallocates memory to hold an additional n samples.

This makes a call to std::vector::reserve( this->getLength() + n).

Example:
// C++
Buffer b1("california.wav");
b1.preallocate(100);
// Python
b1 = Buffer("california.wav")
b1.preallocate(100);

Definition at line 1544 of file Buffer.cc.

References data_.

1545 {
1546  data_.reserve(data_.size() + n);
1547 }
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::readWavefile ( const char *  filename)

Sets this Buffer to the contents of the wavefile on the disk.

If the wavefile is stereo, it only reads the first channel.

Example:
// C++
Buffer b1;
b1.readWavefile("california.wav");
// Python
b1 = Buffer()
b1.readWavefile("california.wav")

Definition at line 1562 of file Buffer.cc.

References M_CHECK_PTR.

1563 {
1564  M_CHECK_PTR(filename);
1565 
1566  *this << filename;
1567 }
#define M_CHECK_PTR(ptr)
Definition: Macros.h:64
void Nsound::Buffer::resample ( float64  factor)
inline

Resamples this Buffer using discrete-time resampling.

Example:
// C++
Buffer b1("california.wav");
b1.resample(0.666);
// Python
b1 = Buffer("california.wav")
b1.resample(0.666)

Definition at line 1446 of file Buffer.h.

References getResample().

Referenced by main().

1447  { *this = getResample(factor, 10, 5.0); }
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 Nsound::Buffer::resample ( const Buffer factor)
inline

Resamples this Buffer using discrete-time resampling.

Example:
// C++
Buffer b1("california.wav");
Sine gen(44100);
Buffer factor = gen.generate(2.0, 5.0);
b1.resample(factor);
// Python
b1 = Buffer("california.wav")
gen = Sine(44100)
factor = Buffer()
factor << gen.generate(2.0, 5.0)
b1.resample(factor)

Definition at line 1467 of file Buffer.h.

References getResample().

1468  { *this = getResample(factor, 10, 5.0); }
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
Buffer Nsound::Buffer::getResample ( float64  factor,
const uint32  N = 10,
float64  beta = 5.0 
) const

Resamples a copy of this Buffer using discrete-time resampling.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getResample(0.666);
// Python
b1 = Buffer("california.wav")
b2 = b1.getResample(0.666)

Definition at line 1607 of file Buffer.cc.

References Nsound::find_fraction().

Referenced by BufferResample_UnitTest(), getDownSample(), getResample(), getUpSample(), Nsound::Stretcher::pitchShift(), and resample().

1611 {
1612  uint32 L = 0;
1613  uint32 M = 0;
1614 
1615  find_fraction(factor, 0.0001, L, M);
1616 
1617  return getResample(L,M,N,beta);
1618 }
unsigned int uint32
Definition: Nsound.h:153
void find_fraction(float64 fraction, float64 gamma, uint32 &a, uint32 &b)
Definition: Buffer.cc:1570
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
Buffer Nsound::Buffer::getResample ( const Buffer factor,
const uint32  N = 10,
float64  beta = 5.0 
) const

Resamples a copy of this Buffer using discrete-time resampling.

Example:
// C++
Buffer b1("california.wav");
Sine gen(44100);
Buffer factor = gen.generate(2.0, 5.0);
Buffer b2 = b1.getResample(factor);
// Python
b1 = Buffer("california.wav")
gen = Sine(44100)
factor = Buffer()
factor << gen.generate(2.0, 5.0)
b2 = b1.getResample(factor)

Definition at line 1622 of file Buffer.cc.

References getLength(), getMean(), Nsound::BufferWindowSearch::getNextWindow(), getResample(), Nsound::BufferWindowSearch::getSamplesLeft(), and subbuffer().

1626 {
1627  BufferWindowSearch search(*this, 1024);
1628 
1629  uint32 pos = 0;
1630 
1631  Buffer y;
1632 
1633  while(search.getSamplesLeft() > 0)
1634  {
1635  Buffer window = search.getNextWindow();
1636 
1637  uint32 Lw = window.getLength();
1638 
1639  Buffer ratio = factor.subbuffer(pos, Lw);
1640 
1641  float64 r = ratio.getMean();
1642 
1643  y << window.getResample(r,N,beta);
1644 
1645  pos += Lw;
1646  }
1647 
1648  return y;
1649 }
unsigned int uint32
Definition: Nsound.h:153
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
double float64
Definition: Nsound.h:146
Buffer Nsound::Buffer::getResample ( const uint32  L,
const uint32  M,
const uint32  N = 10,
float64  beta = 5.0 
) const

Resamples a copy of this Buffer using discrete-time resampling.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getResample(1, 3);
// Python
b1 = Buffer("california.wav")
b2 = b1.getResample(1, 3)

Definition at line 1653 of file Buffer.cc.

References _get_resample(), getLength(), Nsound::BufferWindowSearch::getNextWindow(), and Nsound::BufferWindowSearch::getSamplesLeft().

1658 {
1659  Buffer y;
1660 
1661  // If the Buffer is really long, break it up into window to reduce memory
1662  // usage.
1663  if(getLength() > 40000)
1664  {
1665  BufferWindowSearch search(*this, 2048);
1666 
1667  while(search.getSamplesLeft() > 0)
1668  {
1669  Buffer window = search.getNextWindow();
1670 
1671  y << window._get_resample(L,M,N,beta);
1672  }
1673  }
1674  else
1675  {
1676  y << _get_resample(L,M,N,beta);
1677  }
1678 
1679  return y;
1680 }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer _get_resample(const uint32 L, const uint32 M, const uint32 N, float64 beta) const
Definition: Buffer.cc:1684
void Nsound::Buffer::reverse ( )

Reverses the samples in this Buffer.

Example:
// C++
Buffer b1("california.wav");
b1.reverse();
// Python
b1 = Buffer("california.wav")
b1.reverse()

Definition at line 1850 of file Buffer.cc.

References data_, and getLength().

Referenced by Buffer_UnitTest(), and getReverse().

1851 {
1852  for(uint32 front = 0, back = getLength() - 1;
1853  front < back;
1854  ++front, --back)
1855  {
1856  float64 temp = data_[front];
1857  data_[front] = data_[back];
1858  data_[back] = temp;
1859  }
1860 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
Buffer Nsound::Buffer::getReverse ( ) const
inline

Reverses the samples in a copy of this Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getReverse();
// Python
b1 = Buffer("california.wav")
b2 = b1.getReverse()

Definition at line 1558 of file Buffer.h.

References reverse().

Referenced by _get_resample(), Nsound::Granulator::Granulator(), Nsound::FFTransform::ifft(), Nsound::FilterLeastSquaresFIR::makeKernel(), and Nsound::Stretcher::overlapAdd().

1559  { Buffer temp(*this); temp.reverse(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::round ( )

Rounds the samples in this Buffer to the nearest integer value.

Values with remainders 0.5 or greater are rounded up, otherwise truncated.

Example:
// C++
Buffer b1("california.wav");
b1.round();
// Python
b1 = Buffer("california.wav")
b1.round()

Definition at line 1864 of file Buffer.cc.

References begin(), and end().

Referenced by getRound().

1865 {
1866  for(Buffer::iterator i = this->begin();
1867  i != this->end();
1868  ++i)
1869  {
1870  *i = std::floor(*i + 0.5);
1871  }
1872 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
FloatVector::iterator iterator
Definition: Buffer.h:69
Buffer Nsound::Buffer::getRound ( ) const
inline

Rounds the samples in a copy of this Buffer to the nearest integer value.

Values with remainders 0.5 or greater are rounded up, otherwise truncated.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getRound();
// Python
b1 = Buffer("california.wav")
b2 = b1.getRound()

Definition at line 1593 of file Buffer.h.

References round().

1594  { Buffer temp(*this); temp.round(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
BufferSelection Nsound::Buffer::select ( const uint32  start_index,
const uint32  stop_index 
)

Returns a BufferSelection for the range of indicies.

Parameters
start_indexthe start of the selection
stop_indexthe stop of the selection
Example:
// C++
Buffer b1("california.wav");
BufferSelection selection = b1.select(20,80);
// Python
b1 = Buffer("california.wav")
selection = b1.select(20,80)

Definition at line 1897 of file Buffer.cc.

References getLength().

1898 {
1899  uint32 n_samples = getLength();
1900 
1901  BooleanVector bv;
1902 
1903  bv.reserve(n_samples);
1904 
1905  for(uint32 i = 0; i < n_samples; ++i)
1906  {
1907  if( i >= start_index && i <= stop_index)
1908  {
1909  bv.push_back(true);
1910  }
1911  else
1912  {
1913  bv.push_back(false);
1914  }
1915  }
1916 
1917  return BufferSelection(*this, bv);
1918 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
std::vector< boolean > BooleanVector
std::ostream & Nsound::Buffer::write ( std::ostream &  out) const

Serializes the Buffer to output stream, no endian checks.

Parameters
outthe std::ostream to write bytes to

Definition at line 1922 of file Buffer.cc.

References data_, and getLength().

1923 {
1924  out & 'b' & 'u' & 'f' & 'f' & getLength();
1925 
1926  for(uint32 i = 0; i < getLength(); ++i)
1927  {
1928  out & data_[i];
1929  }
1930 
1931  return out;
1932 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
std::string Nsound::Buffer::write ( ) const

Definition at line 1936 of file Buffer.cc.

1937 {
1938  std::stringstream ss;
1939  write(ss);
1940  return ss.str();
1941 }
std::string write() const
Definition: Buffer.cc:1936
std::istream & Nsound::Buffer::read ( std::istream &  stream_in)

Constructs a Buffer from seralized data in the inputstream.

Parameters
inthe std::istream to read bytes from

Definition at line 1945 of file Buffer.cc.

References data_, and M_THROW.

Referenced by read().

1946 {
1947  char id[4];
1948  uint32 size = 0;
1949 
1950  in & id[0] & id[1] & id[2] & id[3] & size;
1951 
1952  if(id[0] != 'b' || id[1] != 'u' || id[2] != 'f' || id[3] != 'f')
1953  {
1954  M_THROW("Did not find any Nsound Buffer data in input stream!");
1955  }
1956 
1957  data_.clear();
1958  data_.reserve(size);
1959 
1960  for(uint32 i = 0; i < size; ++i)
1961  {
1962  float64 d;
1963  in & d;
1964  data_.push_back(d);
1965  }
1966 
1967  return in;
1968 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
#define M_THROW(message)
Definition: Macros.h:108
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::read ( const std::string &  string_in)

Definition at line 1972 of file Buffer.cc.

References read().

1973 {
1974  std::stringstream ss(in);
1975 
1976  read(ss);
1977 }
std::istream & read(std::istream &stream_in)
Constructs a Buffer from seralized data in the inputstream.
Definition: Buffer.cc:1945
void Nsound::Buffer::smooth ( uint32  n_passes,
uint32  n_samples_per_average 
)

Applies a moving average filter to smooth this Buffer.

Parameters
n_passesthe number of times to repeat the moving average
n_samples_per_averagethe number of samples to average
Example:
// C++
Buffer b1("california.wav");
b1.smooth(1, 15);
// Python
b1 = Buffer("california.wav")
b1.smooth(1,15)

Definition at line 1981 of file Buffer.cc.

References begin(), end(), Nsound::FilterMovingAverage::filter(), and Nsound::FilterMovingAverage::reset().

Referenced by Buffer_UnitTest(), Nsound::Pluck::generate(), and getSmooth().

1982 {
1983  if(n_samples_to_average < 2)
1984  {
1985  return;
1986  }
1987 
1988  FilterMovingAverage maf(n_samples_to_average);
1989 
1990  Buffer::iterator end = this->end();
1991 
1992  for(uint32 i = 0; i < n_passes; ++i)
1993  {
1994  maf.reset();
1995 
1996  Buffer::iterator itor = this->begin();
1997 
1998  while(itor != end)
1999  {
2000  *itor = maf.filter(*itor);
2001  ++itor;
2002  }
2003  }
2004 }
unsigned int uint32
Definition: Nsound.h:153
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
FloatVector::iterator iterator
Definition: Buffer.h:69
Buffer Nsound::Buffer::getSmooth ( uint32  n_passes,
uint32  n_samples_per_average 
) const
inline

Applies a moving average filter to smooth a copy of this Buffer.

Parameters
n_passesthe number of times to repeat the moving average
n_samples_per_averagethe number of samples to average
Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getSmooth(1, 15);
// Python
b1 = Buffer("california.wav")
b2 = b1.getSmooth(1,15)

Definition at line 1672 of file Buffer.h.

References smooth().

1673  { Buffer temp(*this); temp.smooth(n_passes, n_samples_per_average); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::speedUp ( float64  step_size)

Resamples this Buffer by the step_size, no interpolation.

Example:
// C++
Buffer b1("california.wav");
b1.speedUp(2.0); // b1 is now half the length
b1.speedUp(0.5); // b1 is now original length
// Python
b1 = Buffer("california.wav")
b1.speedUp(2.0) // b1 is now half the length
b1.speedUp(0.5) // b1 is now original length

Definition at line 2008 of file Buffer.cc.

References data_.

Referenced by getSpeedUp().

2009 {
2010  Buffer new_buffer;
2011 
2012  float64 n_samples = static_cast<float64>(data_.size());
2013 
2014  for(float64 i = 0.0; i < n_samples; i += step_size)
2015  {
2016  new_buffer << data_[static_cast<uint32>(i)];
2017  }
2018 
2019  *this = new_buffer;
2020 }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
double float64
Definition: Nsound.h:146
FloatVector data_
Definition: Buffer.h:1874
Buffer Nsound::Buffer::getSpeedUp ( float64  step_size) const
inline

Resamples a copy of this Buffer by the step_size, no interpolation.

Example:
// C++
Buffer b1("california.wav");
b1.speedUp(2.0); // b1 is now half the length
b1.speedUp(0.5); // b1 is now original length
// Python
b1 = Buffer("california.wav")
b1.speedUp(2.0) // b1 is now half the length
b1.speedUp(0.5) // b1 is now original length

Definition at line 1707 of file Buffer.h.

References speedUp().

Referenced by Nsound::FFTransform::fft(), and Nsound::Stretcher::pitchShift().

1708  { Buffer temp(*this); temp.speedUp(step_size); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::speedUp ( const Buffer step_size)

Resamples this Buffer on a sample by sample basis, no interpolation.

Example:
// C++
Buffer b1("california.wav");
Sine gen(44100);
Buffer factor = gen.generate(2.0, 5.0);
b1.speedUp(factor);
// Python
b1 = Buffer("california.wav")
gen = Sine(44100)
factor = Buffer()
factor << gen.generate(2.0, 5.0)
b1.speedUp(factor)

Definition at line 2024 of file Buffer.cc.

References getLength().

2025 {
2026  Buffer new_buffer;
2027 
2028  float64 n_samples = static_cast<float64>(getLength());
2029  uint32 step_buffer_size = step_buffer.getLength();
2030 
2031  uint32 step_buffer_index = 0;
2032  float64 step_size = 0.0;
2033 
2034  for(float64 i = 0.0; i < n_samples; i += step_size)
2035  {
2036  new_buffer << data_[static_cast<size_t>(i)];
2037 
2038  step_size = step_buffer[step_buffer_index++];
2039 
2040  if(step_buffer_index >= step_buffer_size)
2041  {
2042  step_buffer_index -= step_buffer_size;
2043  }
2044  }
2045 
2046  *this = new_buffer;
2047 }
unsigned int uint32
Definition: Nsound.h:153
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer Nsound::Buffer::getSpeedUp ( const Buffer step_size) const
inline

Resamples a copy of this Buffer on a sample by sample basis, no interpolation.

Example:
// C++
Buffer b1("california.wav");
Sine gen(44100);
Buffer factor = gen.generate(2.0, 5.0);
Buffer b2 = b1.getSpeedUp(factor);
// Python
b1 = Buffer("california.wav")
gen = Sine(44100)
factor = Buffer()
factor << gen.generate(2.0, 5.0)
b2 = b1.getSpeedUp(factor)

Definition at line 1748 of file Buffer.h.

References speedUp().

1749  { Buffer temp(*this); temp.speedUp(step_size); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
void Nsound::Buffer::sqrt ( )

Takes the square root of each sample in this Buffer.

Example:
// C++
Buffer b1("california.wav");
b1.sqrt();
// Python
b1 = Buffer("california.wav")
b1.sqrt()

Definition at line 2051 of file Buffer.cc.

References begin(), and end().

Referenced by getSqrt(), and zNorm().

2052 {
2053  Buffer::iterator itor = this->begin();
2054  Buffer::iterator end = this->end();
2055 
2056  while(itor != end)
2057  {
2058  float64 v = *itor;
2059  if(v > 0.0)
2060  {
2061  *itor = std::sqrt(v);
2062  }
2063  else if(v < 0.0)
2064  {
2065  *itor = -std::sqrt(-v);
2066  }
2067  ++itor;
2068  }
2069 }
double float64
Definition: Nsound.h:146
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
FloatVector::iterator iterator
Definition: Buffer.h:69
Buffer Nsound::Buffer::getSqrt ( ) const
inline

Taks the square root of each sample in a copy of this Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getSqrt();
// Python
b1 = Buffer("california.wav")
b2 = b1.getSqrt()

Definition at line 1779 of file Buffer.h.

References sqrt().

Referenced by Nsound::DrumBD01::play().

1780  { Buffer temp(*this); temp.sqrt(); return temp; }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
Buffer Nsound::Buffer::subbuffer ( uint32  start_index,
uint32  n_samples = 0 
) const

Slice the Buffer.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.subbuffer(5,1000);
// Python
b1 = Buffer("california.wav")
b2 = b1.subbuffer(5,1000)
// OR
b2 = b1[5:1005]

Definition at line 2073 of file Buffer.cc.

References data_, and getLength().

Referenced by _get_resample(), Buffer_UnitTest(), Nsound::Spectrogram::computeMagnitude(), Nsound::FFTransform::fft(), Nsound::FFTChunk::getImaginary(), Nsound::FFTChunk::getMagnitude(), Nsound::BufferWindowSearch::getNextWindow(), Nsound::FFTChunk::getPhase(), Nsound::Filter::getPhaseResponse(), Nsound::FFTChunk::getReal(), getResample(), Nsound::FFTransform::ifft(), Nsound::FilterLeastSquaresFIR::makeKernel(), my_main(), Nsound::Stretcher::overlapAdd(), Nsound::FFTChunk::plot(), Nsound::Stretcher::searchForBestMatch(), Nsound::Spectrogram::Spectrogram(), and Nsound::Triangle::Triangle().

2074 {
2075  uint32 n = n_samples;
2076 
2077  if(n > getLength()) n = getLength();
2078 
2079  Buffer new_buffer(n);
2080 
2081  if(start_index >= getLength())
2082  return new_buffer;
2083 
2084  uint32 stop_index = start_index + n;
2085 
2086  if(n == 0 || stop_index >= getLength())
2087  {
2088  stop_index = getLength();
2089  }
2090 
2091  for(uint32 i = start_index; i < stop_index; ++i)
2092  {
2093  new_buffer << data_[i];
2094  }
2095 
2096  return new_buffer;
2097 }
unsigned int uint32
Definition: Nsound.h:153
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::upSample ( uint32  n)

Upsample this Buffer by a integral factor. N must be > 1.

Example:
// C++
Buffer b1("california.wav");
b1.upSample(2);
// Python
b1 = Buffer("california.wav")
b1.upSample(2)

Definition at line 2101 of file Buffer.cc.

References getUpSample().

2102 {
2103  *this = getUpSample(n);
2104 }
Buffer getUpSample(uint32 n) const
Upsample a copy of this Buffer by a integral factor. N must be > 1.
Definition: Buffer.cc:2111
Buffer Nsound::Buffer::getUpSample ( uint32  n) const

Upsample a copy of this Buffer by a integral factor. N must be > 1.

Example:
// C++
Buffer b1("california.wav");
Buffer b2 = b1.getUpSample(2);
// Python
b1 = Buffer("california.wav")
b2 = b1.getUpSample(2)

Definition at line 2111 of file Buffer.cc.

References getResample().

Referenced by upSample().

2112 {
2113  return getResample(L,static_cast<uint32>(1));
2114 }
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 Nsound::Buffer::writeWavefile ( const char *  filename) const

Writes the Buffer to a Wavefile.

Example:
// C++
Buffer b1("california.wav");
b1.writeWavefile("california_copy.wav");
// Python
b1 = Buffer("california.wav")
b1.writeWavefile("california_copy.wav")

Definition at line 2118 of file Buffer.cc.

References M_CHECK_PTR, and Nsound::operator>>().

2119 {
2120  M_CHECK_PTR(filename);
2121 
2122  operator>>(*this, filename);
2123 }
#define M_CHECK_PTR(ptr)
Definition: Macros.h:64
void operator>>(const AudioStream &lhs, AudioPlayback &rhs)
void Nsound::Buffer::_set_at_index ( int32  index,
float64  x 
)

SWIG helper function function to shadow.

Definition at line 1876 of file Buffer.cc.

References data_, getLength(), and M_THROW.

1877 {
1878  int32 len = getLength();
1879 
1880  if(index >= 0 && index < len)
1881  {
1882  data_[index] = d;
1883  }
1884  else if(index < 0 && index >= -len)
1885  {
1886  data_[len + index] = d;
1887  }
1888  else
1889  {
1890  M_THROW(
1891  "IndexError: " << index << " is out of bounds (0 : " << len << ")");
1892  }
1893 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#define M_THROW(message)
Definition: Macros.h:108
signed int int32
Definition: Nsound.h:142
FloatVector data_
Definition: Buffer.h:1874
void Nsound::Buffer::_swig_shadow ( )
inline

SWIG helper function function to shadow.

Definition at line 1848 of file Buffer.h.

1848 {}
Buffer Nsound::Buffer::ones ( const uint32  n_samples)
static

Returns a Buffer full of ones of length n_samples.

Definition at line 2239 of file Buffer.cc.

References Nsound::Generator::drawLine().

Referenced by Buffer_UnitTest(), and Nsound::AudioStream::pad().

2240 {
2241  Buffer b(n_samples);
2242 
2243  Generator g(1);
2244 
2245  b << g.drawLine(n_samples, 1.0, 1.0);
2246 
2247  return b;
2248 }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
Buffer Nsound::Buffer::rand ( const uint32  n_samples)
static

Returns a Buffer full of random values of length n_samples.

Definition at line 2252 of file Buffer.cc.

References Nsound::Generator::whiteNoise().

2253 {
2254  Buffer b(n_samples);
2255 
2256  Generator g(1);
2257 
2258  b << g.whiteNoise(n_samples);
2259 
2260  return b;
2261 }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
Buffer Nsound::Buffer::zeros ( const uint32  n_samples)
static

Returns a Buffer full of zeros of length n_samples.

Definition at line 2265 of file Buffer.cc.

References Nsound::Generator::drawLine().

Referenced by Nsound::Spectrogram::computeMagnitude(), Nsound::Sawtooth::Sawtooth(), and Nsound::Square::Square().

2266 {
2267  Buffer b(n_samples);
2268 
2269  Generator g(1);
2270 
2271  b << g.drawLine(n_samples, 0.0, 0.0);
2272 
2273  return b;
2274 }
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
Buffer Nsound::Buffer::_get_resample ( const uint32  L,
const uint32  M,
const uint32  N,
float64  beta 
) const
protected

Definition at line 1684 of file Buffer.cc.

References begin(), data_, end(), Nsound::FilterLeastSquaresFIR::filter(), Nsound::FilterLeastSquaresFIR::getKernel(), getLength(), getReverse(), M_ASSERT_VALUE, Nsound::FilterLeastSquaresFIR::setKernel(), sr, and subbuffer().

Referenced by getResample().

1689 {
1690  if(L == 1 && M == 1)
1691  {
1692  return *this;
1693  }
1694 
1695  M_ASSERT_VALUE(L, !=, 0);
1696  M_ASSERT_VALUE(M, !=, 0);
1697  M_ASSERT_VALUE(N, !=, 0);
1698  M_ASSERT_VALUE(beta, >=, 0.0);
1699 
1700  uint32 LMmax = (L > M) ? L : M;
1701 
1702  float64 fc = 1.0 / 2.0 / static_cast<float64>(LMmax);
1703 
1704  // ensure kernel length is a multiple of L
1705  uint32 Lh = 2 * N * LMmax;
1706 
1707  float64 sr = 1000.0; // arbitrary, but usefull if the filter is plotted.
1708 
1709  Buffer f(4);
1710  Buffer a(4);
1711 
1712  f << 0.0 << sr * fc << sr * fc << sr * 0.5;
1713  a << 1.0 << 1.0 << 0.0 << 0.0;
1714 
1715  FilterLeastSquaresFIR lpf(sr, Lh, f, a, beta);
1716 
1717  uint32 delay = (Lh-1)/2;
1718 
1719  // Create Polyphase filter bank if interpolating
1720  std::vector<FilterLeastSquaresFIR> bank;
1721  if(L > 1)
1722  {
1723  // Get the kernel.
1724  Buffer h = lpf.getKernel();
1725 
1726  Buffer pseudo_f(2);
1727  Buffer pseudo_a(2);
1728 
1729  pseudo_f << 0.0 << 0.5;
1730  pseudo_a << 0.0 << 0.0;
1731 
1732  for(uint32 i = 0; i < L; ++i)
1733  {
1734  FilterLeastSquaresFIR poly_f(1.0, L, pseudo_f, pseudo_a, beta);
1735 
1736  Buffer poly_h(Lh / L);
1737 
1738  // Pull out cooefficents for the polyphase filter
1739  for(int32 j = Lh - L; j >= 0 && j < static_cast<int32>(Lh); j -= L)
1740  {
1741  poly_h << h[j + i];
1742  }
1743 
1744  poly_f.setKernel(poly_h.getReverse());
1745 
1746  bank.push_back(poly_f);
1747  }
1748  }
1749 
1750  uint32 n_filters = static_cast<uint32>(bank.size());
1751 
1752  // Interpolation
1753 
1754  Buffer::const_iterator x = data_.begin();
1755  Buffer::const_iterator x_end = data_.end();
1756 
1757  uint32 Lx = getLength();
1758  uint32 Ly = getLength() * L / M;
1759 
1760  Buffer y(Ly);
1761 
1762  if(L > 1)
1763  {
1764 //~ cerr << "Interpolation by " << L << endl << flush;
1765 
1766  float64 scale = static_cast<float64>(L);
1767 
1768  // Loop over all the samples
1769  while(x != x_end)
1770  {
1771  // Polyphase filter the input, for all L-1 samples
1772  for(uint32 j = 0; j < L; ++j)
1773  {
1774  float64 sample = *x * scale;
1775 
1776  for(uint32 k = 0; k < n_filters; ++k)
1777  {
1778  y << bank[k].filter(sample);
1779  }
1780  ++x;
1781  if(x == x_end) break;
1782  }
1783 
1784  // Add some samples to the end for filter delay compansation.
1785  if(x == x_end)
1786  {
1787  float64 x_n = data_[Lx - 1] * scale;
1788 
1789  for(uint32 j = 0; j < Lh/L; ++j)
1790  {
1791  for(uint32 k = 0; k < n_filters; ++k)
1792  {
1793  y << bank[k].filter(x_n);
1794  }
1795  }
1796  break;
1797  }
1798  }
1799  }
1800 
1801  // Only down sampling, still need to filter.
1802  else
1803  {
1804  for(uint32 i = 0; i < Lx; ++i)
1805  {
1806  y << lpf.filter(data_[i]);
1807  }
1808 
1809  // Add some samples to the end for filter delay compansation.
1810  float64 x_n = data_[Lx - 1];
1811  for(uint32 i = 0; i < Lh; ++i)
1812  {
1813  y << lpf.filter(x_n);
1814  }
1815  }
1816 
1817  // Compensate for filter delay.
1818  y = y.subbuffer(delay, Lx*L);
1819 
1820  // Decimation
1821  if(M > 1)
1822  {
1823 //~ cerr << "Decimation by " << M << endl << flush;
1824 
1825  x = y.begin();
1826  x_end = y.end();
1827 
1828  Buffer y2(Ly / M);
1829 
1830  uint32 i = 0;
1831  while(x != x_end)
1832  {
1833  if(i % M == 0)
1834  {
1835  y2 << *x;
1836  }
1837 
1838  ++x;
1839  ++i;
1840  }
1841 
1842  y = y2;
1843  }
1844 
1845  return y;
1846 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
Definition: Buffer.cc:62
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
signed int int32
Definition: Nsound.h:142
FloatVector data_
Definition: Buffer.h:1874
float64 sr
Definition: example3.cc:24

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const Buffer rhs 
)
friend

Sends the contents of the Buffer to the output stream.

Definition at line 1338 of file Buffer.cc.

1339 {
1340  std::vector<float64>::const_iterator rhs = rhs_buffer.data_.begin();
1341  std::vector<float64>::const_iterator end = rhs_buffer.data_.end();
1342 
1343  while(rhs != end)
1344  {
1345  out << *rhs << endl;
1346  ++rhs;
1347  }
1348 
1349  return out;
1350 }
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348

Member Data Documentation

FloatVector Nsound::Buffer::data_
protected
const uint32 Nsound::Buffer::bytes_per_sample_ = sizeof(float64)
staticprotected

Definition at line 1876 of file Buffer.h.

Referenced by getNBytes().


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