2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* eq.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2017-01-21 22:00:25 +00:00
|
|
|
// Author: reduzio@gmail.com (C) 2006
|
2017-03-05 14:47:28 +00:00
|
|
|
|
2017-01-21 22:00:25 +00:00
|
|
|
#include "eq.h"
|
|
|
|
#include "error_macros.h"
|
2017-01-24 17:56:43 +00:00
|
|
|
#include "math_funcs.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include <math.h>
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define POW2(v) ((v) * (v))
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
/* Helper */
|
2017-03-05 15:44:50 +00:00
|
|
|
static int solve_quadratic(double a, double b, double c, double *r1, double *r2) {
|
|
|
|
//solves quadractic and returns number of roots
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double base = 2 * a;
|
2017-01-24 17:56:43 +00:00
|
|
|
if (base == 0.0f)
|
|
|
|
return 0;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double squared = b * b - 4 * a * c;
|
|
|
|
if (squared < 0.0)
|
2017-01-24 17:56:43 +00:00
|
|
|
return 0;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
squared = sqrt(squared);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
*r1 = (-b + squared) / base;
|
|
|
|
*r2 = (-b - squared) / base;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (*r1 == *r2)
|
2017-01-24 17:56:43 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 2;
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
EQ::BandProcess::BandProcess() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c1 = c2 = c3 = history.a1 = history.a2 = history.a3 = 0;
|
|
|
|
history.b1 = history.b2 = history.b3 = 0;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EQ::recalculate_band_coefficients() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define BAND_LOG(m_f) (log((m_f)) / log(2.))
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < band.size(); i++) {
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
double octave_size;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double frq = band[i].freq;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i == 0) {
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
octave_size = BAND_LOG(band[1].freq) - BAND_LOG(frq);
|
|
|
|
} else if (i == (band.size() - 1)) {
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
octave_size = BAND_LOG(frq) - BAND_LOG(band[i - 1].freq);
|
2017-01-21 22:00:25 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double next = BAND_LOG(band[i + 1].freq) - BAND_LOG(frq);
|
|
|
|
double prev = BAND_LOG(frq) - BAND_LOG(band[i - 1].freq);
|
|
|
|
octave_size = (next + prev) / 2.0;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double frq_l = round(frq / pow(2.0, octave_size / 2.0));
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double side_gain2 = POW2(Math_SQRT12);
|
|
|
|
double th = 2.0 * Math_PI * frq / mix_rate;
|
|
|
|
double th_l = 2.0 * Math_PI * frq_l / mix_rate;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double c2a = side_gain2 * POW2(cos(th)) - 2.0 * side_gain2 * cos(th_l) * cos(th) + side_gain2 - POW2(sin(th_l));
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double c2b = 2.0 * side_gain2 * POW2(cos(th_l)) + side_gain2 * POW2(cos(th)) - 2.0 * side_gain2 * cos(th_l) * cos(th) - side_gain2 + POW2(sin(th_l));
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double c2c = 0.25 * side_gain2 * POW2(cos(th)) - 0.5 * side_gain2 * cos(th_l) * cos(th) + 0.25 * side_gain2 - 0.25 * POW2(sin(th_l));
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
//printf("band %i, precoefs = %f,%f,%f\n",i,c2a,c2b,c2c);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double r1, r2; //roots
|
|
|
|
int roots = solve_quadratic(c2a, c2b, c2c, &r1, &r2);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_CONTINUE(roots == 0);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
band[i].c1 = 2.0 * ((0.5 - r1) / 2.0);
|
|
|
|
band[i].c2 = 2.0 * r1;
|
|
|
|
band[i].c3 = 2.0 * (0.5 + r1) * cos(th);
|
2017-01-21 22:00:25 +00:00
|
|
|
//printf("band %i, coefs = %f,%f,%f\n",i,(float)bands[i].c1,(float)bands[i].c2,(float)bands[i].c3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EQ::set_preset_band_mode(Preset p_preset) {
|
|
|
|
|
|
|
|
band.clear();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define PUSH_BANDS(m_bands) \
|
|
|
|
for (int i = 0; i < m_bands; i++) { \
|
|
|
|
Band b; \
|
|
|
|
b.freq = bands[i]; \
|
|
|
|
band.push_back(b); \
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (p_preset) {
|
|
|
|
|
|
|
|
case PRESET_6_BANDS: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const double bands[] = { 32, 100, 320, 1e3, 3200, 10e3 };
|
2017-01-21 22:00:25 +00:00
|
|
|
PUSH_BANDS(6);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case PRESET_8_BANDS: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const double bands[] = { 32, 72, 192, 512, 1200, 3000, 7500, 16e3 };
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
PUSH_BANDS(8);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case PRESET_10_BANDS: {
|
2017-03-05 15:44:50 +00:00
|
|
|
static const double bands[] = { 31.25, 62.5, 125, 250, 500, 1e3, 2e3, 4e3, 8e3, 16e3 };
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
PUSH_BANDS(10);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case PRESET_21_BANDS: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const double bands[] = { 22, 32, 44, 63, 90, 125, 175, 250, 350, 500, 700, 1e3, 1400, 2e3, 2800, 4e3, 5600, 8e3, 11e3, 16e3, 22e3 };
|
2017-01-21 22:00:25 +00:00
|
|
|
PUSH_BANDS(21);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case PRESET_31_BANDS: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const double bands[] = { 20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1e3, 1250, 1600, 2e3, 2500, 3150, 4e3, 5e3, 6300, 8e3, 10e3, 12500, 16e3, 20e3 };
|
2017-01-21 22:00:25 +00:00
|
|
|
PUSH_BANDS(31);
|
|
|
|
} break;
|
|
|
|
};
|
|
|
|
|
|
|
|
recalculate_band_coefficients();
|
|
|
|
}
|
|
|
|
|
|
|
|
int EQ::get_band_count() const {
|
|
|
|
|
|
|
|
return band.size();
|
|
|
|
}
|
|
|
|
float EQ::get_band_frequency(int p_band) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_band, band.size(), 0);
|
2017-01-21 22:00:25 +00:00
|
|
|
return band[p_band].freq;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void EQ::set_bands(const Vector<float> &p_bands) {
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
band.resize(p_bands.size());
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_bands.size(); i++) {
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
band[i].freq = p_bands[i];
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
recalculate_band_coefficients();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EQ::set_mix_rate(float p_mix_rate) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mix_rate = p_mix_rate;
|
2017-01-21 22:00:25 +00:00
|
|
|
recalculate_band_coefficients();
|
|
|
|
}
|
|
|
|
|
|
|
|
EQ::BandProcess EQ::get_band_processor(int p_band) const {
|
|
|
|
|
|
|
|
EQ::BandProcess band_proc;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_band, band.size(), band_proc);
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
band_proc.c1 = band[p_band].c1;
|
|
|
|
band_proc.c2 = band[p_band].c2;
|
|
|
|
band_proc.c3 = band[p_band].c3;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
|
|
|
return band_proc;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
EQ::EQ() {
|
|
|
|
mix_rate = 44100;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
EQ::~EQ() {
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|