Nsound  0.9.4
Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
Nsound::AudioStream Class Reference

#include <Nsound/AudioStream.h>

Public Member Functions

 AudioStream ()
 Create an AudioStream. More...
 
 AudioStream (float64 sample_rate, uint32 n_channels=1, uint32 n_samples_pre_allocate=4096)
 
 AudioStream (const std::string &filename)
 
 ~AudioStream ()
 Destructor. More...
 
 AudioStream (const AudioStream &rhs)
 Copy Constructor. More...
 
void abs ()
 This method calls abs on all buffers held in the stream. More...
 
AudioStream getAbs () const
 Modifies the AudioStream by making any negative value positive. More...
 
void add (const AudioStream &as, uint32 offset, uint32 n_samples=0)
 This method adds the passed AudioStream to this AudioStream. More...
 
void add (const AudioStream &as, float64 offset_seconds, float64 duration_seconds=0.0)
 This method adds the passed AudioStream to this AudioStream. More...
 
void convolve (const Buffer &b)
 Convolves every channel in the AudioStream with the Buffer. More...
 
AudioStream getConvolve (const Buffer &b) const
 Convolves every channel in the AudioStream with the Buffer. More...
 
void dB ()
 Modifies the AudioStream so each sample is converted to dB, 20 * log10(sample). More...
 
AudioStream getdB () const
 Returns the AudioStream in dB. More...
 
void derivative (uint32 n)
 Calculates the nth derivative of the AudioStream. More...
 
AudioStream getDerivative (uint32 n) const
 Returns the nth derivative of the Buffer. More...
 
void downSample (uint32 n)
 Downsample by a integral factor. N must be > 1. More...
 
AudioStream getDownSample (uint32 n) const
 Downsample by a integral factor. N must be > 1. More...
 
float64 getDuration () const
 Returns the number of seconds of audio data in the stream. More...
 
uint32 getLength () const
 Returns the number of samples of audio data in the stream. More...
 
void limit (float64 min, float64 max)
 Limits the AudioStream the min and max values. More...
 
void limit (const Buffer &min, const Buffer &max)
 Limits the AudioStream the min and max values. More...
 
AudioStream getLimit (float64 min, float64 max) const
 Limits the AudioStream the min and max values. More...
 
AudioStream getLimit (const Buffer &min, const Buffer &max) const
 Limits the AudioStream the min and max values. More...
 
float64 getMax () const
 Returns the maximum sample value in the Audiostream. More...
 
float64 getMaxMagnitude () const
 Returns the maximum sample magnitude value in the AudioStream, i.e. max(abs(samples)).. More...
 
float64 getMin () const
 Returns the minimum sample value in the AudioStream. More...
 
void mono ()
 Collapses all channels into one Buffer making it mono. More...
 
AudioStream getMono () const
 Collapses all channels into one Buffer making it mono. More...
 
uint32 getNChannels (void) const
 Returns the number of audio channels in the stream. More...
 
float64 getSampleRate () const
 Returns the sample rate of the stream. More...
 
void normalize ()
 Multiplies the AudioStream by a constant gain so the peak sample has magnitude 1.0. More...
 
AudioStream getNormalize () const
 Returns the normalized AudioStream. More...
 
AudioStreamSelection operator() (const BooleanVectorVector &bv)
 Returns an AudioStreamSelection object used for manipulation of a selected region of samples. More...
 
const Bufferoperator[] (uint32 index) const
 These methods provide a reference to the Buffer object held in the channel. More...
 
Bufferoperator[] (uint32 index)
 
float64 operator() (uint32 channel, uint32 index) const
 Returns one sample from the specified channel and index. More...
 
AudioStreamoperator= (const AudioStream &rhs)
 Assignment operator. More...
 
AudioStreamoperator= (const Buffer &rhs)
 Assignment operator, deletes any existing data and sets one channels to rhs. More...
 
boolean operator== (const AudioStream &rhs) const
 Boolean == operator. More...
 
boolean operator!= (const AudioStream &rhs) const
 Boolean != operator. More...
 
AudioStreamoperator<< (const AudioStream &rhs)
 Concatenates or appends rhs to the AudioStream. More...
 
AudioStreamoperator<< (const Buffer &rhs)
 Concatenates or appends rhs to all the channels in the AudioStream. More...
 
AudioStreamoperator<< (float64 d)
 Concatenates or appends rhs to all the channels in the AudioStream. More...
 
BooleanVectorVector operator> (float64 rhs)
 
BooleanVectorVector operator>= (float64 rhs)
 
BooleanVectorVector operator< (float64 rhs)
 
BooleanVectorVector operator<= (float64 rhs)
 
BooleanVectorVector operator== (float64 rhs)
 
BooleanVectorVector operator!= (float64 rhs)
 
AudioStreamoperator+= (const AudioStream &rhs)
 
AudioStreamoperator-= (const AudioStream &rhs)
 
AudioStreamoperator*= (const AudioStream &rhs)
 
AudioStreamoperator/= (const AudioStream &rhs)
 
AudioStreamoperator^= (const AudioStream &rhs)
 
AudioStreamoperator+= (const Buffer &rhs)
 
AudioStreamoperator-= (const Buffer &rhs)
 
AudioStreamoperator*= (const Buffer &rhs)
 
AudioStreamoperator/= (const Buffer &rhs)
 
AudioStreamoperator^= (const Buffer &rhs)
 
AudioStreamoperator+= (float64 d)
 
AudioStreamoperator-= (float64 d)
 
AudioStreamoperator*= (float64 d)
 
AudioStreamoperator/= (float64 d)
 
AudioStreamoperator^= (float64 d)
 
void pad (float64 fill=0.0)
 Pads the AudioStream so that each channel has exactly the same number of samples. More...
 
AudioStream getPad (float64 fill=0.0) const
 
void pan (float64 pan)
 Sets the amplitude level left vs right. More...
 
AudioStream getPan (float64 pan) const
 Sets the amplitude level left vs right. More...
 
void pan (const Buffer &pan)
 Sets the amplitude level left vs right. More...
 
AudioStream getPan (const Buffer &pan) const
 Sets the amplitude level left vs right. More...
 
void plot (const std::string &title="AudioStream") const
 
void readWavefile (const char *filename)
 Read a Wavefile. More...
 
void resample (float64 factor)
 Resample by a non-integer factor. More...
 
void resample (const Buffer &factor)
 Resample by a dynamic non-integer factor. More...
 
AudioStream getResample (float64 factor) const
 Resample by a non-integer factor. More...
 
AudioStream getResample (const Buffer &factor) const
 Resample by a dynamic non-integer factor. More...
 
void resample2 (float64 new_sample_rate)
 Resample to the specified sample rate. More...
 
