43 using namespace Nsound;
54 sample_rate_(44100.0),
65 uint32 n_samples_pre_allocate)
67 sample_rate_(sample_rate),
76 ptr->preallocate(n_samples_pre_allocate);
84 sample_rate_(44100.0),
89 *
this << filename.c_str();
96 sample_rate_(copy.sample_rate_),
97 channels_(copy.channels_),
109 for(
auto * ptr :
buffers_)
delete ptr;
116 for(
auto * ptr :
buffers_) ptr->abs();
156 for(
auto * ptr :
buffers_) ptr->convolve(b);
163 for(
auto * ptr :
buffers_) ptr->dB();
170 for(
auto * ptr :
buffers_) ptr->derivative(n);
178 for(
auto * ptr :
buffers_) ptr->downSample(n);
189 length = std::min(length, ptr->getLength());
203 length = std::min(length, ptr->getLength());
213 for(
auto * ptr :
buffers_) ptr->limit(min, max);
220 for(
auto * ptr :
buffers_) ptr->limit(min, max);
227 float64 max = std::numeric_limits<float64>::min();
229 for(
auto * ptr :
buffers_) max = std::max(max, ptr->getMax());
238 float64 max = std::numeric_limits<float64>::min();
240 for(
auto * ptr :
buffers_) max = std::max(max, ptr->getMaxMagnitude());
249 float64 min = std::numeric_limits<float64>::max();
251 for(
auto * ptr :
buffers_) min = std::min(min, ptr->getMin());
289 float64 normal_factor = 1.0 / peak;
291 *
this *= normal_factor;
315 if(
this == &rhs)
return *
this;
385 if(rhs.
channels_ == 1)
return *
this << rhs[0];
391 (*this)[i] << rhs[i];
401 for(
auto * ptr :
buffers_) *ptr << rhs;
409 if(rhs.
channels_ == 1)
return *
this += rhs[0];
415 (*this)[i] += rhs[i];
424 for(
auto * ptr :
buffers_) *ptr += rhs;
432 if(rhs.
channels_ == 1)
return *
this -= rhs[0];
438 (*this)[i] -= rhs[i];
447 for(
auto * ptr :
buffers_) *ptr -= rhs;
455 if(rhs.
channels_ == 1)
return *
this *= rhs[0];
461 (*this)[i] *= rhs[i];
470 for(
auto * ptr :
buffers_) *ptr *= rhs;
479 if(rhs.
channels_ == 1)
return *
this /= rhs[0];
485 (*this)[i] /= rhs[i];
495 for(
auto * ptr :
buffers_) *ptr /= rhs;
504 if(rhs.
channels_ == 1)
return *
this ^= rhs[0];
510 (*this)[i] ^= rhs[i];
520 for(
auto * ptr :
buffers_) *ptr ^= rhs;
529 for(
auto * ptr :
buffers_) *ptr << d;
537 for(
auto * ptr :
buffers_) *ptr += d;
545 for(
auto * ptr :
buffers_) *ptr -= d;
553 for(
auto * ptr :
buffers_) *ptr *= d;
561 for(
auto * ptr :
buffers_) *ptr /= d;
569 for(
auto * ptr :
buffers_) *ptr ^= d;
590 for(
uint32 j = 0; j < n_samples; j++)
604 uint32 min = std::numeric_limits<uint32>::max();
609 min = std::min(min, ptr->getLength());
610 max = std::max(max, ptr->getLength());
613 if(min == max)
return;
615 for(
auto * ptr : buffers_)
617 uint32 n_fill = max - ptr->getLength();
619 if(n_fill == 0)
continue;
631 float64 left_amplitude = (pan + 1.0) * 0.5;
632 float64 right_amplitude = ((pan * -1.0) + 1.0) * 0.5;
649 for(
uint32 i = 0; i < length; ++i)
651 float64 left_amplitude = (pan[i] + 1.0) * 0.5;
652 float64 right_amplitude = ((pan[i] * -1.0) + 1.0) * 0.5;
654 left[i] *= left_amplitude;
655 right[i] *= right_amplitude;
661 plot(
const std::string & title)
const
679 pylab.
subplot(n_rows, n_columns, i + 1);
686 pylab.
plot(x_axis, *ptr);
688 pylab.
xlabel(
"Time (sec)");
689 pylab.
ylabel(
"Amplitude");
708 for(
auto * ptr :
buffers_) ptr->resample(factor);
715 for(
auto * ptr :
buffers_) ptr->resample(factor);
735 for(
auto * ptr :
buffers_) ptr->reverse();
746 for(
uint32 i = start_index; i < stop_index; ++i)
763 if(i >= 0 && i < len)
768 if(i < 0 && i >= -len)
775 "IndexError: " << i <<
" is out of bounds (0 : " << len <<
")");
785 for(
auto * ptr :
buffers_)
delete ptr;
799 for(
auto * ptr :
buffers_) ptr->smooth(n_passes, n_samples_per_average);
806 for(
auto * ptr :
buffers_) ptr->speedUp(step_size);
813 for(
auto * ptr :
buffers_) ptr->speedUp(step_buffer);
820 for(
auto * ptr :
buffers_) ptr->sqrt();
831 new_stream[i] = (*this)[i].subbuffer(start_index, n_samples);
846 static_cast<uint32>(start_index),
847 static_cast<uint32>(n_samples));
860 if(n_seconds == 0 || n_samples + start_index >=
getLength())
867 new_stream[i] =
buffers_[i]->subbuffer(start_index, n_samples);
886 new_as[i] << (*this)[j][i];
897 for(
auto * ptr :
buffers_) ptr->upSample(n);
904 out &
'a' &
'u' &
'd' &
'i' &
'o' &
's' &
't' &
'r'
908 for(
auto * ptr :
buffers_) ptr->write(out);
917 std::stringstream ss;
931 &
id[0] &
id[1] &
id[2] &
id[3] &
id[4] &
id[5] &
id[6] &
id[7]
935 id[0] !=
'a' ||
id[1] !=
'u' ||
id[2] !=
'd' ||
id[3] !=
'i' ||
936 id[4] !=
'o' ||
id[5] !=
's' ||
id[6] !=
't' ||
id[7] !=
'r')
938 M_THROW(
"Did not find any Nsound AudioStream data in input stream!");
945 for(
auto * ptr :
buffers_) ptr->read(in);
954 std::stringstream ss(in);
974 for(
auto * ptr : buffers_) bv.push_back( *ptr > rhs );
986 for(
auto * ptr : buffers_) bv.push_back( *ptr >= rhs );
998 for(
auto * ptr : buffers_) bv.push_back( *ptr < rhs );
1010 for(
auto * ptr : buffers_) bv.push_back( *ptr <= rhs );
1022 for(
auto * ptr : buffers_) bv.push_back( *ptr == rhs );
1034 for(
auto * ptr : buffers_) bv.push_back( *ptr != rhs );
1050 a << g.
drawLine(duration, 1.0, 1.0);
1082 a << g.
drawLine(duration, 0.0, 0.0);
void xlabel(const std::string &label, const std::string &kwargs="")
Add a label x axis.
void speedUp(float32 step_size)
Resamples the AudioStream by the step_size, no interpolation.
#define M_ASSERT_VALUE(a, op, value)
BooleanVectorVector operator>(float64 rhs)
std::ostream & operator<<(std::ostream &out, const Buffer &rhs_buffer)
float64 getDuration() const
Returns the number of seconds of audio data in the stream.
void downSample(uint32 n)
Downsample by a integral factor. N must be > 1.
boolean operator!=(const AudioStream &rhs) const
Boolean != operator.
float64 getMin() const
Returns the minimum sample value in the AudioStream.
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
AudioStream & operator*=(const AudioStream &rhs)
float64 getSampleRate() const
Returns the sample rate of the stream.
void readWavefile(const char *filename)
Read a Wavefile.
AudioStreamSelection operator()(const BooleanVectorVector &bv)
Returns an AudioStreamSelection object used for manipulation of a selected region of samples...
std::istream & read(std::istream &stream_in)
Constructs an AudioStream from seralized data in the inputstream.
uint32 getLength() const
Returns the number of samples of audio data in the stream.
std::string write() const
AudioStreamSelection select(const uint32 start_index, const uint32 stop_index)
Returns an AudioStreamSelection for the range of indicies.
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
AudioStream & operator+=(const AudioStream &rhs)
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
void reverse()
Reverses the AudioStream.
std::vector< Buffer * > buffers_
AudioStream & operator^=(const AudioStream &rhs)
BooleanVectorVector operator<(float64 rhs)
AudioStream substream(uint32 start_index, uint32 n_samples=0) const
float64 getMax() const
Returns the maximum sample value in the Audiostream.
void upSample(uint32 n)
Upsample by a integral factor. N must be > 1.
uint32 getLength() const
Returns the number of samples in the Buffer.
void convolve(const Buffer &b)
Convolves every channel in the AudioStream with the Buffer.
void setNChannels(uint32 channels)
Axes subplot(const uint32 n_rows, const uint32 n_cols, const uint32 n, const std::string &kwargs="", Axes *sharex=NULL, Axes *sharey=NULL)
Creates a figure in a subplot, subplot(A, B, C, **kwargs)
void normalize()
Multiplies the AudioStream by a constant gain so the peak sample has magnitude 1.0.
BooleanVectorVector operator<=(float64 rhs)
~AudioStream()
Destructor.
AudioStream & operator-=(const AudioStream &rhs)
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
void plot(const std::string &title="AudioStream") const
void mono()
Collapses all channels into one Buffer making it mono.
const Buffer & operator[](uint32 index) const
These methods provide a reference to the Buffer object held in the channel.
void writeWavefile(const char *filename) const
Write the AudioStream to a Wavefile.
static Buffer ones(const uint32 n_samples)
Returns a Buffer full of ones of length n_samples.
std::vector< std::vector< boolean > > BooleanVectorVector
void derivative(uint32 n)
Calculates the nth derivative of the AudioStream.
void _set_at_index(int32 i, const Buffer &)
SWIG helper function.
void resample2(float64 new_sample_rate)
Resample to the specified sample rate.
AudioStream & operator/=(const AudioStream &rhs)
AudioStream & operator<<(const AudioStream &rhs)
Concatenates or appends rhs to the AudioStream.
void transpose()
Treating the AudioStream as a matrix, this peforms a matrix transpose.
void ylabel(const std::string &label, const std::string &kwargs="")
Add a label y axis.
void dB()
Modifies the AudioStream so each sample is converted to dB, 20 * log10(sample).
A Buffer for storing audio samples.
std::vector< boolean > BooleanVector
Buffer whiteNoise(const float64 &duration) const
This method generates noise from a uniform distribution.
static AudioStream rand(float64 sample_rate, const uint32 n_channels, float64 duration)
Returns a Buffer full of random values of length n_samples.
boolean operator==(const AudioStream &rhs) const
Boolean == operator.
static AudioStream ones(float64 sample_rate, const uint32 n_channels, float64 duration)
Returns an AudioStream full of ones of duration seconds.
void limit(float64 min, float64 max)
Limits the AudioStream the min and max values.
void add(const AudioStream &as, uint32 offset, uint32 n_samples=0)
This method adds the passed AudioStream to this AudioStream.
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
void pan(float64 pan)
Sets the amplitude level left vs right.
static AudioStream zeros(float64 sample_rate, const uint32 n_channels, float64 duration)
Returns a Buffer full of zeros of length n_samples.
Buffer drawLine(const float64 &duration, const float64 &litude_start, const float64 &litude_finish) const
This method draws a linear line beteween 2 points.
void abs()
This method calls abs on all buffers held in the stream.
A helper class for advance operators.
BooleanVectorVector operator>=(float64 rhs)
void smooth(uint32 n_passes, uint32 n_samples_per_average)
Implements a standard moving average filter to smooth the waveform.
void sqrt()
Takes the square root of each sample in the AudioStream.
A class the provides draw utilities and a wavetable oscillator.
AudioStream()
Create an AudioStream.
void resample(float64 factor)
Resample by a non-integer factor.
float64 getMaxMagnitude() const
Returns the maximum sample magnitude value in the AudioStream, i.e. max(abs(samples))..
void pad(float64 fill=0.0)
Pads the AudioStream so that each channel has exactly the same number of samples. ...
AudioStream & operator=(const AudioStream &rhs)
Assignment operator.