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;
367template <
class T =
int16_t>
371 float phase = 0.0f) {
372 LOGD(
"FastIntSineGenerator");
385 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"FastIntSineGenerator",
394 if (frequency > 0.0f) {
400 bool begin(
int channels,
int sample_rate,
float frequency) {
413 LOGI(
"setFrequency: %.2f", frequency);
420 double turns = phase / (2.0 *
PI);
454 if (sample_rate > 0) {
456 (uint32_t)((
double)
m_frequency / sample_rate * 4294967296.0);
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};
504template <
class T =
int16_t>
508 float phase = 0.0f) {
509 LOGD(
"SquareWaveGenerator");
522 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"SquareWaveGenerator",
531 if (frequency > 0.0f) {
537 bool begin(
int channels,
int sample_rate,
float frequency) {
550 LOGI(
"setFrequency: %.2f", frequency);
557 double turns = phase / (2.0 *
PI);
586 if (sample_rate > 0) {
588 (uint32_t)((
double)
m_frequency / sample_rate * 4294967296.0);
608template <
class T =
int16_t>
612 float phase = 0.0f) {
613 LOGD(
"SawToothGenerator");
626 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"SawToothGenerator",
635 if (frequency > 0.0f) {
641 bool begin(
int channels,
int sample_rate,
float frequency) {
654 LOGI(
"setFrequency: %.2f", frequency);
661 double turns = phase / (2.0 *
PI);
676 int32_t ramp = (int32_t)phase;
692 if (sample_rate > 0) {
694 (uint32_t)((
double)
m_frequency / sample_rate * 4294967296.0);
706template <
class T =
int16_t>
718 int random(
int min,
int max) {
return min + rand() % ((max + 1) - min); }
728template <
class T =
int16_t>
748 int diff = last_key ^
key;
750 for (
int i = 0; i < 5; i++) {
775template <
class T =
int16_t>
797template <
class T =
int16_t>
830 for (
int j = 0; j <
channels; j++) {
861template <
class T =
int16_t>
877 template <
size_t arrayLen>
879 bool setInactiveAtEnd =
false,
size_t startIndex = 0) {
887 template <
int arrayLen>
895 for (
int j = 0; j < size; j++) {
898 LOGI(
"table_length: %d", (
int)size);
927 if (
table.size() == 0) {
935 const float table_size =
static_cast<float>(
table.size());
958 if (idx1 >=
static_cast<int>(
table.size())) {
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);
980 LOGE(
"setFrequency failed: sample_rate=%d table_size=%d",
994 int setupSine(
int sampleRate,
float reqFrequency,
float amplitude = 1.0) {
996 static_cast<float>(sampleRate) /
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;
1004 return sampleRate / sample_count;
1029template <
class T =
int16_t>
1066template <
class T =
int16_t>
1084 if (
angle >= 360.0f) {
1085 while (
angle >= 360.0f) {
1113 bool begin(
int channels,
int sample_rate, uint16_t frequency = 0) {
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,
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;
1178 T result = v1 < v2 ?
map(angle_positive, angle_int1, angle_int2, v1, v2)
1179 :
map(angle_positive, angle_int1, angle_int2, v2, v1);
1181 return positive ? result : -result;
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;
1193 if (abs(diff) >= 1.0f) {
1207template <
class T =
int16_t>
1220 for (
auto& generator :
vector) {
1221 if (generator->isActive()) {
1222 T sample = generator->readSample();
1227 return count > 0.0f ? total / count : 0;
1243template <
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