139 lines
4.6 KiB
C++
139 lines
4.6 KiB
C++
/*
|
|
* alaw.h
|
|
* Copyright (c) 2025 Christopher Herb
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
|
|
#include "lut.h"
|
|
|
|
namespace trnr {
|
|
|
|
constexpr float A_LAW_A = 87.6f;
|
|
constexpr float A_LAW_LN_A = 4.474f;
|
|
|
|
constexpr int32_t A_LAW_A_Q15 = static_cast<int32_t>(A_LAW_A * (1 << 15));
|
|
constexpr int32_t A_LAW_LN_A_Q15 = static_cast<int32_t>(A_LAW_LN_A * (1 << 15));
|
|
constexpr int32_t ONE_LN_A_Q15 = (1 << 15) + A_LAW_LN_A_Q15;
|
|
|
|
// Threshold in Q15: 1/A ≈ 0.0114 → 373 in Q15
|
|
constexpr int32_t ENCODE_THRESHOLD_Q15 = static_cast<int32_t>((1.0f / A_LAW_A) * (1 << 15));
|
|
// Threshold in Q15: 1/(1+ln(A)) ≈ 0.183 → 5996 in Q15
|
|
constexpr int32_t DECODE_THRESHOLD_Q15 = static_cast<int32_t>((1.0f / (1.0f + A_LAW_LN_A)) * (1 << 15));
|
|
|
|
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;
|
|
}
|
|
|
|
inline int16_t alaw_encode_fixed(int16_t input)
|
|
{
|
|
int32_t sign = (input >= 0) ? 1 : -1;
|
|
int32_t abs_sample = std::abs(input);
|
|
int32_t output;
|
|
|
|
if (abs_sample < ENCODE_THRESHOLD_Q15) {
|
|
// Linear region: y = A*x / (1 + ln(A))
|
|
// A_Q15 * x_Q15 / ONE_LN_A_Q15 where ONE_LN_A_Q15 is already scaled by 32768
|
|
output = (int32_t)(((int64_t)A_LAW_A_Q15 * abs_sample) / ONE_LN_A_Q15);
|
|
} else {
|
|
// Logarithmic region: y = (1 + ln(A*x)) / (1 + ln(A))
|
|
// Scale Q15 sample to log table input range [4, 4096]
|
|
int32_t log_input = std::max(4, abs_sample >> 3);
|
|
int32_t log_x_q15 = fixed_log(log_input);
|
|
|
|
// ln(A*x) = ln(A) + ln(x) in Q15
|
|
int32_t log_ax_q15 = A_LAW_LN_A_Q15 + log_x_q15;
|
|
|
|
// y = (1 + ln(A*x)) / (1 + ln(A))
|
|
// numerator in Q15, denominator is ONE_LN_A_Q15 (which is (1+ln(A))*32768)
|
|
int64_t numerator = ((int64_t)1 << 15) + log_ax_q15;
|
|
output = (int32_t)((numerator << 15) / ONE_LN_A_Q15);
|
|
}
|
|
|
|
output *= sign;
|
|
|
|
// Clamp to int16 range
|
|
if (output > 32767) output = 32767;
|
|
if (output < -32768) output = -32768;
|
|
return static_cast<int16_t>(output);
|
|
}
|
|
|
|
inline int16_t alaw_decode_fixed(int16_t input)
|
|
{
|
|
int32_t sign = (input >= 0) ? 1 : -1;
|
|
int32_t abs_comp = std::abs(input);
|
|
int32_t sample;
|
|
|
|
if (abs_comp < DECODE_THRESHOLD_Q15) {
|
|
// Linear region: x = y * (1 + ln(A)) / A
|
|
sample = (int32_t)(((int64_t)abs_comp * ONE_LN_A_Q15) / A_LAW_A_Q15);
|
|
} else {
|
|
// Logarithmic region: x = exp(y * (1 + ln(A)) - 1) / A
|
|
// exp_arg_q12 = ((y_q15 * ONE_LN_A_Q15 * 8192) / (32768*32768*9)) - 910
|
|
// Computing in 64-bit to avoid overflow
|
|
int64_t temp = (((int64_t)abs_comp * ONE_LN_A_Q15) * 8192LL) / (32768LL * 32768LL * 9LL) - 910;
|
|
int32_t exp_arg_q12 = (int32_t)temp;
|
|
|
|
// Clamp to valid range
|
|
if (exp_arg_q12 < 0) exp_arg_q12 = 0;
|
|
if (exp_arg_q12 > 4096) exp_arg_q12 = 4096;
|
|
|
|
int32_t exp_val_q15 = fixed_exp(exp_arg_q12);
|
|
sample = (int32_t)(((int64_t)exp_val_q15 << 15) / A_LAW_A_Q15);
|
|
}
|
|
|
|
sample *= sign;
|
|
if (sample > 32767) sample = 32767;
|
|
if (sample < -32768) sample = -32768;
|
|
return static_cast<int16_t>(sample);
|
|
}
|
|
|
|
} // namespace trnr
|