649 this->amplitude = amplitude;
650 this->amplitude_to_be = amplitude;
654 void setAmplitude(
float amplitude) { this->amplitude_to_be = amplitude; }
663 if (angle >= 360.0f) {
664 while (angle >= 360.0f) {
670 updateAmplitudeInSteps();
673 return interpolate(angle);
684 bool begin(AudioInfo info,
float frequency) {
685 SoundGenerator<T>::begin(info);
692 bool begin(
int channels,
int sample_rate, uint16_t frequency = 0) {
693 SoundGenerator<T>::info.channels = channels;
694 SoundGenerator<T>::info.sample_rate = sample_rate;
695 return begin(SoundGenerator<T>::info, frequency);
699 step_new = freq / base_frequency;
704 LOGD(
"step: %f", step_new);
708 bool is_first =
true;
710 float amplitude_to_be;
711 float max_amplitude_step = 50.0f;
712 float base_frequency = 1.0f;
714 float step_new = 1.0f;
717 const float values[181] = {
718 0, 0.0174524, 0.0348995, 0.052336, 0.0697565, 0.0871557,
719 0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809,
720 0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372,
721 0.309017, 0.325568, 0.34202, 0.358368, 0.374607, 0.390731,
722 0.406737, 0.422618, 0.438371, 0.45399, 0.469472, 0.48481,
723 0.5, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576,
724 0.587785, 0.601815, 0.615661, 0.62932, 0.642788, 0.656059,
725 0.669131, 0.681998, 0.694658, 0.707107, 0.71934, 0.731354,
726 0.743145, 0.75471, 0.766044, 0.777146, 0.788011, 0.798636,
727 0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167,
728 0.866025, 0.87462, 0.882948, 0.891007, 0.898794, 0.906308,
729 0.913545, 0.920505, 0.927184, 0.93358, 0.939693, 0.945519,
730 0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.97437,
731 0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546,
732 0.994522, 0.996195, 0.997564, 0.99863, 0.999391, 0.999848,
733 1, 0.999848, 0.999391, 0.99863, 0.997564, 0.996195,
734 0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
735 0.978148, 0.97437, 0.970296, 0.965926, 0.961262, 0.956305,
736 0.951057, 0.945519, 0.939693, 0.93358, 0.927184, 0.920505,
737 0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.87462,
738 0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
739 0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.75471,
740 0.743145, 0.731354, 0.71934, 0.707107, 0.694658, 0.681998,
741 0.669131, 0.656059, 0.642788, 0.62932, 0.615661, 0.601815,
742 0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
743 0.5, 0.48481, 0.469472, 0.45399, 0.438371, 0.422618,
744 0.406737, 0.390731, 0.374607, 0.358368, 0.34202, 0.325568,
745 0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
746 0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
747 0.104528, 0.0871557, 0.0697565, 0.052336, 0.0348995, 0.0174524,
750 T interpolate(
float angle) {
751 bool positive = (angle <= 180.0f);
752 float angle_positive = positive ? angle : angle - 180.0f;
753 int angle_int1 = angle_positive;
754 int angle_int2 = angle_int1 + 1;
755 T v1 = values[angle_int1] * amplitude;
756 T v2 = values[angle_int2] * amplitude;
757 T result = v1 < v2 ? map(angle_positive, angle_int1, angle_int2, v1, v2)
758 : map(angle_positive, angle_int1, angle_int2, v2, v1);
760 return positive ? result : -result;
763 T map(T x, T in_min, T in_max, T out_min, T out_max) {
764 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
767 void updateAmplitudeInSteps() {
768 float diff = amplitude_to_be - amplitude;
769 if (abs(diff) > max_amplitude_step) {
770 diff = (diff < 0) ? -max_amplitude_step : max_amplitude_step;
772 if (abs(diff) >= 1.0f) {