Nsound  0.9.4
AudioStream.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: AudioStream.h 912 2015-07-26 00:50:29Z weegreenblobbie $
4 //
5 // Nsound is a C++ library and Python module for audio synthesis featuring
6 // dynamic digital filters. Nsound lets you easily shape waveforms and write
7 // to disk or plot them. Nsound aims to be as powerful as Csound but easy to
8 // use.
9 //
10 // Copyright (c) 2004-Present Nick Hilton
11 //
12 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
13 //
14 //-----------------------------------------------------------------------------
15 
16 //-----------------------------------------------------------------------------
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU Library General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 //
32 //-----------------------------------------------------------------------------
33 #ifndef _NSOUND_AUDIO_STREAM_H_
34 #define _NSOUND_AUDIO_STREAM_H_
35 
36 #include <Nsound/Nsound.h>
38 #include <Nsound/Buffer.h>
39 
40 #include <iostream>
41 #include <string>
42 #include <vector>
43 
44 namespace Nsound
45 {
46 
47 //-----------------------------------------------------------------------------
48 //
49 // Class AudioStream
50 //
51 //-----------------------------------------------------------------------------
53 {
54 
55 public:
56 
58  AudioStream();
60  float64 sample_rate,
61  uint32 n_channels = 1,
62  uint32 n_samples_pre_allocate = 4096);
63 
64  AudioStream(const std::string & filename);
65 
67  ~AudioStream();
68 
70  AudioStream(const AudioStream & rhs);
71 
72  // abs()
73  //
75  //
76  void
77  abs();
78 
81  getAbs() const
82  {AudioStream temp(*this); temp.abs(); return temp;};
83 
84  // add()
85  //
87  //
99  void
100  add(const AudioStream & as,
101  uint32 offset,
102  uint32 n_samples = 0);
103 
104  // add()
105  //
107  //
121  void
122  add(
123  const AudioStream & as,
124  float64 offset_seconds,
125  float64 duration_seconds = 0.0);
126 
128  void
129  convolve(const Buffer & b);
130 
133  getConvolve(const Buffer & b) const
134  { AudioStream temp(*this); temp.convolve(b); return temp; };
135 
137  void
138  dB();
139 
142  getdB() const
143  {AudioStream temp(*this); temp.dB(); return temp;};
144 
146  void
147  derivative(uint32 n);
148 
152  { AudioStream temp(*this); temp.derivative(n); return temp; };
153 
155  void
156  downSample(uint32 n);
157 
161  { AudioStream temp(*this); temp.downSample(n); return temp; };
162 
164  float64
165  getDuration() const;
166 
168  uint32
169  getLength() const;
170 
172  void
173  limit(float64 min, float64 max);
174 
176  void
177  limit(const Buffer & min, const Buffer & max);
178 
181  getLimit(float64 min, float64 max) const
182  { AudioStream temp(*this); temp.limit(min, max); return temp; };
183 
186  getLimit(const Buffer & min, const Buffer & max) const
187  { AudioStream temp(*this); temp.limit(min, max); return temp; };
188 
190  float64
191  getMax() const;
192 
194  float64
195  getMaxMagnitude() const;
196 
198  float64
199  getMin() const;
200 
202  void
203  mono();
204 
207  getMono() const;
208 
210  inline
211  uint32
212  getNChannels(void) const {return channels_;}
213 
215  inline
216  float64
217  getSampleRate() const {return sample_rate_;}
218 
220  void
221  normalize();
222 
225  getNormalize() const
226  {AudioStream temp(*this); temp.normalize(); return temp;};
227 
230  operator()(const BooleanVectorVector & bv);
231 
233  const Buffer &
234  operator[](uint32 index) const;
235 
236  Buffer &
237  operator[](uint32 index);
238 
239  // Operator ()
241  float64
242  operator()(uint32 channel, uint32 index) const;
243 
244  // Operator =
246  AudioStream &
247  operator=(const AudioStream & rhs);
248 
249  // Operator =
251  AudioStream &
252  operator=(const Buffer & rhs);
253 
254  // Operator ==
256  boolean
257  operator==(const AudioStream & rhs) const;
258 
259  // Operator !=
261  inline boolean
262  operator!=(const AudioStream & rhs) const {return !(*this == rhs);};
263 
265  AudioStream &
266  operator<<(const AudioStream & rhs);
267 
269  AudioStream &
270  operator<<(const Buffer & rhs);
271 
273  AudioStream &
274  operator<<(float64 d);
275 
282 
283  //
284  // math operators
285  //
286  // DOXME
287  AudioStream & operator+=(const AudioStream & rhs);
288  AudioStream & operator-=(const AudioStream & rhs);
289  AudioStream & operator*=(const AudioStream & rhs);
290  AudioStream & operator/=(const AudioStream & rhs);
291  AudioStream & operator^=(const AudioStream & rhs);
292 
293  AudioStream & operator+=(const Buffer & rhs);
294  AudioStream & operator-=(const Buffer & rhs);
295  AudioStream & operator*=(const Buffer & rhs);
296  AudioStream & operator/=(const Buffer & rhs);
297  AudioStream & operator^=(const Buffer & rhs);
298 
304 
305  #ifndef SWIG
306  // Output Operator << AudioStream
307  // DOXME
308  friend
309  std::ostream &
310  operator<<(std::ostream & out, const AudioStream & rhs);
311  #endif
312 
314  //
315  // DOXME
316  void
317  pad(float64 fill = 0.0);
318 
319  //
320  // DOXME
322  getPad(float64 fill = 0.0) const
323  { AudioStream temp(*this); temp.pad(fill); return temp;};
324 
326  //
336  //
337  void
338  pan(float64 pan);
339 
341  //
351  //
354  { AudioStream temp(*this); temp.pan(pan); return temp;};
355 
357  //
367  //
368  void
369  pan(const Buffer & pan);
370 
372  //
382  //
384  getPan(const Buffer & pan) const
385  { AudioStream temp(*this); temp.pan(pan); return temp;};
386 
387  // Plot the AudioStream
388  void
389  plot(const std::string & title = "AudioStream") const;
390 
392  void
393  readWavefile(const char * filename);
394 
396  void
397  resample(float64 factor);
398 
400  void
401  resample(const Buffer & factor);
402 
405  getResample(float64 factor) const
406  { AudioStream temp(*this); temp.resample(factor); return temp; };
407 
410  getResample(const Buffer & factor) const
411  { AudioStream temp(*this); temp.resample(factor); return temp; };
412 
414  void
415  resample2(float64 new_sample_rate);
416 
419  getResample2(float64 new_sample_rate) const
420  {
421  AudioStream temp(*this);
422  temp.resample2(new_sample_rate);
423  return temp;
424  };
425 
426  // reverse()
428  void
429  reverse();
430 
433  getReverse() const
434  {AudioStream temp(*this); temp.reverse(); return temp;};
435 
437  //
452  select(const uint32 start_index, const uint32 stop_index);
453 
455  //
457  //
458  std::ostream &
459  write(std::ostream & out) const;
460 
461  std::string
462  write() const;
463 
465  //
467  //
468  std::istream &
469  read(std::istream & stream_in);
470 
471  void
472  read(const std::string & string_in);
473 
474  // setNChannels()
475  // DOXME
476  void
477  setNChannels(uint32 channels);
478 
479  // setSampleRate()
480  // DOXME
481  inline
482  void
483  setSampleRate(uint32 sample_rate) {sample_rate_ = sample_rate;}
484 
486  //
490  void
491  smooth(uint32 n_passes, uint32 n_samples_per_average);
492 
495  getSmooth(uint32 n_passes, uint32 n_samples_per_average) const
496  {AudioStream temp(*this); temp.smooth(n_passes, n_samples_per_average); return temp;};
497 
499  void
500  speedUp(float32 step_size);
501 
504  getSpeedUp(float32 step_size) const
505  { AudioStream temp(*this); temp.speedUp(step_size); return temp; };
506 
508  void
509  speedUp(const Buffer & step_size);
510 
513  getSpeedUp(const Buffer & step_size) const
514  { AudioStream temp(*this); temp.speedUp(step_size); return temp; };
515 
517  void
518  sqrt();
519 
522  getSqrt() const
523  { AudioStream temp(*this); temp.sqrt(); return temp; };
524 
525  // DOXME
527  substream(uint32 start_index, uint32 n_samples = 0) const;
528 
530  substream(int32 start_index, int32 n_samples = 0) const;
531 
532  // DOXME
534  substream(float64 start_time, float64 duration = 0) const;
535 
537  //
549  void
550  transpose();
551 
554  getTranspose() const
555  { AudioStream temp(*this); temp.transpose(); return temp; };
556 
558  void
559  upSample(uint32 n);
560 
564  { AudioStream temp(*this); temp.upSample(n); return temp; };
565 
567  void
568  writeWavefile(const char * filename) const;
569 
571  void _set_at_index(int32 i, const Buffer &);
572 
574  void _swig_shadow(){};
575 
577  static
579  ones(
580  float64 sample_rate,
581  const uint32 n_channels,
582  float64 duration);
583 
585  static
587  rand(
588  float64 sample_rate,
589  const uint32 n_channels,
590  float64 duration);
591 
593  static
595  zeros(
596  float64 sample_rate,
597  const uint32 n_channels,
598  float64 duration);
599 
600 private:
601 
602 //~ // DOXME
603 //~ enum MathOperator
604 //~ {
605 //~ ADD,
606 //~ SUBTRACT,
607 //~ MULTIPLY,
608 //~ DIVIDE,
609 //~ POW
610 //~ };
611 
612 //~
613 //~
614 //~ AudioStream & vectorOperator(const AudioStream & rhs, MathOperator op);
615 //~ AudioStream & scalarOperator(float64 rhs, MathOperator op);
616 
617  // Data members
618 
621 
622  std::vector<Buffer *> buffers_;
623 
624 }; // class
625 
626 //-----------------------------------------------------------------------------
627 // Non class methods
628 
629 inline AudioStream operator+(const AudioStream & lhs, const AudioStream & rhs)
630 {
631  AudioStream temp(lhs);
632  return temp += rhs;
633 }
634 
635 inline AudioStream operator-(const AudioStream & lhs, const AudioStream & rhs)
636 {
637  AudioStream temp(lhs);
638  return temp -= rhs;
639 }
640 
641 inline AudioStream operator*(const AudioStream & lhs, const AudioStream & rhs)
642 {
643  AudioStream temp(lhs);
644  return temp *= rhs;
645 }
646 
647 inline AudioStream operator/(const AudioStream & lhs, const AudioStream & rhs)
648 {
649  AudioStream temp(lhs);
650  return temp /= rhs;
651 }
652 
653 inline AudioStream operator^(const AudioStream & lhs, const AudioStream & rhs)
654 {
655  AudioStream temp(lhs);
656  return temp ^= rhs;
657 }
658 
659 inline AudioStream operator+(const AudioStream & lhs, const Buffer & rhs)
660 {
661  AudioStream temp(lhs);
662  return temp += rhs;
663 }
664 
665 inline AudioStream operator-(const AudioStream & lhs, const Buffer & rhs)
666 {
667  AudioStream temp(lhs);
668  return temp -= rhs;
669 }
670 
671 inline AudioStream operator*(const AudioStream & lhs, const Buffer & rhs)
672 {
673  AudioStream temp(lhs);
674  return temp *= rhs;
675 }
676 
677 inline AudioStream operator/(const AudioStream & lhs, const Buffer & rhs)
678 {
679  AudioStream temp(lhs);
680  return temp /= rhs;
681 }
682 
683 inline AudioStream operator^(const AudioStream & lhs, const Buffer & rhs)
684 {
685  AudioStream temp(lhs);
686  return temp ^= rhs;
687 }
688 
689 inline AudioStream operator+(const AudioStream & lhs, float64 d)
690 {
691  AudioStream temp(lhs);
692  return temp += d;
693 }
694 
695 inline AudioStream operator-(const AudioStream & lhs, float64 d)
696 {
697  AudioStream temp(lhs);
698  return temp -= d;
699 }
700 
701 inline AudioStream operator*(const AudioStream & lhs, float64 d)
702 {
703  AudioStream temp(lhs);
704  return temp *= d;
705 }
706 
707 inline AudioStream operator/(const AudioStream & lhs, float64 d)
708 {
709  AudioStream temp(lhs);
710  return temp /= d;
711 }
712 
713 inline AudioStream operator^(const AudioStream & lhs, float64 d)
714 {
715  AudioStream temp(lhs);
716  return temp ^= d;
717 }
718 
719 inline AudioStream operator+(float64 d, const AudioStream & rhs)
720 {
721  AudioStream temp(rhs);
722  return temp += d;
723 }
724 
725 inline AudioStream operator-(float64 d, const AudioStream & rhs)
726 {
727  AudioStream temp(rhs * -1.0);
728  return temp += d;
729 }
730 
731 inline AudioStream operator*(float64 d, const AudioStream & rhs)
732 {
733  AudioStream temp(rhs);
734  return temp *= d;
735 }
736 
737 inline AudioStream operator/(float64 d, const AudioStream & rhs)
738 {
739  AudioStream temp(rhs.getSampleRate(), rhs.getNChannels());
740 
741  for(uint32 i = 0; i < rhs.getNChannels(); ++i)
742  {
743  temp[i] = d / rhs[i];
744  }
745 
746  return temp;
747 }
748 
749 
750 }// Nsound
751 
752 // :mode=c++:
753 
754 #endif
unsigned int uint32
Definition: Nsound.h:153
AudioStream getPan(const Buffer &pan) const
Sets the amplitude level left vs right.
Definition: AudioStream.h:384
AudioStream getPad(float64 fill=0.0) const
Definition: AudioStream.h:322
void speedUp(float32 step_size)
Resamples the AudioStream by the step_size, no interpolation.
Definition: AudioStream.cc:804
BooleanVectorVector operator>(float64 rhs)
Definition: AudioStream.cc:970
AudioStream operator-(const AudioStream &lhs, const AudioStream &rhs)
Definition: AudioStream.h:635
AudioStream getdB() const
Returns the AudioStream in dB.
Definition: AudioStream.h:142
float64 getDuration() const
Returns the number of seconds of audio data in the stream.
Definition: AudioStream.cc:183
void downSample(uint32 n)
Downsample by a integral factor. N must be > 1.
Definition: AudioStream.cc:176
AudioStream getSqrt() const
Returns an AudioStream after taking the square root of each sample.
Definition: AudioStream.h:522
AudioStream getNormalize() const
Returns the normalized AudioStream.
Definition: AudioStream.h:225
boolean operator!=(const AudioStream &rhs) const
Boolean != operator.
Definition: AudioStream.h:262
float64 getMin() const
Returns the minimum sample value in the AudioStream.
Definition: AudioStream.cc:247
AudioStream operator+(const AudioStream &lhs, const AudioStream &rhs)
Definition: AudioStream.h:629
AudioStream & operator*=(const AudioStream &rhs)
Definition: AudioStream.cc:453
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
void readWavefile(const char *filename)
Read a Wavefile.
Definition: AudioStream.cc:697
AudioStream operator/(const AudioStream &lhs, const AudioStream &rhs)
Definition: AudioStream.h:647
AudioStream getTranspose() const
Retuns a copy of the AudioStream transposed.
Definition: AudioStream.h:554
AudioStream operator^(const AudioStream &lhs, const AudioStream &rhs)
Definition: AudioStream.h:653
AudioStream getLimit(const Buffer &min, const Buffer &max) const
Limits the AudioStream the min and max values.
Definition: AudioStream.h:186
AudioStreamSelection operator()(const BooleanVectorVector &bv)
Returns an AudioStreamSelection object used for manipulation of a selected region of samples...
Definition: AudioStream.cc:376
std::istream & read(std::istream &stream_in)
Constructs an AudioStream from seralized data in the inputstream.
Definition: AudioStream.cc:924
AudioStream getDownSample(uint32 n) const
Downsample by a integral factor. N must be > 1.
Definition: AudioStream.h:160
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
std::string write() const
Definition: AudioStream.cc:915
AudioStreamSelection select(const uint32 start_index, const uint32 stop_index)
Returns an AudioStreamSelection for the range of indicies.
Definition: AudioStream.cc:740
AudioStream & operator+=(const AudioStream &rhs)
Definition: AudioStream.cc:407
void reverse()
Reverses the AudioStream.
Definition: AudioStream.cc:733
std::vector< Buffer * > buffers_
Definition: AudioStream.h:622
AudioStream & operator^=(const AudioStream &rhs)
Definition: AudioStream.cc:502
BooleanVectorVector operator<(float64 rhs)
Definition: AudioStream.cc:994
void _swig_shadow()
SWIG helper function function to shadow.
Definition: AudioStream.h:574
AudioStream operator*(const AudioStream &lhs, const AudioStream &rhs)
Definition: AudioStream.h:641
AudioStream substream(uint32 start_index, uint32 n_samples=0) const
Definition: AudioStream.cc:825
double float64
Definition: Nsound.h:146
static double start_time
Definition: TicToc.cc:35
float64 getMax() const
Returns the maximum sample value in the Audiostream.
Definition: AudioStream.cc:225
void upSample(uint32 n)
Upsample by a integral factor. N must be > 1.
Definition: AudioStream.cc:895
void convolve(const Buffer &b)
Convolves every channel in the AudioStream with the Buffer.
Definition: AudioStream.cc:154
void setNChannels(uint32 channels)
Definition: AudioStream.cc:781
void normalize()
Multiplies the AudioStream by a constant gain so the peak sample has magnitude 1.0.
Definition: AudioStream.cc:285
BooleanVectorVector operator<=(float64 rhs)
~AudioStream()
Destructor.
Definition: AudioStream.cc:107
AudioStream & operator-=(const AudioStream &rhs)
Definition: AudioStream.cc:430
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream getConvolve(const Buffer &b) const
Convolves every channel in the AudioStream with the Buffer.
Definition: AudioStream.h:133
void plot(const std::string &title="AudioStream") const
Definition: AudioStream.cc:661
void mono()
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:258
const Buffer & operator[](uint32 index) const
These methods provide a reference to the Buffer object held in the channel.
Definition: AudioStream.cc:297
void writeWavefile(const char *filename) const
Write the AudioStream to a Wavefile.
Definition: AudioStream.cc:960
AudioStream getResample2(float64 new_sample_rate) const
Resample to the specified sample rate.
Definition: AudioStream.h:419
AudioStream getPan(float64 pan) const
Sets the amplitude level left vs right.
Definition: AudioStream.h:353
std::vector< std::vector< boolean > > BooleanVectorVector
AudioStream getLimit(float64 min, float64 max) const
Limits the AudioStream the min and max values.
Definition: AudioStream.h:181
void derivative(uint32 n)
Calculates the nth derivative of the AudioStream.
Definition: AudioStream.cc:168
void _set_at_index(int32 i, const Buffer &)
SWIG helper function.
Definition: AudioStream.cc:759
void resample2(float64 new_sample_rate)
Resample to the specified sample rate.
Definition: AudioStream.cc:720
AudioStream & operator/=(const AudioStream &rhs)
Definition: AudioStream.cc:477
AudioStream & operator<<(const AudioStream &rhs)
Concatenates or appends rhs to the AudioStream.
Definition: AudioStream.cc:383
AudioStream getSpeedUp(float32 step_size) const
Resamples the AudioStream by the step_size, no interpolation.
Definition: AudioStream.h:504
void transpose()
Treating the AudioStream as a matrix, this peforms a matrix transpose.
Definition: AudioStream.cc:875
void dB()
Modifies the AudioStream so each sample is converted to dB, 20 * log10(sample).
Definition: AudioStream.cc:161
A Buffer for storing audio samples.
Definition: Buffer.h:60
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.
Definition: AudioStream.cc:343
signed int int32
Definition: Nsound.h:142
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.
Definition: AudioStream.cc:211
void add(const AudioStream &as, uint32 offset, uint32 n_samples=0)
This method adds the passed AudioStream to this AudioStream.
Definition: AudioStream.cc:121
float float32
Definition: Nsound.h:145
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:265
void pan(float64 pan)
Sets the amplitude level left vs right.
Definition: AudioStream.cc:627
static AudioStream zeros(float64 sample_rate, const uint32 n_channels, float64 duration)
Returns a Buffer full of zeros of length n_samples.
AudioStream getUpSample(uint32 n) const
Upsample by a integral factor. N must be > 1.
Definition: AudioStream.h:563
void abs()
This method calls abs on all buffers held in the stream.
Definition: AudioStream.cc:114
AudioStream getResample(const Buffer &factor) const
Resample by a dynamic non-integer factor.
Definition: AudioStream.h:410
AudioStream getAbs() const
Modifies the AudioStream by making any negative value positive.
Definition: AudioStream.h:81
AudioStream getReverse() const
Reverses the samples in the AudioStream.
Definition: AudioStream.h:433
A helper class for advance operators.
BooleanVectorVector operator>=(float64 rhs)
Definition: AudioStream.cc:982
void setSampleRate(uint32 sample_rate)
Definition: AudioStream.h:483
AudioStream getResample(float64 factor) const
Resample by a non-integer factor.
Definition: AudioStream.h:405
void smooth(uint32 n_passes, uint32 n_samples_per_average)
Implements a standard moving average filter to smooth the waveform.
Definition: AudioStream.cc:797
AudioStream getSpeedUp(const Buffer &step_size) const
Resamples the AudioStream by a variable step_size, no interpolation.
Definition: AudioStream.h:513
void sqrt()
Takes the square root of each sample in the AudioStream.
Definition: AudioStream.cc:818
AudioStream getSmooth(uint32 n_passes, uint32 n_samples_per_average) const
Implements a standard moving average filter to smooth the waveform.
Definition: AudioStream.h:495
AudioStream()
Create an AudioStream.
Definition: AudioStream.cc:52
void resample(float64 factor)
Resample by a non-integer factor.
Definition: AudioStream.cc:706
float64 getMaxMagnitude() const
Returns the maximum sample magnitude value in the AudioStream, i.e. max(abs(samples))..
Definition: AudioStream.cc:236
AudioStream getDerivative(uint32 n) const
Returns the nth derivative of the Buffer.
Definition: AudioStream.h:151
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 & operator=(const AudioStream &rhs)
Assignment operator.
Definition: AudioStream.cc:313