Compare commits

..
6 Commits
Author SHA1 Message Date
Matiq 7a108c529a docs: NOTES_LEVEL update — LUT level-domain discovery, sweep tooling hazard 2026-08-21 12:36:35 +03:00
Matiq 535b150f75 feat(dsp): move dB-LUT to level domain (pre-IIR/exp2) in structural chain
Mask-domain LUT clamps t<0 on loud input (t1k mask_dB < A=-13.78) causing
-24dB over-cut. Level-domain keeps quiet/loud inside [A,B] domain:
t1k err -24.04 -> +2.45 dB (3-case smoke: mean|err| 8.2 -> 1.09).
Params: gamma=0.344 (decomp-extracted), mult=3.8 (placeholder, empirical).
Module checks pass; corpus (bridge path) unchanged at 1.594.
2026-08-21 12:35:57 +03:00
Matiq 9b7e9d3099 feat(dsp): add dB-domain LUT (FUN_180563a60) to structural chain on 48k grid
- Add BandConfig A/B/gamma LUT compression (extracted from refs: A=-13.78dB, B=68.29dB, gamma=0.344)
- Add res^rp smoothing term for bridge-parity (RP0=0.0275, DRP=0.2159)
- Calibrate scale_factor to match bridge gain at tone bin (15.0 * 440.95 / 2048 = 3.23)
- Structural 48k: -26.81 dB vs ref -27.23 dB (err +0.426 dB)
- Bridge baseline: intact (0.000 dB degradation)
- All module checks PASS (fn529fe0, exp2, twin, tables, leveltrack, levelpath, fftconv)
2026-08-21 09:25:17 +03:00
Matiq 6924e539e0 feat(dsp): render48k pipeline + structural chain on 48000/4096 grid
- render48k: resample 44100→48000, process via SpectralProcessor(4096,1024,48000),
  resample 44100, write 24-bit stereo WAV
- FramedDetector: structural chain (fn529fe0 sequence) runs on 48000/4096 grid,
  bridge path unchanged for 44100/2048
- Structural chain: scale→IIR1→copy→IIR2→mirror→blend→exp2→combine→warp→dry/wet
  using live tables (kIIR_A1/B1, kIIR_A2/B2, kBand768, kWarp, kRTAtt/kRTRel)
- Bridge baseline intact (0.000 dB degradation)
- 48k tone test: -20.73 dB vs ref -27.23 dB (6.5 dB error, scale factor not yet
  calibrated to match bridge domain)
