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
224template <class T = int16_t>
226 public:
227 // the scale defines the max value which is generated
228 FloatSineGenerator(float amplitude = NumberConverter::maxValueT<T>(),
229 float phase = 0.0f) {
230 LOGD("FloatSineGenerator");
231 m_amplitude = amplitude;
232 m_phase = phase;
233 }
234
235 bool begin() override {
236 TRACEI();
238 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
239 return true;
240 }
241
242 bool begin(AudioInfo info) override {
243 LOGI("%s::begin(channels=%d, sample_rate=%d)", "FloatSineGenerator",
244 (int)info.channels, (int)info.sample_rate);
246 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
247 return true;
248 }
249
250 bool begin(AudioInfo info, float frequency) {
251 LOGI("%s::begin(channels=%d, sample_rate=%d, frequency=%.2f)",
252 "FloatSineGenerator", (int)info.channels, (int)info.sample_rate,
253 frequency);
255 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
256 if (frequency > 0.0f) {
257 setFrequency(frequency);
258 }
259 return true;
260 }
261
262 bool begin(int channels, int sample_rate, float frequency) {
263 SoundGenerator<T>::info.channels = channels;
264 SoundGenerator<T>::info.sample_rate = sample_rate;
265 return begin(SoundGenerator<T>::info, frequency);
266 }
267
268 // update m_deltaTime
269 virtual void setAudioInfo(AudioInfo info) override {
271 this->m_deltaTime = 1.0f / SoundGenerator<T>::info.sample_rate;
272 }
273
274 virtual AudioInfo defaultConfig() override {
276 }
277
279 void setFrequency(float frequency) override {
280 LOGI("setFrequency: %.2f", frequency);
281 LOGI("active: %s", SoundGenerator<T>::active ? "true" : "false");
282 if (m_frequency != frequency) {
283 m_cycles = 0.0f; // reset cycles to avoid phase jumps
284 m_phase = 0.0f; // reset phase to avoid jumps
285 }
286 m_frequency = frequency;
287 }
288
290 virtual T readSample() override {
291 float angle = double_Pi * m_cycles + m_phase;
292 T result = m_amplitude * sinf(angle);
294 if (m_cycles > 1.0f) {
295 m_cycles -= 1.0f;
296 }
297 return result;
298 }
299
300 void setAmplitude(float amp) { m_amplitude = amp; }
301
302 protected:
303 volatile float m_frequency = 0.0f;
304 float m_cycles = 0.0f; // Varies between 0.0 and 1.0
305 float m_amplitude = 1.0f;
306 float m_deltaTime = 0.0f;
307 float m_phase = 0.0f;
308 const float double_Pi = 2.0f * PI;
309
310 void logStatus() {
311 SoundGenerator<T>::info.logStatus();
312 LOGI("amplitude: %f", this->m_amplitude);
313 LOGI("active: %s", SoundGenerator<T>::active ? "true" : "false");
314 }
315};
316
324template <class T = int16_t>
326 public:
327 FastSineGenerator(float amplitude = NumberConverter::maxValueT<T>(), float phase = 0.0)
328 : FloatSineGenerator<T>(amplitude, phase) {
329 LOGD("FastSineGenerator");
330 }
331
343
344 protected:
346 inline float sine(float t) {
347 float p = (t - (int)t) - 0.5f; // 0 <= p <= 1
348 float pp = p * p;
349 return (p - 6.283211f * pp * p + 9.132843f * pp * pp * p) * -6.221086f;
350 }
351};
352
369template <class T = int16_t>
371 public:
372 FastIntSineGenerator(float amplitude = NumberConverter::maxValueT<T>(),
373 float phase = 0.0f) {
374 LOGD("FastIntSineGenerator");
375 setAmplitude(amplitude);
376 setPhase(phase);
377 }
378
379 bool begin() override {
380 TRACEI();
383 return true;
384 }
385
386 bool begin(AudioInfo info) override {
387 LOGI("%s::begin(channels=%d, sample_rate=%d)", "FastIntSineGenerator",
388 (int)info.channels, (int)info.sample_rate);
391 return true;
392 }
393
394 bool begin(AudioInfo info, float frequency) {
396 if (frequency > 0.0f) {
397 setFrequency(frequency);
398 }
399 return true;
400 }
401
402 bool begin(int channels, int sample_rate, float frequency) {
403 SoundGenerator<T>::info.channels = channels;
404 SoundGenerator<T>::info.sample_rate = sample_rate;
405 return begin(SoundGenerator<T>::info, frequency);
406 }
407
412
414 void setFrequency(float frequency) override {
415 LOGI("setFrequency: %.2f", frequency);
416 m_frequency = frequency;
418 }
419
421 void setPhase(float phase) {
422 double turns = phase / (2.0 * PI);
423 m_phase_offset = (uint32_t)(turns * 4294967296.0);
424 }
425
426 void setAmplitude(float amp) {
427 m_amplitude = amp;
428 m_amplitude_i = (int32_t)amp;
429 }
430
432 virtual T readSample() override {
433 // top kTableBits of the 32 bit accumulator select the table entry
434 uint32_t index = (m_phase_acc + m_phase_offset) >> kIndexShift;
436 int32_t sine_q15 = sine_table[index];
437 return (T)(((int64_t)sine_q15 * m_amplitude_i) >> 15);
438 }
439
440 protected:
441 static const int kTableBits = 8;
442 static const int kTableSize = 1 << kTableBits; // 256 entries
443 static const int kIndexShift = 32 - kTableBits;
444
445 volatile float m_frequency = 0.0f;
446 float m_amplitude = 1.0f;
447 int32_t m_amplitude_i = 32767;
448 // fixed point (32 bit) phase: wrapping is implicit via unsigned overflow
449 uint32_t m_phase_acc = 0;
450 uint32_t m_phase_offset = 0;
451 uint32_t m_phase_increment = 0;
452
453 // only called from begin()/setFrequency()/setAudioInfo(), never per sample
455 uint32_t sample_rate = SoundGenerator<T>::info.sample_rate;
456 if (sample_rate > 0) {
458 (uint32_t)((double)m_frequency / sample_rate * 4294967296.0);
459 }
460 }
461
462 // one sine period in Q15 fixed point (-32767..32767)
463 static constexpr int16_t sine_table[kTableSize] = {
464 0, 804, 1608, 2410, 3212, 4011, 4808, 5602, 6393, 7179,
465 7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732,
466 15446, 16151, 16846, 17530, 18204, 18868, 19519, 20159, 20787, 21403,
467 22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790,
468 27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571,
469 30852, 31113, 31356, 31580, 31785, 31971, 32137, 32285, 32412, 32521,
470 32609, 32678, 32728, 32757, 32767, 32757, 32728, 32678, 32609, 32521,
471 32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571,
472 30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790,
473 26319, 25832, 25329, 24811, 24279, 23731, 23170, 22594, 22005, 21403,
474 20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732,
475 14010, 13279, 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179,
476 6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804,
477 -1608, -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739,
478 -9512, -10278,-11039,-11793,-12539,-13279,-14010,-14732,-15446,-16151,
479 -16846,-17530,-18204,-18868,-19519,-20159,-20787,-21403,-22005,-22594,
480 -23170,-23731,-24279,-24811,-25329,-25832,-26319,-26790,-27245,-27683,
481 -28105,-28510,-28898,-29268,-29621,-29956,-30273,-30571,-30852,-31113,
482 -31356,-31580,-31785,-31971,-32137,-32285,-32412,-32521,-32609,-32678,
483 -32728,-32757,-32767,-32757,-32728,-32678,-32609,-32521,-32412,-32285,
484 -32137,-31971,-31785,-31580,-31356,-31113,-30852,-30571,-30273,-29956,
485 -29621,-29268,-28898,-28510,-28105,-27683,-27245,-26790,-26319,-25832,
486 -25329,-24811,-24279,-23731,-23170,-22594,-22005,-21403,-20787,-20159,
487 -19519,-18868,-18204,-17530,-16846,-16151,-15446,-14732,-14010,-13279,
488 -12539,-11793,-11039,-10278,-9512, -8739, -7962, -7179, -6393, -5602,
489 -4808, -4011, -3212, -2410, -1608, -804};
490};
491
494#if PREFER_FIXEDPOINT
495template <class T = int16_t>
496using SineGenerator = FastIntSineGenerator<T>;
497#else
498template <class T = int16_t>
500#endif
501
503template <class T = int16_t>
505
520template <class T = int16_t>
522 public:
523 SquareWaveGenerator(float amplitude = NumberConverter::maxValueT<T>(),
524 float phase = 0.0f) {
525 LOGD("SquareWaveGenerator");
526 setAmplitude(amplitude);
527 setPhase(phase);
528 }
529
530 bool begin() override {
531 TRACEI();
534 return true;
535 }
536
537 bool begin(AudioInfo info) override {
538 LOGI("%s::begin(channels=%d, sample_rate=%d)", "SquareWaveGenerator",
539 (int)info.channels, (int)info.sample_rate);
542 return true;
543 }
544
545 bool begin(AudioInfo info, float frequency) {
547 if (frequency > 0.0f) {
548 setFrequency(frequency);
549 }
550 return true;
551 }
552
553 bool begin(int channels, int sample_rate, float frequency) {
554 SoundGenerator<T>::info.channels = channels;
555 SoundGenerator<T>::info.sample_rate = sample_rate;
556 return begin(SoundGenerator<T>::info, frequency);
557 }
558
563
565 void setFrequency(float frequency) override {
566 LOGI("setFrequency: %.2f", frequency);
567 m_frequency = frequency;
569 }
570
572 void setPhase(float phase) {
573 double turns = phase / (2.0 * PI);
574 m_phase_offset = (uint32_t)(turns * 4294967296.0);
575 }
576
577 void setAmplitude(float amp) {
578 m_amplitude = amp;
579 m_amplitude_i = (int32_t)amp;
580 }
581
583 virtual T readSample() override {
584 uint32_t phase = m_phase_acc + m_phase_offset;
586 // top bit of the phase marks the half of the cycle we're in
587 return (int32_t)phase >= 0 ? (T)m_amplitude_i : (T)(-m_amplitude_i);
588 }
589
590 protected:
591 volatile float m_frequency = 0.0f;
592 float m_amplitude = 1.0f;
593 int32_t m_amplitude_i = 32767;
594 // fixed point (32 bit) phase: wrapping is implicit via unsigned overflow
595 uint32_t m_phase_acc = 0;
596 uint32_t m_phase_offset = 0;
597 uint32_t m_phase_increment = 0;
598
599 // only called from begin()/setFrequency()/setAudioInfo(), never per sample
601 uint32_t sample_rate = SoundGenerator<T>::info.sample_rate;
602 if (sample_rate > 0) {
604 (uint32_t)((double)m_frequency / sample_rate * 4294967296.0);
605 }
606 }
607};
608
624template <class T = int16_t>
626 public:
627 SawToothGenerator(float amplitude = NumberConverter::maxValueT<T>(),
628 float phase = 0.0f) {
629 LOGD("SawToothGenerator");
630 setAmplitude(amplitude);
631 setPhase(phase);
632 }
633
634 bool begin() override {
635 TRACEI();
638 return true;
639 }
640
641 bool begin(AudioInfo info) override {
642 LOGI("%s::begin(channels=%d, sample_rate=%d)", "SawToothGenerator",
643 (int)info.channels, (int)info.sample_rate);
646 return true;
647 }
648
649 bool begin(AudioInfo info, float frequency) {
651 if (frequency > 0.0f) {
652 setFrequency(frequency);
653 }
654 return true;
655 }
656
657 bool begin(int channels, int sample_rate, float frequency) {
658 SoundGenerator<T>::info.channels = channels;
659 SoundGenerator<T>::info.sample_rate = sample_rate;
660 return begin(SoundGenerator<T>::info, frequency);
661 }
662
667
669 void setFrequency(float frequency) override {
670 LOGI("setFrequency: %.2f", frequency);
671 m_frequency = frequency;
673 }
674
676 void setPhase(float phase) {
677 double turns = phase / (2.0 * PI);
678 m_phase_offset = (uint32_t)(turns * 4294967296.0);
679 }
680
681 void setAmplitude(float amp) {
682 m_amplitude = amp;
683 m_amplitude_i = (int32_t)amp;
684 }
685
687 virtual T readSample() override {
688 uint32_t phase = m_phase_acc + m_phase_offset;
690 // reinterpreting the wrapping unsigned phase as signed already gives
691 // a linear ramp from -2^31 to 2^31-1: exactly a saw tooth
692 int32_t ramp = (int32_t)phase;
693 return (T)(((int64_t)ramp * m_amplitude_i) >> 31);
694 }
695
696 protected:
697 volatile float m_frequency = 0.0f;
698 float m_amplitude = 1.0f;
699 int32_t m_amplitude_i = 32767;
700 // fixed point (32 bit) phase: wrapping is implicit via unsigned overflow
701 uint32_t m_phase_acc = 0;
702 uint32_t m_phase_offset = 0;
703 uint32_t m_phase_increment = 0;
704
705 // only called from begin()/setFrequency()/setAudioInfo(), never per sample
707 uint32_t sample_rate = SoundGenerator<T>::info.sample_rate;
708 if (sample_rate > 0) {
710 (uint32_t)((double)m_frequency / sample_rate * 4294967296.0);
711 }
712 }
713};
714
722template <class T = int16_t>
724 public:
727
730
731 protected:
733 // //range : [min, max]
734 int random(int min, int max) { return min + rand() % ((max + 1) - min); }
735};
736
744template <class T = int16_t>
746 public:
749 this->amplitude = amplitude;
750 max_key = 0x1f; // Five bits set
751 key = 0;
752 for (int i = 0; i < 5; i++) white_values[i] = rand() % (amplitude / 5);
753 }
754
757 T last_key = key;
758 unsigned int sum;
759
760 key++;
761 if (key > max_key) key = 0;
762 // Exclusive-Or previous value with current value. This gives
763 // a list of bits that have changed.
764 int diff = last_key ^ key;
765 sum = 0;
766 for (int i = 0; i < 5; i++) {
767 // If bit changed get new random number for corresponding
768 // white_value
769 if (diff & (1 << i)) white_values[i] = rand() % (amplitude / 5);
770 sum += white_values[i];
771 }
772 return sum;
773 }
774
775 protected:
778 unsigned int white_values[5];
779 unsigned int amplitude;
780};
781
791template <class T = int16_t>
793 public:
794 // the scale defines the max value which is generated
795 SilenceGenerator(T value = 0) { this->value = value; }
796
799 return value; // return 0
800 }
801
802 protected:
804};
805
813template <class T = int16_t>
815 public:
819
829 GeneratorFromStream(Stream& input, int channels = 1, float volume = 1.0) {
830 maxValue = NumberConverter::maxValue(sizeof(T) * 8);
831 setStream(input);
834 }
835
837 void setStream(Stream& input) { this->p_stream = &input; }
838
839 void setChannels(int channels) { this->channels = channels; }
840
843 T data = 0;
844 float total = 0;
845 if (p_stream != nullptr) {
846 for (int j = 0; j < channels; j++) {
847 p_stream->readBytes((uint8_t*)&data, sizeof(T));
848 total += data;
849 }
850 float avg = (total / channels) * volume();
851 if (avg > maxValue) {
852 data = maxValue;
853 } else if (avg < -maxValue) {
854 data = -maxValue;
855 } else {
856 data = avg;
857 }
858 }
859 return data;
860 }
861
862 protected:
863 Stream* p_stream = nullptr;
864 int channels = 1;
865 float maxValue;
866};
867
877template <class T = int16_t>
879 public:
893 template <size_t arrayLen>
894 GeneratorFromArray(T (&array)[arrayLen], int repeat = 0,
895 bool setInactiveAtEnd = false, size_t startIndex = 0) {
896 TRACED();
897 this->max_repeat = repeat;
898 this->inactive_at_end = setInactiveAtEnd;
899 this->sound_index = startIndex;
900 setArray(array, arrayLen);
901 }
902
903 template <int arrayLen>
904 void setArray(T (&array)[arrayLen]) {
905 TRACED();
906 setArray(array, arrayLen);
907 }
908
909 void setArray(T* array, size_t size) {
910 table.resize(size);
911 for (int j = 0; j < size; j++) {
912 table[j] = array[j];
913 }
914 LOGI("table_length: %d", (int)size);
915 }
916
918 bool begin(AudioInfo info) override {
920 }
921
924 bool rc = begin(info);
926 return rc;
927 }
928
930 bool begin() override {
931 TRACEI();
933 sound_index = 0.0f;
934 repeat_counter = 0;
935 is_running = true;
936 return true;
937 }
938
939 void end() override { table.resize(0); }
940
942 T readSample() override {
943 if (table.size() == 0) {
944 return 0;
945 }
946
947 if (!this->is_running) {
948 return 0;
949 }
950
951 const float table_size = static_cast<float>(table.size());
952
953 // at end deactivate output
954 while (sound_index >= table_size) {
955 // LOGD("reset index - sound_index: %d, table_length:
956 // %d",sound_index,table_length);
957 sound_index -= table_size;
958 // deactivate when count has been used up
959 if (max_repeat >= 1 && ++repeat_counter >= max_repeat) {
960 LOGD("atEnd");
961 this->is_running = false;
962 if (inactive_at_end) {
963 this->active = false;
964 }
965 return 0;
966 }
967 }
968
969 // LOGD("index: %d - active: %d", sound_index, this->active);
970 T result = 0;
971 if (this->is_running) {
972 int idx0 = static_cast<int>(sound_index);
973 int idx1 = idx0 + 1;
974 if (idx1 >= static_cast<int>(table.size())) {
975 idx1 = 0;
976 }
977 float frac = sound_index - static_cast<float>(idx0);
978 float sample = static_cast<float>(table[idx0]) * (1.0f - frac) +
979 static_cast<float>(table[idx1]) * frac;
980 result = static_cast<T>(sample);
982 }
983
984 return result;
985 }
986
987 // step size the sound index is incremented (default = 1)
988 void setIncrement(int inc) {
989 index_increment = inc;
990 frequency = 0.0f;
991 }
992
994 void setFrequency(float frequency) override {
995 if (SoundGenerator<T>::audioInfo().sample_rate <= 0 || table.size() == 0) {
996 LOGE("setFrequency failed: sample_rate=%d table_size=%d",
997 (int)SoundGenerator<T>::audioInfo().sample_rate, (int)table.size());
998 return;
999 }
1000 if (frequency < 0.0f) {
1001 frequency = 0.0f;
1002 }
1003 this->frequency = frequency;
1005 frequency * static_cast<float>(table.size()) /
1006 static_cast<float>(SoundGenerator<T>::audioInfo().sample_rate);
1007 }
1008
1009 // Sets up a sine table - returns the effective frequency
1010 int setupSine(int sampleRate, float reqFrequency, float amplitude = 1.0) {
1011 int sample_count =
1012 static_cast<float>(sampleRate) /
1013 reqFrequency; // e.g. 44100 / 300hz = 147 samples per wave
1014 float angle = 2.0 * PI / sample_count;
1015 table.resize(sample_count);
1016 for (int j = 0; j < sample_count; j++) {
1017 table[j] = sinf(j * angle) * amplitude;
1018 }
1019 // calculate effective frequency
1020 return sampleRate / sample_count;
1021 }
1022
1023 // Similar like is active to check if the array is still playing.
1024 bool isRunning() { return is_running; }
1025
1026 protected:
1027 float sound_index = 0.0f;
1028 int max_repeat = 0;
1030 bool inactive_at_end = false;
1031 bool is_running = false;
1032 bool owns_data = false;
1034 float index_increment = 1.0f;
1035 float frequency = 0.0f;
1036};
1037
1045template <class T = int16_t>
1047 public:
1049
1051
1052 void setValue(T value) { value_set = value; }
1053
1055 bool begin() override {
1056 TRACEI();
1058 is_running = true;
1060 return true;
1061 }
1062
1064 T readSample() override { return value_return; }
1065
1066 // Similar like is active to check if the array is still playing.
1067 bool isRunning() { return is_running; }
1068
1069 protected:
1072 bool is_running = false;
1073};
1074
1082template <class T = int16_t>
1084 public:
1085 SineFromTable(float amplitude = NumberConverter::maxValueT<T>()) {
1086 this->amplitude = amplitude;
1087 this->amplitude_to_be = amplitude;
1088 }
1089
1092
1096
1098 // update angle
1099 angle += step;
1100 if (angle >= 360.0f) {
1101 while (angle >= 360.0f) {
1102 angle -= 360.0f;
1103 }
1104 // update frequency at start of circle (near 0 degrees)
1105 step = step_new;
1106
1108 // amplitude = amplitude_to_be;
1109 }
1110 return interpolate(angle);
1111 }
1112
1113 bool begin() {
1114 is_first = true;
1117 360.0f; // 122.5 hz (at 44100); 61 hz (at 22050)
1118 return true;
1119 }
1120
1121 bool begin(AudioInfo info, float frequency) {
1124 360.0f; // 122.5 hz (at 44100); 61 hz (at 22050)
1125 setFrequency(frequency);
1126 return true;
1127 }
1128
1129 bool begin(int channels, int sample_rate, uint16_t frequency = 0) {
1130 SoundGenerator<T>::info.channels = channels;
1131 SoundGenerator<T>::info.sample_rate = sample_rate;
1132 return begin(SoundGenerator<T>::info, frequency);
1133 }
1134
1135 void setFrequency(float freq) {
1136 step_new = freq / base_frequency;
1137 if (is_first) {
1138 step = step_new;
1139 is_first = false;
1140 }
1141 LOGD("step: %f", step_new);
1142 }
1143
1144 protected:
1145 bool is_first = true;
1148 float max_amplitude_step = 50.0f;
1149 float base_frequency = 1.0f;
1150 float step = 1.0f;
1151 float step_new = 1.0f;
1152 float angle = 0.0f;
1153 // 122.5 hz (at 44100); 61 hz (at 22050)
1154 const float values[181] = {
1155 0, 0.0174524, 0.0348995, 0.052336, 0.0697565, 0.0871557,
1156 0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809,
1157 0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372,
1158 0.309017, 0.325568, 0.34202, 0.358368, 0.374607, 0.390731,
1159 0.406737, 0.422618, 0.438371, 0.45399, 0.469472, 0.48481,
1160 0.5, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576,
1161 0.587785, 0.601815, 0.615661, 0.62932, 0.642788, 0.656059,
1162 0.669131, 0.681998, 0.694658, 0.707107, 0.71934, 0.731354,
1163 0.743145, 0.75471, 0.766044, 0.777146, 0.788011, 0.798636,
1164 0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167,
1165 0.866025, 0.87462, 0.882948, 0.891007, 0.898794, 0.906308,
1166 0.913545, 0.920505, 0.927184, 0.93358, 0.939693, 0.945519,
1167 0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.97437,
1168 0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546,
1169 0.994522, 0.996195, 0.997564, 0.99863, 0.999391, 0.999848,
1170 1, 0.999848, 0.999391, 0.99863, 0.997564, 0.996195,
1171 0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
1172 0.978148, 0.97437, 0.970296, 0.965926, 0.961262, 0.956305,
1173 0.951057, 0.945519, 0.939693, 0.93358, 0.927184, 0.920505,
1174 0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.87462,
1175 0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
1176 0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.75471,
1177 0.743145, 0.731354, 0.71934, 0.707107, 0.694658, 0.681998,
1178 0.669131, 0.656059, 0.642788, 0.62932, 0.615661, 0.601815,
1179 0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
1180 0.5, 0.48481, 0.469472, 0.45399, 0.438371, 0.422618,
1181 0.406737, 0.390731, 0.374607, 0.358368, 0.34202, 0.325568,
1182 0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
1183 0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
1184 0.104528, 0.0871557, 0.0697565, 0.052336, 0.0348995, 0.0174524,
1185 0};
1186
1188 bool positive = (angle <= 180.0f);
1189 float angle_positive = positive ? angle : angle - 180.0f;
1190 int angle_int1 = angle_positive;
1191 int angle_int2 = angle_int1 + 1;
1192 T v1 = values[angle_int1] * amplitude;
1193 T v2 = values[angle_int2] * amplitude;
1194 T result = v1 < v2 ? map(angle_positive, angle_int1, angle_int2, v1, v2)
1195 : map(angle_positive, angle_int1, angle_int2, v2, v1);
1196 // float result = v1;
1197 return positive ? result : -result;
1198 }
1199
1200 T map(T x, T in_min, T in_max, T out_min, T out_max) {
1201 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
1202 }
1203
1205 float diff = amplitude_to_be - amplitude;
1206 if (abs(diff) > max_amplitude_step) {
1207 diff = (diff < 0) ? -max_amplitude_step : max_amplitude_step;
1208 }
1209 if (abs(diff) >= 1.0f) {
1210 amplitude += diff;
1211 }
1212 }
1213};
1214
1223template <class T = int16_t>
1225 public:
1226 GeneratorMixer() = default;
1227
1228 void add(SoundGenerator<T>& generator) { vector.push_back(&generator); }
1229 void add(SoundGenerator<T>* generator) { vector.push_back(generator); }
1230
1231 void clear() { vector.clear(); }
1232
1234 float total = 0.0f;
1235 float count = 0.0f;
1236 for (auto& generator : vector) {
1237 if (generator->isActive()) {
1238 T sample = generator->readSample();
1239 total += sample;
1240 count += 1.0f;
1241 }
1242 }
1243 return count > 0.0f ? total / count : 0;
1244 }
1245
1246 protected:
1249};
1250
1259template <class T = int16_t>
1261 public:
1262 TestGenerator(T max = 1000, T inc = 1) { this->max = max; }
1263
1264 T readSample() override {
1265 value += inc;
1266 if (abs(value) >= max) {
1267 inc = -inc;
1268 value += (inc * 2);
1269 }
1270 return value;
1271 }
1272
1273 protected:
1275 T value = 0;
1276 T inc = 1;
1277};
1278
1279} // namespace audio_tools
#define PI
Definition AudioEffectsSuite.h:28
#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
Sine wave generator that does not use any floating point operations in the readSample() hot path: it ...
Definition SoundGenerator.h:370
void updatePhaseIncrement()
Definition SoundGenerator.h:454
static const int kTableSize
Definition SoundGenerator.h:442
void setFrequency(float frequency) override
Defines the frequency - the only place where float math is used.
Definition SoundGenerator.h:414
void setAmplitude(float amp)
Definition SoundGenerator.h:426
uint32_t m_phase_increment
Definition SoundGenerator.h:451
static constexpr int16_t sine_table[kTableSize]
Definition SoundGenerator.h:463
virtual T readSample() override
Provides a single sample - integer only, no float ops or sin() calls!
Definition SoundGenerator.h:432
volatile float m_frequency
Definition SoundGenerator.h:445
static const int kIndexShift
Definition SoundGenerator.h:443
FastIntSineGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:372
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:402
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:408
static const int kTableBits
Definition SoundGenerator.h:441
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:386
bool begin() override
Starts the processing.
Definition SoundGenerator.h:379
uint32_t m_phase_acc
Definition SoundGenerator.h:449
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:394
void setPhase(float phase)
Defines the starting phase in radians.
Definition SoundGenerator.h:421
uint32_t m_phase_offset
Definition SoundGenerator.h:450
int32_t m_amplitude_i
Definition SoundGenerator.h:447
float m_amplitude
Definition SoundGenerator.h:446
Sine wave which is based on a fast approximation function using floating point math.
Definition SoundGenerator.h:325
float sine(float t)
sine approximation.
Definition SoundGenerator.h:346
virtual T readSample() override
Provides a single sample.
Definition SoundGenerator.h:332
FastSineGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0)
Definition SoundGenerator.h:327
Generates a Sound with the help of sin() function. If performance is of concern, I suggest to use the...
Definition SoundGenerator.h:225
void setFrequency(float frequency) override
Defines the frequency - after the processing has been started.
Definition SoundGenerator.h:279
float m_phase
Definition SoundGenerator.h:307
void setAmplitude(float amp)
Definition SoundGenerator.h:300
virtual T readSample() override
Provides a single sample.
Definition SoundGenerator.h:290
volatile float m_frequency
Definition SoundGenerator.h:303
float m_deltaTime
Definition SoundGenerator.h:306
const float double_Pi
Definition SoundGenerator.h:308
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:262
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:269
virtual AudioInfo defaultConfig() override
Provides the default configuration.
Definition SoundGenerator.h:274
FloatSineGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:228
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:242
bool begin() override
Starts the processing.
Definition SoundGenerator.h:235
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:250
void logStatus()
Definition SoundGenerator.h:310
float m_cycles
Definition SoundGenerator.h:304
float m_amplitude
Definition SoundGenerator.h:305
Just returns a constant value.
Definition SoundGenerator.h:1046
T value_set
Definition SoundGenerator.h:1070
bool isRunning()
Definition SoundGenerator.h:1067
virtual bool begin(AudioInfo info)
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:1050
T readSample() override
Provides a single sample.
Definition SoundGenerator.h:1064
bool is_running
Definition SoundGenerator.h:1072
bool begin() override
Starts the generation of samples.
Definition SoundGenerator.h:1055
T value_return
Definition SoundGenerator.h:1071
void setValue(T value)
Definition SoundGenerator.h:1052
We generate the samples from an array which is provided in the constructor.
Definition SoundGenerator.h:878
void setFrequency(float frequency) override
Defines the output frequency based on sample rate and table size.
Definition SoundGenerator.h:994
void setIncrement(int inc)
Definition SoundGenerator.h:988
int setupSine(int sampleRate, float reqFrequency, float amplitude=1.0)
Definition SoundGenerator.h:1010
bool isRunning()
Definition SoundGenerator.h:1024
bool inactive_at_end
Definition SoundGenerator.h:1030
Vector< T > table
Definition SoundGenerator.h:1033
void end() override
Ends the processing.
Definition SoundGenerator.h:939
float sound_index
Definition SoundGenerator.h:1027
void setArray(T *array, size_t size)
Definition SoundGenerator.h:909
float index_increment
Definition SoundGenerator.h:1034
GeneratorFromArray(T(&array)[arrayLen], int repeat=0, bool setInactiveAtEnd=false, size_t startIndex=0)
Construct a new Generator from an array.
Definition SoundGenerator.h:894
T readSample() override
Provides a single sample.
Definition SoundGenerator.h:942
bool is_running
Definition SoundGenerator.h:1031
int max_repeat
Definition SoundGenerator.h:1028
bool owns_data
Definition SoundGenerator.h:1032
int repeat_counter
Definition SoundGenerator.h:1029
bool begin(AudioInfo info) override
Starts the generation of samples with the provided AudioInfo.
Definition SoundGenerator.h:918
bool begin() override
Starts the generation of samples.
Definition SoundGenerator.h:930
float frequency
Definition SoundGenerator.h:1035
bool begin(AudioInfo info, float frequency)
Starts the generation of samples with the provided AudioInfo and frequency.
Definition SoundGenerator.h:923
void setArray(T(&array)[arrayLen])
Definition SoundGenerator.h:904
An Adapter Class which lets you use any Stream as a Generator.
Definition SoundGenerator.h:814
int channels
Definition SoundGenerator.h:864
GeneratorFromStream()
Definition SoundGenerator.h:816
Stream * p_stream
Definition SoundGenerator.h:863
void setStream(Stream &input)
(Re-)Assigns a stream to the Adapter class
Definition SoundGenerator.h:837
void setChannels(int channels)
Definition SoundGenerator.h:839
float maxValue
Definition SoundGenerator.h:865
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:829
T readSample()
Provides a single sample from the stream.
Definition SoundGenerator.h:842
Generator which combines (mixes) multiple sound generators into one output.
Definition SoundGenerator.h:1224
void add(SoundGenerator< T > *generator)
Definition SoundGenerator.h:1229
Vector< SoundGenerator< T > * > vector
Definition SoundGenerator.h:1247
int actualChannel
Definition SoundGenerator.h:1248
void clear()
Definition SoundGenerator.h:1231
T readSample()
Provides a single sample.
Definition SoundGenerator.h:1233
void add(SoundGenerator< T > &generator)
Definition SoundGenerator.h:1228
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition AudioTypes.h:298
Generates pink noise.
Definition SoundGenerator.h:745
PinkNoiseGenerator(T amplitude=32767)
the amplitude defines the max value which is generated
Definition SoundGenerator.h:748
T key
Definition SoundGenerator.h:777
unsigned int amplitude
Definition SoundGenerator.h:779
T max_key
Definition SoundGenerator.h:776
unsigned int white_values[5]
Definition SoundGenerator.h:778
T readSample()
Provides a single sample.
Definition SoundGenerator.h:756
Implements a typed Ringbuffer.
Definition Buffers.h:358
virtual int readArray(T data[], int len) override
reads multiple values
Definition Buffers.h:407
virtual int writeArray(const T data[], int len) override
Fills the buffer data.
Definition Buffers.h:427
virtual bool resize(size_t len)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:478
bool isEmpty()
Definition Buffers.h:448
Generates a saw tooth wave sound. Uses a 32 bit phase accumulator with an integer increment,...
Definition SoundGenerator.h:625
void updatePhaseIncrement()
Definition SoundGenerator.h:706
void setFrequency(float frequency) override
Defines the frequency - the only place where float math is used.
Definition SoundGenerator.h:669
void setAmplitude(float amp)
Definition SoundGenerator.h:681
uint32_t m_phase_increment
Definition SoundGenerator.h:703
virtual T readSample() override
Provides a single sample - integer only, no float ops!
Definition SoundGenerator.h:687
volatile float m_frequency
Definition SoundGenerator.h:697
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:657
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:663
SawToothGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:627
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:641
bool begin() override
Starts the processing.
Definition SoundGenerator.h:634
uint32_t m_phase_acc
Definition SoundGenerator.h:701
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:649
void setPhase(float phase)
Defines the starting phase in radians.
Definition SoundGenerator.h:676
uint32_t m_phase_offset
Definition SoundGenerator.h:702
int32_t m_amplitude_i
Definition SoundGenerator.h:699
float m_amplitude
Definition SoundGenerator.h:698
Provides a fixed value (e.g. 0) as sound data. This can be used e.g. to test the output functionality...
Definition SoundGenerator.h:792
SilenceGenerator(T value=0)
Definition SoundGenerator.h:795
T value
Definition SoundGenerator.h:803
T readSample()
Provides a single sample.
Definition SoundGenerator.h:798
A sine generator based on a table. The table is created using degrees where one full wave is 360 degr...
Definition SoundGenerator.h:1083
float amplitude
Definition SoundGenerator.h:1146
T interpolate(float angle)
Definition SoundGenerator.h:1187
float base_frequency
Definition SoundGenerator.h:1149
const float values[181]
Definition SoundGenerator.h:1154
T map(T x, T in_min, T in_max, T out_min, T out_max)
Definition SoundGenerator.h:1200
void updateAmplitudeInSteps()
Definition SoundGenerator.h:1204
bool is_first
Definition SoundGenerator.h:1145
float step_new
Definition SoundGenerator.h:1151
bool begin()
Starts the processing.
Definition SoundGenerator.h:1113
bool begin(int channels, int sample_rate, uint16_t frequency=0)
Definition SoundGenerator.h:1129
void setFrequency(float freq)
Abstract method: not implemented! Just provides an error message...
Definition SoundGenerator.h:1135
float step
Definition SoundGenerator.h:1150
float amplitude_to_be
Definition SoundGenerator.h:1147
float angle
Definition SoundGenerator.h:1152
float max_amplitude_step
Definition SoundGenerator.h:1148
void setAmplitude(float amplitude)
Defines the new amplitude (volume)
Definition SoundGenerator.h:1091
void setMaxAmplitudeStep(float step)
Definition SoundGenerator.h:1095
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:1121
T readSample()
Provides a single sample.
Definition SoundGenerator.h:1097
SineFromTable(float amplitude=NumberConverter::maxValueT< T >())
Definition SoundGenerator.h:1085
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:521
SquareWaveGenerator(float amplitude=NumberConverter::maxValueT< T >(), float phase=0.0f)
Definition SoundGenerator.h:523
void updatePhaseIncrement()
Definition SoundGenerator.h:600
void setFrequency(float frequency) override
Defines the frequency - the only place where float math is used.
Definition SoundGenerator.h:565
void setAmplitude(float amp)
Definition SoundGenerator.h:577
uint32_t m_phase_increment
Definition SoundGenerator.h:597
virtual T readSample() override
Provides a single sample - integer only, no float ops!
Definition SoundGenerator.h:583
volatile float m_frequency
Definition SoundGenerator.h:591
bool begin(int channels, int sample_rate, float frequency)
Definition SoundGenerator.h:553
virtual void setAudioInfo(AudioInfo info) override
Defines/updates the AudioInfo.
Definition SoundGenerator.h:559
bool begin(AudioInfo info) override
Starts the processing with the provided AudioInfo.
Definition SoundGenerator.h:537
bool begin() override
Starts the processing.
Definition SoundGenerator.h:530
uint32_t m_phase_acc
Definition SoundGenerator.h:595
bool begin(AudioInfo info, float frequency)
Definition SoundGenerator.h:545
void setPhase(float phase)
Defines the starting phase in radians.
Definition SoundGenerator.h:572
uint32_t m_phase_offset
Definition SoundGenerator.h:596
int32_t m_amplitude_i
Definition SoundGenerator.h:593
float m_amplitude
Definition SoundGenerator.h:592
Generates a test signal which is easy to check because the values are incremented or decremented by 1...
Definition SoundGenerator.h:1260
T inc
Definition SoundGenerator.h:1276
T value
Definition SoundGenerator.h:1275
T max
Definition SoundGenerator.h:1274
T readSample() override
Provides a single sample.
Definition SoundGenerator.h:1264
TestGenerator(T max=1000, T inc=1)
Definition SoundGenerator.h:1262
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:188
virtual float volume()
provides the actual volume in the range of 0.0f to 1.0f
Definition AudioTypes.h:191
virtual bool setVolume(float volume)
define the actual volume in the range of 0.0f to 1.0f
Definition AudioTypes.h:193
Generates a random noise sound with the help of rand() function.
Definition SoundGenerator.h:723
T amplitude
Definition SoundGenerator.h:732
WhiteNoiseGenerator(T amplitude=32767)
the scale defines the max value which is generated
Definition SoundGenerator.h:726
int random(int min, int max)
Definition SoundGenerator.h:734
T readSample()
Provides a single sample.
Definition SoundGenerator.h:729
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
FloatSineGenerator< T > SineGenerator
Definition SoundGenerator.h:499
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:122