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];
224template <
class T =
int16_t>
229 float phase = 0.0f) {
230 LOGD(
"FloatSineGenerator");
243 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"FloatSineGenerator",
251 LOGI(
"%s::begin(channels=%d, sample_rate=%d, frequency=%.2f)",
256 if (frequency > 0.0f) {
262 bool begin(
int channels,
int sample_rate,
float frequency) {
280 LOGI(
"setFrequency: %.2f", frequency);
312 LOGI(
"amplitude: %f", this->m_amplitude);
324template <
class T =
int16_t>
329 LOGD(
"FastSineGenerator");
347 float p = (t - (int)t) - 0.5f;
349 return (p - 6.283211f * pp * p + 9.132843f * pp * pp * p) * -6.221086f;
369template <
class T =
int16_t>
373 float phase = 0.0f) {
374 LOGD(
"FastIntSineGenerator");
387 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"FastIntSineGenerator",
396 if (frequency > 0.0f) {
402 bool begin(
int channels,
int sample_rate,
float frequency) {
415 LOGI(
"setFrequency: %.2f", frequency);
422 double turns = phase / (2.0 *
PI);
456 if (sample_rate > 0) {
458 (uint32_t)((
double)
m_frequency / sample_rate * 4294967296.0);
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};
495template <
class T =
int16_t>
498template <
class T =
int16_t>
503template <
class T =
int16_t>
520template <
class T =
int16_t>
524 float phase = 0.0f) {
525 LOGD(
"SquareWaveGenerator");
538 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"SquareWaveGenerator",
547 if (frequency > 0.0f) {
553 bool begin(
int channels,
int sample_rate,
float frequency) {
566 LOGI(
"setFrequency: %.2f", frequency);
573 double turns = phase / (2.0 *
PI);
602 if (sample_rate > 0) {
604 (uint32_t)((
double)
m_frequency / sample_rate * 4294967296.0);
624template <
class T =
int16_t>
628 float phase = 0.0f) {
629 LOGD(
"SawToothGenerator");
642 LOGI(
"%s::begin(channels=%d, sample_rate=%d)",
"SawToothGenerator",
651 if (frequency > 0.0f) {
657 bool begin(
int channels,
int sample_rate,
float frequency) {
670 LOGI(
"setFrequency: %.2f", frequency);
677 double turns = phase / (2.0 *
PI);
692 int32_t ramp = (int32_t)phase;
708 if (sample_rate > 0) {
710 (uint32_t)((
double)
m_frequency / sample_rate * 4294967296.0);
722template <
class T =
int16_t>
734 int random(
int min,
int max) {
return min + rand() % ((max + 1) - min); }
744template <
class T =
int16_t>
764 int diff = last_key ^
key;
766 for (
int i = 0; i < 5; i++) {
791template <
class T =
int16_t>
813template <
class T =
int16_t>
846 for (
int j = 0; j <
channels; j++) {
877template <
class T =
int16_t>
893 template <
size_t arrayLen>
895 bool setInactiveAtEnd =
false,
size_t startIndex = 0) {
903 template <
int arrayLen>
911 for (
int j = 0; j < size; j++) {
914 LOGI(
"table_length: %d", (
int)size);
943 if (
table.size() == 0) {
951 const float table_size =
static_cast<float>(
table.size());
974 if (idx1 >=
static_cast<int>(
table.size())) {
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);
996 LOGE(
"setFrequency failed: sample_rate=%d table_size=%d",
1010 int setupSine(
int sampleRate,
float reqFrequency,
float amplitude = 1.0) {
1012 static_cast<float>(sampleRate) /
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;
1020 return sampleRate / sample_count;
1045template <
class T =
int16_t>
1082template <
class T =
int16_t>
1100 if (
angle >= 360.0f) {
1101 while (
angle >= 360.0f) {
1129 bool begin(
int channels,
int sample_rate, uint16_t frequency = 0) {
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,
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;
1194 T result = v1 < v2 ?
map(angle_positive, angle_int1, angle_int2, v1, v2)
1195 :
map(angle_positive, angle_int1, angle_int2, v2, v1);
1197 return positive ? result : -result;
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;
1209 if (abs(diff) >= 1.0f) {
1223template <
class T =
int16_t>
1236 for (
auto& generator :
vector) {
1237 if (generator->isActive()) {
1238 T sample = generator->readSample();
1243 return count > 0.0f ? total / count : 0;
1259template <
class T =
int16_t>
#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
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140