arduino-audio-tools
SoundGenerator.h
1 #pragma once
2 
3 #include "AudioBasic/Collections.h"
4 #include "AudioTools/AudioLogger.h"
5 #include "AudioTools/AudioTypes.h"
6 #include <math.h>
7 
14 namespace audio_tools {
15 
25 template <class T> class SoundGenerator {
26 public:
27  SoundGenerator() {
28  info.bits_per_sample = sizeof(T) * 8;
29  }
30 
31  virtual ~SoundGenerator() { end(); }
32 
33  virtual bool begin(AudioInfo info) {
34  this->info = info;
35  return begin();
36  }
37 
38  virtual bool begin() {
39  TRACED();
40  active = true;
41  activeWarningIssued = false;
42  info.logInfo("SoundGenerator:");
43 
44  // support bytes < framesize
45  ring_buffer.resize(info.channels * sizeof(T));
46 
47  return true;
48  }
49 
51  virtual void end() { active = false; }
52 
55  virtual bool isActive() { return active; }
56 
58  virtual T readSample() = 0;
59 
61  virtual size_t readBytes(uint8_t *buffer, size_t lengthBytes) {
62  LOGD("readBytes: %d", (int)lengthBytes);
63  if (!active)
64  return 0;
65  int channels = audioInfo().channels;
66  int frame_size = sizeof(T) * channels;
67  int frames = lengthBytes / frame_size;
68  if (lengthBytes >= frame_size) {
69  return readBytesFrames(buffer, lengthBytes, frames, channels);
70  }
71  return readBytesFromBuffer(buffer, lengthBytes, frame_size, channels);
72  }
73 
76  AudioInfo def;
77  def.bits_per_sample = sizeof(T) * 8;
78  return def;
79  }
80 
82  virtual void setFrequency(float frequency) {
83  LOGE("setFrequency not supported");
84  }
85 
87  virtual AudioInfo audioInfo() { return info; }
88 
90  virtual void setAudioInfo(AudioInfo info) {
91  this->info = info;
92  if (info.bits_per_sample != sizeof(T) * 8) {
93  LOGE("invalid bits_per_sample: %d", info.channels);
94  }
95  }
96 
97 protected:
98  bool active = false;
99  bool activeWarningIssued = false;
100  //int output_channels = 1;
101  AudioInfo info;
102  RingBuffer<uint8_t> ring_buffer{0};
103 
104  size_t readBytesFrames(uint8_t *buffer, size_t lengthBytes, int frames,
105  int channels) {
106  T *result_buffer = (T *)buffer;
107  for (int j = 0; j < frames; j++) {
108  T sample = readSample();
109  for (int ch = 0; ch < channels; ch++) {
110  *result_buffer++ = sample;
111  }
112  }
113  return frames * sizeof(T) * channels;
114  }
115 
116  size_t readBytesFromBuffer(uint8_t *buffer, size_t lengthBytes,
117  int frame_size, int channels) {
118  // fill ringbuffer with one frame
119  if (ring_buffer.isEmpty()) {
120  uint8_t tmp[frame_size];
121  readBytesFrames(tmp, frame_size, 1, channels);
122  ring_buffer.writeArray(tmp, frame_size);
123  }
124  // provide result
125  return ring_buffer.readArray(buffer, lengthBytes);
126  }
127 };
128 
138 template <class T> class SineWaveGenerator : public SoundGenerator<T> {
139 public:
140  // the scale defines the max value which is generated
141  SineWaveGenerator(float amplitude = 0.9f * NumberConverter::maxValueT<T>(),
142  float phase = 0.0f) {
143  LOGD("SineWaveGenerator");
144  m_amplitude = amplitude;
145  m_phase = phase;
146  }
147 
148  bool begin() override {
149  TRACEI();
151  this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
152  return true;
153  }
154 
155  bool begin(AudioInfo info) override {
156  LOGI("%s::begin(channels=%d, sample_rate=%d)", "SineWaveGenerator",
157  (int)info.channels, (int) info.sample_rate);
159  this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
160  return true;
161  }
162 
163  bool begin(AudioInfo info, float frequency) {
164  LOGI("%s::begin(channels=%d, sample_rate=%d, frequency=%.2f)",
165  "SineWaveGenerator", (int)info.channels,(int) info.sample_rate, frequency);
167  this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
168  if (frequency > 0.0f) {
169  setFrequency(frequency);
170  }
171  return true;
172  }
173 
174  bool begin(int channels, int sample_rate, float frequency) {
175  SoundGenerator<T>::info.channels = channels;
176  SoundGenerator<T>::info.sample_rate = sample_rate;
177  return begin(SoundGenerator<T>::info, frequency);
178  }
179 
180  // update m_deltaTime
181  virtual void setAudioInfo(AudioInfo info) override {
183  this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
184  }
185 
186  virtual AudioInfo defaultConfig() override {
188  }
189 
191  void setFrequency(float frequency) override {
192  LOGI("setFrequency: %.2f", frequency);
193  LOGI("active: %s", SoundGenerator<T>::active ? "true" : "false");
194  m_frequency = frequency;
195  }
196 
198  virtual T readSample() override {
199  float angle = double_Pi * m_cycles + m_phase;
200  T result = m_amplitude * sinf(angle);
201  m_cycles += m_frequency * m_deltaTime;
202  if (m_cycles > 1.0f) {
203  m_cycles -= 1.0f;
204  }
205  return result;
206  }
207 
208  void setAmplitude(float amp) { m_amplitude = amp; }
209 
210 protected:
211  volatile float m_frequency = 0.0f;
212  float m_cycles = 0.0f; // Varies between 0.0 and 1.0
213  float m_amplitude = 1.0f;
214  float m_deltaTime = 0.0f;
215  float m_phase = 0.0f;
216  const float double_Pi = PI * 2.0f;
217 
218  void logStatus() {
219  SoundGenerator<T>::info.logStatus();
220  LOGI("amplitude: %f", this->m_amplitude);
221  LOGI("active: %s", SoundGenerator<T>::active ? "true" : "false");
222  }
223 };
224 
232 template <class T> class SquareWaveGenerator : public SineWaveGenerator<T> {
233 public:
234  SquareWaveGenerator(float amplitude = 32767.0f, float phase = 0.0f)
235  : SineWaveGenerator<T>(amplitude, phase) {
236  LOGD("SquareWaveGenerator");
237  }
238 
239  virtual T readSample() {
240  return value(SineWaveGenerator<T>::readSample(),
242  }
243 
244 protected:
245  // returns amplitude for positive vales and -amplitude for negative values
246  T value(T value, T amplitude) {
247  return (value >= 0) ? amplitude : -amplitude;
248  }
249 };
250 
258 template <class T> class FastSineGenerator : public SineWaveGenerator<T> {
259 public:
260  FastSineGenerator(float amplitude = 32767.0, float phase = 0.0)
261  : SineWaveGenerator<T>(amplitude, phase) {
262  LOGD("FastSineGenerator");
263  }
264 
265  virtual T readSample() override {
266  float angle =
268  T result = SineWaveGenerator<T>::m_amplitude * sine(angle);
271  if (SineWaveGenerator<T>::m_cycles > 1.0) {
273  }
274  return result;
275  }
276 
277 protected:
279  inline float sine(float t) {
280  float p = (t - (int)t) - 0.5f; // 0 <= p <= 1
281  float pp = p * p;
282  return (p - 6.283211f * pp * p + 9.132843f * pp * pp * p) * -6.221086f;
283  }
284 };
285 
293 template <class T> class WhiteNoiseGenerator : public SoundGenerator<T> {
294 public:
296  WhiteNoiseGenerator(T amplitude = 32767) { this->amplitude = amplitude; }
297 
299  T readSample() { return (random(-amplitude, amplitude)); }
300 
301 protected:
302  T amplitude;
303  // //range : [min, max]
304  int random(int min, int max) { return min + rand() % ((max + 1) - min); }
305 };
306 
314 template <class T> class PinkNoiseGenerator : public SoundGenerator<T> {
315 public:
317  PinkNoiseGenerator(T amplitude = 32767) {
318  this->amplitude = amplitude;
319  max_key = 0x1f; // Five bits set
320  key = 0;
321  for (int i = 0; i < 5; i++)
322  white_values[i] = rand() % (amplitude / 5);
323  }
324 
327  T last_key = key;
328  unsigned int sum;
329 
330  key++;
331  if (key > max_key)
332  key = 0;
333  // Exclusive-Or previous value with current value. This gives
334  // a list of bits that have changed.
335  int diff = last_key ^ key;
336  sum = 0;
337  for (int i = 0; i < 5; i++) {
338  // If bit changed get new random number for corresponding
339  // white_value
340  if (diff & (1 << i))
341  white_values[i] = rand() % (amplitude / 5);
342  sum += white_values[i];
343  }
344  return sum;
345  }
346 
347 protected:
348  T max_key;
349  T key;
350  unsigned int white_values[5];
351  unsigned int amplitude;
352 };
353 
363 template <class T> class SilenceGenerator : public SoundGenerator<T> {
364 public:
365  // the scale defines the max value which is generated
366  SilenceGenerator(T value = 0) { this->value = value; }
367 
370  return value; // return 0
371  }
372 
373 protected:
374  T value;
375 };
376 
384 template <class T> class GeneratorFromStream : public SoundGenerator<T>, public VolumeSupport {
385 public:
387  maxValue = NumberConverter::maxValue(sizeof(T) * 8);
388  };
389 
399  GeneratorFromStream(Stream &input, int channels = 1, float volume = 1.0) {
400  maxValue = NumberConverter::maxValue(sizeof(T) * 8);
401  setStream(input);
402  setVolume(volume);
403  setChannels(channels);
404  }
405 
407  void setStream(Stream &input) { this->p_stream = &input; }
408 
409  void setChannels(int channels) { this->channels = channels; }
410 
413  T data = 0;
414  float total = 0;
415  if (p_stream != nullptr) {
416  for (int j = 0; j < channels; j++) {
417  p_stream->readBytes((uint8_t *)&data, sizeof(T));
418  total += data;
419  }
420  float avg = (total / channels) * volume();
421  if (avg > maxValue) {
422  data = maxValue;
423  } else if (avg < -maxValue) {
424  data = -maxValue;
425  } else {
426  data = avg;
427  }
428  }
429  return data;
430  }
431 
432 protected:
433  Stream *p_stream = nullptr;
434  int channels = 1;
435  float maxValue;
436 };
437 
447 template <class T> class GeneratorFromArray : public SoundGenerator<T> {
448 public:
449  GeneratorFromArray() = default;
462  template <size_t arrayLen>
463  GeneratorFromArray(T (&array)[arrayLen], int repeat = 0,
464  bool setInactiveAtEnd = false, size_t startIndex = 0) {
465  TRACED();
466  this->max_repeat = repeat;
467  this->inactive_at_end = setInactiveAtEnd;
468  this->sound_index = startIndex;
469  setArray(array, arrayLen);
470  }
471 
472  ~GeneratorFromArray() {
473  if (owns_data) {
474  delete[] table;
475  }
476  }
477 
478  template <int arrayLen> void setArray(T (&array)[arrayLen]) {
479  TRACED();
480  setArray(array, arrayLen);
481  }
482 
483  void setArray(T *array, size_t size) {
484  this->table_length = size;
485  this->table = array;
486  LOGI("table_length: %d", (int)size);
487  }
488 
489  virtual bool begin(AudioInfo info) override {
490  return SoundGenerator<T>::begin(info);
491  }
492 
494  bool begin() override {
495  TRACEI();
497  sound_index = 0;
498  repeat_counter = 0;
499  is_running = true;
500  return true;
501  }
502 
504  T readSample() override {
505  // at end deactivate output
506  if (sound_index >= table_length) {
507  // LOGD("reset index - sound_index: %d, table_length:
508  // %d",sound_index,table_length);
509  sound_index = 0;
510  // deactivate when count has been used up
511  if (max_repeat >= 1 && ++repeat_counter >= max_repeat) {
512  LOGD("atEnd");
513  this->is_running = false;
514  if (inactive_at_end) {
515  this->active = false;
516  }
517  }
518  }
519 
520  // LOGD("index: %d - active: %d", sound_index, this->active);
521  T result = 0;
522  if (this->is_running) {
523  result = table[sound_index];
524  sound_index += index_increment;
525  }
526 
527  return result;
528  }
529 
530  // step size the sound index is incremented (default = 1)
531  void setIncrement(int inc) { index_increment = inc; }
532 
533  // Sets up a sine table - returns the effective frequency
534  int setupSine(int sampleRate, float reqFrequency, float amplitude = 1.0) {
535  int sample_count =
536  static_cast<float>(sampleRate) /
537  reqFrequency; // e.g. 44100 / 300hz = 147 samples per wave
538  float angle = 2.0 * PI / sample_count;
539  table = new T[sample_count];
540  for (int j = 0; j < sample_count; j++) {
541  table[j] = sinf(j * angle) * amplitude;
542  }
543  owns_data = true;
544  table_length = sample_count;
545  // calculate effective frequency
546  return sampleRate / sample_count;
547  }
548 
549  // Similar like is active to check if the array is still playing.
550  bool isRunning() { return is_running; }
551 
552 protected:
553  int sound_index = 0;
554  int max_repeat = 0;
555  int repeat_counter = 0;
556  bool inactive_at_end;
557  bool is_running = false;
558  bool owns_data = false;
559  T *table = nullptr;
560  size_t table_length = 0;
561  int index_increment = 1;
562 };
563 
571 template <class T> class GeneratorFixedValue : public SoundGenerator<T> {
572 public:
573  GeneratorFixedValue() = default;
574 
575  virtual bool begin(AudioInfo info) { return SoundGenerator<T>::begin(info); }
576 
577  void setValue(T value) { value_set = value; }
578 
580  bool begin() override {
581  TRACEI();
583  is_running = true;
584  value_return = value_set;
585  return true;
586  }
587 
589  T readSample() override { return value_return; }
590 
591  // Similar like is active to check if the array is still playing.
592  bool isRunning() { return is_running; }
593 
594 protected:
595  T value_set = 0;
596  T value_return = 0;
597  bool is_running = false;
598 };
599 
607 template <class T> class SineFromTable : public SoundGenerator<T> {
608 public:
609  SineFromTable(float amplitude = 32767.0) {
610  this->amplitude = amplitude;
611  this->amplitude_to_be = amplitude;
612  }
613 
615  void setAmplitude(float amplitude) { this->amplitude_to_be = amplitude; }
616 
619  void setMaxAmplitudeStep(float step) { max_amplitude_step = step; }
620 
622  // update angle
623  angle += step;
624  if (angle >= 360.0f) {
625  while (angle >= 360.0f) {
626  angle -= 360.0f;
627  }
628  // update frequency at start of circle (near 0 degrees)
629  step = step_new;
630 
631  updateAmplitudeInSteps();
632  // amplitude = amplitude_to_be;
633  }
634  return interpolate(angle);
635  }
636 
637  bool begin() {
638  is_first = true;
640  base_frequency = SoundGenerator<T>::audioInfo().sample_rate /
641  360.0f; // 122.5 hz (at 44100); 61 hz (at 22050)
642  return true;
643  }
644 
645  bool begin(AudioInfo info, float frequency) {
646  SoundGenerator<T>::begin(info);
647  base_frequency = SoundGenerator<T>::audioInfo().sample_rate /
648  360.0f; // 122.5 hz (at 44100); 61 hz (at 22050)
649  setFrequency(frequency);
650  return true;
651  }
652 
653  bool begin(int channels, int sample_rate, uint16_t frequency = 0) {
654  SoundGenerator<T>::info.channels = channels;
655  SoundGenerator<T>::info.sample_rate = sample_rate;
656  return begin(SoundGenerator<T>::info, frequency);
657  }
658 
659  void setFrequency(float freq) {
660  step_new = freq / base_frequency;
661  if (is_first) {
662  step = step_new;
663  is_first = false;
664  }
665  LOGD("step: %f", step_new);
666  }
667 
668 protected:
669  bool is_first = true;
670  float amplitude;
671  float amplitude_to_be;
672  float max_amplitude_step = 50.0f;
673  float base_frequency = 1.0f;
674  float step = 1.0f;
675  float step_new = 1.0f;
676  float angle = 0.0f;
677  // 122.5 hz (at 44100); 61 hz (at 22050)
678  const float values[181] = {
679  0, 0.0174524, 0.0348995, 0.052336, 0.0697565, 0.0871557,
680  0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809,
681  0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372,
682  0.309017, 0.325568, 0.34202, 0.358368, 0.374607, 0.390731,
683  0.406737, 0.422618, 0.438371, 0.45399, 0.469472, 0.48481,
684  0.5, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576,
685  0.587785, 0.601815, 0.615661, 0.62932, 0.642788, 0.656059,
686  0.669131, 0.681998, 0.694658, 0.707107, 0.71934, 0.731354,
687  0.743145, 0.75471, 0.766044, 0.777146, 0.788011, 0.798636,
688  0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167,
689  0.866025, 0.87462, 0.882948, 0.891007, 0.898794, 0.906308,
690  0.913545, 0.920505, 0.927184, 0.93358, 0.939693, 0.945519,
691  0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.97437,
692  0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546,
693  0.994522, 0.996195, 0.997564, 0.99863, 0.999391, 0.999848,
694  1, 0.999848, 0.999391, 0.99863, 0.997564, 0.996195,
695  0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
696  0.978148, 0.97437, 0.970296, 0.965926, 0.961262, 0.956305,
697  0.951057, 0.945519, 0.939693, 0.93358, 0.927184, 0.920505,
698  0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.87462,
699  0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
700  0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.75471,
701  0.743145, 0.731354, 0.71934, 0.707107, 0.694658, 0.681998,
702  0.669131, 0.656059, 0.642788, 0.62932, 0.615661, 0.601815,
703  0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
704  0.5, 0.48481, 0.469472, 0.45399, 0.438371, 0.422618,
705  0.406737, 0.390731, 0.374607, 0.358368, 0.34202, 0.325568,
706  0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
707  0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
708  0.104528, 0.0871557, 0.0697565, 0.052336, 0.0348995, 0.0174524,
709  0};
710 
711  T interpolate(float angle) {
712  bool positive = (angle <= 180.0f);
713  float angle_positive = positive ? angle : angle - 180.0f;
714  int angle_int1 = angle_positive;
715  int angle_int2 = angle_int1 + 1;
716  T v1 = values[angle_int1] * amplitude;
717  T v2 = values[angle_int2] * amplitude;
718  T result = v1 < v2 ? map(angle_positive, angle_int1, angle_int2, v1, v2)
719  : map(angle_positive, angle_int1, angle_int2, v2, v1);
720  // float result = v1;
721  return positive ? result : -result;
722  }
723 
724  T map(T x, T in_min, T in_max, T out_min, T out_max) {
725  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
726  }
727 
728  void updateAmplitudeInSteps() {
729  float diff = amplitude_to_be - amplitude;
730  if (abs(diff) > max_amplitude_step) {
731  diff = (diff < 0) ? -max_amplitude_step : max_amplitude_step;
732  }
733  if (abs(diff) >= 1.0f) {
734  amplitude += diff;
735  }
736  }
737 };
738 
746 template <class T> class GeneratorMixer : public SoundGenerator<T> {
747 public:
748  GeneratorMixer() = default;
749 
750  void add(SoundGenerator<T> &generator) { vector.push_back(&generator); }
751  void add(SoundGenerator<T> *generator) { vector.push_back(generator); }
752 
753  void clear() { vector.clear(); }
754 
756  float total = 0.0f;
757  float count = 0.0f;
758  for (auto &generator : vector) {
759  if (generator->isActive()){
760  T sample = generator->readSample();
761  total += sample;
762  count += 1.0f;
763  }
764  }
765  return count > 0.0f ? total / count : 0;
766  }
767 
768 protected:
769  Vector<SoundGenerator<T> *> vector;
770  int actualChannel = 0;
771 };
772 
781 template <class T> class TestGenerator : public SoundGenerator<T> {
782 public:
783  TestGenerator(T max = 1000, T inc = 1) { this->max = max; }
784 
785  T readSample() override {
786  value += inc;
787  if (abs(value) >= max) {
788  inc = -inc;
789  value += (inc * 2);
790  }
791  return value;
792  }
793 
794 protected:
795  T max;
796  T value = 0;
797  T inc = 1;
798 };
799 
800 } // namespace audio_tools
virtual int readArray(T data[], int len)
reads multiple values
Definition: Buffers.h:41
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition: Buffers.h:65
Sine wave which is based on a fast approximation function.
Definition: SoundGenerator.h:258
float sine(float t)
sine approximation.
Definition: SoundGenerator.h:279
virtual T readSample() override
Provides a single sample.
Definition: SoundGenerator.h:265
Just returns a constant value.
Definition: SoundGenerator.h:571
T readSample() override
Provides a single sample.
Definition: SoundGenerator.h:589
bool begin() override
Starts the generation of samples.
Definition: SoundGenerator.h:580
We generate the samples from an array which is provided in the constructor.
Definition: SoundGenerator.h:447
GeneratorFromArray(T(&array)[arrayLen], int repeat=0, bool setInactiveAtEnd=false, size_t startIndex=0)
Construct a new Generator from an array.
Definition: SoundGenerator.h:463
T readSample() override
Provides a single sample.
Definition: SoundGenerator.h:504
bool begin() override
Starts the generation of samples.
Definition: SoundGenerator.h:494
An Adapter Class which lets you use any Stream as a Generator.
Definition: SoundGenerator.h:384
void setStream(Stream &input)
(Re-)Assigns a stream to the Adapter class
Definition: SoundGenerator.h:407
GeneratorFromStream(Stream &input, int channels=1, float volume=1.0)
Constructs a new Generator from a Stream object that can be used e.g. as input for AudioEffectss.
Definition: SoundGenerator.h:399
T readSample()
Provides a single sample from the stream.
Definition: SoundGenerator.h:412
Generator which combines (mixes) multiple sound generators into one output.
Definition: SoundGenerator.h:746
T readSample()
Provides a single sample.
Definition: SoundGenerator.h:755
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition: AudioTypes.h:330
Generates pink noise.
Definition: SoundGenerator.h:314
PinkNoiseGenerator(T amplitude=32767)
the amplitude defines the max value which is generated
Definition: SoundGenerator.h:317
T readSample()
Provides a single sample.
Definition: SoundGenerator.h:326
Provides a fixed value (e.g. 0) as sound data. This can be used e.g. to test the output functionality...
Definition: SoundGenerator.h:363
T readSample()
Provides a single sample.
Definition: SoundGenerator.h:369
A sine generator based on a table. The table is created using degrees where one full wave is 360 degr...
Definition: SoundGenerator.h:607
void setFrequency(float freq)
Abstract method: not implemented! Just provides an error message...
Definition: SoundGenerator.h:659
void setAmplitude(float amplitude)
Defines the new amplitude (volume)
Definition: SoundGenerator.h:615
void setMaxAmplitudeStep(float step)
Definition: SoundGenerator.h:619
T readSample()
Provides a single sample.
Definition: SoundGenerator.h:621
Generates a Sound with the help of sin() function. If you plan to change the amplitude or frequency (...
Definition: SoundGenerator.h:138
void setFrequency(float frequency) override
Defines the frequency - after the processing has been started.
Definition: SoundGenerator.h:191
virtual T readSample() override
Provides a single sample.
Definition: SoundGenerator.h:198
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition: SoundGenerator.h:181
virtual AudioInfo defaultConfig() override
Provides the default configuration.
Definition: SoundGenerator.h:186
Base class to define the abstract interface for the sound generating classes.
Definition: SoundGenerator.h:25
virtual size_t readBytes(uint8_t *buffer, size_t lengthBytes)
Provides the data as byte array with the requested number of channels.
Definition: SoundGenerator.h:61
virtual T readSample()=0
Provides a single sample.
virtual void setFrequency(float frequency)
Abstract method: not implemented! Just provides an error message...
Definition: SoundGenerator.h:82
virtual bool isActive()
Definition: SoundGenerator.h:55
virtual void setAudioInfo(AudioInfo info)
Defines/updates the AudioInfo.
Definition: SoundGenerator.h:90
virtual AudioInfo audioInfo()
Provides the AudioInfo.
Definition: SoundGenerator.h:87
virtual void end()
ends the processing
Definition: SoundGenerator.h:51
virtual AudioInfo defaultConfig()
Provides the default configuration.
Definition: SoundGenerator.h:75
Generates a square wave sound.
Definition: SoundGenerator.h:232
virtual T readSample()
Provides a single sample.
Definition: SoundGenerator.h:239
Definition: NoArduino.h:125
Generates a test signal which is easy to check because the values are incremented or decremented by 1...
Definition: SoundGenerator.h:781
T readSample() override
Provides a single sample.
Definition: SoundGenerator.h:785
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
Supports the setting and getting of the volume.
Definition: AudioTypes.h:210
virtual float volume()
provides the actual volume in the range of 0.0f to 1.0f
Definition: AudioTypes.h:213
virtual bool setVolume(float volume)
define the actual volume in the range of 0.0f to 1.0f
Definition: AudioTypes.h:215
Generates a random noise sound with the help of rand() function.
Definition: SoundGenerator.h:293
WhiteNoiseGenerator(T amplitude=32767)
the scale defines the max value which is generated
Definition: SoundGenerator.h:296
T readSample()
Provides a single sample.
Definition: SoundGenerator.h:299
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:50
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:57