Nsound  0.9.4
AudioStream.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: AudioStream.cc 927 2015-08-23 18:46:27Z weegreenblobbie $
4 //
5 // Copyright (c) 2009-Present Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <Nsound/AudioStream.h>
30 #include <Nsound/Buffer.h>
31 #include <Nsound/Generator.h>
32 #include <Nsound/Nsound.h>
33 #include <Nsound/Plotter.h>
34 #include <Nsound/Sine.h>
35 #include <Nsound/Wavefile.h>
36 #include <Nsound/StreamOperators.h>
37 
38 #include <algorithm>
39 #include <iostream>
40 #include <limits>
41 #include <sstream>
42 
43 using namespace Nsound;
44 
45 using std::cerr;
46 using std::cout;
47 using std::endl;
48 using std::flush;
49 
50 
53  :
54  sample_rate_(44100.0),
55  channels_(1),
56  buffers_(1, new Buffer())
57 {
58  M_ASSERT_VALUE(channels_, !=, 0);
59 }
60 
63  float64 sample_rate,
64  uint32 channels,
65  uint32 n_samples_pre_allocate)
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 }
80 
82 AudioStream(const std::string & filename)
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 }
91 
92 
94 AudioStream(const AudioStream & copy)
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 }
105 
108 {
109  for(auto * ptr : buffers_) delete ptr;
110 }
111 
112 void
115 {
116  for(auto * ptr : buffers_) ptr->abs();
117 }
118 
119 void
122  const AudioStream & as,
123  uint32 offset,
124  uint32 n_samples)
125 {
127 
128  for(uint32 i = 0; i < as.channels_; ++i)
129  {
130  buffers_[i]->add(*as.buffers_[i], offset, n_samples);
131  }
132 }
133 
134 void
137  const AudioStream & as,
138  float64 offset_seconds,
139  float64 duration_seconds)
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 }
151 
152 void
154 convolve(const Buffer & b)
155 {
156  for(auto * ptr : buffers_) ptr->convolve(b);
157 }
158 
159 void
161 dB()
162 {
163  for(auto * ptr : buffers_) ptr->dB();
164 }
165 
166 void
169 {
170  for(auto * ptr : buffers_) ptr->derivative(n);
171 }
172 
173 
174 void
177 {
178  for(auto * ptr : buffers_) ptr->downSample(n);
179 }
180 
181 float64
183 getDuration() const
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 }
194 
195 uint32
197 getLength() const
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 }
208 
209 void
212 {
213  for(auto * ptr : buffers_) ptr->limit(min, max);
214 }
215 
216 void
218 limit(const Buffer & min, const Buffer & max)
219 {
220  for(auto * ptr : buffers_) ptr->limit(min, max);
221 }
222 
223 float64
225 getMax() const
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 }
233 
234 float64
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 }
244 
245 float64
247 getMin() const
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 }
255 
256 void
259 {
260  *this = this->getMono();
261 }
262 
265 getMono() const
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 }
282 
283 void
286 {
287  float64 peak = getMaxMagnitude();
288 
289  float64 normal_factor = 1.0 / peak;
290 
291  *this *= normal_factor;
292 }
293 
294 
295 const Buffer &
298 {
299  M_ASSERT_VALUE(i, <, channels_);
300  return *buffers_[i];
301 }
302 
303 Buffer &
306 {
307  M_ASSERT_VALUE(i, <, channels_);
308  return *buffers_[i];
309 }
310 
311 AudioStream &
313 operator=(const AudioStream & rhs)
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 }
328 
329 AudioStream &
331 operator=(const Buffer & rhs)
332 {
333  for(auto * ptr : buffers_)
334  {
335  *ptr = rhs;
336  }
337 
338  return *this;
339 }
340 
341 boolean
343 operator==(const AudioStream & rhs) const
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 }
365 
366 float64
368 operator()(uint32 channel, uint32 index) const
369 {
370  M_ASSERT_VALUE(channel, <, channels_);
371  return (*buffers_[channel])[index];
372 }
373 
377 {
378  return AudioStreamSelection(*this, bvv);
379 }
380 
381 AudioStream &
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 }
396 
397 AudioStream &
399 operator<<(const Buffer & rhs)
400 {
401  for(auto * ptr : buffers_) *ptr << rhs;
402  return *this;
403 }
404 
405 AudioStream &
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 }
419 
420 AudioStream &
422 operator+=(const Buffer & rhs)
423 {
424  for(auto * ptr : buffers_) *ptr += rhs;
425  return *this;
426 }
427 
428 AudioStream &
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 }
442 
443 AudioStream &
445 operator-=(const Buffer & rhs)
446 {
447  for(auto * ptr : buffers_) *ptr -= rhs;
448  return *this;
449 }
450 
451 AudioStream &
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 }
465 
466 AudioStream &
468 operator*=(const Buffer & rhs)
469 {
470  for(auto * ptr : buffers_) *ptr *= rhs;
471  return *this;
472 }
473 
474 
475 AudioStream &
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 }
489 
490 
491 AudioStream &
493 operator/=(const Buffer & rhs)
494 {
495  for(auto * ptr : buffers_) *ptr /= rhs;
496  return *this;
497 }
498 
499 
500 AudioStream &
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 }
514 
515 
516 AudioStream &
518 operator^=(const Buffer & rhs)
519 {
520  for(auto * ptr : buffers_) *ptr ^= rhs;
521  return *this;
522 }
523 
524 
525 AudioStream &
528 {
529  for(auto * ptr : buffers_) *ptr << d;
530  return *this;
531 }
532 
533 AudioStream &
536 {
537  for(auto * ptr : buffers_) *ptr += d;
538  return *this;
539 }
540 
541 AudioStream &
544 {
545  for(auto * ptr : buffers_) *ptr -= d;
546  return *this;
547 }
548 
549 AudioStream &
552 {
553  for(auto * ptr : buffers_) *ptr *= d;
554  return *this;
555 }
556 
557 AudioStream &
560 {
561  for(auto * ptr : buffers_) *ptr /= d;
562  return *this;
563 }
564 
565 AudioStream &
568 {
569  for(auto * ptr : buffers_) *ptr ^= d;
570  return *this;
571 }
572 
573 std::ostream &
574 Nsound::
575 operator<<(std::ostream & out, const AudioStream & rhs)
576 {
577  uint32 n_samples = std::min(10u, rhs.getLength());
578 
579  for(uint32 i = 0; i < rhs.channels_; ++i)
580  {
581  out << "channel["
582  << i
583  << "].length = "
584  << rhs.buffers_[i]->getLength()
585  << endl
586  << "channel["
587  << i
588  << "] = ";
589 
590  for(uint32 j = 0; j < n_samples; j++)
591  {
592  out << rhs[i][j]
593  << " ";
594  }
595  }
596  return out;
597 }
598 
599 
600 void
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 }
624 
625 void
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 }
637 
638 void
640 pan(const Buffer & pan)
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 }
658 
659 void
661 plot(const std::string & title) const
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 }
694 
695 void
697 readWavefile(const char * filename)
698 {
699  M_CHECK_PTR(filename);
700 
701  *this << filename;
702 }
703 
704 void
707 {
708  for(auto * ptr : buffers_) ptr->resample(factor);
709 }
710 
711 void
713 resample(const Buffer & factor)
714 {
715  for(auto * ptr : buffers_) ptr->resample(factor);
716 }
717 
718 void
720 resample2(float64 new_sample_rate)
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 }
730 
731 void
734 {
735  for(auto * ptr : buffers_) ptr->reverse();
736 }
737 
740 select(const uint32 start_index, const uint32 stop_index)
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 }
756 
757 void
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 }
778 
779 void
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 }
794 
795 void
797 smooth(uint32 n_passes, uint32 n_samples_per_average)
798 {
799  for(auto * ptr : buffers_) ptr->smooth(n_passes, n_samples_per_average);
800 }
801 
802 void
804 speedUp(float32 step_size)
805 {
806  for(auto * ptr : buffers_) ptr->speedUp(step_size);
807 }
808 
809 void
811 speedUp(const Buffer & step_buffer)
812 {
813  for(auto * ptr : buffers_) ptr->speedUp(step_buffer);
814 }
815 
816 void
819 {
820  for(auto * ptr : buffers_) ptr->sqrt();
821 }
822 
825 substream(uint32 start_index, uint32 n_samples) const
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 }
836 
837 
840 substream(int32 start_index, int32 n_samples) const
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 }
849 
850 
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 }
872 
873 void
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 }
892 
893 void
896 {
897  for(auto * ptr : buffers_) ptr->upSample(n);
898 }
899 
900 std::ostream &
902 write(std::ostream & out) const
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 }
912 
913 std::string
915 write() const
916 {
917  std::stringstream ss;
918  write(ss);
919  return ss.str();
920 }
921 
922 std::istream &
924 read(std::istream & in)
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 }
949 
950 void
952 read(const std::string & in)
953 {
954  std::stringstream ss(in);
955  read(ss);
956 }
957 
958 void
960 writeWavefile(const char * filename) const
961 {
962  M_CHECK_PTR(filename);
963  *this >> filename;
964 }
965 
966 
968 Nsound::
971 {
973 
974  for(auto * ptr : buffers_) bv.push_back( *ptr > rhs );
975 
976  return bv;
977 }
978 
980 Nsound::
983 {
985 
986  for(auto * ptr : buffers_) bv.push_back( *ptr >= rhs );
987 
988  return bv;
989 }
990 
992 Nsound::
995 {
997 
998  for(auto * ptr : buffers_) bv.push_back( *ptr < rhs );
999 
1000  return bv;
1001 }
1002 
1004 Nsound::
1007 {
1009 
1010  for(auto * ptr : buffers_) bv.push_back( *ptr <= rhs );
1011 
1012  return bv;
1013 }
1014 
1016 Nsound::
1019 {
1021 
1022  for(auto * ptr : buffers_) bv.push_back( *ptr == rhs );
1023 
1024  return bv;
1025 }
1026 
1028 Nsound::
1031 {
1033 
1034  for(auto * ptr : buffers_) bv.push_back( *ptr != rhs );
1035 
1036  return bv;
1037 }
1038 
1042  float64 sample_rate,
1043  const uint32 n_channels,
1044  float64 duration)
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 }
1054 
1058  float64 sample_rate,
1059  const uint32 n_channels,
1060  float64 duration)
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 }
1070 
1074  float64 sample_rate,
1075  const uint32 n_channels,
1076  float64 duration)
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 }
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
void speedUp(float32 step_size)
Resamples the AudioStream by the step_size, no interpolation.
Definition: AudioStream.cc:804
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
BooleanVectorVector operator>(float64 rhs)
Definition: AudioStream.cc:970
std::ostream & operator<<(std::ostream &out, const Buffer &rhs_buffer)
Definition: Buffer.cc:1338
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
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
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
Definition: Plotter.cc:765
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
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
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
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
Definition: Plotter.cc:455
AudioStream & operator+=(const AudioStream &rhs)
Definition: AudioStream.cc:407
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
#define M_CHECK_PTR(ptr)
Definition: Macros.h:64
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
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
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
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
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 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
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
static Buffer ones(const uint32 n_samples)
Returns a Buffer full of ones of length n_samples.
Definition: Buffer.cc:2239
std::vector< std::vector< boolean > > BooleanVectorVector
void derivative(uint32 n)
Calculates the nth derivative of the AudioStream.
Definition: AudioStream.cc:168
#define M_THROW(message)
Definition: Macros.h:108
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
void transpose()
Treating the AudioStream as a matrix, this peforms a matrix transpose.
Definition: AudioStream.cc:875
void ylabel(const std::string &label, const std::string &kwargs="")
Add a label y axis.
Definition: Plotter.cc:1180
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
std::vector< boolean > BooleanVector
Buffer whiteNoise(const float64 &duration) const
This method generates noise from a uniform distribution.
Definition: Generator.cc:1325
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.
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
void abs()
This method calls abs on all buffers held in the stream.
Definition: AudioStream.cc:114
A helper class for advance operators.
BooleanVectorVector operator>=(float64 rhs)
Definition: AudioStream.cc:982
void smooth(uint32 n_passes, uint32 n_samples_per_average)
Implements a standard moving average filter to smooth the waveform.
Definition: AudioStream.cc:797
DOXME.
Definition: Sine.h:43
void sqrt()
Takes the square root of each sample in the AudioStream.
Definition: AudioStream.cc:818
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
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
float64 sr
Definition: example3.cc:24
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