add a-law companding functions

This commit is contained in:
2025-09-17 19:50:02 +02:00
parent 41b2dce01a
commit 64f252acfc
3 changed files with 38 additions and 156 deletions

38
companding/alaw.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
#include <cmath>
namespace trnr {
constexpr float A_LAW_A = 87.6f;
inline float alaw_encode(float input)
{
float sign = (input >= 0.0f) ? 1.0f : -1.0f;
float abs_sample = std::fabs(input);
float output;
if (abs_sample < (1.0f / A_LAW_A)) {
output = sign * (A_LAW_A * abs_sample) / (1.0f + std::log(A_LAW_A));
} else {
output = sign * (1.0f + std::log(A_LAW_A * abs_sample)) / (1.0f + std::log(A_LAW_A));
}
return output;
}
inline float alaw_decode(float input)
{
float sign = (input >= 0.0f) ? 1.0f : -1.0f;
float abs_comp = std::fabs(input);
float sample;
if (abs_comp < (1.0f / (1.0f + std::log(A_LAW_A)))) {
sample = sign * (abs_comp * (1.0f + std::log(A_LAW_A))) / A_LAW_A;
} else {
sample = sign * std::exp(abs_comp * (1.0f + std::log(A_LAW_A)) - 1.0f) / A_LAW_A;
}
return sample;
}
} // namespace trnr