AudioStream getResample2 (float64 new_sample_rate) const
 Resample to the specified sample rate. More...
 
void reverse ()
 Reverses the AudioStream. More...
 
AudioStream getReverse () const
 Reverses the samples in the AudioStream. More...
 
AudioStreamSelection select (const uint32 start_index, const uint32 stop_index)
 Returns an AudioStreamSelection for the range of indicies. More...
 
std::ostream & write (std::ostream &out) const
 Serializes the AudioStream to output stream, no endian checks. More...
 
std::string write () const
 
std::istream & read (std::istream &stream_in)
 Constructs an AudioStream from seralized data in the inputstream. More...
 
void read (const std::string &string_in)
 
void setNChannels (uint32 channels)
 
void setSampleRate (uint32 sample_rate)
 
void smooth (uint32 n_passes, uint32 n_samples_per_average)
 Implements a standard moving average filter to smooth the waveform. More...
 
AudioStream getSmooth (uint32 n_passes, uint32 n_samples_per_average) const
 Implements a standard moving average filter to smooth the waveform. More...
 
void speedUp (float32 step_size)
 Resamples the AudioStream by the step_size, no interpolation. More...
 
AudioStream getSpeedUp (float32 step_size) const
 Resamples the AudioStream by the step_size, no interpolation. More...
 
void speedUp (const Buffer &step_size)
 Resamples the AudioStream by a variable step_size, no interpolation. More...
 
AudioStream getSpeedUp (const Buffer &step_size) const
 Resamples the AudioStream by a variable step_size, no interpolation. More...
 
void sqrt ()
 Takes the square root of each sample in the AudioStream. More...
 
AudioStream getSqrt () const
 Returns an AudioStream after taking the square root of each sample. More...
 
AudioStream substream (uint32 start_index, uint32 n_samples=0) const
 
AudioStream substream (int32 start_index, int32 n_samples=0) const
 
AudioStream substream (float64 start_time, float64 duration=0) const
 
void transpose ()
 Treating the AudioStream as a matrix, this peforms a matrix transpose. More...
 
AudioStream getTranspose () const
 Retuns a copy of the AudioStream transposed. More...
 
void upSample (uint32 n)
 Upsample by a integral factor. N must be > 1. More...
 
AudioStream getUpSample (uint32 n) const
 Upsample by a integral factor. N must be > 1. More...
 
void writeWavefile (const char *filename) const
 Write the AudioStream to a Wavefile. More...
 
void _set_at_index (int32 i, const Buffer &)
 SWIG helper function. More...
 
void _swig_shadow ()
 SWIG helper function function to shadow. More...
 

Static Public Member Functions

static AudioStream ones (float64 sample_rate, const uint32 n_channels, float64 duration)
 Returns an AudioStream full of ones of duration seconds. More...
 
static AudioStream rand (float64 sample_rate, const uint32 n_channels, float64 duration)
 Returns a Buffer full of random values of length n_samples. More...
 
static AudioStream zeros (float64 sample_rate, const uint32 n_channels, float64 duration)
 Returns a Buffer full of zeros of length n_samples. More...
 

Private Attributes

float64 sample_rate_
 
uint32 channels_
 
std::vector< Buffer * > buffers_
 

Friends

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

Detailed Description

Definition at line 52 of file AudioStream.h.

Constructor & Destructor Documentation

AudioStream::AudioStream ( )

Create an AudioStream.

Definition at line 52 of file AudioStream.cc.

References channels_, and M_ASSERT_VALUE.

