arduino-audio-tools
Loading...
Searching...
No Matches
SoundGenerator.h
Go to the documentation of this file.
1#pragma once
2
3#include <math.h>
4
9
16namespace audio_tools {
17
27template <class T = int16_t>
29 public:
30 SoundGenerator() { info.bits_per_sample = sizeof(T) * 8; }
31
32 virtual ~SoundGenerator() { end(); }
33
35 virtual bool begin(AudioInfo info) {
36 this->info = info;
37 return begin();
38 }
39
41 virtual bool begin() {
42 TRACED();
43 active = true;
44 activeWarningIssued = false;
45 info.logInfo("SoundGenerator:");
46
47 // support bytes < framesize
48 ring_buffer.resize(info.channels * sizeof(T));
49
50 return true;
51 }
52
54 virtual void end() { active = false; }
55
57 virtual bool isActive() { return active; }
58
60 virtual T readSample() = 0;
61
63 virtual size_t readBytes(uint8_t* data, size_t len) {
64 LOGD("readBytes: %d", (int)len);
65 if (!active) return 0;
66 int channels = audioInfo().channels;
67 int frame_size = sizeof(T) * channels;
68 int frames = len / frame_size;
69 if (len >= frame_size) {
70 return readBytesFrames(data, len, frames, channels);
71 }
72 return readBytesFromBuffer(data, len, frame_size, channels);
73 }
74
77 AudioInfo def;
78 def.bits_per_sample = sizeof(T) * 8;
79 return def;
80 }
81
83 virtual void setFrequency(float frequency) {
84 LOGE("setFrequency not supported");
85 }
86
88 virtual AudioInfo audioInfo() { return info; }
89
91 virtual void setAudioInfo(AudioInfo info) {
92 this->info = info;
93 if (info.bits_per_sample != sizeof(T) * 8) {
94 LOGE("invalid bits_per_sample: %d", info.channels);
95 }
97 }
98
100 void setPlayTime(uint32_t playMs, uint8_t upPercent = 20,
101 uint8_t downPercent = 30) {
102 LOGI("setPlayTime: playMs=%d, upPercent=%d, downPercent=%d", playMs,
104 this->playMs = playMs;
105 this->upPercent = upPercent;
106 this->downPercent = downPercent;
107 currentSample = 0;
109 factor = 0.0f;
110 }
111
112 protected:
113 bool active = false;
115 // int output_channels = 1;
118 uint32_t playMs = 0;
119 uint8_t upPercent = 5;
120 uint8_t downPercent = 40;
121 uint32_t playSamples = 0;
122 uint32_t upSamples = 0;
123 uint32_t rampDownSamples = 0;
124 float rampUpInc = 0.0;
125 float rampDownDec = 0.0;
126 float factor = 1.0f;
127 uint32_t currentSample = 0;
128
130 if (upPercent + downPercent > 100) {
131 downPercent = 100 - upPercent;
132 }
134 upSamples = (playSamples * upPercent) / 100;
136 rampUpInc = 0;
137 if (upSamples > 0) {
138 rampUpInc = 1.0f / upSamples;
139 }
140 rampDownDec = 0;
141 if (rampDownSamples > 0) {
143 }
144 }
145
146 size_t readBytesFrames(uint8_t* buffer, size_t lengthBytes, int frames,
147 int channels) {
148 T* result_buffer = (T*)buffer;
149 int frames_written = 0;
150 if (playMs > 0 && currentSample > playSamples) {
151 return 0;
152 }
153
154 for (int j = 0; j < frames; j++) {
155 T sample = readSample();
156
157 // if we requested a play time
158 if (playMs > 0) {
160 sample = applyRamp(sample);
161 }
162
163 for (int ch = 0; ch < channels; ch++) {
164 *result_buffer++ = sample;
165 }
166
167 frames_written++;
168 // exit loop if we have reached the requested play time
169 if (playMs > 0 && currentSample > playSamples) {
170 break;
171 }
172 }
173 return frames_written * sizeof(T) * channels;
174 }
175
176 // Applies ramp up and ramp down logic to the sample
177 T applyRamp(T sample) {
178 // Ramp up
179 if (rampUpInc > 0 && currentSample <= upSamples) {
180 factor += rampUpInc;
181 if (factor > 1.0f) {
182 factor = 1.0f;
183 }
184 }
185 // Ramp down
188 if (factor < 0.0f) {
189 factor = 0.0f;
190 }
191 }
192 // Sustain
193 else {
194 factor = 1.0f;
195 }
196 return (T)(factor * sample);
197 }
198
199 size_t readBytesFromBuffer(uint8_t* buffer, size_t lengthBytes,
200 int frame_size, int channels) {
201 // fill ringbuffer with one frame
202 if (ring_buffer.isEmpty()) {
203 uint8_t tmp[frame_size];
204 readBytesFrames(tmp, frame_size, 1, channels);
205 ring_buffer.writeArray(tmp, frame_size);
206 }
207 // provide result
208 return ring_buffer.readArray(buffer, lengthBytes);
209 }
210};
211
221template <class T = int16_t>
222class SineGenerator : public SoundGenerator<T> {
223 public:
224 // the scale defines the max value which is generated
225 SineGenerator(float amplitude = NumberConverter::maxValueT<T>(),
226 float phase = 0.0f) {
227 LOGD("SineGenerator");
228 m_amplitude = amplitude;
229 m_phase = phase;
230 }
231
232 bool begin() override {
233 TRACEI();
235 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
236 return true;
237 }
238
239 bool begin(AudioInfo info) override {
240 LOGI("%s::begin(channels=%d, sample_rate=%d)", "SineGenerator",
241 (int)info.channels, (int)info.sample_rate);
243 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
244 return true;
245 }
246
247 bool begin(AudioInfo info, float frequency) {
248 LOGI("%s::begin(channels=%d, sample_rate=%d, frequency=%.2f)",
249 "SineGenerator", (int)info.channels, (int)info.sample_rate,
250 frequency);
252 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
253 if (frequency > 0.0f) {
254 setFrequency(frequency);
255 }
256 return true;
257 }
258
259 bool begin(int channels, int sample_rate, float frequency) {
260 SoundGenerator<T>::info.channels = channels;
261 SoundGenerator<T>::info.sample_rate = sample_rate;
262 return begin(SoundGenerator<T>::info, frequency);
263 }
264
265 // update m_deltaTime
266 virtual void setAudioInfo(AudioInfo info) override {
268 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
269 }
270
271 virtual AudioInfo defaultConfig() override {
273 }
274
276 void setFrequency(float frequency) override {
277 LOGI("setFrequency: %.2f", frequency);
278 LOGI("active: %s", SoundGenerator<T>::active ? "true" : "false");
279 if (m_frequency != frequency) {
280 m_cycles = 0.0f; // reset cycles to avoid phase jumps
281 m_phase = 0.0f; // reset phase to avoid jumps
282 }
283 m_frequency = frequency;
284 }
285
287 virtual T readSample() override {
288 float angle = double_Pi * m_cycles + m_phase;
289 T result = m_amplitude * sinf(angle);
291 if (m_cycles > 1.0f) {
292 m_cycles -= 1.0f;
293 }
294 return result;
295 }
296
297 void setAmplitude(float amp) { m_amplitude = amp; }
298
299 protected:
300 volatile float m_frequency = 0.0f;
301 float m_cycles = 0.0f; // Varies between 0.0 and 1.0
302 float m_amplitude = 1.0f;
303 float m_deltaTime = 0.0f;
304 float m_phase = 0.0f;
305 const float double_Pi = 2.0f * PI;
306
307 void logStatus() {
308 SoundGenerator<T>::info.logStatus();
309 LOGI("amplitude: %f", this->m_amplitude);
310 LOGI("active: %s", SoundGenerator<T>::active ? "true" : "false");
311 }
312};
313
315template <class T = int16_t>
317
325template <class T = int16_t>
327 public:
328 FastSineGenerator(float amplitude = NumberConverter::maxValueT<T>(), float phase = 0.0)
329 : SineGenerator<T>(amplitude, phase) {
330 LOGD("FastSineGenerator");
331 }
332
333 virtual T readSample() override {
334 float angle =
336 T result = SineGenerator<T>::m_amplitude * sine(angle);
339 if (SineGenerator<T>::m_cycles > 1.0f) {
341 }
342 return result;
343 }
344
345 protected:
347 inline float sine(float t) {
348 float p = (t - (int)t) - 0.5f; // 0 <= p <= 1
349 float pp = p * p;
350 return (p - 6.283211f * pp * p + 9.132843f * pp * pp * p) * -6.221086f;
351 }
352};
353
367template <class T = int16_t>
369 public:
370 FastIntSineGenerator(float amplitude = NumberConverter::maxValueT<T>(),
371 float phase = 0.0f) {
372 LOGD("FastIntSineGenerator");
373 setAmplitude(amplitude);
374 setPhase(phase);
375 }
376
377 bool begin() override {
378 TRACEI();
381 return true;
382 }
383
384 bool begin(AudioInfo info) override {
385 LOGI("%s::begin(channels=%d, sample_rate=%d)", "FastIntSineGenerator",
386 (int)info.channels, (int)info.sample_rate);
389 return true;
390 }
391
392 bool begin(AudioInfo info, float frequency) {
394 if (frequency > 0.0f) {
395 setFrequency(frequency);
396 }
397 return true;
398 }
399
400 bool begin(int channels, int sample_rate, float frequency) {
401 SoundGenerator<T>::info.channels = channels;
402 SoundGenerator<T>::info.sample_rate = sample_rate;
403 return begin(SoundGenerator<T>::info, frequency);
404 }
405
410
412 void setFrequency(float frequency) override {
413 LOGI("setFrequency: %.2f", frequency);
414 m_frequency = frequency;
416 }
417
419 void setPhase(float phase) {
420 double turns = phase / (2.0 * PI);
421 m_phase_offset = (uint32_t)(turns * 4294967296.0);
422 }
423
424 void setAmplitude(float amp) {
425 m_amplitude = amp;
426 m_amplitude_i = (int32_t)amp;
427 }
428
430 virtual T readSample() override {
431 // top kTableBits of the 32 bit accumulator select the table entry
432 uint32_t index = (m_phase_acc + m_phase_offset) >> kIndexShift;
434 int32_t sine_q15 = sine_table[index];
435 return (T)(((int64_t)sine_q15 * m_amplitude_i) >> 15);
436 }
437
438 protected:
439 static const int kTableBits = 8;
440 static const int kTableSize = 1 << kTableBits; // 256 entries
441 static const int kIndexShift = 32 - kTableBits;
442
443 volatile float m_frequency = 0.0f;
444 float m_amplitude = 1.0f;
445 int32_t m_amplitude_i = 32767;
446 // fixed point (32 bit) phase: wrapping is implicit via unsigned overflow
447 uint32_t m_phase_acc = 0;
448 uint32_t m_phase_offset = 0;
449 uint32_t m_phase_increment = 0;
450
451 // only called from begin()/setFrequency()/setAudioInfo(), never per sample
453 uint32_t sample_rate = SoundGenerator<T>::info.sample_rate;
454 if (sample_rate > 0) {
456 (uint32_t)((double)m_frequency / sample_rate * 4294967296.0);
457 }
458 }
459
460 // one sine period in Q15 fixed point (-32767..32767)
461 static constexpr int16_t sine_table[kTableSize] = {
462 0, 804, 1608, 2410, 3212, 4011, 4808, 5602, 6393, 7179,
463 7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732,
464 15446, 16151, 16846, 17530, 18204, 18868, 19519, 20159, 20787, 21403,
465 22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790,
466 27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571,
467 30852, 31113, 31356, 31580, 31785, 31971, 32137, 32285, 32412, 32521,
468 32609, 32678, 32728, 32757, 32767, 32757, 32728, 32678, 32609, 32521,
469 32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571,
470 30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790,
471 26319, 25832, 25329, 24811, 24279, 23731, 23170, 22594, 22005, 21403,
472 20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732,
473 14010, 13279, 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179,
474 6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804,
475 -1608, -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739,
476 -9512, -10278,-11039,-11793,-12539,-13279,-14010,-14732,-15446,-16151,
477 -16846,-17530,-18204,-18868,-19519,-20159,-20787,-21403,-22005,-22594,
478 -23170,-23731,-24279,-24811,-25329,-25832,-26319,-26790,-27245,-27683,
479 -28105,-28510,-28898,-29268,-29621,-29956,-30273,-30571,-30852,-31113,
480 -31356,-31580,-31785,-31971,-32137,-32285,-32412,-32521,-32609,-32678,
481 -32728,-32757,-32767,-32757,-32728,-32678,-32609,-32521,-32412,-32285,
482 -32137,-31971,-31785,-31580,-31356,-31113,-30852,-30571,-30273,-29956,
483 -29621,-29268,-28898,-28510,-28105,-27683,-27245,-26790,-26319,-25832,
484 -25329,-24811,-24279,-23731,-23170,-22594,-22005,-21403,-20787,-20159,
485 -19519,-18868,-18204,-17530,-16846,-16151,-15446,-14732,-14010,-13279,
486 -12539,-11793,-11039,-10278,-9512, -8739, -7962, -7179, -6393, -5602,
487 -4808, -4011, -3212, -2410, -1608, -804};
488};
489
504template <class T = int16_t>
506 public:
507 SquareWaveGenerator(float amplitude = NumberConverter::maxValueT<T>(),
508 float phase = 0.0f) {
509 LOGD("SquareWaveGenerator");
510 setAmplitude(amplitude);
511 setPhase(phase);
512 }
513
514 bool begin() override {
515 TRACEI();
518 return true;
519 }
520
521 bool begin(AudioInfo info) override {
522 LOGI("%s::begin(channels=%d, sample_rate=%d)", "SquareWaveGenerator",
523 (int)info.channels, (int)info.sample_rate);
526 return true;
527 }
528
529 bool begin(AudioInfo info, float frequency) {
531 if (frequency > 0.0f) {
532 setFrequency(frequency);
533 }
534 return true;
535 }
536
537 bool begin(int channels, int sample_rate, float frequency) {
538 SoundGenerator<T>::info.channels = channels;
539 SoundGenerator<T>::info.sample_rate = sample_rate;
540 return begin(SoundGenerator<T>::info, frequency);
541 }
542
547
549 void setFrequency(float frequency) override {
550 LOGI("setFrequency: %.2f", frequency);
551 m_frequency = frequency;
553 }
554
556 void setPhase(float phase) {
557 double turns = phase / (2.0 * PI);
558 m_phase_offset = (uint32_t)(turns * 4294967296.0);
559 }
560
561 void setAmplitude(float amp) {
562 m_amplitude = amp;
563 m_amplitude_i = (int32_t)amp;
564 }
565
567 virtual T readSample() override {
568 uint32_t phase = m_phase_acc + m_phase_offset;
570 // top bit of the phase marks the half of the cycle we're in
571 return (int32_t)phase >= 0 ? (T)m_amplitude_i : (T)(-m_amplitude_i);
572 }
573
574 protected:
575 volatile float m_frequency = 0.0f;
576 float m_amplitude = 1.0f;
577 int32_t m_amplitude_i = 32767;
578 // fixed point (32 bit) phase: wrapping is implicit via unsigned overflow
579 uint32_t m_phase_acc = 0;
580 uint32_t m_phase_offset = 0;
581 uint32_t m_phase_increment = 0;
582
583 // only called from begin()/setFrequency()/setAudioInfo(), never per sample
585 uint32_t sample_rate = SoundGenerator<T>::info.sample_rate;
586 if (sample_rate > 0) {
588 (uint32_t)((double)m_frequency / sample_rate * 4294967296.0);
589 }
590 }
591};
592
608template <class T = int16_t>
610 public:
611 SawToothGenerator(float amplitude = NumberConverter::maxValueT<T>(),
612 float phase = 0.0f) {
613 LOGD("SawToothGenerator");
614 setAmplitude(amplitude);
615 setPhase(phase);
616 }
617
618 bool begin() override {
619 TRACEI();
622 return true;
623 }
624
625 bool begin(AudioInfo info) override {
626 LOGI("%s::begin(channels=%d, sample_rate=%d)", "SawToothGenerator",
627 (int)info.channels, (int)info.sample_rate);
630 return true;
631 }
632
633 bool begin(AudioInfo info, float frequency) {
635 if (frequency > 0.0f) {
636 setFrequency(frequency);
637 }
638 return true;
639 }
640
641 bool begin(int channels, int sample_rate, float frequency) {
642 SoundGenerator<T>::info.channels = channels;
643 SoundGenerator<T>::info.sample_rate = sample_rate;
644 return begin(SoundGenerator<T>::info, frequency);
645 }
646
651
653 void setFrequency(float frequency) override {
654 LOGI("setFrequency: %.2f", frequency);
655 m_frequency = frequency;
657 }
658
660 void setPhase(float phase) {
661 double turns = phase / (2.0 * PI);
662 m_phase_offset = (uint32_t)(turns * 4294967296.0);
663 }
664
665 void setAmplitude(float amp) {
666 m_amplitude = amp;
667 m_amplitude_i = (int32_t)amp;
668 }
669
671 virtual T readSample() override {
672 uint32_t phase = m_phase_acc + m_phase_offset;
674 // reinterpreting the wrapping unsigned phase as signed already gives
675 // a linear ramp from -2^31 to 2^31-1: exactly a saw tooth
676 int32_t ramp = (int32_t)phase;
677 return (T)(((int64_t)ramp * m_amplitude_i) >> 31);
678 }
679
680 protected:
681 volatile float m_frequency = 0.0f;
682 float m_amplitude = 1.0f;
683 int32_t m_amplitude_i = 32767;
684 // fixed point (32 bit) phase: wrapping is implicit via unsigned overflow
685 uint32_t m_phase_acc = 0;
686 uint32_t m_phase_offset = 0;
687 uint32_t m_phase_increment = 0;
688
689 // only called from begin()/setFrequency()/setAudioInfo(), never per sample
691 uint32_t sample_rate = SoundGenerator<T>::info.sample_rate;
692 if (sample_rate > 0) {
694 (uint32_t)((double)m_frequency / sample_rate * 4294967296.0);
695 }
696 }
697};
698
706template <class T = int16_t>
708 public:
711
714
715 protected:
717 // //range : [min, max]
718 int random(int min, int max) { return min + rand() % ((max + 1) - min); }
719};
720
728template <class T = int16_t>
730 public:
733 this->amplitude = amplitude;
734 max_key = 0x1f; // Five bits set
735 key = 0;
736 for (int i = 0; i < 5; i++) white_values[i] = rand() % (amplitude / 5);
737 }
738
741 T last_key = key;
742 unsigned int sum;
743
744 key++;
745 if (key > max_key) key = 0;
746 // Exclusive-Or previous value with current value. This gives
747 // a list of bits that have changed.
748 int diff = last_key ^ key;
749 sum = 0;
750 for (int i = 0; i < 5; i++) {
751 // If bit changed get new random number for corresponding
752 // white_value
753 if (diff & (1 << i)) white_values[i] = rand() % (amplitude / 5);
754 sum += white_values[i];
755 }
756 return sum;
757 }
758
759 protected:
762 unsigned int white_values[5];
763 unsigned int amplitude;
764};
765
775template <class T = int16_t>
777 public:
778 // the scale defines the max value which is generated
779 SilenceGenerator(T value = 0) { this->value = value; }
780
783 return value; // return 0
784 }
785
786 protected:
788};
789
797template <class T = int16_t>
799 public:
803
813 GeneratorFromStream(Stream& input, int channels = 1, float volume = 1.0) {
814 maxValue = NumberConverter::maxValue(sizeof(T) * 8);
815 setStream(input);
818 }
819
821 void setStream(Stream& input) { this->p_stream = &input; }
822
823 void setChannels(int channels) { this->channels = channels; }
824
827 T data = 0;
828 float total = 0;
829 if (p_stream != nullptr) {
830 for (int j = 0; j < channels; j++) {
831 p_stream->readBytes((uint8_t*)&data, sizeof(T));
832 total += data;
833 }
834 float avg = (total / channels) * volume();
835 if (avg > maxValue) {
836 data = maxValue;
837 } else if (avg < -maxValue) {
838 data = -maxValue;
839 } else {
840 data = avg;
841 }
842 }
843 return data;
844 }
845
846 protected:
847 Stream* p_stream = nullptr;
848 int channels = 1;
849 float maxValue;
850};
851
861template <class T = int16_t>
863 public:
877 template <size_t arrayLen>
878 GeneratorFromArray(T (&array)[arrayLen], int repeat = 0,
879 bool setInactiveAtEnd = false, size_t startIndex = 0) {
880 TRACED();
881 this->max_repeat = repeat;
882 this->inactive_at_end = setInactiveAtEnd;
883 this->sound_index = startIndex;
884 setArray(array, arrayLen);
885 }
886
887 template <int arrayLen>
888 void setArray(T (&array)[arrayLen]) {
889 TRACED();
890 setArray(array, arrayLen);
891 }
892
893 void setArray(T* array, size_t size) {
894 table.resize(size);
895 for (int j = 0; j < size; j++) {
896 table[j] = array[j];
897 }
898 LOGI("table_length: %d", (int)size);
899 }
900
902 bool begin(AudioInfo info) override {
904 }
905
908 bool rc = begin(info);
910 return rc;
911 }
912
914 bool begin() override {
915 TRACEI();
917 sound_index = 0.0f;
918 repeat_counter = 0;
919 is_running = true;
920 return true;
921 }
922
923 void end() override { table.resize(0); }
924
926 T readSample() override {
927 if (table.size() == 0) {
928 return 0;
929 }
930
931 if (!this->is_running) {
932 return 0;
933 }
934
935 const float table_size = static_cast<float>(table.size());
936
937 // at end deactivate output
938 while (sound_index >= table_size) {
939 // LOGD("reset index - sound_index: %d, table_length:
940 // %d",sound_index,table_length);
941 sound_index -= table_size;
942 // deactivate when count has been used up
943 if (max_repeat >= 1 && ++repeat_counter >= max_repeat) {
944 LOGD("atEnd");
945 this->is_running = false;
946 if (inactive_at_end) {
947 this->active = false;
948 }
949 return 0;
950 }
951 }
952
953 // LOGD("index: %d - active: %d", sound_index, this->active);
954 T result = 0;
955 if (this->is_running) {
956 int idx0 = static_cast<int>(sound_index);
957 int idx1 = idx0 + 1;
958 if (idx1 >= static_cast<int>(table.size())) {
959 idx1 = 0;
960 }
961 float frac = sound_index - static_cast<float>(idx0);
962 float sample = static_cast<float>(table[idx0]) * (1.0f - frac) +
963 static_cast<float>(table[idx1]) * frac;
964 result = static_cast<T>(sample);
966 }
967
968 return result;
969 }
970
971 // step size the sound index is incremented (default = 1)
972 void setIncrement(int inc) {
973 index_increment = inc;
974 frequency = 0.0f;
975 }
976
978 void setFrequency(float frequency) override {
979 if (SoundGenerator<T>::audioInfo().sample_rate <= 0 || table.size() == 0) {
980 LOGE("setFrequency failed: sample_rate=%d table_size=%d",
981 (int)SoundGenerator<T>::audioInfo().sample_rate, (int)table.size());
982 return;
983 }
984 if (frequency < 0.0f) {
985 frequency = 0.0f;
986 }
987 this->frequency = frequency;
989 frequency * static_cast<float>(table.size()) /
990 static_cast<float>(SoundGenerator<T>::audioInfo().sample_rate);
991 }
992
993 // Sets up a sine table - returns the effective frequency
994 int setupSine(int sampleRate, float reqFrequency, float amplitude = 1.0) {
995 int sample_count =
996 static_cast<float>(sampleRate) /
997 reqFrequency; // e.g. 44100 / 300hz = 147 samples per wave
998 float angle = 2.0 * PI / sample_count;
999 table.resize(sample_count);
1000 for (int j = 0; j < sample_count; j++) {
1001 table[j] = sinf(j * angle) * amplitude;
1002 }
1003 // calculate effective frequency
1004 return sampleRate / sample_count;
1005 }
1006
1007 // Similar like is active to check if the array is still playing.
1008 bool isRunning() { return is_running; }
1009
1010 protected:
1011 float sound_index = 0.0f;
1012 int max_repeat = 0;
1014 bool inactive_at_end = false;
1015 bool is_running = false;
1016 bool owns_data = false;
1018 float index_increment = 1.0f;
1019 float frequency = 0.0f;
1020};
1021
1029template <class T = int16_t>
1031 public:
1033
1035
1036 void setValue(T value) { value_set = value; }
1037
1039 bool begin() override {
1040 TRACEI();
1042 is_running = true;
1044 return true;
1045 }
1046
1048 T readSample() override { return value_return; }
1049
1050 // Similar like is active to check if the array is still playing.
1051 bool isRunning() { return is_running; }
1052
1053 protected:
1056 bool is_running = false;
1057};
1058
1066template <class T = int16_t>
1068 public:
1069 SineFromTable(float amplitude = NumberConverter::maxValueT<T>()) {
1070 this->amplitude = amplitude;
1071 this->amplitude_to_be = amplitude;
1072 }
1073
1076
1080
1082 // update angle
1083 angle += step;
1084 if (angle >= 360.0f) {
1085 while (angle >= 360.0f) {
1086 angle -= 360.0f;
1087 }
1088 // update frequency at start of circle (near 0 degrees)
1089 step = step_new;
1090
1092 // amplitude = amplitude_to_be;
1093 }
1094 return interpolate(angle);
1095 }
1096
1097 bool begin() {
1098 is_first = true;
1101 360.0f; // 122.5 hz (at 44100); 61 hz (at 22050)
1102 return true;
1103 }
1104
1105 bool begin(AudioInfo info, float frequency) {
1108 360.0f; // 122.5 hz (at 44100); 61 hz (at 22050)
1109 setFrequency(frequency);
1110 return true;
1111 }
1112
1113 bool begin(int channels, int sample_rate, uint16_t frequency = 0) {
1114 SoundGenerator<T>::info.channels = channels;
1115 SoundGenerator<T>::info.sample_rate = sample_rate;
1116 return begin(SoundGenerator<T>::info, frequency);
1117 }
1118
1119 void setFrequency(float freq) {
1120 step_new = freq / base_frequency;
1121 if (is_first) {
1122 step = step_new;
1123 is_first = false;
1124 }
1125 LOGD("step: %f", step_new);
1126 }
1127
1128 protected:
1129 bool is_first = true;
1132 float max_amplitude_step = 50.0f;
1133 float base_frequency = 1.0f;
1134 float step = 1.0f;
1135 float step_new = 1.0f;
1136 float angle = 0.0f;
1137 // 122.5 hz (at 44100); 61 hz (at 22050)
1138 const float values[181] = {
1139 0, 0.0174524, 0.0348995, 0.052336, 0.0697565, 0.0871557,
1140 0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809,
1141 0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372,
1142 0.309017, 0.325568, 0.34202, 0.358368, 0.374607, 0.390731,
1143 0.406737, 0.422618, 0.438371, 0.45399, 0.469472, 0.48481,
1144 0.5, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576,
1145 0.587785, 0.601815, 0.615661, 0.62932, 0.642788, 0.656059,
1146 0.669131, 0.681998, 0.694658, 0.707107, 0.71934, 0.731354,
1147 0.743145, 0.75471, 0.766044, 0.777146, 0.788011, 0.798636,
1148 0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167,
1149 0.866025, 0.87462, 0.882948, 0.891007, 0.898794, 0.906308,
1150 0.913545, 0.920505, 0.927184, 0.93358, 0.939693, 0.945519,
1151 0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.97437,
1152 0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546,
1153 0.994522, 0.996195, 0.997564, 0.99863, 0.999391, 0.999848,
1154 1, 0.999848, 0.999391, 0.99863, 0.997564, 0.996195,
1155 0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
1156 0.978148, 0.97437, 0.970296, 0.965926, 0.961262, 0.956305,
1157 0.951057, 0.945519, 0.939693, 0.93358, 0.927184, 0.920505,
1158 0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.87462,
1159 0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
1160 0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.75471,
1161 0.743145, 0.731354, 0.71934, 0.707107, 0.694658, 0.681998,
1162 0.669131, 0.656059, 0.642788, 0.62932, 0.615661, 0.601815,
1163 0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
1164 0.5, 0.48481, 0.469472, 0.45399, 0.438371, 0.422618,
1165 0.406737, 0.390731, 0.374607, 0.358368, 0.34202, 0.325568,
1166 0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
1167 0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
1168 0.104528, 0.0871557, 0.0697565, 0.052336, 0.0348995, 0.0174524,
1169 0};
1170
1172 bool positive = (angle <= 180.0f);
1173 float angle_positive = positive ? angle : angle - 180.0f;
1174 int angle_int1 = angle_positive;
1175 int angle_int2 = angle_int1 + 1;
1176 T v1 = values[angle_int1] * amplitude;
1177 T v2 = values[angle_int2] * amplitude;
1178 T result = v1 < v2 ? map(angle_positive, angle_int1, angle_int2, v1, v2)
1179 : map(angle_positive, angle_int1, angle_int2, v2, v1);
1180 // float result = v1;
1181 return positive ? result : -result;
1182 }
1183
1184 T map(T x, T in_min, T in_max, T out_min, T out_max) {
1185 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
1186 }
1187
1189 float diff = amplitude_to_be - amplitude;
1190 if (abs(diff) > max_amplitude_step) {
1191 diff = (diff < 0) ? -max_amplitude_step : max_amplitude_step;
1192 }
1193 if (abs(diff) >= 1.0f) {
1194 amplitude += diff;
1195 }
1196 }
1197};
1198
1207template <class T = int16_t>
1209 public:
1210 GeneratorMixer() = default;
1211
1212 void add(SoundGenerator<T>& generator) { vector.push_back(&generator); }
1213 void add(SoundGenerator<T>* generator) { vector.push_back(generator); }
1214
1215 void clear() { vector.clear(); }
1216
1218 float total = 0.0f;
1219 float count = 0.0f;
1220 for (auto& generator : vector) {
1221 if (generator->isActive()) {
1222 T sample = generator->readSample();
1223 total += sample;
1224 count += 1.0f;
1225 }
1226 }
1227 return count > 0.0f ? total / count : 0;
1228 }
1229
1230 protected:
1233};
1234
1243template <class T = int16_t>
1245 public:
1246 TestGenerator(T max = 1000, T inc = 1) { this->max = max; }
1247
1248 T readSample() override {
1249 value += inc;
1250 if (abs(value) >= max) {
1251 inc = -inc;
1252 value += (inc * 2);
1253 }
1254 return value;
1255 }
1256
1257 protected:
1259 T value = 0;
1260 T inc = 1;
1261};
1262
1263} // namespace audio_tools
#define PI
Definition AudioEffectsSuite.h:27
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:136
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140
virtual int readArray(T data[], int len)
reads multiple values
Definition Buffers.h:34
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:56
Sine wave generator that does not use any floating point operations in the readSample() hot path: it ...
Definition SoundGenerator.h:368
void updatePhaseIncrement()
Definition SoundGenerator.h:452
static const int kTableSize
Definition SoundGenerator.h:440
void setFrequency(float frequency) override
Defines the frequency - the only place where float math is used.
Definition SoundGenerator.h:412
void setAmplitude(float amp)
Definition SoundGenerator.h:424
uint32_t m_phase_increment
Definition SoundGenerator.h:449
static constexpr int16_t sine_table[kTableSize]
Definition SoundGenerator.h:461
virtual T readSample() override
Provides a single sample - integer only, no float ops or sin() calls!
Definition SoundGenerator.h:430
volatile float m_frequency
Definition SoundGenerator.h:443
static const int kIndexShift
Definition SoundGenerator.h:441
FastIntSineGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:370
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:400
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:406
static const int kTableBits
Definition SoundGenerator.h:439
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:384
bool begin() override
Starts the processing.
Definition SoundGenerator.h:377
uint32_t m_phase_acc
Definition SoundGenerator.h:447
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:392
void setPhase(float phase)
Defines the starting phase in radians.
Definition SoundGenerator.h:419
uint32_t m_phase_offset
Definition SoundGenerator.h:448
int32_t m_amplitude_i
Definition SoundGenerator.h:445
float m_amplitude
Definition SoundGenerator.h:444
Sine wave which is based on a fast approximation function using floating point math.
Definition SoundGenerator.h:326
float sine(float t)
sine approximation.
Definition SoundGenerator.h:347
virtual T readSample() override
Provides a single sample.
Definition SoundGenerator.h:333
FastSineGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0)
Definition SoundGenerator.h:328
Just returns a constant value.
Definition SoundGenerator.h:1030
T value_set
Definition SoundGenerator.h:1054
bool isRunning()
Definition SoundGenerator.h:1051
virtual bool begin(AudioInfo info)
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:1034
T readSample() override
Provides a single sample.
Definition SoundGenerator.h:1048
bool is_running
Definition SoundGenerator.h:1056
bool begin() override
Starts the generation of samples.
Definition SoundGenerator.h:1039
T value_return
Definition SoundGenerator.h:1055
void setValue(T value)
Definition SoundGenerator.h:1036
We generate the samples from an array which is provided in the constructor.
Definition SoundGenerator.h:862
void setFrequency(float frequency) override
Defines the output frequency based on sample rate and table size.
Definition SoundGenerator.h:978
void setIncrement(int inc)
Definition SoundGenerator.h:972
int setupSine(int sampleRate, float reqFrequency, float amplitude=1.0)
Definition SoundGenerator.h:994
bool isRunning()
Definition SoundGenerator.h:1008
bool inactive_at_end
Definition SoundGenerator.h:1014
Vector< T > table
Definition SoundGenerator.h:1017
void end() override
Ends the processing.
Definition SoundGenerator.h:923
float sound_index
Definition SoundGenerator.h:1011
void setArray(T *array, size_t size)
Definition SoundGenerator.h:893
float index_increment
Definition SoundGenerator.h:1018
GeneratorFromArray(T(&array)[arrayLen], int repeat=0, bool setInactiveAtEnd=false, size_t startIndex=0)
Construct a new Generator from an array.
Definition SoundGenerator.h:878
T readSample() override
Provides a single sample.
Definition SoundGenerator.h:926
bool is_running
Definition SoundGenerator.h:1015
int max_repeat
Definition SoundGenerator.h:1012
bool owns_data
Definition SoundGenerator.h:1016
int repeat_counter
Definition SoundGenerator.h:1013
bool begin(AudioInfo info) override
Starts the generation of samples with the provided AudioInfo.
Definition SoundGenerator.h:902
bool begin() override
Starts the generation of samples.
Definition SoundGenerator.h:914
float frequency
Definition SoundGenerator.h:1019
bool begin(AudioInfo info, float frequency)
Starts the generation of samples with the provided AudioInfo and frequency.
Definition SoundGenerator.h:907
void setArray(T(&array)[arrayLen])
Definition SoundGenerator.h:888
An Adapter Class which lets you use any Stream as a Generator.
Definition SoundGenerator.h:798
int channels
Definition SoundGenerator.h:848
GeneratorFromStream()
Definition SoundGenerator.h:800
Stream * p_stream
Definition SoundGenerator.h:847
void setStream(Stream &input)
(Re-)Assigns a stream to the Adapter class
Definition SoundGenerator.h:821
void setChannels(int channels)
Definition SoundGenerator.h:823
float maxValue
Definition SoundGenerator.h:849
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:813
T readSample()
Provides a single sample from the stream.
Definition SoundGenerator.h:826
Generator which combines (mixes) multiple sound generators into one output.
Definition SoundGenerator.h:1208
void add(SoundGenerator< T > *generator)
Definition SoundGenerator.h:1213
Vector< SoundGenerator< T > * > vector
Definition SoundGenerator.h:1231
int actualChannel
Definition SoundGenerator.h:1232
void clear()
Definition SoundGenerator.h:1215
T readSample()
Provides a single sample.
Definition SoundGenerator.h:1217
void add(SoundGenerator< T > &generator)
Definition SoundGenerator.h:1212
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition AudioTypes.h:297
Generates pink noise.
Definition SoundGenerator.h:729
PinkNoiseGenerator(T amplitude=32767)
the amplitude defines the max value which is generated
Definition SoundGenerator.h:732
T key
Definition SoundGenerator.h:761
unsigned int amplitude
Definition SoundGenerator.h:763
T max_key
Definition SoundGenerator.h:760
unsigned int white_values[5]
Definition SoundGenerator.h:762
T readSample()
Provides a single sample.
Definition SoundGenerator.h:740
Implements a typed Ringbuffer.
Definition Buffers.h:353
virtual bool resize(size_t len)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:430
bool isEmpty()
Definition Buffers.h:400
Generates a saw tooth wave sound. Uses a 32 bit phase accumulator with an integer increment,...
Definition SoundGenerator.h:609
void updatePhaseIncrement()
Definition SoundGenerator.h:690
void setFrequency(float frequency) override
Defines the frequency - the only place where float math is used.
Definition SoundGenerator.h:653
void setAmplitude(float amp)
Definition SoundGenerator.h:665
uint32_t m_phase_increment
Definition SoundGenerator.h:687
virtual T readSample() override
Provides a single sample - integer only, no float ops!
Definition SoundGenerator.h:671
volatile float m_frequency
Definition SoundGenerator.h:681
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:641
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:647
SawToothGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:611
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:625
bool begin() override
Starts the processing.
Definition SoundGenerator.h:618
uint32_t m_phase_acc
Definition SoundGenerator.h:685
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:633
void setPhase(float phase)
Defines the starting phase in radians.
Definition SoundGenerator.h:660
uint32_t m_phase_offset
Definition SoundGenerator.h:686
int32_t m_amplitude_i
Definition SoundGenerator.h:683
float m_amplitude
Definition SoundGenerator.h:682
Provides a fixed value (e.g. 0) as sound data. This can be used e.g. to test the output functionality...
Definition SoundGenerator.h:776
SilenceGenerator(T value=0)
Definition SoundGenerator.h:779
T value
Definition SoundGenerator.h:787
T readSample()
Provides a single sample.
Definition SoundGenerator.h:782
A sine generator based on a table. The table is created using degrees where one full wave is 360 degr...
Definition SoundGenerator.h:1067
float amplitude
Definition SoundGenerator.h:1130
T interpolate(float angle)
Definition SoundGenerator.h:1171
float base_frequency
Definition SoundGenerator.h:1133
const float values[181]
Definition SoundGenerator.h:1138
T map(T x, T in_min, T in_max, T out_min, T out_max)
Definition SoundGenerator.h:1184
void updateAmplitudeInSteps()
Definition SoundGenerator.h:1188
bool is_first
Definition SoundGenerator.h:1129
float step_new
Definition SoundGenerator.h:1135
bool begin()
Starts the processing.
Definition SoundGenerator.h:1097
bool begin(int channels, int sample_rate, uint16_t frequency=0)
Definition SoundGenerator.h:1113
void setFrequency(float freq)
Abstract method: not implemented! Just provides an error message...
Definition SoundGenerator.h:1119
float step
Definition SoundGenerator.h:1134
float amplitude_to_be
Definition SoundGenerator.h:1131
float angle
Definition SoundGenerator.h:1136
float max_amplitude_step
Definition SoundGenerator.h:1132
void setAmplitude(float amplitude)
Defines the new amplitude (volume)
Definition SoundGenerator.h:1075
void setMaxAmplitudeStep(float step)
Definition SoundGenerator.h:1079
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:1105
T readSample()
Provides a single sample.
Definition SoundGenerator.h:1081
SineFromTable(float amplitude=NumberConverter::maxValueT< T >())
Definition SoundGenerator.h:1069
Generates a Sound with the help of sin() function. If performance is of concern, I suggest to use the...
Definition SoundGenerator.h:222
void setFrequency(float frequency) override
Defines the frequency - after the processing has been started.
Definition SoundGenerator.h:276
float m_phase
Definition SoundGenerator.h:304
void setAmplitude(float amp)
Definition SoundGenerator.h:297
virtual T readSample() override
Provides a single sample.
Definition SoundGenerator.h:287
volatile float m_frequency
Definition SoundGenerator.h:300
float m_deltaTime
Definition SoundGenerator.h:303
const float double_Pi
Definition SoundGenerator.h:305
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:259
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:266
virtual AudioInfo defaultConfig() override
Provides the default configuration.
Definition SoundGenerator.h:271
SineGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:225
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:239
bool begin() override
Starts the processing.
Definition SoundGenerator.h:232
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:247
void logStatus()
Definition SoundGenerator.h:307
float m_cycles
Definition SoundGenerator.h:301
float m_amplitude
Definition SoundGenerator.h:302
Base class to define the abstract interface for the sound generating classes.
Definition SoundGenerator.h:28
uint32_t rampDownSamples
Definition SoundGenerator.h:123
bool active
Definition SoundGenerator.h:113
void setPlayTime(uint32_t playMs, uint8_t upPercent=20, uint8_t downPercent=30)
Defines the play time in ms and the ramp up and ramp down time in percent.
Definition SoundGenerator.h:100
virtual size_t readBytes(uint8_t *data, size_t len)
Provides the data as byte array with the requested number of channels.
Definition SoundGenerator.h:63
virtual T readSample()=0
Provides a single sample.
bool activeWarningIssued
Definition SoundGenerator.h:114
float factor
Definition SoundGenerator.h:126
uint32_t playMs
Definition SoundGenerator.h:118
virtual bool begin(AudioInfo info)
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:35
AudioInfo info
Definition SoundGenerator.h:116
float rampDownDec
Definition SoundGenerator.h:125
SoundGenerator()
Definition SoundGenerator.h:30
virtual void setFrequency(float frequency)
Abstract method: not implemented! Just provides an error message...
Definition SoundGenerator.h:83
virtual bool begin()
Starts the processing.
Definition SoundGenerator.h:41
virtual bool isActive()
Checks if the begin method has been called - after end() isActive is false.
Definition SoundGenerator.h:57
void recalculatePlayTime()
Definition SoundGenerator.h:129
virtual void setAudioInfo(AudioInfo info)
Defines/updates the AudioInfo.
Definition SoundGenerator.h:91
T applyRamp(T sample)
Definition SoundGenerator.h:177
float rampUpInc
Definition SoundGenerator.h:124
size_t readBytesFrames(uint8_t *buffer, size_t lengthBytes, int frames, int channels)
Definition SoundGenerator.h:146
uint32_t upSamples
Definition SoundGenerator.h:122
uint32_t playSamples
Definition SoundGenerator.h:121
virtual AudioInfo audioInfo()
Provides the AudioInfo.
Definition SoundGenerator.h:88
virtual void end()
Ends the processing.
Definition SoundGenerator.h:54
virtual ~SoundGenerator()
Definition SoundGenerator.h:32
uint8_t downPercent
Definition SoundGenerator.h:120
uint8_t upPercent
Definition SoundGenerator.h:119
size_t readBytesFromBuffer(uint8_t *buffer, size_t lengthBytes, int frame_size, int channels)
Definition SoundGenerator.h:199
virtual AudioInfo defaultConfig()
Provides the default configuration.
Definition SoundGenerator.h:76
RingBuffer< uint8_t > ring_buffer
Definition SoundGenerator.h:117
uint32_t currentSample
Definition SoundGenerator.h:127
Generates a square wave sound. Uses the same 32 bit phase accumulator as SawToothGenerator/FastIntSin...
Definition SoundGenerator.h:505
SquareWaveGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:507
void updatePhaseIncrement()
Definition SoundGenerator.h:584
void setFrequency(float frequency) override
Defines the frequency - the only place where float math is used.
Definition SoundGenerator.h:549
void setAmplitude(float amp)
Definition SoundGenerator.h:561
uint32_t m_phase_increment
Definition SoundGenerator.h:581
virtual T readSample() override
Provides a single sample - integer only, no float ops!
Definition SoundGenerator.h:567
volatile float m_frequency
Definition SoundGenerator.h:575
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:537
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:543
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:521
bool begin() override
Starts the processing.
Definition SoundGenerator.h:514
uint32_t m_phase_acc
Definition SoundGenerator.h:579
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:529
void setPhase(float phase)
Defines the starting phase in radians.
Definition SoundGenerator.h:556
uint32_t m_phase_offset
Definition SoundGenerator.h:580
int32_t m_amplitude_i
Definition SoundGenerator.h:577
float m_amplitude
Definition SoundGenerator.h:576
Generates a test signal which is easy to check because the values are incremented or decremented by 1...
Definition SoundGenerator.h:1244
T inc
Definition SoundGenerator.h:1260
T value
Definition SoundGenerator.h:1259
T max
Definition SoundGenerator.h:1258
T readSample() override
Provides a single sample.
Definition SoundGenerator.h:1248
TestGenerator(T max=1000, T inc=1)
Definition SoundGenerator.h:1246
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:187
virtual float volume()
provides the actual volume in the range of 0.0f to 1.0f
Definition AudioTypes.h:190
virtual bool setVolume(float volume)
define the actual volume in the range of 0.0f to 1.0f
Definition AudioTypes.h:192
Generates a random noise sound with the help of rand() function.
Definition SoundGenerator.h:707
T amplitude
Definition SoundGenerator.h:716
WhiteNoiseGenerator(T amplitude=32767)
the scale defines the max value which is generated
Definition SoundGenerator.h:710
int random(int min, int max)
Definition SoundGenerator.h:718
T readSample()
Provides a single sample.
Definition SoundGenerator.h:713
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
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
virtual void logInfo(const char *source="")
Definition AudioTypes.h:121