75T eqAssignCoeff(
float value,
const char* name) {
77 float roundtrip = (float)t;
78 if (fabs(roundtrip - value) > fabs(value) * 0.01f + 1e-4f) {
80 "Equalizer coefficient %s=%f does not fit in the range of T (stored "
81 "as %f) - the equalizer will be inaccurate",
82 name, value, roundtrip);
96struct EqDenormalGuard {
97 static T value() {
return T(0); }
100struct EqDenormalGuard<float> {
101 static float value() {
return 1.0f / 4294967295.0f; }
104struct EqDenormalGuard<double> {
105 static double value() {
return 1.0 / 4294967295.0; }
117template <
typename IntT,
typename T>
118struct EqSampleConverter {
119 static T toEqType(IntT sample,
int bits) {
120 return FilterSampleConverter<IntT, T>::toFilterType(sample);
122 static IntT fromEqType(T value,
int bits) {
123 return FilterSampleConverter<IntT, T>::fromFilterType(value);
126template <
typename IntT>
127struct EqSampleConverter<IntT, float> {
128 static float toEqType(IntT sample,
int bits) {
129 return NumberConverter::toFloat((int32_t)sample, bits);
131 static IntT fromEqType(
float value,
int bits) {
132 return (IntT)NumberConverter::fromFloat(value, bits);
171template <
typename T =
float>
240 state[j] = EQSTATE();
243 state[j].lf = eqAssignCoeff<T>(
244 2 * sin((
float)
PI * ((
float)
p_cfg->freq_low /
245 (
float)
p_cfg->sample_rate)),
247 state[j].hf = eqAssignCoeff<T>(
248 2 * sin((
float)
PI * ((
float)
p_cfg->freq_high /
249 (
float)
p_cfg->sample_rate)),
263 p_cfg->bits_per_sample =
info.bits_per_sample;
271 size_t write(
const uint8_t* data,
size_t len)
override {
273 return p_out->write(data, len);
290 if (
p_io !=
nullptr) {
291 result =
p_io->readBytes(data, len);
300 return p_io !=
nullptr ?
p_io->available() : 0;
307 const T
vsa = EqDenormalGuard<T>::value();
343 switch (
p_cfg->bits_per_sample) {
345 int16_t* p_dataT = (int16_t*)data;
346 size_t sample_count = len /
sizeof(int16_t);
347 for (
size_t j = 0; j < sample_count; j +=
p_cfg->channels) {
348 for (
int ch = 0; ch <
p_cfg->channels; ch++) {
349 p_dataT[j + ch] = EqSampleConverter<int16_t, T>::fromEqType(
350 sample(
state[ch], EqSampleConverter<int16_t, T>::toEqType(
351 p_dataT[j + ch], 16)),
358 size_t sample_count = len /
sizeof(
int24_t);
359 for (
size_t j = 0; j < sample_count; j +=
p_cfg->channels) {
360 for (
int ch = 0; ch <
p_cfg->channels; ch++) {
361 p_dataT[j + ch] = EqSampleConverter<int24_t, T>::fromEqType(
362 sample(
state[ch], EqSampleConverter<int24_t, T>::toEqType(
363 p_dataT[j + ch], 24)),
369 int32_t* p_dataT = (int32_t*)data;
370 size_t sample_count = len /
sizeof(int32_t);
371 for (
size_t j = 0; j < sample_count; j +=
p_cfg->channels) {
372 for (
int ch = 0; ch <
p_cfg->channels; ch++) {
373 p_dataT[j + ch] = EqSampleConverter<int32_t, T>::fromEqType(
374 sample(
state[ch], EqSampleConverter<int32_t, T>::toEqType(
375 p_dataT[j + ch], 32)),
382 LOGE(
"Only 16 bits supported: %d",
p_cfg->bits_per_sample);
395 es.f1p0 += (es.lf * (
sample - es.f1p0)) +
vsa;
396 es.f1p1 += (es.lf * (es.f1p0 - es.f1p1));
397 es.f1p2 += (es.lf * (es.f1p1 - es.f1p2));
398 es.f1p3 += (es.lf * (es.f1p2 - es.f1p3));
403 es.f2p0 += (es.hf * (
sample - es.f2p0)) +
vsa;
404 es.f2p1 += (es.hf * (es.f2p0 - es.f2p1));
405 es.f2p2 += (es.hf * (es.f2p1 - es.f2p2));
406 es.f2p3 += (es.hf * (es.f2p2 - es.f2p3));
408 h = es.sdm3 - es.f2p3;
410 m = es.sdm3 - (h + l);
412 l = l *
p_cfg->gain_low;
413 m = m *
p_cfg->gain_medium;
414 h = h *
p_cfg->gain_high;
443template <
typename T =
float>
510 for (
int j = 0; j <
p_cfg->channels; j++) {
511 state[j] = EQSTATE();
514 state[j].lf = eqAssignCoeff<T>(
518 state[j].hf = eqAssignCoeff<T>(
532 p_cfg->bits_per_sample =
info.bits_per_sample;
542 if (channel >= 0 && channel < p_cfg->channels && !
freq_low.empty()) {
547 if (
state !=
nullptr) {
548 state[channel].lf = eqAssignCoeff<T>(
550 ((
float)freq_low_val / (
float)
p_cfg->sample_rate)),
552 state[channel].hf = eqAssignCoeff<T>(
554 ((
float)freq_high_val / (
float)
p_cfg->sample_rate)),
566 float gain_high_val) {
568 if (channel >= 0 && channel < p_cfg->channels && !
gain_low.empty()) {
581 int& freq_high_val) {
582 if (channel >= 0 && channel < p_cfg->channels && !
freq_low.empty()) {
597 float& gain_high_val) {
598 if (channel >= 0 && channel < p_cfg->channels && !
gain_low.empty()) {
611 size_t write(
const uint8_t* data,
size_t len)
override {
613 return p_out->write(data, len);
630 if (
p_io !=
nullptr) {
631 result =
p_io->readBytes(data, len);
640 return p_io !=
nullptr ?
p_io->available() : 0;
647 const T
vsa = EqDenormalGuard<T>::value();
699 for (
int i = 0; i < num_channels; i++) {
713 switch (
p_cfg->bits_per_sample) {
715 int16_t* p_dataT = (int16_t*)data;
716 size_t sample_count = len /
sizeof(int16_t);
717 for (
size_t j = 0; j < sample_count; j +=
p_cfg->channels) {
718 for (
int ch = 0; ch <
p_cfg->channels; ch++) {
719 p_dataT[j + ch] = EqSampleConverter<int16_t, T>::fromEqType(
720 sample(ch, EqSampleConverter<int16_t, T>::toEqType(
721 p_dataT[j + ch], 16)),
728 size_t sample_count = len /
sizeof(
int24_t);
729 for (
size_t j = 0; j < sample_count; j +=
p_cfg->channels) {
730 for (
int ch = 0; ch <
p_cfg->channels; ch++) {
731 p_dataT[j + ch] = EqSampleConverter<int24_t, T>::fromEqType(
732 sample(ch, EqSampleConverter<int24_t, T>::toEqType(
733 p_dataT[j + ch], 24)),
739 int32_t* p_dataT = (int32_t*)data;
740 size_t sample_count = len /
sizeof(int32_t);
741 for (
size_t j = 0; j < sample_count; j +=
p_cfg->channels) {
742 for (
int ch = 0; ch <
p_cfg->channels; ch++) {
743 p_dataT[j + ch] = EqSampleConverter<int32_t, T>::fromEqType(
744 sample(ch, EqSampleConverter<int32_t, T>::toEqType(
745 p_dataT[j + ch], 32)),
752 LOGE(
"Only 16 bits supported: %d",
p_cfg->bits_per_sample);
763 EQSTATE& es =
state[channel];
769 es.f1p0 += (es.lf * (sample_val - es.f1p0)) +
vsa;
770 es.f1p1 += (es.lf * (es.f1p0 - es.f1p1));
771 es.f1p2 += (es.lf * (es.f1p1 - es.f1p2));
772 es.f1p3 += (es.lf * (es.f1p2 - es.f1p3));
777 es.f2p0 += (es.hf * (sample_val - es.f2p0)) +
vsa;
778 es.f2p1 += (es.hf * (es.f2p0 - es.f2p1));
779 es.f2p2 += (es.hf * (es.f2p1 - es.f2p2));
780 es.f2p3 += (es.hf * (es.f2p2 - es.f2p3));
782 h = es.sdm3 - es.f2p3;
785 m = es.sdm3 - (h + l);
795 es.sdm1 = sample_val;
Equalizer3BandsT< float > Equalizer3Bands
Definition Equalizer3Bands.h:429
Equalizer3BandsPerChannelT< float > Equalizer3BandsPerChannel
Definition Equalizer3Bands.h:806