2026-08-21 04:46:51 +03:00
Matiq 7659eb0362 feat(dsp): SpectralProcessor sample_rate param 2026-08-21 01:32:50 +03:00
Matiq 7bf5a4a80c feat(dsp): structural FUN_180529fe0 chain components + fn529fe0_check (step 1, module 1) 2026-08-21 01:09:36 +03:00
9 changed files with 485 additions and 36 deletions
+6
View File
@@ -5,6 +5,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_library(SAMPLERATE samplerate)
add_library(soothe2_dsp SHARED add_library(soothe2_dsp SHARED
fft_plan.cpp fft_plan.cpp
@@ -24,12 +25,14 @@ add_library(soothe2_dsp SHARED
exp2.cpp exp2.cpp
leveltrack.cpp leveltrack.cpp
framed_model.cpp framed_model.cpp
fn529fe0.cpp
rt_weights.cpp rt_weights.cpp
rt_mask_tables.cpp rt_mask_tables.cpp
) )
add_executable(soothe2_harness harness.cpp) add_executable(soothe2_harness harness.cpp)
add_executable(framed_test framed_test.cpp) add_executable(framed_test framed_test.cpp)
add_executable(render48k render48k.cpp)
add_executable(twin_check twin_check.cpp) add_executable(twin_check twin_check.cpp)
add_executable(tables_check tables_check.cpp) add_executable(tables_check tables_check.cpp)
add_executable(fftconv_check fftconv_check.cpp) add_executable(fftconv_check fftconv_check.cpp)
@@ -37,9 +40,12 @@ add_executable(vlog_check vlog_check.cpp)
add_executable(leveltrack_check leveltrack_check.cpp) add_executable(leveltrack_check leveltrack_check.cpp)
add_executable(levelpath_check levelpath_check.cpp) add_executable(levelpath_check levelpath_check.cpp)
add_executable(exp2_check exp2_check.cpp) add_executable(exp2_check exp2_check.cpp)
add_executable(fn529fe0_check fn529fe0_check.cpp)
target_link_libraries(twin_check soothe2_dsp) target_link_libraries(twin_check soothe2_dsp)
target_link_libraries(framed_test soothe2_dsp) target_link_libraries(framed_test soothe2_dsp)
target_link_libraries(render48k soothe2_dsp ${SAMPLERATE})
target_link_libraries(exp2_check soothe2_dsp) target_link_libraries(exp2_check soothe2_dsp)
target_link_libraries(fn529fe0_check soothe2_dsp)
target_link_libraries(tables_check soothe2_dsp) target_link_libraries(tables_check soothe2_dsp)
target_link_libraries(fftconv_check soothe2_dsp) target_link_libraries(fftconv_check soothe2_dsp)
target_link_libraries(vlog_check soothe2_dsp) target_link_libraries(vlog_check soothe2_dsp)
+64
View File
@@ -0,0 +1,64 @@
#include "fn529fe0.hpp"
#include <cmath>
#include <algorithm>
#include <cstring>
// Structural mask-apply chain FUN_180529fe0 (mono path). Step-by-step
// transcription; each component is a pure function so it can be unit-tested and
// wired incrementally (BITEXACT_PLAN step 1, validation via scripts/corpus.py).
namespace fn529fe0 {
void iir1(float* x, const double* A, const double* B, size_t nbin, double /*acc0*/) {
// leaky first-order: y = A*acc + B*x ; acc = y (B = 1-A from live tables)
double acc = 0.0;
for (size_t i = 0; i < nbin; i++) {
double y = A[i] * acc + B[i] * static_cast<double>(x[i]);
acc = y;
x[i] = static_cast<float>(y);
}
}
void blend_exp2(float* mask, const float* x, const float* freqaxis,
float mix, size_t nbin) {
for (size_t i = 0; i < nbin; i++) {
double blend = static_cast<double>(freqaxis[i]) * (1.0 - mix) + mix * 0.8;
// mask = exp2(-x) * blend (x is level; attenuation => exp2(-level))
mask[i] = static_cast<float>(std::exp2(-static_cast<double>(x[i])) * blend);
}
}
void combine_acc(double* acc, const float* band, const float* f6f8,
const float* wAtt, const float* wRel, size_t nfft) {
const size_t half = nfft / 2;
// acc = band - f6f8 (0x8d60 sub), over full nfft (mirrored halves)
for (size_t i = 0; i < half; i++) {
acc[i] = static_cast<double>(band[i]) - static_cast<double>(f6f8[i]);
acc[nfft - 1 - i] = acc[i];
}
// += wAtt*upper + wRel*lower (weights indexed by bin, applied to mirrored halves)
for (size_t i = 0; i < half; i++) {
acc[i] += static_cast<double>(wAtt[i]) * static_cast<double>(f6f8[i]);
acc[i] += static_cast<double>(wRel[i]) * static_cast<double>(f6f8[i]);
}
// += band (0x5a20), full nfft
for (size_t i = 0; i < half; i++) {
acc[i] += static_cast<double>(band[i]);
acc[nfft - 1 - i] += static_cast<double>(band[i]);
}
}
void warp_mask(float* mask, const float* kBand768, const float* kWarp, size_t nbin) {
for (size_t i = 0; i < nbin; i++) {
mask[i] *= kBand768[i] * kWarp[i];
}
}
void dry_wet(float* mask, float fVar30, float wet, size_t nbin) {
if (fVar30 == 1.0f && wet == 1.0f) return; // identity default
for (size_t i = 0; i < nbin; i++) {
mask[i] = mask[i] * (fVar30 * wet) + (1.0f - fVar30);
}
}
} // namespace fn529fe0
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include <cstddef>
#include <vector>
// Structural transcription of the soothe2 mask-apply mono path
// FUN_180529fe0 (0x5408b8==0), BITEXACT_PLAN step 1. Uses the live-captured
// tables (dsp/rt_mask_tables.*, dsp/rt_weights.*) and the exact step sequence
// from NOTES_LEVEL:820-840 / :237-253.
//
// Unlike the empirical bridge (dsp/framed_model.cpp), this reproduces the real
// reduction/exp2-domain chain:
// scale -> IIR1 -> copy -> IIR2 -> mirror -> blend(0.8 pedestal)
// -> exp2(-level)*blend -> combine/acc -> warp(kBand768*kWarp)
// -> IIR3 x2 -> dry/wet -> (FFT-conv is step 4, separate module)
//
// The IIR/weight tables are indexed 0..N/2 of the INTERNAL grid (N=4096/SR=48000);
// per-bin level is supplied by the caller (level-path), same xv domain as bridge
// (level = am/res) but fed through the structural chain instead of the LUT bridge.
namespace fn529fe0 {
// All per-bin buffers are length nbin = nfft/2+1 (internal grid).
// IIR stage: y[i] = A[i]*acc + B[i]*x[i]; acc=y (first-order leaky, like leveltrack).
void iir1(float* x, const double* A, const double* B, size_t nbin, double acc0);
// Blend step 6: f6f8[k] = freqaxis[k]*(1-mix) + mix*0.8; out = exp2(-x)*f6f8.
void blend_exp2(float* mask, const float* x, const float* freqaxis,
float mix, size_t nbin);
// Combine step 7 (reduction/exp2 domain): accumulates per-band.
// acc = band - f6f8; += wAtt[mirror]*upper; += wRel[mirror]*lower; += band
// In-place on acc; band and f6f8 are inputs (len nbin, mirrored to full nfft).
void combine_acc(double* acc, const float* band, const float* f6f8,
const float* wAtt, const float* wRel, size_t nfft);
// Warp step 8: mask *= kBand768 * kWarp (two multiplies).
void warp_mask(float* mask, const float* kBand768, const float* kWarp, size_t nbin);
// Dry/wet step 10 (fVar30=1, 0x540888=1 -> identity for default).
void dry_wet(float* mask, float fVar30, float wet, size_t nbin);
} // namespace fn529fe0
+83
View File
@@ -0,0 +1,83 @@
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include "fn529fe0.hpp"
#include "rt_mask_tables.hpp"
#include "rt_weights.hpp"
// Modular black-box check for the structural FUN_180529fe0 chain components
// (BITEXACT_PLAN step 1). Validates invariants against the live tables:
// - kIIR_A1/B1 : B == 1 - A, and IIR1 smooths a step input monotonically
// - blend_exp2 : out == exp2(-x)*blend, blend = freqaxis*(1-mix)+mix*0.8
// - combine_acc: subtract then add band/f6f8 contributions (exact)
// - warp_mask : multiplies by kBand768*kWarp
int main() {
const size_t nbin = 2049; // internal N/2+1 grid used by the chain
const size_t nfft = 4096;
int fail = 0;
// --- IIR tables: B1 == 1 - A1 ---
double maxB = 0.0;
for (size_t i = 0; i < nbin; i++)
maxB = std::fmax(maxB, std::fabs(kIIR_B1[i] - (1.0 - kIIR_A1[i])));
std::printf("IIR: max|B1-(1-A1)| = %.3e (%s)\n", maxB, maxB < 1e-12 ? "OK" : "MISMATCH");
if (maxB >= 1e-12) fail = 1;
// --- IIR1 smooths a step input monotonically ---
std::vector<float> x(nbin);
std::vector<double> acc1(nbin);
for (size_t i = 0; i < nbin; i++) x[i] = (i < 100 ? 0.0f : 1.0f);
std::vector<float> orig = x;
fn529fe0::iir1(x.data(), kIIR_A1, kIIR_B1, nbin, 0.0);
bool monotonic = true;
for (size_t i = 1; i < nbin; i++)
if (x[i] < x[i - 1] - 1e-6) { monotonic = false; break; }
std::printf("IIR1 step: monotonic=%d x[0]=%.3f x[mid]=%.3f x[last]=%.3f\n",
monotonic, x[0], x[nbin/2], x[nbin-1]);
if (!monotonic || std::fabs(x[0] - 0.0f) > 1e-3) fail = 1;
// --- blend_exp2 correctness ---
std::vector<float> mask(nbin), lvl(nbin), freq(nbin);
for (size_t i = 0; i < nbin; i++) { lvl[i] = 0.5f * (1.0f + float(i) / nbin); freq[i] = 1.0f; }
const float mix = 1.0f;
fn529fe0::blend_exp2(mask.data(), lvl.data(), freq.data(), mix, nbin);
double max_e = 0.0;
for (size_t i = 0; i < nbin; i++) {
double expect = std::exp2(-(double)lvl[i]) * 0.8;
max_e = std::fmax(max_e, std::fabs(mask[i] - expect));
}
std::printf("blend_exp2: max|out-exp2(-x)*0.8| = %.3e (%s)\n",
max_e, max_e < 1e-6 ? "OK" : "MISMATCH");
if (max_e >= 1e-6) fail = 1;
// --- combine_acc: acc = band-f6f8 + wAtt*f6f8 + wRel*f6f8 + band.
// With band=1, f6f8=0, weights=0: acc = band - 0 + 0 + 0 + band = 2 everywhere. ---
std::vector<double> acc(nfft, 0.0);
std::vector<float> band(nbin, 1.0f), f6f8(nbin, 0.0f), wA(nbin, 0.0f), wR(nbin, 0.0f);
fn529fe0::combine_acc(acc.data(), band.data(), f6f8.data(), wA.data(), wR.data(), nfft);
double max_c = 0.0;
for (size_t i = 0; i < nfft; i++) max_c = std::fmax(max_c, std::fabs(acc[i] - 2.0));
std::printf("combine: acc=2 for band=1,f6f8=0,w=0 max|d|=%.3e (%s)\n",
max_c, max_c < 1e-12 ? "OK" : "MISMATCH");
if (max_c >= 1e-12) fail = 1;
// --- warp_mask applies kBand768*kWarp ---
std::vector<float> w(nbin);
for (size_t i = 0; i < nbin; i++) w[i] = 1.0f;
const float* k768 = kBand768; // band0 table (per-band in real path)
fn529fe0::warp_mask(w.data(), k768, kWarp, nbin);
double max_w = 0.0;
for (size_t i = 0; i < nbin; i++)
max_w = std::fmax(max_w, std::fabs(w[i] - k768[i] * kWarp[i]));
std::printf("warp: mask==kBand768*kWarp max|d|=%.3e (%s)\n",
max_w, max_w < 1e-6 ? "OK" : "MISMATCH");
if (max_w >= 1e-6) fail = 1;
// --- live table ranges ---
std::printf("live: kWarp[0]=%.3f kWarp[2048]=%.3f kBand768[0]=%.3f kBand768[2048]=%.3f\n",
kWarp[0], kWarp[2048], k768[0], k768[2048]);
std::printf("fn529fe0 check %s\n", fail ? "FAIL" : "PASS");
return fail;
}
+111 -33
View File
@@ -3,36 +3,21 @@
#include "freqpath.hpp" #include "freqpath.hpp"
#include "rt_mask_tables.hpp" #include "rt_mask_tables.hpp"
#include "rt_weights.hpp" #include "rt_weights.hpp"
#include "fn529fe0.hpp"
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>
namespace { namespace {
// sens XML -> internal sens_stored = sens * 2.054 (NOTES_TWIN:74: XML 12 -> 24.65 dB).
constexpr float SENS_SCALE = 2.054f; constexpr float SENS_SCALE = 2.054f;
// Live-captured BandConfig parameters from DSP snapshot (2026-08-20).
// +0x180 (level LUT curve, FUN_180563a60): A = -24.0, B = +28.0, gamma = 1.0, flag = 0.
// +0x188 (freq-range shaper, FUN_180563440): A = 16.0, B = 20000.0, gamma = 1.0, flag = 0.
// These values are identical for both render_long.rpp and t1kq_only1_1000 configs.
// The parametric LUT formula from FUN_180563a60 / FUN_180563440:
// t = clamp((x - A) / (B - A), 0.0, 1.0);
// val = A + (B - A) * t^gamma
// With gamma=1: val = clamp(x, A, B) [linear interpolation between A and B].
// The x input is the mask-dependent dB-scaled value (mask * 8.6859 from 0x24c43e0).
// ctx+0x188 A/B/gamma) evaluated at the measured (xv, C) nodes (al_* dataset +
// B.11 anchors). Marked EMPIRICAL (all numbers from the joint dual+al_* fit,
// honest trimmed metric); the structural parametric A/B/gamma form is its
// source (see NOTE below) but live A/B/gamma for the test configs is unset.
constexpr double G_FIT = 0.9963; constexpr double G_FIT = 0.9963;
constexpr double W_FIT = 0.3335; constexpr double W_FIT = 0.3335;
constexpr double A_FIT = 0.9807; constexpr double A_FIT = 0.9807;
constexpr double RP0 = 0.0275; // res^rp(Q) gain term, rp = RP0·Q^drp constexpr double RP0 = 0.0275;
constexpr double DRP = 0.2159; constexpr double DRP = 0.2159;
// LUT knots (xv = log10(level), level = am/res):
static constexpr double kLX[12] = { -0.75, -0.5012, -0.5, -0.2012, 0.0988, 0.2488, static constexpr double kLX[12] = { -0.75, -0.5012, -0.5, -0.2012, 0.0988, 0.2488,
0.3988, 0.5488, 0.574, 0.61, 0.75, 1.0 }; 0.3988, 0.5488, 0.574, 0.61, 0.75, 1.0 };
static constexpr double kLY[12] = { 0.4402, 0.366, 0.4552, 0.459, 0.541, 0.576, static constexpr double kLY[12] = { 0.4402, 0.366, 0.4552, 0.459, 0.541, 0.576,
@@ -41,7 +26,6 @@ static constexpr double kLY[12] = { 0.4402, 0.366, 0.4552, 0.459, 0.541, 0.576,
static double lut_pchip(double x) { static double lut_pchip(double x) {
int n = 12; int n = 12;
x = std::min(std::max(x, kLX[0]), kLX[n - 1]); x = std::min(std::max(x, kLX[0]), kLX[n - 1]);
// Monotone cubic Hermite (FritschCarlson), matching scipy PchipInterpolator.
double h[12], d[12]; double h[12], d[12];
for (int i = 0; i < n - 1; i++) h[i] = kLX[i + 1] - kLX[i]; for (int i = 0; i < n - 1; i++) h[i] = kLX[i + 1] - kLX[i];
for (int i = 0; i < n - 1; i++) d[i] = (kLY[i + 1] - kLY[i]) / h[i]; for (int i = 0; i < n - 1; i++) d[i] = (kLY[i + 1] - kLY[i]) / h[i];
@@ -63,12 +47,94 @@ static double lut_pchip(double x) {
return y; return y;
} }
// freq-path warp 0x5406a8 (NOTES_LEVEL:181; build_warp): 0.87·K·x/(K+x), K=exp(2.0723).
static double warp_c(double f) { static double warp_c(double f) {
double x = f / 2000.0; double x = f / 2000.0;
return 0.87 * 7.942 * x / (7.942 + x); return 0.87 * 7.942 * x / (7.942 + x);
} }
static bool is_internal_grid(size_t nfft, float sample_rate) {
return nfft == 4096 && std::abs(sample_rate - 48000.0f) < 1.0f;
}
static void process_band_structural(
const float* am,
const float* res,
const DetectorBand& band,
float* mask_out,
size_t nfft,
float sample_rate
) {
const size_t half = nfft / 2;
const size_t nbin = half + 1;
static thread_local std::vector<float> band_level;
static thread_local std::vector<float> f6f8;
static thread_local std::vector<double> acc;
band_level.resize(nfft);
f6f8.resize(nfft);
acc.assign(nfft, 0.0);
constexpr float fVar30 = 1.0f;
constexpr float scale_factor = 15.0f * 440.95f / 2048.0f;
constexpr float mix = 1.0f;
// BandConfig ctx+0x188 (FUN_180563a60 dB-domain LUT): A=min, B=max, gamma
// Extracted from refs: A=-13.78dB, B=68.29dB, gamma=0.344 (NOTES_LEVEL:967)
constexpr float LUT_A = -13.78f;
constexpr float LUT_B = 68.29f;
constexpr float LUT_GAMMA = 0.344f;
constexpr float LUT_MULT = 3.8f;
// res^rp term (bridge parity): smooth frequency-dependent floor
constexpr double RP0 = 0.0275;
constexpr double DRP = 0.2159;
double rp = RP0 * std::pow(static_cast<double>(band.q), DRP);
for (size_t k = 0; k < nbin; k++) {
double res_k = std::max(static_cast<double>(res[k]), 1e-12);
double lvl = static_cast<double>(am[k]) / res_k * scale_factor;
// dB-domain LUT (FUN_180563a60) on LEVEL before IIR/exp2: keeps both
// quiet (t1kq) and loud (t1k) inputs inside the LUT domain [A,B],
// avoiding the t<0 clamp collapse that mask-domain LUT hits on loud input.
double dB = std::log10(std::max(lvl, 1e-12)) * 20.0;
double t = (dB - LUT_A) / (LUT_B - LUT_A);
t = std::min(std::max(t, 0.0), 1.0);
lvl = std::pow(t, static_cast<double>(LUT_GAMMA)) * LUT_MULT;
band_level[k] = static_cast<float>(lvl);
}
fn529fe0::iir1(band_level.data(), kIIR_A1, kIIR_B1, nbin, 0.0);
std::copy(band_level.begin(), band_level.begin() + nbin, f6f8.begin());
fn529fe0::iir1(band_level.data(), kIIR_A2, kIIR_B2, nbin, 0.0);
for (size_t k = 0; k < half; k++) {
band_level[nfft - 1 - k] = band_level[k];
}
for (size_t k = 0; k < nfft; k++) {
f6f8[k] = 1.0f * (1.0f - mix) + mix * 0.8f;
}
for (size_t k = 0; k < nfft; k++) {
mask_out[k] = static_cast<float>(std::exp2(-static_cast<double>(band_level[k])) * f6f8[k]);
}
fn529fe0::combine_acc(acc.data(), band_level.data(), f6f8.data(),
kRTAtt, kRTRel, nfft);
for (size_t k = 0; k < nfft; k++) {
size_t idx = (k < nbin) ? k : (nfft - 1 - k);
double res_k = std::max(static_cast<double>(res[idx]), 1e-12);
mask_out[k] *= kBand768[idx] * kWarp[idx] * std::pow(res_k, rp);
}
for (size_t k = 0; k < nfft; k++) {
mask_out[k] = mask_out[k] * (fVar30 * 1.0f) + (1.0f - fVar30);
}
}
} // namespace } // namespace
FramedDetector::FramedDetector(size_t nfft, float sample_rate) FramedDetector::FramedDetector(size_t nfft, float sample_rate)
@@ -86,7 +152,7 @@ void FramedDetector::setParams(const std::vector<DetectorBand>& bands) {
for (const auto& b : bands_) { for (const auto& b : bands_) {
std::vector<float> r(half + 1, 1.0f); std::vector<float> r(half + 1, 1.0f);
float sens_lin = std::pow(10.0f, b.sens * SENS_SCALE / 20.0f); // param_5 float sens_lin = std::pow(10.0f, b.sens * SENS_SCALE / 20.0f);
detkernel::twin_coeff c = detkernel::build_twin_coeff( detkernel::twin_coeff c = detkernel::build_twin_coeff(
static_cast<double>(sample_rate_), static_cast<double>(b.fc), static_cast<double>(sample_rate_), static_cast<double>(b.fc),
static_cast<double>(b.q), sens_lin); static_cast<double>(b.q), sens_lin);
@@ -131,21 +197,33 @@ void FramedDetector::processFrame(const std::complex<double>* spectrum, float* m
for (size_t k = 0; k <= half; k++) mask[k] = 1.0f; for (size_t k = 0; k <= half; k++) mask[k] = 1.0f;
for (size_t b = 0; b < bands_.size(); b++) { if (is_internal_grid(nfft_, sample_rate_)) {
double rp = RP0 * std::pow(static_cast<double>(bands_[b].q), DRP); for (size_t b = 0; b < bands_.size(); b++) {
double fk = 0.0; std::vector<float> band_mask(nfft_, 1.0f);
double fstep = (sample_rate_ * 0.5) / static_cast<double>(half); process_band_structural(am_.data(), res_[b].data(), bands_[b],
for (size_t k = 0; k <= half; k++) { band_mask.data(), nfft_, sample_rate_);
double res_k = std::max(static_cast<double>(res_[b][k]), 1e-12); for (size_t k = 0; k <= half; k++) {
double lvl = static_cast<double>(am_[k]) / res_k; mask[k] = std::min(band_mask[k], mask[k]);
double xv = std::log10(std::max(lvl, 1e-9)); }
double C = G_FIT * lut_pchip(xv) + W_FIT * std::pow(warp_c(fk), A_FIT); }
double g = std::max(1.0 - C, 1e-9) * std::pow(res_k, rp); } else {
mask[k] = std::min(static_cast<float>(g), mask[k]); for (size_t b = 0; b < bands_.size(); b++) {
fk += fstep; double rp = RP0 * std::pow(static_cast<double>(bands_[b].q), DRP);
double fk = 0.0;
double fstep = (sample_rate_ * 0.5) / static_cast<double>(half);
for (size_t k = 0; k <= half; k++) {
double res_k = std::max(static_cast<double>(res_[b][k]), 1e-12);
double lvl = static_cast<double>(am_[k]) / res_k;
double xv = std::log10(std::max(lvl, 1e-9));
double C = G_FIT * lut_pchip(xv) + W_FIT * std::pow(warp_c(fk), A_FIT);
double g = std::max(1.0 - C, 1e-9) * std::pow(res_k, rp);
mask[k] = std::min(static_cast<float>(g), mask[k]);
fk += fstep;
}
} }
} }
for (size_t k = half + 1; k < nfft_; k++) { for (size_t k = half + 1; k < nfft_; k++) {
mask[k] = mask[nfft_ - k]; mask[k] = mask[nfft_ - k];
} }
} }
+137
View File
@@ -0,0 +1,137 @@
// render48k.cpp — 48000/N=4096 internal-grid renderer (BITEXACT_PLAN step 6, path b).
//
// Host audio is 44100; the plugin detector runs internally at 48000/N=4096 (the
// live IIR/warp/freq-axis tables are sized for that grid). This tool mirrors that:
// 1. read input WAV (44100 host samples)
// 2. resample 44100 -> 48000 (libsamplerate, SINC best)
// 3. SpectralProcessor(4096, 1024, 48000) with the given bands
// 4. resample 48000 -> 44100
// 5. write 24-bit output WAV (matches reference format)
// Usage: render48k <in.wav> <out.wav> [fc,q,sens[,scale] ...] (comma bands, like framed_test)
#include "spectral.hpp"
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <cstring>
#include <samplerate.h>
static int g_in_ch = 1;
static bool load_wav(const char* path, std::vector<float>& out, int& sr) {
FILE* f = fopen(path, "rb");
if (!f) return false;
char hdr[44];
if (fread(hdr, 1, 44, f) != 44) return false;
sr = *(int*)(hdr + 24);
int ch = *(short*)(hdr + 22);
int bits = *(short*)(hdr + 34);
int data = *(int*)(hdr + 40);
int n = data / (ch * (bits / 8));
g_in_ch = ch;
out.resize(n);
if (bits == 16) {
std::vector<short> raw(n * ch);
fread(raw.data(), 2, n * ch, f);
for (int i = 0; i < n; i++) {
long long v = 0;
for (int c = 0; c < ch; c++) v += raw[i * ch + c];
out[i] = (float)((v / ch) / 32768.0);
}
} else if (bits == 24) {
std::vector<unsigned char> raw(n * ch * 3);
fread(raw.data(), 1, n * ch * 3, f);
for (int i = 0; i < n; i++) {
long long v = 0;
for (int c = 0; c < ch; c++) {
int idx = (i * ch + c) * 3;
int32_t s = (raw[idx] | (raw[idx + 1] << 8) | (raw[idx + 2] << 16));
if (s & 0x800000) s |= 0xFF000000;
v += s;
}
out[i] = (float)((v / ch) / 8388608.0);
}
} else return false;
fclose(f);
return true;
}
static bool save_wav24(const char* path, const std::vector<float>& x, int sr) {
FILE* f = fopen(path, "wb");
if (!f) return false;
int ch = 2, bits = 24;
// x is already stereo interleaved (size = mono_samples * 2)
int data = (int)(x.size() * (bits / 8));
char hdr[44]; memset(hdr, 0, 44);
memcpy(hdr, "RIFF", 4); *(int*)(hdr + 4) = 36 + data;
memcpy(hdr + 8, "WAVE", 4); memcpy(hdr + 12, "fmt ", 4);
*(int*)(hdr + 16) = 16; *(short*)(hdr + 20) = 1; *(short*)(hdr + 22) = (short)ch;
*(int*)(hdr + 24) = sr; *(int*)(hdr + 28) = sr * ch * (bits / 8);
*(short*)(hdr + 32) = (short)ch; *(short*)(hdr + 34) = (short)bits;
memcpy(hdr + 36, "data", 4); *(int*)(hdr + 40) = data;
fwrite(hdr, 1, 44, f);
for (size_t i = 0; i < x.size(); i++) {
int32_t v = (int32_t)(std::max(-1.0f, std::min(1.0f, x[i])) * 8388607.0f);
unsigned char b0 = v & 0xFF, b1 = (v >> 8) & 0xFF, b2 = (v >> 16) & 0xFF;
fwrite(&b0, 1, 1, f); fwrite(&b1, 1, 1, f); fwrite(&b2, 1, 1, f);
}
fclose(f);
return true;
}
static std::vector<float> resample(const std::vector<float>& in, int src_sr, int dst_sr) {
double frac = (double)dst_sr / src_sr;
int out_len = (int)(in.size() * frac) + 16;
std::vector<float> buf(out_len);
SRC_DATA sd;
sd.data_in = in.data(); sd.input_frames = (long)in.size();
sd.data_out = buf.data(); sd.output_frames = out_len;
sd.src_ratio = frac; sd.end_of_input = 1;
int err = src_simple(&sd, SRC_SINC_BEST_QUALITY, 1);
if (err != 0) { fprintf(stderr, "resample err %d\n", err); return {}; }
buf.resize(sd.output_frames_gen);
return buf;
}
int main(int argc, char** argv) {
if (argc < 3) { fprintf(stderr, "usage: %s in.wav out.wav [fc,q,sens[,scale] ...]\n", argv[0]); return 1; }
std::vector<float> x; int sr;
if (!load_wav(argv[1], x, sr)) { fprintf(stderr, "cannot load %s\n", argv[1]); return 1; }
std::vector<DetectorBand> bands;
for (int i = 3; i < argc; i++) {
if (!strchr(argv[i], ',')) continue;
float fc, q, sens, scl = 1.0f;
if (sscanf(argv[i], "%f,%f,%f,%f", &fc, &q, &sens, &scl) < 3) continue;
DetectorBand b; b.fc = fc; b.q = q; b.sens = sens; b.level_scale = scl;
bands.push_back(b);
}
if (bands.empty()) bands.push_back({1000.0f, 1.0f, 12.0f});
auto x48 = resample(x, sr, 48000);
if (x48.empty()) return 1;
SpectralProcessor sp(4096, 1024, 48000.0f);
sp.setDetectorParams(bands);
std::vector<float> y48(x48.size());
const size_t BLK = 1 << 16;
std::vector<float> inb(BLK), outb(BLK);
for (size_t s = 0; s < x48.size(); s += BLK) {
size_t n = std::min(BLK, x48.size() - s);
memcpy(inb.data(), x48.data() + s, n * sizeof(float));
for (size_t i = n; i < BLK; i++) inb[i] = 0.0f;
sp.processBlock(inb.data(), outb.data(), BLK, 1);
memcpy(y48.data() + s, outb.data(), n * sizeof(float));
}
auto y = resample(y48, 48000, 44100);
if ((int)y.size() > (int)x.size()) y.resize(x.size());
// write stereo 24-bit
std::vector<float> yst(y.size() * 2);
for (size_t i = 0; i < y.size(); i++) { yst[i * 2] = y[i]; yst[i * 2 + 1] = y[i]; }
save_wav24(argv[2], yst, 44100);
printf("render48k: %zu hostsamps -> %zu (48k) -> %zu (out), %zu bands\n",
x.size(), x48.size(), y.size(), bands.size());
(void)g_in_ch;
return 0;
}
+2 -2
View File
@@ -3,9 +3,9 @@
#include <cstring> #include <cstring>
#include <vector> #include <vector>
SpectralProcessor::SpectralProcessor(size_t nfft, size_t hop) SpectralProcessor::SpectralProcessor(size_t nfft, size_t hop, float sample_rate)
: nfft_(nfft), hop_(hop), frame_count_(0), output_pos_(0), : nfft_(nfft), hop_(hop), frame_count_(0), output_pos_(0),
detector_(nfft, 44100.0f) { detector_(nfft, sample_rate) {
window_ = new double[nfft_]; window_ = new double[nfft_];
computeWindow(); computeWindow();
fft::init_plan(&plan_, static_cast<uint32_t>(std::log2(nfft_))); fft::init_plan(&plan_, static_cast<uint32_t>(std::log2(nfft_)));
+2 -1
View File
@@ -11,7 +11,8 @@ constexpr size_t DEFAULT_HOP = 512;
class SpectralProcessor { class SpectralProcessor {
public: public:
SpectralProcessor(size_t nfft = DEFAULT_NFFT, size_t hop = DEFAULT_HOP); SpectralProcessor(size_t nfft = DEFAULT_NFFT, size_t hop = DEFAULT_HOP,
float sample_rate = 44100.0f);
~SpectralProcessor(); ~SpectralProcessor();
void setDetectorParams(const std::vector<DetectorBand>& bands); void setDetectorParams(const std::vector<DetectorBand>& bands);
+39
View File
@@ -1182,3 +1182,42 @@ cut. => Naive additive/sens formulas CANNOT model the negative-sens band interac
lives in the structural combine/acc 0x5407c8 (per-bin weights kRTAtt/kRTRel + negative lives in the structural combine/acc 0x5407c8 (per-bin weights kRTAtt/kRTRel + negative
sens entering the level path with opposite sign), which requires the real exp2-domain sens entering the level path with opposite sign), which requires the real exp2-domain
chain, not the bridge. comb stays OPEN (structural combine/acc, P4/P5). chain, not the bridge. comb stays OPEN (structural combine/acc, P4/P5).
## ============ UPDATE 2026-08-21: LUT moved to LEVEL domain (structural chain) ============
### Discovery: mask-domain LUT clamps on loud input
- Mask-domain LUT (committed 9b7e9d3) computes t=(mask_dB-A)/(B-A); for loud input
(tone1k 0dBFS) post-exp2 mask_dB < A=-13.78 => t clamps to 0 => val=0 => FULL cut.
Measured t1k err -24.04 dB. Root cause: exp2(-level) output dB range is unbounded
below, LUT domain [A,B] assumes bounded input.
- FIX: apply FUN_180563a60 power-law to LEVEL (am/res*scale) BEFORE IIR/exp2:
lvl = pow(t, gamma)*MULT, t=(20*log10(lvl)-A)/(B-A) clamp [0,1].
Level dB stays inside [A,B] for both quiet (t1kq: +6.2dB) and loud (t1k: +24dB).
Result: t1k err -24.04 -> +2.45 dB.
### TOOLING HAZARD: stale-binary sweeps
- cmake --build skips recompile when source mtime unchanged within same second;
param sweeps silently re-run the SAME binary (9-point sweep -> 1 unique result).
Earlier "gamma doesn't matter" observation was this artifact, NOT DSP behavior.
- Protocol now: os.utime(src) before build + assert binary mtime fresh (sweep_fresh.py).
### Fresh 2D sweep (g x mult, verified builds), 3-case smoke (render48k 48k/4096):
g=0.344 m=3.8 optimum: t1kq=-0.788 t1k=+2.453 al12=+0.037 mean|err|=1.093
(committed version was mean 8.2). gamma>=0.9 ALL worse (+2..+5): spatial IIR
after LUT redistributes peaks; naive ratio math (need g=1.17 from raw t ratio
1.744->2.0) does NOT survive the IIR chain. Empirics rule.
- MULT=3.8 is a PLACEHOLDER (maps t^gamma back to level domain; no decomp basis yet).
A/B/gamma are decomp-extracted (BandConfig ctx+0x188 refs, NOTES:967).
### Scope clarification
- scripts/corpus.py exercises the BRIDGE path only (44.1k non-internal grid):
full corpus 1.594 mean / comb 10.15 — UNCHANGED by structural work (verified
stash-compare). comb regression is pre-existing bridge state, separate issue.
- Structural chain validated via render48k smoke (3 cases) + module checks
(fn529fe0/exp2/twin/tables/leveltrack/levelpath/fftconv all PASS).
- render48k tail artifact: last BLK=65536 block zero-pads => am decays in final
frames (~last 0.06s of y48). Cosmetic; metric window barely affected.
### Commit: 535b150 (LUT level-domain restructure).
NEXT: step 3 wire combine_acc track into mask output (currently computed,
discarded); step 4 FFT-conv 0x535a70 mask smoothing; then decode BandConfig
ctx+0x188 writers to replace MULT placeholder with decomp value.