prd.md: project requirements document. render48k L/R stereo baseline
- Add prd.md (293 lines): project overview, repo structure, build system, DSP architecture, env flags, corpus, status, references - render48k: current L/R stereo version, TOTAL 2.835 (requires parameter tuning vs canonical 0.341 VLAW dual-solution)
This commit is contained in:
+53
-88
@@ -1,24 +1,21 @@
|
||||
// render48k.cpp — 48000/N=4096 internal-grid stereo/M/S renderer
|
||||
// render48k.cpp — 48000/N=4096 internal-grid stereo renderer
|
||||
//
|
||||
// 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, stereo)
|
||||
// 1. read input WAV (44100 host samples, stereo or mono)
|
||||
// 2. resample 44100 -> 48000 (libsamplerate, SINC best)
|
||||
// 3. M/S decode: mid = (L+R)/2, side = (L-R)/2
|
||||
// 4. Process mid and side via SpectralProcessor (stereo link=100%: sum for analysis)
|
||||
// 5. Apply balance: mid_reduction *= 1.0, side_reduction *= balance
|
||||
// 6. M/S encode: L' = mid_out + side_out, R' = mid_out - side_out
|
||||
// 7. resample 48000 -> 44100
|
||||
// 8. write 24-bit output WAV (stereo)
|
||||
// 3. Process L and R channels (stereo link=100%: same processing for both)
|
||||
// 4. Apply balance: scale reduction for R channel
|
||||
// 5. Apply mix: wet-dry mix
|
||||
// 6. resample 48000 -> 44100
|
||||
// 7. write 24-bit output WAV (stereo)
|
||||
//
|
||||
// Usage:
|
||||
// render48k <in.wav> <out.wav> mid_fc,mid_q,mid_sens[,scale] side_fc,side_q,side_sens[,scale]
|
||||
// render48k <in.wav> <out.wav> fc,q,sens[,scale] (mono input, dual-mono processing)
|
||||
// render48k <in.wav> <out.wav> fc,q,sens[,scale] ...
|
||||
// Env:
|
||||
// RT_STEREO_LINK=1.0 (1.0 = sum channels for analysis, 0.0 = dual mono)
|
||||
// RT_STEREO_BALANCE=0.284 (side reduction scale, 1.0 = equal, <1.0 = less on side)
|
||||
// RT_STEREO_BALANCE=0.284 (R channel reduction scale, 1.0 = equal, <1.0 = less on R)
|
||||
// RT_DEPTH=0.864 (sens multiplier)
|
||||
// RT_MODE=1.0 (0=soft, 1=hard)
|
||||
// RT_MIX=1.0 (0=dry, 1=full wet)
|
||||
|
||||
#include "spectral.hpp"
|
||||
@@ -55,7 +52,7 @@ static bool load_wav_stereo(const char* path, std::vector<float>& out, int& sr,
|
||||
if (!data) { fclose(f); return false; }
|
||||
int n = data / (ch * (bits / 8));
|
||||
g_in_ch = ch;
|
||||
channels = 2; // always output stereo interleaved
|
||||
channels = 2;
|
||||
out.resize(n * 2);
|
||||
if (bits == 16) {
|
||||
std::vector<short> raw(n * ch);
|
||||
@@ -64,7 +61,7 @@ static bool load_wav_stereo(const char* path, std::vector<float>& out, int& sr,
|
||||
float v = 0.0f;
|
||||
if (ch == 1) {
|
||||
v = raw[i] / 32768.0f;
|
||||
out[2*i] = v; out[2*i+1] = v; // mono -> stereo
|
||||
out[2*i] = v; out[2*i+1] = v;
|
||||
} else {
|
||||
out[2*i] = raw[2*i] / 32768.0f;
|
||||
out[2*i+1] = raw[2*i+1] / 32768.0f;
|
||||
@@ -131,8 +128,8 @@ static std::vector<float> resample_mono(const std::vector<float>& in, int src_sr
|
||||
buf.resize(sd.output_frames_gen);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static std::vector<float> resample_stereo(const std::vector<float>& in, int src_sr, int dst_sr) {
|
||||
// in is stereo interleaved: L0, R0, L1, R1, ...
|
||||
size_t n = in.size() / 2;
|
||||
double frac = (double)dst_sr / src_sr;
|
||||
int out_len = (int)(n * frac) + 16;
|
||||
@@ -149,8 +146,7 @@ static std::vector<float> resample_stereo(const std::vector<float>& in, int src_
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "usage: %s in.wav out.wav mid_fc,mid_q,mid_sens[,scale] side_fc,side_q,side_sens[,scale]\n", argv[0]);
|
||||
fprintf(stderr, " %s in.wav out.wav fc,q,sens[,scale] (mono input, dual-mono)\n", argv[0]);
|
||||
fprintf(stderr, "usage: %s in.wav out.wav fc,q,sens[,scale] ...\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
std::vector<float> x; int sr, channels;
|
||||
@@ -163,111 +159,80 @@ int main(int argc, char** argv) {
|
||||
float mix = getenv("RT_MIX") ? atof(getenv("RT_MIX")) : 1.0f;
|
||||
|
||||
// Parse bands
|
||||
std::vector<DetectorBand> mid_bands, side_bands;
|
||||
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 * depth; b.level_scale = scl;
|
||||
if (mid_bands.empty()) mid_bands.push_back(b);
|
||||
else side_bands.push_back(b);
|
||||
bands.push_back(b);
|
||||
}
|
||||
if (mid_bands.empty()) mid_bands.push_back({1000.0f, 1.0f, 12.0f * depth});
|
||||
if (side_bands.empty()) side_bands = mid_bands;
|
||||
if (bands.empty()) bands.push_back({1000.0f, 1.0f, 12.0f * depth});
|
||||
|
||||
auto x48 = resample_stereo(x, sr, 48000);
|
||||
if (x48.empty()) return 1;
|
||||
|
||||
size_t n = x48.size() / 2; // samples per channel
|
||||
std::vector<float> mid(n), side(n);
|
||||
size_t n = x48.size() / 2;
|
||||
|
||||
// M/S decode (or mono duplication for mono input)
|
||||
if (channels >= 2) {
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
float L = x48[2*i];
|
||||
float R = x48[2*i+1];
|
||||
mid[i] = (L + R) * 0.5f;
|
||||
side[i] = (L - R) * 0.5f;
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
mid[i] = x48[i];
|
||||
side[i] = x48[i];
|
||||
}
|
||||
}
|
||||
// Stereo processing per soothe2 manual:
|
||||
// "With the stereo link at 100%, Soothe will sum the channels for analysis
|
||||
// and apply the same processing to both channels."
|
||||
// Use separate processors for L and R to avoid stateful interference.
|
||||
|
||||
// For stereo link=100%: sum mid+side for analysis
|
||||
std::vector<float> analysis_buf(n);
|
||||
if (stereo_link >= 1.0f) {
|
||||
for (size_t i = 0; i < n; i++) analysis_buf[i] = mid[i] + side[i];
|
||||
}
|
||||
SpectralProcessor procL(4096, 1024, 48000.0f);
|
||||
SpectralProcessor procR(4096, 1024, 48000.0f);
|
||||
procL.setDetectorParams(bands);
|
||||
procR.setDetectorParams(bands);
|
||||
|
||||
// Create processors
|
||||
SpectralProcessor mid_sp(4096, 1024, 48000.0f);
|
||||
SpectralProcessor side_sp(4096, 1024, 48000.0f);
|
||||
mid_sp.setDetectorParams(mid_bands);
|
||||
side_sp.setDetectorParams(side_bands);
|
||||
|
||||
// Process
|
||||
std::vector<float> mid_out(n), side_out(n);
|
||||
std::vector<float> L_out(n), R_out(n);
|
||||
const size_t BLK = 1 << 16;
|
||||
std::vector<float> inb(BLK), outb(BLK);
|
||||
|
||||
// Process L channel
|
||||
for (size_t s = 0; s < n; s += BLK) {
|
||||
size_t blk = std::min(BLK, n - s);
|
||||
memcpy(inb.data(), mid.data() + s, blk * sizeof(float));
|
||||
memcpy(inb.data(), x48.data() + 2*s, blk * sizeof(float));
|
||||
for (size_t i = blk; i < BLK; i++) inb[i] = 0.0f;
|
||||
mid_sp.processBlock(inb.data(), outb.data(), BLK, 1);
|
||||
memcpy(mid_out.data() + s, outb.data(), blk * sizeof(float));
|
||||
procL.processBlock(inb.data(), outb.data(), BLK, 1);
|
||||
for (size_t i = 0; i < blk; i++) L_out[s+i] = outb[i];
|
||||
}
|
||||
|
||||
// Process R channel
|
||||
for (size_t s = 0; s < n; s += BLK) {
|
||||
size_t blk = std::min(BLK, n - s);
|
||||
memcpy(inb.data(), side.data() + s, blk * sizeof(float));
|
||||
memcpy(inb.data(), x48.data() + 2*s + 1, blk * sizeof(float));
|
||||
for (size_t i = blk; i < BLK; i++) inb[i] = 0.0f;
|
||||
side_sp.processBlock(inb.data(), outb.data(), BLK, 1);
|
||||
memcpy(side_out.data() + s, outb.data(), blk * sizeof(float));
|
||||
procR.processBlock(inb.data(), outb.data(), BLK, 1);
|
||||
for (size_t i = 0; i < blk; i++) R_out[s+i] = outb[i];
|
||||
}
|
||||
|
||||
// M/S encode with balance and mix
|
||||
// mask is [0,1] where 1=no change, <1=reduction
|
||||
// processed = input * mask
|
||||
// output = input * (1 - mix) + processed * mix
|
||||
// For side: apply balance to reduction amount
|
||||
std::vector<float> Lout(n), Rout(n);
|
||||
// Apply balance and mix
|
||||
std::vector<float> L_final(n), R_final(n);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
float m_in = mid[i];
|
||||
float s_in = side[i];
|
||||
float m_proc = mid_out[i]; // m_in * mask_mid
|
||||
float s_proc = side_out[i]; // s_in * mask_side
|
||||
|
||||
// Compute mask values (avoid division by zero)
|
||||
float mask_mid = (std::abs(m_in) > 1e-12f) ? m_proc / m_in : 1.0f;
|
||||
float mask_side = (std::abs(s_in) > 1e-12f) ? s_proc / s_in : 1.0f;
|
||||
|
||||
// Apply balance to side: less reduction when balance < 1
|
||||
// balanced_mask = 1 - (1 - mask) * balance
|
||||
float mask_side_bal = 1.0f - (1.0f - mask_side) * stereo_balance;
|
||||
|
||||
// Apply mix: output = input * (1-mix) + processed*mix
|
||||
// For mid: use mask_mid directly
|
||||
float m_out = m_in * (1.0f - mix) + m_in * mask_mid * mix;
|
||||
// For side: use balanced mask
|
||||
float s_out = s_in * (1.0f - mix) + s_in * mask_side_bal * mix;
|
||||
|
||||
// M/S encode
|
||||
Lout[i] = m_out + s_out;
|
||||
Rout[i] = m_out - s_out;
|
||||
float L = x48[2*i];
|
||||
float R = x48[2*i+1];
|
||||
float L_proc = L_out[i];
|
||||
float R_proc = R_out[i];
|
||||
|
||||
float mask_L = (std::abs(L) > 1e-12f) ? L_proc / L : 1.0f;
|
||||
float mask_R = (std::abs(R) > 1e-12f) ? R_proc / R : 1.0f;
|
||||
|
||||
// Apply balance: scale reduction for R channel
|
||||
float mask_R_bal = 1.0f - (1.0f - mask_R) * stereo_balance;
|
||||
|
||||
// Apply mix
|
||||
L_final[i] = L * (1.0f - mix) + L * mask_L * mix;
|
||||
R_final[i] = R * (1.0f - mix) + R * mask_R_bal * mix;
|
||||
}
|
||||
|
||||
auto L44 = resample_mono(Lout, 48000, 44100);
|
||||
auto R44 = resample_mono(Rout, 48000, 44100);
|
||||
auto L44 = resample_mono(L_final, 48000, 44100);
|
||||
auto R44 = resample_mono(R_final, 48000, 44100);
|
||||
size_t out_len = std::min(L44.size(), R44.size());
|
||||
out_len = std::min(out_len, x.size() / std::max(channels, 1));
|
||||
L44.resize(out_len);
|
||||
R44.resize(out_len);
|
||||
save_wav24_stereo(argv[2], L44, R44, 44100);
|
||||
printf("render48k: %zu hostsamps ch=%d -> %zu (48k) -> %zu (out), stereo_balance=%.3f depth=%.3f\n",
|
||||
printf("render48k: %zu hostsamps ch=%d -> %zu (48k) -> %zu (out), balance=%.3f depth=%.3f\n",
|
||||
x.size(), channels, x48.size(), out_len, stereo_balance, depth);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user