4#include "AudioToolsConfig.h"
5#include "AudioTools/CoreAudio/AudioOutput.h"
6#include "AudioTools/CoreAudio/AudioStreams.h"
38 float gain_medium = 1.0;
39 float gain_high = 1.0;
65 if (state !=
nullptr)
delete[] state;
79 ConfigEqualizer3Bands &defaultConfig() {
return config(); }
81 bool begin(ConfigEqualizer3Bands &config) {
83 if (p_cfg->
channels > max_state_count) {
84 if (state !=
nullptr)
delete[] state;
85 state =
new EQSTATE[p_cfg->
channels];
90 for (
int j = 0; j < max_state_count; j++) {
91 memset(&state[j], 0,
sizeof(EQSTATE));
96 sin((
float)PI * ((
float)p_cfg->freq_low / (
float)p_cfg->
sample_rate));
97 state[j].hf = 2 * sin((
float)PI * ((
float)p_cfg->freq_high /
110 size_t write(
const uint8_t *data,
size_t len)
override {
111 filterSamples(data, len);
112 return p_print->write(data, len);
115 int availableForWrite()
override {
return p_print->availableForWrite(); }
120 if (p_stream !=
nullptr) {
121 result = p_stream->readBytes(data, len);
122 filterSamples(data, len);
127 int available()
override {
128 return p_stream !=
nullptr ? p_stream->available() : 0;
132 ConfigEqualizer3Bands cfg;
133 ConfigEqualizer3Bands *p_cfg = &cfg;
134 const float vsa = (1.0 / 4294967295.0);
135 Print *p_print =
nullptr;
136 Stream *p_stream =
nullptr;
139 int max_state_count = 0;
163 void filterSamples(
const uint8_t *data,
size_t len) {
164 if (state ==
nullptr){
165 LOGE(
"You need to call begin() before using the equalizer");
168 switch (p_cfg->bits_per_sample) {
170 int16_t *p_dataT = (int16_t *)data;
171 size_t sample_count = len /
sizeof(int16_t);
172 for (
size_t j = 0; j < sample_count; j += p_cfg->channels) {
173 for (
int ch = 0; ch < p_cfg->channels; ch++) {
179 int24_t *p_dataT = (int24_t *)data;
180 size_t sample_count = len /
sizeof(int24_t);
181 for (
size_t j = 0; j < sample_count; j += p_cfg->channels) {
182 for (
int ch = 0; ch < p_cfg->channels; ch++) {
188 int32_t *p_dataT = (int32_t *)data;
189 size_t sample_count = len /
sizeof(int32_t);
190 for (
size_t j = 0; j < sample_count; j += p_cfg->channels) {
191 for (
int ch = 0; ch < p_cfg->channels; ch++) {
198 LOGE(
"Only 16 bits supported: %d", p_cfg->bits_per_sample);
205 float sample(EQSTATE &es,
float sample) {
209 es.f1p0 += (es.lf * (sample - es.f1p0)) + vsa;
210 es.f1p1 += (es.lf * (es.f1p0 - es.f1p1));
211 es.f1p2 += (es.lf * (es.f1p1 - es.f1p2));
212 es.f1p3 += (es.lf * (es.f1p2 - es.f1p3));
217 es.f2p0 += (es.hf * (sample - es.f2p0)) + vsa;
218 es.f2p1 += (es.hf * (es.f2p0 - es.f2p1));
219 es.f2p2 += (es.hf * (es.f2p1 - es.f2p2));
220 es.f2p3 += (es.hf * (es.f2p2 - es.f2p3));
222 h = es.sdm3 - es.f2p3;
224 m = es.sdm3 - (h + l);
226 l *= p_cfg->gain_low;
227 m *= p_cfg->gain_medium;
228 h *= p_cfg->gain_high;