27template <
class T =
int16_t>
63 virtual size_t readBytes(uint8_t* data,
size_t len) {
64 LOGD(
"readBytes: %d", (
int)len);
67 int frame_size =
sizeof(T) * channels;
68 int frames = len / frame_size;
69 if (len >= frame_size) {
84 LOGE(
"setFrequency not supported");
102 LOGI(
"setPlayTime: playMs=%d, upPercent=%d, downPercent=%d",
playMs,
148 T* result_buffer = (T*)buffer;
149 int frames_written = 0;
154 for (
int j = 0; j < frames; j++) {
163 for (
int ch = 0; ch < channels; ch++) {
164 *result_buffer++ = sample;
173 return frames_written *
sizeof(T) * channels;
196 return (T)(
factor * sample);
200 int frame_size,
int channels) {
203 uint8_t tmp[frame_size];
221template <
class T =
int16_t>
226 float phase = 0.0f) {
227 LOGD(
"SineGenerator");
240 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"SineGenerator",
248 LOGI(
"%s::begin(channels=%d, sample_rate=%d, frequency=%.2f)",
253 if (frequency > 0.0f) {
259 bool begin(
int channels,
int sample_rate,
float frequency) {
277 LOGI(
"setFrequency: %.2f", frequency);
309 LOGI(
"amplitude: %f", this->m_amplitude);
315template <
class T =
int16_t>
325template <
class T =
int16_t>
330 LOGD(
"FastSineGenerator");
348 float p = (t - (int)t) - 0.5f;
350 return (p - 6.283211f * pp * p + 9.132843f * pp * pp * p) * -6.221086f;
361template <
class T =
int16_t>
366 LOGD(
"SquareWaveGenerator");
377 return (
value >= 0) ? amplitude : -amplitude;
388template <
class T =
int16_t>
393 LOGD(
"SawToothGenerator");
410 inline float saw(
float t) {
411 float p = (t - (int)t) - 0.5f;
423template <
class T =
int16_t>
435 int random(
int min,
int max) {
return min + rand() % ((max + 1) - min); }
445template <
class T =
int16_t>
465 int diff = last_key ^
key;
467 for (
int i = 0; i < 5; i++) {
492template <
class T =
int16_t>
514template <
class T =
int16_t>
547 for (
int j = 0; j <
channels; j++) {
578template <
class T =
int16_t>
594 template <
size_t arrayLen>
596 bool setInactiveAtEnd =
false,
size_t startIndex = 0) {
604 template <
int arrayLen>
612 for (
int j = 0; j < size; j++) {
615 LOGI(
"table_length: %d", (
int)size);
644 if (
table.size() == 0) {
652 const float table_size =
static_cast<float>(
table.size());
675 if (idx1 >=
static_cast<int>(
table.size())) {
678 float frac =
sound_index -
static_cast<float>(idx0);
679 float sample =
static_cast<float>(
table[idx0]) * (1.0f - frac) +
680 static_cast<float>(
table[idx1]) * frac;
681 result =
static_cast<T
>(sample);
697 LOGE(
"setFrequency failed: sample_rate=%d table_size=%d",
711 int setupSine(
int sampleRate,
float reqFrequency,
float amplitude = 1.0) {
713 static_cast<float>(sampleRate) /
715 float angle = 2.0 *
PI / sample_count;
716 table.resize(sample_count);
717 for (
int j = 0; j < sample_count; j++) {
718 table[j] = sinf(j * angle) * amplitude;
721 return sampleRate / sample_count;
746template <
class T =
int16_t>
783template <
class T =
int16_t>
801 if (
angle >= 360.0f) {
802 while (
angle >= 360.0f) {
830 bool begin(
int channels,
int sample_rate, uint16_t frequency = 0) {
856 0, 0.0174524, 0.0348995, 0.052336, 0.0697565, 0.0871557,
857 0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809,
858 0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372,
859 0.309017, 0.325568, 0.34202, 0.358368, 0.374607, 0.390731,
860 0.406737, 0.422618, 0.438371, 0.45399, 0.469472, 0.48481,
861 0.5, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576,
862 0.587785, 0.601815, 0.615661, 0.62932, 0.642788, 0.656059,
863 0.669131, 0.681998, 0.694658, 0.707107, 0.71934, 0.731354,
864 0.743145, 0.75471, 0.766044, 0.777146, 0.788011, 0.798636,
865 0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167,
866 0.866025, 0.87462, 0.882948, 0.891007, 0.898794, 0.906308,
867 0.913545, 0.920505, 0.927184, 0.93358, 0.939693, 0.945519,
868 0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.97437,
869 0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546,
870 0.994522, 0.996195, 0.997564, 0.99863, 0.999391, 0.999848,
871 1, 0.999848, 0.999391, 0.99863, 0.997564, 0.996195,
872 0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627,
873 0.978148, 0.97437, 0.970296, 0.965926, 0.961262, 0.956305,
874 0.951057, 0.945519, 0.939693, 0.93358, 0.927184, 0.920505,
875 0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.87462,
876 0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152,
877 0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.75471,
878 0.743145, 0.731354, 0.71934, 0.707107, 0.694658, 0.681998,
879 0.669131, 0.656059, 0.642788, 0.62932, 0.615661, 0.601815,
880 0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038,
881 0.5, 0.48481, 0.469472, 0.45399, 0.438371, 0.422618,
882 0.406737, 0.390731, 0.374607, 0.358368, 0.34202, 0.325568,
883 0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
884 0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869,
885 0.104528, 0.0871557, 0.0697565, 0.052336, 0.0348995, 0.0174524,
889 bool positive = (
angle <= 180.0f);
890 float angle_positive = positive ?
angle :
angle - 180.0f;
891 int angle_int1 = angle_positive;
892 int angle_int2 = angle_int1 + 1;
895 T result = v1 < v2 ?
map(angle_positive, angle_int1, angle_int2, v1, v2)
896 :
map(angle_positive, angle_int1, angle_int2, v2, v1);
898 return positive ? result : -result;
901 T
map(T x, T in_min, T in_max, T out_min, T out_max) {
902 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
910 if (abs(diff) >= 1.0f) {
924template <
class T =
int16_t>
937 for (
auto& generator :
vector) {
938 if (generator->isActive()) {
939 T sample = generator->readSample();
944 return count > 0.0f ? total / count : 0;
960template <
class T =
int16_t>
#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
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140