74 data_.reserve(chunk_size);
89 data_.reserve(chunk_size);
92 *
this << filename.c_str();
107 std::swap(
data_, move.data_);
238 if(n_samples == 0 && a_length < offset)
241 for(
uint32 i = a_length; i < offset; ++i)
258 else if(n_samples != 0
260 && n_samples <= b_length)
263 for(
uint32 i = a_length; i < offset; ++i)
268 for(
uint32 i = 0; i < n_samples; ++i)
282 else if(n_samples != 0
284 && n_samples > b_length)
287 for(
uint32 i = a_length; i < offset; ++i)
298 for(
uint32 i = b_length; i < n_samples; ++i)
311 else if(n_samples == 0
312 && a_length >= offset
313 && a_length < offset + b_length)
316 for(
uint32 i = offset; i < a_length; ++i)
318 data_[i] += b[b_index++];
321 for(
uint32 i = b_index; i < b_length; ++i)
336 else if(n_samples != 0
337 && a_length >= offset
338 && a_length <= offset + n_samples
339 && n_samples <= b_length)
342 for(
uint32 i = offset; i < a_length; ++i)
344 data_[i] += b[b_index++];
347 for(
uint32 i = b_index; i < n_samples; ++i)
361 else if(n_samples != 0
362 && a_length >= offset
363 && a_length > offset + n_samples
364 && n_samples < b_length)
367 for(
uint32 i = offset; i < offset + n_samples; ++i)
369 data_[i] += b[b_index++];
381 else if(a_length >= offset
382 && a_length <= offset + b_length
383 && n_samples > b_length)
386 for(
uint32 i = offset; i < a_length; ++i)
388 data_[i] += b[b_index++];
391 for(
uint32 i = b_index; i < b_length; ++i)
396 for(
uint32 i = b_length; i < n_samples; ++i)
407 else if(n_samples == 0
408 && a_length >= offset + b_length)
411 for(
uint32 i = offset; i < offset + b_length; ++i)
413 data_[i] += b[b_index++];
424 else if( a_length >= offset + n_samples
425 && n_samples >= b_length)
428 for(
uint32 i = offset; i < offset + b_length; ++i)
430 data_[i] += b[b_index++];
441 else if(n_samples != 0
442 && a_length > offset + b_length
443 && a_length < offset + n_samples)
446 for(
uint32 i = offset; i < offset + b_length; ++i)
448 data_[i] += b[b_index++];
451 for(
uint32 i = a_length; i < offset + n_samples; ++i)
556 y[i + j] += (*this)[i] * H[j];
594 for(
uint32 pass = 0; pass < n; ++pass)
630 uint32 h_window_size = window_size / 2 + 1;
634 for(
uint32 i = 1; i < n_samples - 1; ++i)
636 boolean is_peak =
false;
642 if( sample >
data_[i - 1] &&
643 sample >
data_[i + 1])
654 for(
uint32 j = 1; j < h_window_size; ++j)
658 if(
data_[i - j] > sample)
665 if(i + j < n_samples)
667 if(
data_[i + j] > sample)
676 if(is_peak && sample > min_height)
908 if(s > *imax) s = *imax;
909 if(s < *imin) s = *imin;
919 #define LM_MAX(a,b) ((a) > (b)) ? (a) : (b)
1011 itor !=
data_.end();
1055 for(
uint32 i = b0; i < b1; ++i)
1057 (*this)[i] *= buffer[k];
1070 if(peak != 0.0) scale = 1.0 / peak;
1085 for(
uint32 i = 0; i < length; ++i)
1089 for(
uint32 j = 0; j < N; ++j)
1091 if(i + j < length - 1)
1093 sum += ::fabs(
data_[i + j]);
1123 itor !=
data_.end();
1144 Buffer diff = *
this - mean;
1150 *
this = diff / (std + 1e-20);
1158 if(
this == &rhs_buffer)
1221 return (*
this) << rhs.
getMono()[0];
1228 if(
this == & rhs_buffer)
1232 for(
uint32 i = 0; i < size; ++i)
1240 std::vector<float64>::const_iterator itor = rhs_buffer.
data_.begin();
1241 std::vector<float64>::const_iterator
end = rhs_buffer.
data_.end();
1245 data_.push_back(*itor);
1257 for(std::size_t i = 0; i < N; ++i)
data_[i] += rhs.
data_[i];
1266 for(std::size_t i = 0; i < N; ++i)
data_[i] -= rhs.
data_[i];
1275 for(std::size_t i = 0; i < N; ++i)
data_[i] *= rhs.
data_[i];
1284 for(std::size_t i = 0; i < N; ++i)
data_[i] /= rhs.
data_[i];
1293 for(std::size_t i = 0; i < N; ++i)
data_[i] = std::pow(
data_[i], rhs.
data_[i]);
1301 for(
auto & x :
data_) x += d;
1309 for(
auto & x :
data_) x -= d;
1317 for(
auto & x :
data_) x *= d;
1325 for(
auto & x :
data_) x /= d;
1333 for(
auto & x :
data_) x = std::pow(x, d);
1340 std::vector<float64>::const_iterator rhs = rhs_buffer.
data_.begin();
1341 std::vector<float64>::const_iterator end = rhs_buffer.
data_.end();
1345 out << *rhs << endl;
1583 while( fabs(diff) > gamma )
1595 diff = f - fraction;
1598 a =
static_cast<uint32>(num);
1599 b =
static_cast<uint32>(den);
1690 if(L == 1 && M == 1)
1700 uint32 LMmax = (L > M) ? L : M;
1705 uint32 Lh = 2 * N * LMmax;
1712 f << 0.0 << sr * fc << sr * fc << sr * 0.5;
1713 a << 1.0 << 1.0 << 0.0 << 0.0;
1720 std::vector<FilterLeastSquaresFIR> bank;
1729 pseudo_f << 0.0 << 0.5;
1730 pseudo_a << 0.0 << 0.0;
1732 for(
uint32 i = 0; i < L; ++i)
1739 for(
int32 j = Lh - L; j >= 0 && j < static_cast<int32>(Lh); j -= L)
1746 bank.push_back(poly_f);
1772 for(
uint32 j = 0; j < L; ++j)
1776 for(
uint32 k = 0; k < n_filters; ++k)
1778 y << bank[k].filter(sample);
1781 if(x == x_end)
break;
1789 for(
uint32 j = 0; j < Lh/L; ++j)
1791 for(
uint32 k = 0; k < n_filters; ++k)
1793 y << bank[k].filter(x_n);
1804 for(
uint32 i = 0; i < Lx; ++i)
1811 for(
uint32 i = 0; i < Lh; ++i)
1870 *i = std::floor(*i + 0.5);
1880 if(index >= 0 && index < len)
1884 else if(index < 0 && index >= -len)
1886 data_[len + index] = d;
1891 "IndexError: " << index <<
" is out of bounds (0 : " << len <<
")");
1903 bv.reserve(n_samples);
1905 for(
uint32 i = 0; i < n_samples; ++i)
1907 if( i >= start_index && i <= stop_index)
1913 bv.push_back(
false);
1924 out &
'b' &
'u' &
'f' &
'f' &
getLength();
1938 std::stringstream ss;
1950 in &
id[0] &
id[1] &
id[2] &
id[3] & size;
1952 if(
id[0] !=
'b' ||
id[1] !=
'u' ||
id[2] !=
'f' ||
id[3] !=
'f')
1954 M_THROW(
"Did not find any Nsound Buffer data in input stream!");
1958 data_.reserve(size);
1960 for(
uint32 i = 0; i < size; ++i)
1974 std::stringstream ss(in);
1983 if(n_samples_to_average < 2)
1992 for(
uint32 i = 0; i < n_passes; ++i)
2000 *itor = maf.
filter(*itor);
2014 for(
float64 i = 0.0; i < n_samples; i += step_size)
2016 new_buffer << data_[static_cast<uint32>(i)];
2031 uint32 step_buffer_index = 0;
2034 for(
float64 i = 0.0; i < n_samples; i += step_size)
2036 new_buffer << data_[static_cast<size_t>(i)];
2038 step_size = step_buffer[step_buffer_index++];
2040 if(step_buffer_index >= step_buffer_size)
2042 step_buffer_index -= step_buffer_size;
2061 *itor = std::sqrt(v);
2065 *itor = -std::sqrt(-v);
2084 uint32 stop_index = start_index + n;
2091 for(
uint32 i = start_index; i < stop_index; ++i)
2093 new_buffer <<
data_[i];
2136 if(*itor > rhs) bv.push_back(
true);
2137 else bv.push_back(
false);
2155 if(*itor >= rhs) bv.push_back(
true);
2156 else bv.push_back(
false);
2174 if(*itor < rhs) bv.push_back(
true);
2175 else bv.push_back(
false);
2193 if(*itor <= rhs) bv.push_back(
true);
2194 else bv.push_back(
false);
2212 bv.push_back(*itor == rhs);
2230 bv.push_back(*itor != rhs);
2245 b << g.
drawLine(n_samples, 1.0, 1.0);
2271 b << g.
drawLine(n_samples, 0.0, 0.0);
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
#define M_ASSERT_VALUE(a, op, value)
std::ostream & operator<<(std::ostream &out, const Buffer &rhs_buffer)
float64 getStd() const
Returns the standard deviation of the Buffer.
boolean operator!=(const Buffer &rhs) const
Tests of inequality.
void readWavefile(const char *filename)
Sets this Buffer to the contents of the wavefile on the disk.
boolean operator==(const Buffer &rhs) const
Tests of equality.
void dB()
Modifies the Buffer so each sample is converted to dB, 20 * log10(sample).
Buffer getConvolve(const Buffer &H) const
Convolves a copy of the Buffer with another Buffer.
Buffer()
Creates an empty Buffer that also calss std::vector::reserve() to preallocate memory for samples...
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
AudioStream filter(const AudioStream &x)
Buffer getDownSample(uint32 n) const
Returns a copy of this Buffer downsampled by a integral factor. N must be > 1.
std::vector< float64 > FloatVector
Buffer getSignalEnergy(uint32 window_size) const
Returns the signal energy: E = 1 / N * sum(|x(i)|) over the window of N samples.
void log10()
Sets each sample in the Buffer to log base 10.
void preallocate(uint32 n)
Preallocates memory to hold an additional n samples.
void downSample(uint32 n)
Downsample this Buffer by a integral factor. N must be > 1.
static Buffer zeros(const uint32 n_samples)
Returns a Buffer full of zeros of length n_samples.
Buffer & operator/=(const Buffer &rhs)
Divides each sample in this Buffer with the right hand side (rhs) Buffer.
BufferSelection operator()(const BooleanVector &bv)
Returns a BufferSelection object used for manipulation of a selected region of samples.
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
Buffer & operator=(const Buffer &rhs)
The assignment operator. C++ only, for Python, use the copy constructor.
void plot(const std::string &title="Buffer") const
Requires matplotlib. Creates a plot of this Buffer.
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
float64 getMean() const
Returns the mean sample value in the Buffer.
std::istream & read(std::istream &stream_in)
Constructs a Buffer from seralized data in the inputstream.
void limit(float64 min, float64 max)
Limits the Buffer to min and max.
void writeWavefile(const char *filename) const
Writes the Buffer to a Wavefile.
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
float64 getMaxMagnitude() const
Returns the maximum magnitude value in the Buffer, i.e. max(abs(samples)).
void _set_at_index(int32 index, float64 x)
SWIG helper function function to shadow.
uint32 getLength() const
Returns the number of samples in the Buffer.
Buffer getNextWindow()
Searches the target Buffer for a zero crossing at or after the window_size position.
void find_fraction(float64 fraction, float64 gamma, uint32 &a, uint32 &b)
void convolve(const Buffer &H)
Convolves the Buffer with another Buffer.
iterator end()
Retruns the itreator at the end of the Buffer.
void operator>>(const AudioStream &lhs, AudioPlayback &rhs)
FloatVector::const_iterator const_iterator
void zNorm()
Normalized the Buffer using Z score normalizing.
std::string write() const
void smooth(uint32 n_passes, uint32 n_samples_per_average)
Applies a moving average filter to smooth this Buffer.
iterator begin()
Retruns the itreator at the start of the Buffer.
BooleanVector operator<(float64 rhs)
Creates a BooleanVector where each value is true iff Buffer[n] < rhs.
static Buffer ones(const uint32 n_samples)
Returns a Buffer full of ones of length n_samples.
void speedUp(float64 step_size)
Resamples this Buffer by the step_size, no interpolation.
void reverse()
Reverses the samples in this Buffer.
void normalize()
Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.
std::vector< uint32 > Uint32Vector
static Buffer rand(const uint32 n_samples)
Returns a Buffer full of random values of length n_samples.
void exp()
Each sample in the Buffer becomes the power e^x.
Buffer & operator^=(const Buffer &powers)
Each sample in the Buffer becomes the power x^n.
uint32 argmin() const
Retruns the index of the minimum value in the Buffer.
Buffer & operator*=(const Buffer &rhs)
Multiplies each sample from the right hand side (rhs) with this Buffer.
void abs()
Modifies the Buffer by making any negative value positive.
BufferSelection select(const uint32 start_index, const uint32 stop_index)
Returns a BufferSelection for the range of indicies.
AudioStream filter(const AudioStream &x)
float64 getSum() const
Returns the sum of all samples.
void sqrt()
Takes the square root of each sample in this Buffer.
A Buffer for storing audio samples.
Buffer getUpSample(uint32 n) const
Upsample a copy of this Buffer by a integral factor. N must be > 1.
BooleanVector operator<=(float64 rhs)
Creates a BooleanVector where each value is true iff Buffer[n] <= rhs.
std::vector< boolean > BooleanVector
Buffer whiteNoise(const float64 &duration) const
This method generates noise from a uniform distribution.
float64 getMin() const
Returns the minimum sample value in the Buffer.
void round()
Rounds the samples in this Buffer to the nearest integer value.
Buffer _get_resample(const uint32 L, const uint32 M, const uint32 N, float64 beta) const
A FIR filter that is defined as the least square error to the desired requency response.
Buffer getResample(float64 factor, const uint32 N=10, float64 beta=5.0) const
Resamples a copy of this Buffer using discrete-time resampling.
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Buffer & operator-=(const Buffer &rhs)
Subracts each sample from the right hand side (rhs) Buffer from this Buffer.
void upSample(uint32 n)
Upsample this Buffer by a integral factor. N must be > 1.
FloatVector::iterator iterator
Buffer drawLine(const float64 &duration, const float64 &litude_start, const float64 &litude_finish) const
This method draws a linear line beteween 2 points.
float64 getMax() const
Returns the maximum sample value in the Buffer.
Buffer & operator+=(const Buffer &rhs)
Adds each sample from the right hand side (rhs) Buffer with this Buffer.
Searches the target Buffer for zero crossings at or after the window_size position.
void add(const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
This method adds buffer to *this.
uint32 getSamplesLeft() const
Returns how many samples are left in the target Buffer.
Buffer getReverse() const
Reverses the samples in a copy of this Buffer.
BooleanVector operator>=(float64 rhs)
Creates a BooleanVector where each value is true iff Buffer[n] >= rhs.
~Buffer()
Destroys the Buffer.
Uint32Vector findPeaks(uint32 window_size=0, float64 min_height=0.0) const
Find the peaks in the Buffer and return a vector of indicies.
BooleanVector operator>(float64 rhs)
Creates a BooleanVector where each value is true iff Buffer[n] > rhs.
A helper class for advance operators.
Buffer & operator<<(const AudioStream &rhs)
Concatenates Buffers and AudioStreams together.
void setKernel(const Buffer &k)
A class the provides draw utilities and a wavetable oscillator.
void derivative(uint32 n)
Calculates the nth derivative of the Buffer.
uint32 argmax() const
Retruns the index of the maximum value in the Buffer.
void mul(const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
This method multiplies a Buffer to *this over a sub range.
void log()
Sets each sample in the Buffer to the natural log.