Core: Replace C math headers with C++ equivalents

- Minor restructuring to ensure `math_funcs.h` is the central point for math functions
This commit is contained in:
Thaddeus Crews 2025-03-19 14:18:09 -05:00
parent c5c1cd4440
commit ad40939b6f
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
101 changed files with 414 additions and 498 deletions

View file

@ -72,7 +72,7 @@ void AudioEffectChorusInstance::_process_chunk(const AudioFrame *p_src_frames, A
float max_depth_frames = (v.depth / 1000.0) * mix_rate;
uint64_t local_cycles = cycles[vc];
uint64_t increment = llrint(cycles_to_mix / (double)p_frame_count * (double)(1 << AudioEffectChorus::CYCLES_FRAC));
uint64_t increment = std::rint(cycles_to_mix / (double)p_frame_count * (double)(1 << AudioEffectChorus::CYCLES_FRAC));
//check the LFO doesn't read ahead of the write pos
if ((((unsigned int)max_depth_frames) + 10) > delay_frames) { //10 as some threshold to avoid precision stuff
@ -84,7 +84,7 @@ void AudioEffectChorusInstance::_process_chunk(const AudioFrame *p_src_frames, A
if (v.cutoff == 0) {
continue;
}
float auxlp = expf(-Math::TAU * v.cutoff / mix_rate);
float auxlp = std::exp(-Math::TAU * v.cutoff / mix_rate);
float c1 = 1.0 - auxlp;
float c2 = auxlp;
AudioFrame h = filter_h[vc];
@ -104,9 +104,9 @@ void AudioEffectChorusInstance::_process_chunk(const AudioFrame *p_src_frames, A
float phase = (float)(local_cycles & AudioEffectChorus::CYCLES_MASK) / (float)(1 << AudioEffectChorus::CYCLES_FRAC);
float wave_delay = sinf(phase * Math::TAU) * max_depth_frames;
float wave_delay = std::sin(phase * Math::TAU) * max_depth_frames;
int wave_delay_frames = lrint(floor(wave_delay));
int wave_delay_frames = std::rint(std::floor(wave_delay));
float wave_delay_frac = wave_delay - (float)wave_delay_frames;
/** COMPUTE RINGBUFFER POS**/

View file

@ -35,17 +35,17 @@ void AudioEffectCompressorInstance::process(const AudioFrame *p_src_frames, Audi
float threshold = Math::db_to_linear(base->threshold);
float sample_rate = AudioServer::get_singleton()->get_mix_rate();
float ratatcoef = exp(-1 / (0.00001f * sample_rate));
float ratrelcoef = exp(-1 / (0.5f * sample_rate));
float ratatcoef = std::exp(-1 / (0.00001f * sample_rate));
float ratrelcoef = std::exp(-1 / (0.5f * sample_rate));
float attime = base->attack_us / 1000000.0;
float reltime = base->release_ms / 1000.0;
float atcoef = exp(-1 / (attime * sample_rate));
float relcoef = exp(-1 / (reltime * sample_rate));
float atcoef = std::exp(-1 / (attime * sample_rate));
float relcoef = std::exp(-1 / (reltime * sample_rate));
float makeup = Math::db_to_linear(base->gain);
float mix = base->mix;
float gr_meter_decay = exp(1 / (1 * sample_rate));
float gr_meter_decay = std::exp(1 / (1 * sample_rate));
const AudioFrame *src = p_src_frames;

View file

@ -73,7 +73,7 @@ void AudioEffectDelayInstance::_process_chunk(const AudioFrame *p_src_frames, Au
tap2_vol.right *= CLAMP(1.0 + base->tap_2_pan, 0, 1);
// feedback lowpass here
float lpf_c = expf(-Math::TAU * base->feedback_lowpass / mix_rate); // 0 .. 10khz
float lpf_c = std::exp(-Math::TAU * base->feedback_lowpass / mix_rate); // 0 .. 10khz
float lpf_ic = 1.0 - lpf_c;
const AudioFrame *src = p_src_frames;

View file

@ -36,18 +36,18 @@ void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames, Audi
const float *src = (const float *)p_src_frames;
float *dst = (float *)p_dst_frames;
//float lpf_c=expf(-Math::TAU*keep_hf_hz.get()/(mix_rate*(float)OVERSAMPLE));
float lpf_c = expf(-Math::TAU * base->keep_hf_hz / (AudioServer::get_singleton()->get_mix_rate()));
//float lpf_c=std::exp(-Math::TAU*keep_hf_hz.get()/(mix_rate*(float)OVERSAMPLE));
float lpf_c = std::exp(-Math::TAU * base->keep_hf_hz / (AudioServer::get_singleton()->get_mix_rate()));
float lpf_ic = 1.0 - lpf_c;
float drive_f = base->drive;
float pregain_f = Math::db_to_linear(base->pre_gain);
float postgain_f = Math::db_to_linear(base->post_gain);
float atan_mult = pow(10, drive_f * drive_f * 3.0) - 1.0 + 0.001;
float atan_div = 1.0 / (atanf(atan_mult) * (1.0 + drive_f * 8));
float atan_mult = std::pow(10, drive_f * drive_f * 3.0) - 1.0 + 0.001;
float atan_div = 1.0 / (std::atan(atan_mult) * (1.0 + drive_f * 8));
float lofi_mult = powf(2.0, 2.0 + (1.0 - drive_f) * 14); //goes from 16 to 2 bits
float lofi_mult = std::pow(2.0, 2.0 + (1.0 - drive_f) * 14); //goes from 16 to 2 bits
for (int i = 0; i < p_frame_count * 2; i++) {
float out = undenormalize(src[i] * lpf_ic + lpf_c * h[i & 1]);
@ -59,7 +59,7 @@ void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames, Audi
switch (base->mode) {
case AudioEffectDistortion::MODE_CLIP: {
float a_sign = a < 0 ? -1.0f : 1.0f;
a = powf(abs(a), 1.0001 - drive_f) * a_sign;
a = std::pow(std::abs(a), 1.0001 - drive_f) * a_sign;
if (a > 1.0) {
a = 1.0;
} else if (a < (-1.0)) {
@ -68,23 +68,23 @@ void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames, Audi
} break;
case AudioEffectDistortion::MODE_ATAN: {
a = atanf(a * atan_mult) * atan_div;
a = std::atan(a * atan_mult) * atan_div;
} break;
case AudioEffectDistortion::MODE_LOFI: {
a = floorf(a * lofi_mult + 0.5) / lofi_mult;
a = std::floor(a * lofi_mult + 0.5) / lofi_mult;
} break;
case AudioEffectDistortion::MODE_OVERDRIVE: {
const double x = a * 0.686306;
const double z = 1 + exp(sqrt(fabs(x)) * -0.75);
a = (expf(x) - expf(-x * z)) / (expf(x) + expf(-x));
const double z = 1 + std::exp(std::sqrt(std::abs(x)) * -0.75);
a = (std::exp(x) - std::exp(-x * z)) / (std::exp(x) + std::exp(-x));
} break;
case AudioEffectDistortion::MODE_WAVESHAPE: {
float x = a;
float k = 2 * drive_f / (1.00001 - drive_f);
a = (1.0 + k) * x / (1.0 + k * fabsf(x));
a = (1.0 + k) * x / (1.0 + k * std::abs(x));
} break;
}

View file

@ -46,7 +46,7 @@ void AudioEffectPhaserInstance::process(const AudioFrame *p_src_frames, AudioFra
phase -= Math::TAU;
}
float d = dmin + (dmax - dmin) * ((sin(phase) + 1.f) / 2.f);
float d = dmin + (dmax - dmin) * ((std::sin(phase) + 1.f) / 2.f);
//update filter coeffs
for (int j = 0; j < 6; j++) {

View file

@ -112,7 +112,7 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff
/* do windowing and re,im interleave */
for (k = 0; k < fftFrameSize;k++) {
window = -.5*cos(2.*Math::PI*(double)k/(double)fftFrameSize)+.5;
window = -.5*std::cos(2.*Math::PI*(double)k/(double)fftFrameSize)+.5;
gFFTworksp[2*k] = gInFIFO[k] * window;
gFFTworksp[2*k+1] = 0.;
}
@ -129,8 +129,8 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff
imag = gFFTworksp[2*k+1];
/* compute magnitude and phase */
magn = 2.*sqrt(real*real + imag*imag);
phase = atan2(imag,real);
magn = 2.*std::sqrt(real*real + imag*imag);
phase = std::atan2(imag,real);
/* compute phase difference */
tmp = phase - gLastPhase[k];
@ -194,8 +194,8 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff
phase = gSumPhase[k];
/* get real and imag part and re-interleave */
gFFTworksp[2*k] = magn*cos(phase);
gFFTworksp[2*k+1] = magn*sin(phase);
gFFTworksp[2*k] = magn*std::cos(phase);
gFFTworksp[2*k+1] = magn*std::sin(phase);
}
/* zero negative frequencies */
@ -207,7 +207,7 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff
/* do windowing and add to output accumulator */
for(k=0; k < fftFrameSize; k++) {
window = -.5*cos(2.*Math::PI*(double)k/(double)fftFrameSize)+.5;
window = -.5*std::cos(2.*Math::PI*(double)k/(double)fftFrameSize)+.5;
gOutputAccum[k] += 2.*window*gFFTworksp[2*k]/(fftFrameSize2*osamp);
}
for (k = 0; k < stepSize; k++) { gOutFIFO[k] = gOutputAccum[k];
@ -255,14 +255,14 @@ void SMBPitchShift::smbFft(float *fftBuffer, long fftFrameSize, long sign)
*p1 = *p2; *p2 = temp;
}
}
for (k = 0, le = 2; k < (long)(log((double)fftFrameSize)/log(2.)+.5); k++) {
for (k = 0, le = 2; k < (long)(std::log((double)fftFrameSize)/std::log(2.)+.5); k++) {
le <<= 1;
le2 = le>>1;
ur = 1.0;
ui = 0.0;
arg = Math::PI / (le2>>1);
wr = cos(arg);
wi = sign*sin(arg);
wr = std::cos(arg);
wi = sign*std::sin(arg);
for (j = 0; j < le2; j += 2) {
p1r = fftBuffer+j; p1i = p1r+1;
p2r = p1r+le2; p2i = p2r+1;

View file

@ -66,14 +66,14 @@ static void smbFft(float *fftBuffer, long fftFrameSize, long sign)
*p2 = temp;
}
}
for (k = 0, le = 2; k < (long)(log((double)fftFrameSize) / log(2.) + .5); k++) {
for (k = 0, le = 2; k < (long)(std::log((double)fftFrameSize) / std::log(2.) + .5); k++) {
le <<= 1;
le2 = le >> 1;
ur = 1.0;
ui = 0.0;
arg = Math::PI / (le2 >> 1);
wr = cos(arg);
wi = sign * sin(arg);
wr = std::cos(arg);
wi = sign * std::sin(arg);
for (j = 0; j < le2; j += 2) {
p1r = fftBuffer + j;
p1i = p1r + 1;

View file

@ -31,9 +31,7 @@
#include "eq_filter.h"
#include "core/error/error_macros.h"
#include "core/math/math_defs.h"
#include <math.h>
#include "core/math/math_funcs.h"
#define POW2(v) ((v) * (v))
@ -51,7 +49,7 @@ static int solve_quadratic(double a, double b, double c, double *r1, double *r2)
return 0;
}
squared = sqrt(squared);
squared = std::sqrt(squared);
*r1 = (-b + squared) / base;
*r2 = (-b - squared) / base;
@ -69,7 +67,7 @@ EQ::BandProcess::BandProcess() {
}
void EQ::recalculate_band_coefficients() {
#define BAND_LOG(m_f) (log((m_f)) / log(2.))
#define BAND_LOG(m_f) (std::log((m_f)) / std::log(2.))
for (int i = 0; i < band.size(); i++) {
double octave_size;
@ -86,17 +84,17 @@ void EQ::recalculate_band_coefficients() {
octave_size = (next + prev) / 2.0;
}
double frq_l = round(frq / pow(2.0, octave_size / 2.0));
double frq_l = std::round(frq / std::pow(2.0, octave_size / 2.0));
double side_gain2 = POW2(Math::SQRT12);
double th = Math::TAU * frq / mix_rate;
double th_l = Math::TAU * frq_l / mix_rate;
double c2a = side_gain2 * POW2(cos(th)) - 2.0 * side_gain2 * cos(th_l) * cos(th) + side_gain2 - POW2(sin(th_l));
double c2a = side_gain2 * POW2(std::cos(th)) - 2.0 * side_gain2 * std::cos(th_l) * std::cos(th) + side_gain2 - POW2(std::sin(th_l));
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));
double c2b = 2.0 * side_gain2 * POW2(std::cos(th_l)) + side_gain2 * POW2(std::cos(th)) - 2.0 * side_gain2 * std::cos(th_l) * std::cos(th) - side_gain2 + POW2(std::sin(th_l));
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));
double c2c = 0.25 * side_gain2 * POW2(std::cos(th)) - 0.5 * side_gain2 * std::cos(th_l) * std::cos(th) + 0.25 * side_gain2 - 0.25 * POW2(std::sin(th_l));
//printf("band %i, precoefs = %f,%f,%f\n",i,c2a,c2b,c2c);
@ -109,7 +107,7 @@ void EQ::recalculate_band_coefficients() {
band.write[i].c1 = 2.0 * ((0.5 - r1) / 2.0);
band.write[i].c2 = 2.0 * r1;
band.write[i].c3 = 2.0 * (0.5 + r1) * cos(th);
band.write[i].c3 = 2.0 * (0.5 + r1) * std::cos(th);
//printf("band %i, coefs = %f,%f,%f\n",i,(float)bands[i].c1,(float)bands[i].c2,(float)bands[i].c3);
}
}

View file

@ -33,8 +33,6 @@
#include "core/math/audio_frame.h"
#include "core/os/memory.h"
#include <math.h>
const float Reverb::comb_tunings[MAX_COMBS] = {
//freeverb comb tunings
0.025306122448979593f,
@ -60,7 +58,7 @@ void Reverb::process(float *p_src, float *p_dst, int p_frames) {
p_frames = INPUT_BUFFER_MAX_SIZE;
}
int predelay_frames = lrint((params.predelay / 1000.0) * params.mix_rate);
int predelay_frames = std::rint((params.predelay / 1000.0) * params.mix_rate);
if (predelay_frames < 10) {
predelay_frames = 10;
}
@ -90,7 +88,7 @@ void Reverb::process(float *p_src, float *p_dst, int p_frames) {
}
if (params.hpf > 0) {
float hpaux = expf(-Math::TAU * params.hpf * 6000 / params.mix_rate);
float hpaux = std::exp(-Math::TAU * params.hpf * 6000 / params.mix_rate);
float hp_a1 = (1.0 + hpaux) / 2.0;
float hp_a2 = -(1.0 + hpaux) / 2.0;
float hp_b1 = hpaux;
@ -106,7 +104,7 @@ void Reverb::process(float *p_src, float *p_dst, int p_frames) {
for (int i = 0; i < MAX_COMBS; i++) {
Comb &c = comb[i];
int size_limit = c.size - lrintf((float)c.extra_spread_frames * (1.0 - params.extra_spread));
int size_limit = c.size - std::rint((float)c.extra_spread_frames * (1.0 - params.extra_spread));
for (int j = 0; j < p_frames; j++) {
if (c.pos >= size_limit) { //reset this now just in case
c.pos = 0;
@ -127,7 +125,7 @@ void Reverb::process(float *p_src, float *p_dst, int p_frames) {
for (int i=0;i<MAX_ALLPASS;i++) {
AllPass &a=allpass[i];
ap_size_limit[i]=a.size-lrintf((float)a.extra_spread_frames*(1.0-params.extra_spread));
ap_size_limit[i]=a.size-std::rint((float)a.extra_spread_frames*(1.0-params.extra_spread));
}
for (int i=0;i<p_frames;i++) {
@ -156,7 +154,7 @@ void Reverb::process(float *p_src, float *p_dst, int p_frames) {
for (int i = 0; i < MAX_ALLPASS; i++) {
AllPass &a = allpass[i];
int size_limit = a.size - lrintf((float)a.extra_spread_frames * (1.0 - params.extra_spread));
int size_limit = a.size - std::rint((float)a.extra_spread_frames * (1.0 - params.extra_spread));
for (int j = 0; j < p_frames; j++) {
if (a.pos >= size_limit) {
@ -233,9 +231,9 @@ void Reverb::configure_buffers() {
for (int i = 0; i < MAX_COMBS; i++) {
Comb &c = comb[i];
c.extra_spread_frames = lrint(params.extra_spread_base * params.mix_rate);
c.extra_spread_frames = std::rint(params.extra_spread_base * params.mix_rate);
int len = lrint(comb_tunings[i] * params.mix_rate) + c.extra_spread_frames;
int len = std::rint(comb_tunings[i] * params.mix_rate) + c.extra_spread_frames;
if (len < 5) {
len = 5; //may this happen?
}
@ -251,9 +249,9 @@ void Reverb::configure_buffers() {
for (int i = 0; i < MAX_ALLPASS; i++) {
AllPass &a = allpass[i];
a.extra_spread_frames = lrint(params.extra_spread_base * params.mix_rate);
a.extra_spread_frames = std::rint(params.extra_spread_base * params.mix_rate);
int len = lrint(allpass_tunings[i] * params.mix_rate) + a.extra_spread_frames;
int len = std::rint(allpass_tunings[i] * params.mix_rate) + a.extra_spread_frames;
if (len < 5) {
len = 5; //may this happen?
}
@ -292,7 +290,7 @@ void Reverb::update_parameters() {
float auxdmp = params.damp / 2.0 + 0.5; //only half the range (0.5 .. 1.0 is enough)
auxdmp *= auxdmp;
c.damp = expf(-Math::TAU * auxdmp * 10000 / params.mix_rate); // 0 .. 10khz
c.damp = std::exp(-Math::TAU * auxdmp * 10000 / params.mix_rate); // 0 .. 10khz
}
}