Skip to content

Commit f166ee0

Browse files
committed
SineFromTable
1 parent 0cbabbb commit f166ee0

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/AudioEffects/SoundGenerator.h

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ class SoundGenerator {
2626
end();
2727
}
2828

29-
virtual void begin(AudioBaseInfo info) {
29+
virtual bool begin(AudioBaseInfo info) {
3030
this->info = info;
31-
begin();
31+
return begin();
3232
}
3333

34-
virtual void begin() {
34+
virtual bool begin() {
3535
LOGD(LOG_METHOD);
3636
active = true;
3737
activeWarningIssued = false;
3838
//info.bits_per_sample = sizeof(T)*8;
39+
return true;
3940
}
4041

4142
/// ends the processing
@@ -158,31 +159,34 @@ class SineWaveGenerator : public SoundGenerator<T>{
158159
m_phase = phase;
159160
}
160161

161-
void begin() override {
162+
bool begin() override {
162163
LOGI(LOG_METHOD);
163164
SoundGenerator<T>::begin();
164165
this->m_deltaTime = 1.0 / SoundGenerator<T>::info.sample_rate;
166+
return true;
165167
}
166168

167-
void begin(AudioBaseInfo info) override {
169+
bool begin(AudioBaseInfo info) override {
168170
LOGI("%s::begin(channels=%d, sample_rate=%d)","SineWaveGenerator", info.channels, info.sample_rate);
169171
SoundGenerator<T>::begin(info);
170172
this->m_deltaTime = 1.0 / SoundGenerator<T>::info.sample_rate;
173+
return true;
171174
}
172175

173-
void begin(AudioBaseInfo info, uint16_t frequency){
176+
bool begin(AudioBaseInfo info, uint16_t frequency){
174177
LOGI("%s::begin(channels=%d, sample_rate=%d, frequency=%d)","SineWaveGenerator",info.channels, info.sample_rate,frequency);
175178
SoundGenerator<T>::begin(info);
176179
this->m_deltaTime = 1.0 / SoundGenerator<T>::info.sample_rate;
177180
if (frequency>0){
178181
setFrequency(frequency);
179182
}
183+
return true;
180184
}
181185

182-
void begin(int channels, int sample_rate, uint16_t frequency=0){
186+
bool begin(int channels, int sample_rate, uint16_t frequency=0){
183187
SoundGenerator<T>::info.channels = channels;
184188
SoundGenerator<T>::info.sample_rate = sample_rate;
185-
begin(SoundGenerator<T>::info, frequency);
189+
return begin(SoundGenerator<T>::info, frequency);
186190
}
187191

188192
// update m_deltaTime
@@ -416,17 +420,18 @@ class GeneratorFromArray : public SoundGenerator<T> {
416420
LOGI("table_length: %d", (int)size);
417421
}
418422

419-
virtual void begin(AudioBaseInfo info) {
420-
SoundGenerator<T>::begin(info);
423+
virtual bool begin(AudioBaseInfo info) {
424+
return SoundGenerator<T>::begin(info);
421425
}
422426

423427
/// Starts the generation of samples
424-
void begin() override {
428+
bool begin() override {
425429
LOGI(LOG_METHOD);
426430
SoundGenerator<T>::begin();
427431
sound_index = 0;
428432
repeat_counter = 0;
429433
is_running = true;
434+
return true;
430435
}
431436

432437
/// Provides a single sample
@@ -497,13 +502,14 @@ class SineFromTable : public SoundGenerator<T> {
497502

498503
bool begin() {
499504
is_first = true;
500-
GeneratorFromArray<float>::begin();
505+
SoundGenerator<T>::begin();
501506
base_frequency = SoundGenerator<T>::audioInfo().sample_rate / 360.0; //122.5 hz (at 44100); 61 hz (at 22050)
502507
return true;
503508
}
504509

505510
bool begin(AudioBaseInfo info, float frequency) {
506511
SoundGenerator<T>::begin(info);
512+
base_frequency = SoundGenerator<T>::audioInfo().sample_rate / 360.0; //122.5 hz (at 44100); 61 hz (at 22050)
507513
setFrequency(frequency);
508514
return true;
509515
}
@@ -520,23 +526,28 @@ class SineFromTable : public SoundGenerator<T> {
520526
protected:
521527
bool is_first = true;
522528
float amplitude;
523-
float base_frequency;
529+
float base_frequency = 1.0;
524530
float step = 1.0;
525531
float step_new = 1.0;
526532
float angle = 0;
533+
//122.5 hz (at 44100); 61 hz (at 22050)
527534
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};
528535

529536
float interpolate(float angle){
530537
bool positive = (angle<=180);
531-
float angle_positive = positive ? angle : angle - 180;
538+
float angle_positive = positive ? angle : angle - 180.0;
532539
int angle_int1 = angle_positive;
533540
int angle_int2 = angle_int1+1;
534541
float v1 = values[angle_int1];
535542
float v2 = values[angle_int2];
536-
float result = map(angle,angle_int1,angle_int2,v1, v2);
543+
float result = v1 < v2 ? map(angle_positive,angle_int1,angle_int2,v1, v2) : map(angle_positive,angle_int1,angle_int2,v2, v1) ;
544+
//float result = v1;
537545
return positive ? result : -result;
538546
}
539547

548+
float map(float x, float in_min, float in_max, float out_min, float out_max) {
549+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
550+
}
540551
};
541552

542553
/**
@@ -583,6 +594,4 @@ class GeneratorMixer : public SoundGenerator<T> {
583594

584595
};
585596

586-
587-
588597
}

0 commit comments

Comments
 (0)