Skip to content

Commit db42ee1

Browse files
committed
Equalizer3Bands: is_active
1 parent c62967c commit db42ee1

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/AudioTools/CoreAudio/AudioFilter/Equalizer.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ class Equalizer3Bands : public ModifyingStream {
126126
/// @return true if initialization was successful
127127
bool begin(ConfigEqualizer3Bands &config) {
128128
p_cfg = &config;
129+
return begin();
130+
}
131+
132+
bool begin(){
129133
if (p_cfg->channels > max_state_count) {
130134
if (state != nullptr) delete[] state;
131135
state = new EQSTATE[p_cfg->channels];
@@ -143,9 +147,14 @@ class Equalizer3Bands : public ModifyingStream {
143147
state[j].hf = 2 * sin((float)PI * ((float)p_cfg->freq_high /
144148
(float)p_cfg->sample_rate));
145149
}
150+
is_active = true;
146151
return true;
147152
}
148153

154+
void end(){
155+
is_active = false;
156+
}
157+
149158
/// Called automatically when audio format changes
150159
/// @param info New audio format information
151160
virtual void setAudioInfo(AudioInfo info) override {
@@ -188,6 +197,7 @@ class Equalizer3Bands : public ModifyingStream {
188197
}
189198

190199
protected:
200+
bool is_active = false; ///< Indicates if the equalizer is active
191201
ConfigEqualizer3Bands cfg; ///< Default configuration instance
192202
ConfigEqualizer3Bands *p_cfg = &cfg; ///< Pointer to active configuration
193203
const float vsa = (1.0 / 4294967295.0); ///< Very small amount for denormal fix
@@ -222,10 +232,11 @@ class Equalizer3Bands : public ModifyingStream {
222232
/// @param data Pointer to audio data buffer (modified in-place)
223233
/// @param len Length of data buffer in bytes
224234
void filterSamples(const uint8_t *data, size_t len) {
225-
if (state == nullptr){
226-
LOGE("You need to call begin() before using the equalizer");
227-
return;
228-
}
235+
// no filter if not active
236+
if (!is_active) return;
237+
238+
239+
// process samples
229240
switch (p_cfg->bits_per_sample) {
230241
case 16: {
231242
int16_t *p_dataT = (int16_t *)data;
@@ -378,9 +389,14 @@ class Equalizer3BandsPerChannel : public ModifyingStream {
378389
state[j].lf = 2 * sin((float)PI * ((float)freq_low[j] / (float)p_cfg->sample_rate));
379390
state[j].hf = 2 * sin((float)PI * ((float)freq_high[j] / (float)p_cfg->sample_rate));
380391
}
392+
is_active = true;
381393
return true;
382394
}
383395

396+
void end() override {
397+
is_active = false;
398+
}
399+
384400
virtual void setAudioInfo(AudioInfo info) override {
385401
p_cfg->sample_rate = info.sample_rate;
386402
p_cfg->channels = info.channels;
@@ -483,6 +499,7 @@ class Equalizer3BandsPerChannel : public ModifyingStream {
483499
}
484500

485501
protected:
502+
bool is_active = false;
486503
ConfigEqualizer3Bands cfg; ///< Default configuration instance
487504
ConfigEqualizer3Bands *p_cfg = &cfg; ///< Pointer to active configuration
488505
const float vsa = (1.0 / 4294967295.0); ///< Very small amount for denormal fix
@@ -550,10 +567,7 @@ class Equalizer3BandsPerChannel : public ModifyingStream {
550567
/// @param data Pointer to audio data buffer
551568
/// @param len Length of the data buffer in bytes
552569
void filterSamples(const uint8_t *data, size_t len) {
553-
if (state == nullptr){
554-
LOGE("You need to call begin() before using the equalizer");
555-
return;
556-
}
570+
if (!is_active) return;
557571
switch (p_cfg->bits_per_sample) {
558572
case 16: {
559573
int16_t *p_dataT = (int16_t *)data;

0 commit comments

Comments
 (0)