3 #include "AudioBasic/Collections.h"
4 #include "AudioTools/AudioLogger.h"
5 #include "AudioTools/AudioTypes.h"
38 virtual bool begin() {
41 activeWarningIssued =
false;
42 info.logInfo(
"SoundGenerator:");
45 ring_buffer.resize(info.
channels *
sizeof(T));
51 virtual void end() { active =
false; }
61 virtual size_t readBytes(uint8_t *buffer,
size_t lengthBytes) {
62 LOGD(
"readBytes: %d", (
int)lengthBytes);
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);
71 return readBytesFromBuffer(buffer, lengthBytes, frame_size, channels);
83 LOGE(
"setFrequency not supported");
93 LOGE(
"invalid bits_per_sample: %d", info.
channels);
99 bool activeWarningIssued =
false;
104 size_t readBytesFrames(uint8_t *buffer,
size_t lengthBytes,
int frames,
106 T *result_buffer = (T *)buffer;
107 for (
int j = 0; j < frames; j++) {
109 for (
int ch = 0; ch < channels; ch++) {
110 *result_buffer++ = sample;
113 return frames *
sizeof(T) * channels;
116 size_t readBytesFromBuffer(uint8_t *buffer,
size_t lengthBytes,
117 int frame_size,
int channels) {
119 if (ring_buffer.isEmpty()) {
120 uint8_t tmp[frame_size];
121 readBytesFrames(tmp, frame_size, 1, channels);
125 return ring_buffer.
readArray(buffer, lengthBytes);
142 float phase = 0.0f) {
143 LOGD(
"SineWaveGenerator");
144 m_amplitude = amplitude;
148 bool begin()
override {
156 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"SineWaveGenerator",
163 bool begin(
AudioInfo info,
float frequency) {
164 LOGI(
"%s::begin(channels=%d, sample_rate=%d, frequency=%.2f)",
168 if (frequency > 0.0f) {
174 bool begin(
int channels,
int sample_rate,
float frequency) {
192 LOGI(
"setFrequency: %.2f", frequency);
194 m_frequency = frequency;
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) {
208 void setAmplitude(
float amp) { m_amplitude = amp; }
211 volatile float m_frequency = 0.0f;
212 float m_cycles = 0.0f;
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;
219 SoundGenerator<T>::info.logStatus();
220 LOGI(
"amplitude: %f", this->m_amplitude);
221 LOGI(
"active: %s", SoundGenerator<T>::active ?
"true" :
"false");
236 LOGD(
"SquareWaveGenerator");
246 T value(T value, T amplitude) {
247 return (value >= 0) ? amplitude : -amplitude;
262 LOGD(
"FastSineGenerator");
280 float p = (t - (int)t) - 0.5f;
282 return (p - 6.283211f * pp * p + 9.132843f * pp * pp * p) * -6.221086f;
304 int random(
int min,
int max) {
return min + rand() % ((max + 1) - min); }
318 this->amplitude = amplitude;
321 for (
int i = 0; i < 5; i++)
322 white_values[i] = rand() % (amplitude / 5);
335 int diff = last_key ^ key;
337 for (
int i = 0; i < 5; i++) {
341 white_values[i] = rand() % (amplitude / 5);
342 sum += white_values[i];
350 unsigned int white_values[5];
351 unsigned int amplitude;
403 setChannels(channels);
409 void setChannels(
int channels) { this->channels = channels; }
415 if (p_stream !=
nullptr) {
416 for (
int j = 0; j < channels; j++) {
417 p_stream->readBytes((uint8_t *)&data,
sizeof(T));
420 float avg = (total / channels) *
volume();
421 if (avg > maxValue) {
423 }
else if (avg < -maxValue) {
433 Stream *p_stream =
nullptr;
462 template <
size_t arrayLen>
464 bool setInactiveAtEnd =
false,
size_t startIndex = 0) {
466 this->max_repeat = repeat;
467 this->inactive_at_end = setInactiveAtEnd;
468 this->sound_index = startIndex;
469 setArray(array, arrayLen);
478 template <
int arrayLen>
void setArray(T (&array)[arrayLen]) {
480 setArray(array, arrayLen);
483 void setArray(T *array,
size_t size) {
484 this->table_length = size;
486 LOGI(
"table_length: %d", (
int)size);
489 virtual bool begin(AudioInfo info)
override {
490 return SoundGenerator<T>::begin(info);
506 if (sound_index >= table_length) {
511 if (max_repeat >= 1 && ++repeat_counter >= max_repeat) {
513 this->is_running =
false;
514 if (inactive_at_end) {
515 this->active =
false;
522 if (this->is_running) {
523 result = table[sound_index];
524 sound_index += index_increment;
531 void setIncrement(
int inc) { index_increment = inc; }
534 int setupSine(
int sampleRate,
float reqFrequency,
float amplitude = 1.0) {
536 static_cast<float>(sampleRate) /
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;
544 table_length = sample_count;
546 return sampleRate / sample_count;
550 bool isRunning() {
return is_running; }
555 int repeat_counter = 0;
556 bool inactive_at_end;
557 bool is_running =
false;
558 bool owns_data =
false;
560 size_t table_length = 0;
561 int index_increment = 1;
577 void setValue(T value) { value_set = value; }
584 value_return = value_set;
592 bool isRunning() {
return is_running; }
597 bool is_running =
false;
610 this->amplitude = amplitude;
611 this->amplitude_to_be = amplitude;
615 void setAmplitude(
float amplitude) { this->amplitude_to_be = amplitude; }
624 if (angle >= 360.0f) {
625 while (angle >= 360.0f) {
631 updateAmplitudeInSteps();
634 return interpolate(angle);
645 bool begin(AudioInfo info,
float frequency) {
646 SoundGenerator<T>::begin(info);
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);
660 step_new = freq / base_frequency;
665 LOGD(
"step: %f", step_new);
669 bool is_first =
true;
671 float amplitude_to_be;
672 float max_amplitude_step = 50.0f;
673 float base_frequency = 1.0f;
675 float step_new = 1.0f;
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,
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);
721 return positive ? result : -result;
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;
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;
733 if (abs(diff) >= 1.0f) {
753 void clear() { vector.clear(); }
758 for (
auto &generator : vector) {
765 return count > 0.0f ? total / count : 0;
770 int actualChannel = 0;
787 if (abs(value) >= max) {