53  :
54  sample_rate_(44100.0),
55  channels_(1),
56  buffers_(1, new Buffer())
57 {
58  M_ASSERT_VALUE(channels_, !=, 0);
59 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream::AudioStream ( float64  sample_rate,
uint32  n_channels = 1,
uint32  n_samples_pre_allocate = 4096 
)

Definition at line 62 of file AudioStream.cc.

References buffers_, channels_, and M_ASSERT_VALUE.

66  :
67  sample_rate_(sample_rate),
68  channels_(channels),
69  buffers_()
70 {
71  M_ASSERT_VALUE(channels_, !=, 0);
72 
73  for(uint32 i =0 ; i < channels_; ++i)
74  {
75  auto ptr = new Buffer();
76  ptr->preallocate(n_samples_pre_allocate);
77  buffers_.push_back(ptr);
78  }
79 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream::AudioStream ( const std::string &  filename)

Definition at line 82 of file AudioStream.cc.

83  :
84  sample_rate_(44100.0),
85  channels_(1),
86  buffers_(1, new Buffer())
87 {
88  // This operator is defined in Wavefile.h.
89  *this << filename.c_str();
90 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream::~AudioStream ( )

Destructor.

Definition at line 107 of file AudioStream.cc.

References buffers_.

108 {
109  for(auto * ptr : buffers_) delete ptr;
110 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream::AudioStream ( const AudioStream rhs)

Copy Constructor.

Definition at line 94 of file AudioStream.cc.

References buffers_, and channels_.

95  :
96  sample_rate_(copy.sample_rate_),
97  channels_(copy.channels_),
98  buffers_()
99 {
100  for(uint32 i = 0; i < channels_; ++i)
101  {
102  buffers_.push_back(new Buffer(*copy.buffers_[i]));
103  }
104 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
A Buffer for storing audio samples.
Definition: Buffer.h:60

Member Function Documentation

void AudioStream::abs ( )

This method calls abs on all buffers held in the stream.

Definition at line 114 of file AudioStream.cc.

References buffers_.

Referenced by getAbs(), and Wavefile_UnitTest().

115 {
116  for(auto * ptr : buffers_) ptr->abs();
117 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getAbs ( ) const
inline

Modifies the AudioStream by making any negative value positive.

Definition at line 81 of file AudioStream.h.

References abs().

Referenced by FilterDelay_UnitTest().

82  {AudioStream temp(*this); temp.abs(); return temp;};
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::add ( const AudioStream as,
uint32  offset,
uint32  n_samples = 0 
)

This method adds the passed AudioStream to this AudioStream.

This method adds the passed AudioStream to this AudioStream. If the length of as is longer than this AudioStream, this AudioStream will be extended. If n_samples is 0, the whole length of as will get added, otherwise, only n_samples will get added.

Parameters
offset- The index into this AudioStream to start adding
as- The AudioStream to add data from
n_samples- The number of samples from as to add

Definition at line 121 of file AudioStream.cc.

References buffers_, channels_, and M_ASSERT_VALUE.

Referenced by Nsound::Mixer::getStream(), main(), Nsound::OrganPipe::play(), and Nsound::Hat::play().

125 {
127 
128  for(uint32 i = 0; i < as.channels_; ++i)
129  {
130  buffers_[i]->add(*as.buffers_[i], offset, n_samples);
131  }
132 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
void AudioStream::add ( const AudioStream as,
float64  offset_seconds,
float64  duration_seconds = 0.0 
)

This method adds the passed AudioStream to this AudioStream.

This method adds the passed AudioStream to this AudioStream. If the length of as is longer than this AudioStream, this AudioStream will be extended. If duration_seconds is 0, the whole length of as will get added, otherwise, only duration_seconds will get added.

Parameters
offset_seconds- The offset in seconds into this AudioStream to start adding or appending
as- The source AudioStream to add
duration_seconds- The number of seconds from the source AudioStream to add or append.

Definition at line 136 of file AudioStream.cc.

References buffers_, channels_, M_ASSERT_VALUE, and sample_rate_.

140 {
142 
143  uint32 off = static_cast<uint32>(offset_seconds * sample_rate_);
144  uint32 n_samples = static_cast<uint32>(duration_seconds * sample_rate_);
145 
146  for(uint32 i = 0; i < as.channels_; ++i)
147  {
148  buffers_[i]->add(*as.buffers_[i], off, n_samples);
149  }
150 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
void AudioStream::convolve ( const Buffer b)

Convolves every channel in the AudioStream with the Buffer.

Definition at line 154 of file AudioStream.cc.

References buffers_.

Referenced by getConvolve().

155 {
156  for(auto * ptr : buffers_) ptr->convolve(b);
157 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getConvolve ( const Buffer b) const
inline

Convolves every channel in the AudioStream with the Buffer.

Definition at line 133 of file AudioStream.h.

References convolve().

Referenced by main().

134  { AudioStream temp(*this); temp.convolve(b); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::dB ( )

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

Definition at line 161 of file AudioStream.cc.

References buffers_.

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

162 {
163  for(auto * ptr : buffers_) ptr->dB();
164 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getdB ( ) const
inline

Returns the AudioStream in dB.

Definition at line 142 of file AudioStream.h.

References dB().

143  {AudioStream temp(*this); temp.dB(); return temp;};
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::derivative ( uint32  n)

Calculates the nth derivative of the AudioStream.

Definition at line 168 of file AudioStream.cc.

References buffers_.

Referenced by getDerivative().

169 {
170  for(auto * ptr : buffers_) ptr->derivative(n);
171 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getDerivative ( uint32  n) const
inline

Returns the nth derivative of the Buffer.

Definition at line 151 of file AudioStream.h.

References derivative().

152  { AudioStream temp(*this); temp.derivative(n); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::downSample ( uint32  n)

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

Definition at line 176 of file AudioStream.cc.

References buffers_.

Referenced by getDownSample().

177 {
178  for(auto * ptr : buffers_) ptr->downSample(n);
179 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getDownSample ( uint32  n) const
inline

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

Definition at line 160 of file AudioStream.h.

References downSample().

161  { AudioStream temp(*this); temp.downSample(n); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
float64 AudioStream::getDuration ( ) const

Returns the number of seconds of audio data in the stream.

Definition at line 183 of file AudioStream.cc.

References buffers_, and sample_rate_.

Referenced by main(), my_main(), plot(), and stretch_to().

184 {
185  uint32 length = buffers_[0]->getLength();
186 
187  for(auto * ptr : buffers_)
188  {
189  length = std::min(length, ptr->getLength());
190  }
191 
192  return static_cast<float64>(length) / sample_rate_;
193 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
uint32 AudioStream::getLength ( ) const

Returns the number of samples of audio data in the stream.

Definition at line 197 of file AudioStream.cc.

References buffers_.

Referenced by Nsound::ReverberationRoom::filter(), Nsound::Mixer::getStream(), Nsound::Plotter::imagesc(), main(), my_main(), operator==(), pan(), Nsound::DrumKickBass::play(), play_int(), select(), substream(), testAutioStream(), transpose(), and Nsound::Wavefile::write().

198 {
199  uint32 length = buffers_[0]->getLength();
200 
201  for(auto * ptr : buffers_)
202  {
203  length = std::min(length, ptr->getLength());
204  }
205 
206  return length;
207 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
void AudioStream::limit ( float64  min,
float64  max 
)

Limits the AudioStream the min and max values.

Definition at line 211 of file AudioStream.cc.

References buffers_.

Referenced by getLimit().

212 {
213  for(auto * ptr : buffers_) ptr->limit(min, max);
214 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
void AudioStream::limit ( const Buffer min,
const Buffer max 
)

Limits the AudioStream the min and max values.

Definition at line 218 of file AudioStream.cc.

References buffers_.

219 {
220  for(auto * ptr : buffers_) ptr->limit(min, max);
221 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getLimit ( float64  min,
float64  max 
) const
inline

Limits the AudioStream the min and max values.

Definition at line 181 of file AudioStream.h.

References limit().

182  { AudioStream temp(*this); temp.limit(min, max); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
AudioStream Nsound::AudioStream::getLimit ( const Buffer min,
const Buffer max 
) const
inline

Limits the AudioStream the min and max values.

Definition at line 186 of file AudioStream.h.

References limit().

187  { AudioStream temp(*this); temp.limit(min, max); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
float64 AudioStream::getMax ( ) const

Returns the maximum sample value in the Audiostream.

Definition at line 225 of file AudioStream.cc.

References buffers_.

Referenced by FilterDelay_UnitTest().

226 {
227  float64 max = std::numeric_limits<float64>::min();
228 
229  for(auto * ptr : buffers_) max = std::max(max, ptr->getMax());
230 
231  return max;
232 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
float64 AudioStream::getMaxMagnitude ( ) const

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

Definition at line 236 of file AudioStream.cc.

References buffers_.

Referenced by normalize().

237 {
238  float64 max = std::numeric_limits<float64>::min();
239 
240  for(auto * ptr : buffers_) max = std::max(max, ptr->getMaxMagnitude());
241 
242  return max;
243 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
float64 AudioStream::getMin ( ) const

Returns the minimum sample value in the AudioStream.

Definition at line 247 of file AudioStream.cc.

References buffers_.

248 {
249  float64 min = std::numeric_limits<float64>::max();
250 
251  for(auto * ptr : buffers_) min = std::min(min, ptr->getMin());
252 
253  return min;
254 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
void AudioStream::mono ( )

Collapses all channels into one Buffer making it mono.

Definition at line 258 of file AudioStream.cc.

References getMono().

Referenced by main().

259 {
260  *this = this->getMono();
261 }
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:265
AudioStream AudioStream::getMono ( ) const

Collapses all channels into one Buffer making it mono.

Definition at line 265 of file AudioStream.cc.

References buffers_, channels_, and sample_rate_.

Referenced by mono(), Nsound::Buffer::operator<<(), Nsound::Stretcher::pitchShift(), Nsound::Stretcher::timeShift(), and Nsound::CircularBuffer::write().

266 {
268 
269  a << *buffers_[0];
270 
271  for(uint32 i = 1; i < channels_; ++i)
272  {
273  a += *buffers_[i];
274  }
275 
276  float64 scale = 1.0 / static_cast<float64>(channels_);
277 
278  a *= scale;
279 
280  return a;
281 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
uint32 Nsound::AudioStream::getNChannels ( void  ) const
inline
float64 Nsound::AudioStream::getSampleRate ( ) const
inline
void AudioStream::normalize ( )

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

Definition at line 285 of file AudioStream.cc.

References getMaxMagnitude().

Referenced by getNormalize(), main(), Nsound::Clarinet::play(), Nsound::FluteSlide::play(), and Nsound::OrganPipe::play().

286 {
287  float64 peak = getMaxMagnitude();
288 
289  float64 normal_factor = 1.0 / peak;
290 
291  *this *= normal_factor;
292 }
double float64
Definition: Nsound.h:146
float64 getMaxMagnitude() const
Returns the maximum sample magnitude value in the AudioStream, i.e. max(abs(samples))..
Definition: AudioStream.cc:236
AudioStream Nsound::AudioStream::getNormalize ( ) const
inline

Returns the normalized AudioStream.

Definition at line 225 of file AudioStream.h.

References normalize().

226  {AudioStream temp(*this); temp.normalize(); return temp;};
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
AudioStreamSelection AudioStream::operator() ( const BooleanVectorVector bv)

Returns an AudioStreamSelection object used for manipulation of a selected region of samples.

Definition at line 376 of file AudioStream.cc.

377 {
378  return AudioStreamSelection(*this, bvv);
379 }
A helper class for advance operators.
const Buffer & AudioStream::operator[] ( uint32  index) const

These methods provide a reference to the Buffer object held in the channel.

Definition at line 297 of file AudioStream.cc.

References buffers_, channels_, and M_ASSERT_VALUE.

298 {
299  M_ASSERT_VALUE(i, <, channels_);
300  return *buffers_[i];
301 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
Buffer & AudioStream::operator[] ( uint32  index)

Definition at line 305 of file AudioStream.cc.

References buffers_, channels_, and M_ASSERT_VALUE.

306 {
307  M_ASSERT_VALUE(i, <, channels_);
308  return *buffers_[i];
309 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
float64 AudioStream::operator() ( uint32  channel,
uint32  index 
) const

Returns one sample from the specified channel and index.

Definition at line 368 of file AudioStream.cc.

References buffers_, channels_, and M_ASSERT_VALUE.

369 {
370  M_ASSERT_VALUE(channel, <, channels_);
371  return (*buffers_[channel])[index];
372 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator= ( const AudioStream rhs)

Assignment operator.

Definition at line 313 of file AudioStream.cc.

References buffers_, channels_, sample_rate_, and setNChannels().

314 {
315  if(this == &rhs) return *this;
316 
317  setNChannels(rhs.channels_);
318 
319  for(uint32 i = 0; i < rhs.channels_; ++i)
320  {
321  *buffers_[i] = *rhs.buffers_[i];
322  }
323 
325 
326  return *this;
327 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
void setNChannels(uint32 channels)
Definition: AudioStream.cc:781
AudioStream & AudioStream::operator= ( const Buffer rhs)

Assignment operator, deletes any existing data and sets one channels to rhs.

Definition at line 331 of file AudioStream.cc.

References buffers_.

332 {
333  for(auto * ptr : buffers_)
334  {
335  *ptr = rhs;
336  }
337 
338  return *this;
339 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
boolean AudioStream::operator== ( const AudioStream rhs) const

Boolean == operator.

Definition at line 343 of file AudioStream.cc.

References buffers_, channels_, and getLength().

344 {
345  if(channels_ != rhs.channels_)
346  {
347  return false;
348  }
349 
350  if(getLength() != rhs.getLength())
351  {
352  return false;
353  }
354 
355  for(uint32 i = 0; i < channels_; ++i)
356  {
357  if(*buffers_[i] != *rhs.buffers_[i])
358  {
359  return false;
360  }
361  }
362 
363  return true;
364 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
boolean Nsound::AudioStream::operator!= ( const AudioStream rhs) const
inline

Boolean != operator.

Definition at line 262 of file AudioStream.h.

262 {return !(*this == rhs);};
AudioStream & AudioStream::operator<< ( const AudioStream rhs)

Concatenates or appends rhs to the AudioStream.

Definition at line 383 of file AudioStream.cc.

References channels_, and M_ASSERT_VALUE.

384 {
385  if(rhs.channels_ == 1) return *this << rhs[0];
386 
388 
389  for(uint32 i = 0; i < channels_; ++i)
390  {
391  (*this)[i] << rhs[i];
392  }
393 
394  return *this;
395 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
AudioStream & AudioStream::operator<< ( const Buffer rhs)

Concatenates or appends rhs to all the channels in the AudioStream.

Definition at line 399 of file AudioStream.cc.

References buffers_.

400 {
401  for(auto * ptr : buffers_) *ptr << rhs;
402  return *this;
403 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator<< ( float64  d)

Concatenates or appends rhs to all the channels in the AudioStream.

Definition at line 527 of file AudioStream.cc.

References buffers_.

528 {
529  for(auto * ptr : buffers_) *ptr << d;
530  return *this;
531 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
BooleanVectorVector Nsound::AudioStream::operator> ( float64  rhs)

Definition at line 970 of file AudioStream.cc.

971 {
973 
974  for(auto * ptr : buffers_) bv.push_back( *ptr > rhs );
975 
976  return bv;
977 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
std::vector< std::vector< boolean > > BooleanVectorVector
BooleanVectorVector Nsound::AudioStream::operator>= ( float64  rhs)

Definition at line 982 of file AudioStream.cc.

983 {
985 
986  for(auto * ptr : buffers_) bv.push_back( *ptr >= rhs );
987 
988  return bv;
989 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
std::vector< std::vector< boolean > > BooleanVectorVector
BooleanVectorVector Nsound::AudioStream::operator< ( float64  rhs)

Definition at line 994 of file AudioStream.cc.

995 {
997 
998  for(auto * ptr : buffers_) bv.push_back( *ptr < rhs );
999 
1000  return bv;
1001 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
std::vector< std::vector< boolean > > BooleanVectorVector
BooleanVectorVector Nsound::AudioStream::operator<= ( float64  rhs)

Definition at line 1006 of file AudioStream.cc.

1007 {
1009 
1010  for(auto * ptr : buffers_) bv.push_back( *ptr <= rhs );
1011 
1012  return bv;
1013 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
std::vector< std::vector< boolean > > BooleanVectorVector
BooleanVectorVector Nsound::AudioStream::operator== ( float64  rhs)

Definition at line 1018 of file AudioStream.cc.

1019 {
1021 
1022  for(auto * ptr : buffers_) bv.push_back( *ptr == rhs );
1023 
1024  return bv;
1025 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
std::vector< std::vector< boolean > > BooleanVectorVector
BooleanVectorVector Nsound::AudioStream::operator!= ( float64  rhs)

Definition at line 1030 of file AudioStream.cc.

1031 {
1033 
1034  for(auto * ptr : buffers_) bv.push_back( *ptr != rhs );
1035 
1036  return bv;
1037 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
std::vector< std::vector< boolean > > BooleanVectorVector
AudioStream & AudioStream::operator+= ( const AudioStream rhs)

Definition at line 407 of file AudioStream.cc.

References channels_, getNChannels(), and M_ASSERT_VALUE.

408 {
409  if(rhs.channels_ == 1) return *this += rhs[0];
410 
412 
413  for(uint32 i = 0; i < channels_; ++i)
414  {
415  (*this)[i] += rhs[i];
416  }
417  return *this;
418 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream & AudioStream::operator-= ( const AudioStream rhs)

Definition at line 430 of file AudioStream.cc.

References channels_, and M_ASSERT_VALUE.

431 {
432  if(rhs.channels_ == 1) return *this -= rhs[0];
433 
435 
436  for(uint32 i = 0; i < channels_; ++i)
437  {
438  (*this)[i] -= rhs[i];
439  }
440  return *this;
441 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
AudioStream & AudioStream::operator*= ( const AudioStream rhs)

Definition at line 453 of file AudioStream.cc.

References channels_, and M_ASSERT_VALUE.

454 {
455  if(rhs.channels_ == 1) return *this *= rhs[0];
456 
458 
459  for(uint32 i = 0; i < channels_; ++i)
460  {
461  (*this)[i] *= rhs[i];
462  }
463  return *this;
464 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
AudioStream & AudioStream::operator/= ( const AudioStream rhs)

Definition at line 477 of file AudioStream.cc.

References channels_, and M_ASSERT_VALUE.

478 {
479  if(rhs.channels_ == 1) return *this /= rhs[0];
480 
482 
483  for(uint32 i = 0; i < channels_; ++i)
484  {
485  (*this)[i] /= rhs[i];
486  }
487  return *this;
488 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
AudioStream & AudioStream::operator^= ( const AudioStream rhs)

Definition at line 502 of file AudioStream.cc.

References channels_, and M_ASSERT_VALUE.

503 {
504  if(rhs.channels_ == 1) return *this ^= rhs[0];
505 
507 
508  for(uint32 i = 0; i < channels_; ++i)
509  {
510  (*this)[i] ^= rhs[i];
511  }
512  return *this;
513 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
AudioStream & AudioStream::operator+= ( const Buffer rhs)

Definition at line 422 of file AudioStream.cc.

References buffers_.

423 {
424  for(auto * ptr : buffers_) *ptr += rhs;
425  return *this;
426 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator-= ( const Buffer rhs)

Definition at line 445 of file AudioStream.cc.

References buffers_.

446 {
447  for(auto * ptr : buffers_) *ptr -= rhs;
448  return *this;
449 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator*= ( const Buffer rhs)

Definition at line 468 of file AudioStream.cc.

References buffers_.

469 {
470  for(auto * ptr : buffers_) *ptr *= rhs;
471  return *this;
472 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator/= ( const Buffer rhs)

Definition at line 493 of file AudioStream.cc.

References buffers_.

494 {
495  for(auto * ptr : buffers_) *ptr /= rhs;
496  return *this;
497 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator^= ( const Buffer rhs)

Definition at line 518 of file AudioStream.cc.

References buffers_.

519 {
520  for(auto * ptr : buffers_) *ptr ^= rhs;
521  return *this;
522 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator+= ( float64  d)

Definition at line 535 of file AudioStream.cc.

References buffers_.

536 {
537  for(auto * ptr : buffers_) *ptr += d;
538  return *this;
539 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator-= ( float64  d)

Definition at line 543 of file AudioStream.cc.

References buffers_.

544 {
545  for(auto * ptr : buffers_) *ptr -= d;
546  return *this;
547 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator*= ( float64  d)

Definition at line 551 of file AudioStream.cc.

References buffers_.

552 {
553  for(auto * ptr : buffers_) *ptr *= d;
554  return *this;
555 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator/= ( float64  d)

Definition at line 559 of file AudioStream.cc.

References buffers_.

560 {
561  for(auto * ptr : buffers_) *ptr /= d;
562  return *this;
563 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & AudioStream::operator^= ( float64  d)

Definition at line 567 of file AudioStream.cc.

References buffers_.

568 {
569  for(auto * ptr : buffers_) *ptr ^= d;
570  return *this;
571 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
void AudioStream::pad ( float64  fill = 0.0)

Pads the AudioStream so that each channel has exactly the same number of samples.

Definition at line 602 of file AudioStream.cc.

References buffers_, and Nsound::Buffer::ones().

Referenced by getPad(), and transpose().

603 {
604  uint32 min = std::numeric_limits<uint32>::max();
605  uint32 max = 0;
606 
607  for(auto * ptr : buffers_)
608  {
609  min = std::min(min, ptr->getLength());
610  max = std::max(max, ptr->getLength());
611  }
612 
613  if(min == max) return;
614 
615  for(auto * ptr : buffers_)
616  {
617  uint32 n_fill = max - ptr->getLength();
618 
619  if(n_fill == 0) continue;
620 
621  *ptr << fill * Buffer::ones(n_fill);
622  }
623 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
static Buffer ones(const uint32 n_samples)
Returns a Buffer full of ones of length n_samples.
Definition: Buffer.cc:2239
AudioStream Nsound::AudioStream::getPad ( float64  fill = 0.0) const
inline

Definition at line 322 of file AudioStream.h.

References pad().

323  { AudioStream temp(*this); temp.pad(fill); return temp;};
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::pan ( float64  pan)

Sets the amplitude level left vs right.

Sets the amplitude level between left and right channels.

pre> 1 = 100% left volume, 0% right volume 0 = 50% left volume, 50% right volume -1 = 0% left volume, 100% right volume /pre>

Parameters
pan- Static levels between channels accross all samples

Definition at line 627 of file AudioStream.cc.

References buffers_, channels_, and M_ASSERT_VALUE.

Referenced by getPan(), main(), and testAutioStream().

628 {
629  M_ASSERT_VALUE(channels_, ==, 2);
630 
631  float64 left_amplitude = (pan + 1.0) * 0.5;
632  float64 right_amplitude = ((pan * -1.0) + 1.0) * 0.5;
633 
634  *buffers_[0] *= left_amplitude;
635  *buffers_[1] *= right_amplitude;
636 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
void pan(float64 pan)
Sets the amplitude level left vs right.
Definition: AudioStream.cc:627
AudioStream Nsound::AudioStream::getPan ( float64  pan) const
inline

Sets the amplitude level left vs right.

Sets the amplitude level between left and right channels.

pre> 1 = 100% left volume, 0% right volume 0 = 50% left volume, 50% right volume -1 = 0% left volume, 100% right volume /pre>

Parameters
pan- Static levels between channels accross all samples

Definition at line 353 of file AudioStream.h.

References pan().

Referenced by main().

354  { AudioStream temp(*this); temp.pan(pan); return temp;};
void pan(float64 pan)
Sets the amplitude level left vs right.
Definition: AudioStream.cc:627
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::pan ( const Buffer pan)

Sets the amplitude level left vs right.

Sets the amplitude level between left and right channels.

pre> 1 = 100% left volume, 0% right volume 0 = 50% left volume, 50% right volume -1 = 0% left volume, 100% right volume /pre>

Parameters
pan- Dynamic levels between channels

Definition at line 640 of file AudioStream.cc.

References buffers_, channels_, getLength(), Nsound::Buffer::getLength(), and M_ASSERT_VALUE.

641 {
642  M_ASSERT_VALUE(channels_, ==, 2);
643 
644  uint32 length = std::min(getLength(), pan.getLength());
645 
646  Buffer & left = *buffers_[0];
647  Buffer & right = *buffers_[1];
648 
649  for(uint32 i = 0; i < length; ++i)
650  {
651  float64 left_amplitude = (pan[i] + 1.0) * 0.5;
652  float64 right_amplitude = ((pan[i] * -1.0) + 1.0) * 0.5;
653 
654  left[i] *= left_amplitude;
655  right[i] *= right_amplitude;
656  }
657 }
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 of audio data in the stream.
Definition: AudioStream.cc:197
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream Nsound::AudioStream::getPan ( const Buffer pan) const
inline

Sets the amplitude level left vs right.

Sets the amplitude level between left and right channels.

pre> 1 = 100% left volume, 0% right volume 0 = 50% left volume, 50% right volume -1 = 0% left volume, 100% right volume /pre>

Parameters
pan- Dynamic levels between channels

Definition at line 384 of file AudioStream.h.

References pan().

385  { AudioStream temp(*this); temp.pan(pan); return temp;};
void pan(float64 pan)
Sets the amplitude level left vs right.
Definition: AudioStream.cc:627
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::plot ( const std::string &  title = "AudioStream") const

Definition at line 661 of file AudioStream.cc.

References buffers_, channels_, Nsound::Generator::drawLine(), Nsound::Plotter::figure(), getDuration(), Nsound::Plotter::plot(), sample_rate_, Nsound::Plotter::subplot(), Nsound::Plotter::title(), Nsound::Plotter::xlabel(), and Nsound::Plotter::ylabel().

Referenced by FilterDelay_UnitTest(), main(), and Wavefile_UnitTest().

662 {
663  Plotter pylab;
664 
665  pylab.figure();
666 
667  uint32 n_rows = channels_;
668  uint32 n_columns = 1;
669 
670  // Create the x axis based on seconds.
671  Sine sin(sample_rate_);
672 
673  Buffer x_axis = sin.drawLine(getDuration(), 0.0, getDuration());
674 
675  // For each buffer, plot it
676  uint32 i = 0;
677  for(auto * ptr : buffers_)
678  {
679  pylab.subplot(n_rows, n_columns, i + 1);
680 
681  if(i == 0)
682  {
683  pylab.title(title);
684  }
685 
686  pylab.plot(x_axis, *ptr);
687 
688  pylab.xlabel("Time (sec)");
689  pylab.ylabel("Amplitude");
690 
691  ++i;
692  }
693 }
unsigned int uint32
Definition: Nsound.h:153
void xlabel(const std::string &label, const std::string &kwargs="")
Add a label x axis.
Definition: Plotter.cc:1154
float64 getDuration() const
Returns the number of seconds of audio data in the stream.
Definition: AudioStream.cc:183
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
Definition: Plotter.cc:765
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
Definition: Plotter.cc:455
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
Definition: Plotter.cc:1127
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
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)
Definition: Plotter.cc:1031
void ylabel(const std::string &label, const std::string &kwargs="")
Add a label y axis.
Definition: Plotter.cc:1180
A Buffer for storing audio samples.
Definition: Buffer.h:60
DOXME.
Definition: Sine.h:43
void AudioStream::readWavefile ( const char *  filename)

Read a Wavefile.

Definition at line 697 of file AudioStream.cc.

References M_CHECK_PTR.

698 {
699  M_CHECK_PTR(filename);
700 
701  *this << filename;
702 }
#define M_CHECK_PTR(ptr)
Definition: Macros.h:64
void AudioStream::resample ( float64  factor)

Resample by a non-integer factor.

Definition at line 706 of file AudioStream.cc.

References buffers_.

Referenced by getResample(), my_main(), and resample2().

707 {
708  for(auto * ptr : buffers_) ptr->resample(factor);
709 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
void AudioStream::resample ( const Buffer factor)

Resample by a dynamic non-integer factor.

Definition at line 713 of file AudioStream.cc.

References buffers_.

714 {
715  for(auto * ptr : buffers_) ptr->resample(factor);
716 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getResample ( float64  factor) const
inline

Resample by a non-integer factor.

Definition at line 405 of file AudioStream.h.

References resample().

406  { AudioStream temp(*this); temp.resample(factor); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
AudioStream Nsound::AudioStream::getResample ( const Buffer factor) const
inline

Resample by a dynamic non-integer factor.

Definition at line 410 of file AudioStream.h.

References resample().

411  { AudioStream temp(*this); temp.resample(factor); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::resample2 ( float64  new_sample_rate)

Resample to the specified sample rate.

Definition at line 720 of file AudioStream.cc.

References M_ASSERT_VALUE, resample(), and sample_rate_.

Referenced by getResample2(), and main().

721 {
722  M_ASSERT_VALUE(new_sample_rate, >, 0.0);
723 
724  float64 ratio = new_sample_rate / sample_rate_;
725 
726  resample(ratio);
727 
728  sample_rate_ = new_sample_rate;
729 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
void resample(float64 factor)
Resample by a non-integer factor.
Definition: AudioStream.cc:706
AudioStream Nsound::AudioStream::getResample2 ( float64  new_sample_rate) const
inline

Resample to the specified sample rate.

Definition at line 419 of file AudioStream.h.

References resample2().

420  {
421  AudioStream temp(*this);
422  temp.resample2(new_sample_rate);
423  return temp;
424  };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::reverse ( )

Reverses the AudioStream.

Definition at line 733 of file AudioStream.cc.

References buffers_.

Referenced by getReverse(), and testAutioStream().

734 {
735  for(auto * ptr : buffers_) ptr->reverse();
736 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getReverse ( ) const
inline

Reverses the samples in the AudioStream.

Definition at line 433 of file AudioStream.h.

References reverse().

Referenced by main().

434  {AudioStream temp(*this); temp.reverse(); return temp;};
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
AudioStreamSelection AudioStream::select ( const uint32  start_index,
const uint32  stop_index 
)

Returns an AudioStreamSelection for the range of indicies.

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

Definition at line 740 of file AudioStream.cc.

References getLength().

741 {
743 
744  uint32 n_samples = getLength();
745 
746  for(uint32 i = start_index; i < stop_index; ++i)
747  {
748  // Create a BooleanVector of true for the length of the Buffer
749  BooleanVector bv(n_samples, true);
750 
751  bvv.push_back(bv);
752  }
753 
754  return AudioStreamSelection(*this, bvv);
755 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
std::vector< std::vector< boolean > > BooleanVectorVector
std::vector< boolean > BooleanVector
A helper class for advance operators.
std::ostream & AudioStream::write ( std::ostream &  out) const

Serializes the AudioStream to output stream, no endian checks.

Parameters
outthe std::ostream to write bytes to

Definition at line 902 of file AudioStream.cc.

References buffers_, getNChannels(), and getSampleRate().

903 {
904  out & 'a' & 'u' & 'd' & 'i' & 'o' & 's' & 't' & 'r'
905  & getSampleRate()
906  & getNChannels();
907 
908  for(auto * ptr : buffers_) ptr->write(out);
909 
910  return out;
911 }
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
std::string AudioStream::write ( ) const

Definition at line 915 of file AudioStream.cc.

916 {
917  std::stringstream ss;
918  write(ss);
919  return ss.str();
920 }
std::string write() const
Definition: AudioStream.cc:915
std::istream & AudioStream::read ( std::istream &  stream_in)

Constructs an AudioStream from seralized data in the inputstream.

Parameters
inthe std::istream to read bytes from

Definition at line 924 of file AudioStream.cc.

References buffers_, M_THROW, sample_rate_, setNChannels(), and sr.

Referenced by read().

925 {
926  char id[8];
927  float64 sr = 0;
928  uint32 n_channels = 0;
929 
930  in
931  & id[0] & id[1] & id[2] & id[3] & id[4] & id[5] & id[6] & id[7]
932  & sr & n_channels;
933 
934  if(
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')
937  {
938  M_THROW("Did not find any Nsound AudioStream data in input stream!");
939  }
940 
941  sample_rate_ = sr;
942 
943  setNChannels(n_channels);
944 
945  for(auto * ptr : buffers_) ptr->read(in);
946 
947  return in;
948 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
double float64
Definition: Nsound.h:146
void setNChannels(uint32 channels)
Definition: AudioStream.cc:781
#define M_THROW(message)
Definition: Macros.h:108
float64 sr
Definition: example3.cc:24
void AudioStream::read ( const std::string &  string_in)

Definition at line 952 of file AudioStream.cc.

References read().

953 {
954  std::stringstream ss(in);
955  read(ss);
956 }
std::istream & read(std::istream &stream_in)
Constructs an AudioStream from seralized data in the inputstream.
Definition: AudioStream.cc:924
void AudioStream::setNChannels ( uint32  channels)

Definition at line 781 of file AudioStream.cc.

References buffers_, and channels_.

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

782 {
783  if(channels_ != channels)
784  {
785  for(auto * ptr : buffers_) delete ptr;
786 
787  buffers_.clear();
788 
789  channels_ = channels;
790 
791  for(uint32 i = 0 ; i < channels_; ++i) buffers_.push_back(new Buffer());
792  }
793 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
A Buffer for storing audio samples.
Definition: Buffer.h:60
void Nsound::AudioStream::setSampleRate ( uint32  sample_rate)
inline

Definition at line 483 of file AudioStream.h.

References sample_rate_.

Referenced by my_main().

483 {sample_rate_ = sample_rate;}
void AudioStream::smooth ( uint32  n_passes,
uint32  n_samples_per_average 
)

Implements a standard moving average filter to smooth the waveform.

n_passes = the number of times to calculate the moving average n_samples_per_average = the number of samples to average to create one sample in the output.

Definition at line 797 of file AudioStream.cc.

References buffers_.

Referenced by getSmooth().

798 {
799  for(auto * ptr : buffers_) ptr->smooth(n_passes, n_samples_per_average);
800 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getSmooth ( uint32  n_passes,
uint32  n_samples_per_average 
) const
inline

Implements a standard moving average filter to smooth the waveform.

Definition at line 495 of file AudioStream.h.

References smooth().

496  {AudioStream temp(*this); temp.smooth(n_passes, n_samples_per_average); return temp;};
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::speedUp ( float32  step_size)

Resamples the AudioStream by the step_size, no interpolation.

Definition at line 804 of file AudioStream.cc.

References buffers_.

Referenced by getSpeedUp().

805 {
806  for(auto * ptr : buffers_) ptr->speedUp(step_size);
807 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getSpeedUp ( float32  step_size) const
inline

Resamples the AudioStream by the step_size, no interpolation.

Definition at line 504 of file AudioStream.h.

References speedUp().

505  { AudioStream temp(*this); temp.speedUp(step_size); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::speedUp ( const Buffer step_size)

Resamples the AudioStream by a variable step_size, no interpolation.

Definition at line 811 of file AudioStream.cc.

References buffers_.

812 {
813  for(auto * ptr : buffers_) ptr->speedUp(step_buffer);
814 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getSpeedUp ( const Buffer step_size) const
inline

Resamples the AudioStream by a variable step_size, no interpolation.

Definition at line 513 of file AudioStream.h.

References speedUp().

514  { AudioStream temp(*this); temp.speedUp(step_size); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::sqrt ( )

Takes the square root of each sample in the AudioStream.

Definition at line 818 of file AudioStream.cc.

References buffers_.

Referenced by getSqrt().

819 {
820  for(auto * ptr : buffers_) ptr->sqrt();
821 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getSqrt ( ) const
inline

Returns an AudioStream after taking the square root of each sample.

Definition at line 522 of file AudioStream.h.

References sqrt().

523  { AudioStream temp(*this); temp.sqrt(); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
AudioStream AudioStream::substream ( uint32  start_index,
uint32  n_samples = 0 
) const

Definition at line 825 of file AudioStream.cc.

References channels_, and sample_rate_.

Referenced by Nsound::Mixer::getStream(), main(), Nsound::Clarinet::play(), substream(), and testAutioStream().

826 {
827  AudioStream new_stream(sample_rate_, channels_);
828 
829  for(uint32 i = 0; i < channels_; ++i)
830  {
831  new_stream[i] = (*this)[i].subbuffer(start_index, n_samples);
832  }
833 
834  return new_stream;
835 }
unsigned int uint32
Definition: Nsound.h:153
AudioStream AudioStream::substream ( int32  start_index,
int32  n_samples = 0 
) const

Definition at line 840 of file AudioStream.cc.

References M_ASSERT_VALUE, and substream().

841 {
842  M_ASSERT_VALUE(start_index, >=, 0);
843  M_ASSERT_VALUE(n_samples, >=, 0);
844 
845  return substream(
846  static_cast<uint32>(start_index),
847  static_cast<uint32>(n_samples));
848 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
AudioStream substream(uint32 start_index, uint32 n_samples=0) const
Definition: AudioStream.cc:825
AudioStream AudioStream::substream ( float64  start_time,
float64  duration = 0 
) const

Definition at line 853 of file AudioStream.cc.

References buffers_, channels_, getLength(), and sample_rate_.

854 {
855  AudioStream new_stream(sample_rate_, channels_);
856 
857  uint32 start_index = static_cast<uint32>(start_time * sample_rate_);
858  uint32 n_samples = static_cast<uint32>(n_seconds * sample_rate_);
859 
860  if(n_seconds == 0 || n_samples + start_index >= getLength())
861  {
862  n_samples = getLength() - start_index;
863  }
864 
865  for(uint32 i = 0; i < channels_; ++i)
866  {
867  new_stream[i] = buffers_[i]->subbuffer(start_index, n_samples);
868  }
869 
870  return new_stream;
871 }
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
static double start_time
Definition: TicToc.cc:35
void AudioStream::transpose ( )

Treating the AudioStream as a matrix, this peforms a matrix transpose.

Treat the AudioStream like a 2D matrix where the number of channels is the first dimension, or the rows, and the length of each Buffer in the AudioStream is the number of columns.

Transposing the matrix swaps the rows with the columns. For example, the first row (or AudioStream channel) becomes the first column, etc.

| 0 1 |^T | 0 2 4 | | 2 3 | = | 1 3 5 | | 4 5 |

Definition at line 875 of file AudioStream.cc.

References channels_, getLength(), getSampleRate(), and pad().

Referenced by getTranspose(), my_main(), and Nsound::Spectrogram::plot().

876 {
877  // Pad with zeros if necessary.
878  this->pad(0.0);
879 
880  AudioStream new_as(getSampleRate(), getLength());
881 
882  for(uint32 i = 0; i < getLength(); ++i)
883  {
884  for(uint32 j = 0; j < channels_; ++j)
885  {
886  new_as[i] << (*this)[j][i];
887  }
888  }
889 
890  *this = new_as;
891 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
void pad(float64 fill=0.0)
Pads the AudioStream so that each channel has exactly the same number of samples. ...
Definition: AudioStream.cc:602
AudioStream Nsound::AudioStream::getTranspose ( ) const
inline

Retuns a copy of the AudioStream transposed.

Definition at line 554 of file AudioStream.h.

References transpose().

Referenced by my_main().

555  { AudioStream temp(*this); temp.transpose(); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::upSample ( uint32  n)

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

Definition at line 895 of file AudioStream.cc.

References buffers_.

Referenced by getUpSample().

896 {
897  for(auto * ptr : buffers_) ptr->upSample(n);
898 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream Nsound::AudioStream::getUpSample ( uint32  n) const
inline

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

Definition at line 563 of file AudioStream.h.

References upSample().

564  { AudioStream temp(*this); temp.upSample(n); return temp; };
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void AudioStream::writeWavefile ( const char *  filename) const

Write the AudioStream to a Wavefile.

Definition at line 960 of file AudioStream.cc.

References M_CHECK_PTR.

961 {
962  M_CHECK_PTR(filename);
963  *this >> filename;
964 }
#define M_CHECK_PTR(ptr)
Definition: Macros.h:64
void AudioStream::_set_at_index ( int32  i,
const Buffer b 
)

SWIG helper function.

Definition at line 759 of file AudioStream.cc.

References buffers_, getNChannels(), and M_THROW.

760 {
761  int32 len = getNChannels();
762 
763  if(i >= 0 && i < len)
764  {
765  *buffers_[i] = b;
766  }
767  else
768  if(i < 0 && i >= -len)
769  {
770  *buffers_[len + i] = b;
771  }
772  else
773  {
774  M_THROW(
775  "IndexError: " << i << " is out of bounds (0 : " << len << ")");
776  }
777 }
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
#define M_THROW(message)
Definition: Macros.h:108
signed int int32
Definition: Nsound.h:142
void Nsound::AudioStream::_swig_shadow ( )
inline

SWIG helper function function to shadow.

Definition at line 574 of file AudioStream.h.

574 {};
AudioStream AudioStream::ones ( float64  sample_rate,
const uint32  n_channels,
float64  duration 
)
static

Returns an AudioStream full of ones of duration seconds.

Definition at line 1041 of file AudioStream.cc.

References Nsound::Generator::drawLine().

1045 {
1046  AudioStream a(sample_rate, n_channels);
1047 
1048  Generator g(sample_rate);
1049 
1050  a << g.drawLine(duration, 1.0, 1.0);
1051 
1052  return a;
1053 }
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
AudioStream AudioStream::rand ( float64  sample_rate,
const uint32  n_channels,
float64  duration 
)
static

Returns a Buffer full of random values of length n_samples.

Definition at line 1057 of file AudioStream.cc.

References Nsound::Generator::whiteNoise().

1061 {
1062  AudioStream a(sample_rate, n_channels);
1063 
1064  Generator g(sample_rate);
1065 
1066  a << g.whiteNoise(duration);
1067 
1068  return a;
1069 }
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
AudioStream AudioStream::zeros ( float64  sample_rate,
const uint32  n_channels,
float64  duration 
)
static

Returns a Buffer full of zeros of length n_samples.

Definition at line 1073 of file AudioStream.cc.

References Nsound::Generator::drawLine().

1077 {
1078  AudioStream a(sample_rate, n_channels);
1079 
1080  Generator g(sample_rate);
1081 
1082  a << g.drawLine(duration, 0.0, 0.0);
1083 
1084  return a;
1085 }
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50

Friends And Related Function Documentation

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

Member Data Documentation

float64 Nsound::AudioStream::sample_rate_
private
uint32 Nsound::AudioStream::channels_
private
std::vector<Buffer *> Nsound::AudioStream::buffers_
private

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