Skip to content

Commit 5292227

Browse files
committed
Optimized SineFromTable
1 parent 0be60f6 commit 5292227

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#include "AudioTools.h"
3+
4+
SineWaveGenerator<int16_t> sine_wave(32000); // subclass of SoundGenerator with max amplitude of 32000
5+
SineFromTable<int16_t> sine_table(32000); // subclass of SoundGenerator with max amplitude of 32000
6+
FastSineGenerator<int16_t> sine_fast(32000);
7+
int sec = 5;
8+
9+
size_t measure(int sec, SoundGenerator<int16_t> *gen){
10+
uint64_t end = millis()+(1000*sec); // 10 seconds
11+
size_t count;
12+
while(millis()<end){
13+
int16_t s = gen->readSample();
14+
count++;
15+
}
16+
return count;
17+
}
18+
19+
const char* resultStr(const char* name, size_t count){
20+
static char msg[160];
21+
sprintf(msg, "%s: %ld", name, count);
22+
return msg;
23+
}
24+
25+
void setup(){
26+
Serial.begin(115200);
27+
Serial.print("Number of call in ");
28+
Serial.print(sec);
29+
Serial.println(" seconds:");
30+
Serial.println();
31+
}
32+
33+
void loop(){
34+
size_t result1 = measure(sec, &sine_wave);
35+
size_t result2 = measure(sec, &sine_table);
36+
size_t result3 = measure(sec, &sine_fast);
37+
38+
Serial.print(resultStr("SineWaveGenerator", result1));
39+
Serial.print(" - ");
40+
Serial.print(resultStr("SineFromTable", result2));
41+
Serial.print(" - ");
42+
Serial.print(resultStr("FastSineGenerator", result3));
43+
Serial.println();
44+
}

src/AudioConfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
// cheange USE_CHECK_MEMORY to 1 to activate memory checks
4646
#define USE_CHECK_MEMORY 0
4747
#if USE_CHECK_MEMORY
48-
# define CHECK_MEMORY()
49-
#else
5048
# define CHECK_MEMORY() checkMemory(true)
49+
#else
50+
# define CHECK_MEMORY()
5151
#endif
5252

5353
// Change USE_INLINE_VARS to 0 if inline variables are not supported

src/AudioEffects/SoundGenerator.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ class SineFromTable : public SoundGenerator<T> {
612612
updateAmplitudeInSteps();
613613
//amplitude = amplitude_to_be;
614614
}
615-
return amplitude * interpolate(angle);
615+
return interpolate(angle);
616616
}
617617

618618
bool begin() {
@@ -656,19 +656,19 @@ class SineFromTable : public SoundGenerator<T> {
656656
//122.5 hz (at 44100); 61 hz (at 22050)
657657
const float values[181] = {0, 0.0174524, 0.0348995, 0.052336, 0.0697565, 0.0871557, 0.104528, 0.121869, 0.139173, 0.156434, 0.173648, 0.190809, 0.207912, 0.224951, 0.241922, 0.258819, 0.275637, 0.292372, 0.309017, 0.325568, 0.34202, 0.358368, 0.374607, 0.390731, 0.406737, 0.422618, 0.438371, 0.45399, 0.469472, 0.48481, 0.5, 0.515038, 0.529919, 0.544639, 0.559193, 0.573576, 0.587785, 0.601815, 0.615661, 0.62932, 0.642788, 0.656059, 0.669131, 0.681998, 0.694658, 0.707107, 0.71934, 0.731354, 0.743145, 0.75471, 0.766044, 0.777146, 0.788011, 0.798636, 0.809017, 0.819152, 0.829038, 0.838671, 0.848048, 0.857167, 0.866025, 0.87462, 0.882948, 0.891007, 0.898794, 0.906308, 0.913545, 0.920505, 0.927184, 0.93358, 0.939693, 0.945519, 0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.97437, 0.978148, 0.981627, 0.984808, 0.987688, 0.990268, 0.992546, 0.994522, 0.996195, 0.997564, 0.99863, 0.999391, 0.999848, 1, 0.999848, 0.999391, 0.99863, 0.997564, 0.996195, 0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627, 0.978148, 0.97437, 0.970296, 0.965926, 0.961262, 0.956305, 0.951057, 0.945519, 0.939693, 0.93358, 0.927184, 0.920505, 0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.87462, 0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152, 0.809017, 0.798636, 0.788011, 0.777146, 0.766044, 0.75471, 0.743145, 0.731354, 0.71934, 0.707107, 0.694658, 0.681998, 0.669131, 0.656059, 0.642788, 0.62932, 0.615661, 0.601815, 0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038, 0.5, 0.48481, 0.469472, 0.45399, 0.438371, 0.422618, 0.406737, 0.390731, 0.374607, 0.358368, 0.34202, 0.325568, 0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951, 0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869, 0.104528, 0.0871557, 0.0697565, 0.052336, 0.0348995, 0.0174524, 0};
658658

659-
float interpolate(float angle){
659+
T interpolate(float angle){
660660
bool positive = (angle<=180);
661661
float angle_positive = positive ? angle : angle - 180.0f;
662662
int angle_int1 = angle_positive;
663663
int angle_int2 = angle_int1+1;
664-
float v1 = values[angle_int1];
665-
float v2 = values[angle_int2];
666-
float result = v1 < v2 ? map(angle_positive,angle_int1,angle_int2,v1, v2) : map(angle_positive,angle_int1,angle_int2,v2, v1) ;
664+
T v1 = values[angle_int1] * amplitude;
665+
T v2 = values[angle_int2] * amplitude;
666+
T result = v1 < v2 ? map(angle_positive,angle_int1,angle_int2,v1, v2) : map(angle_positive,angle_int1,angle_int2,v2, v1) ;
667667
//float result = v1;
668668
return positive ? result : -result;
669669
}
670670

671-
float map(float x, float in_min, float in_max, float out_min, float out_max) {
671+
T map(T x, T in_min, T in_max, T out_min, T out_max) {
672672
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
673673
}
674674

0 commit comments

Comments
 (0)