// 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 or mono) // 2. resample 44100 -> 48000 (libsamplerate, SINC best) // 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 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 (R channel reduction scale, 1.0 = equal, <1.0 = less on R) // RT_DEPTH=0.864 (sens multiplier) // RT_MIX=1.0 (0=dry, 1=full wet) #include "spectral.hpp" #include #include #include #include #include #include static int g_in_ch = 1; static bool load_wav_stereo(const char* path, std::vector& out, int& sr, int& channels) { 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 = 0; int64_t pos = 12; fseek(f, 12, SEEK_SET); while (pos < 32 * 1024 * 1024) { char cid[4]; int csize; if (fread(cid, 1, 4, f) < 4 || fread(&csize, 4, 1, f) < 1) break; pos += 8; if (memcmp(cid, "data", 4) == 0) { data = csize; break; } pos += csize; int skip = csize; if (csize % 2) skip++; fseek(f, skip, SEEK_CUR); } if (!data) { fclose(f); return false; } int n = data / (ch * (bits / 8)); g_in_ch = ch; channels = 2; out.resize(n * 2); if (bits == 16) { std::vector raw(n * ch); fread(raw.data(), 2, n * ch, f); for (int i = 0; i < n; i++) { float v = 0.0f; if (ch == 1) { v = raw[i] / 32768.0f; 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; } } } else if (bits == 24) { std::vector raw(n * ch * 3); fread(raw.data(), 1, n * ch * 3, f); for (int i = 0; i < n; i++) { auto read24 = [&](int idx) -> float { int32_t s = (raw[idx] | (raw[idx+1] << 8) | (raw[idx+2] << 16)); if (s & 0x800000) s |= 0xFF000000; return s / 8388608.0f; }; if (ch == 1) { float v = read24(i * 3); out[2*i] = v; out[2*i+1] = v; } else { out[2*i] = read24(2*i * 3); out[2*i+1] = read24((2*i+1) * 3); } } } else return false; fclose(f); return true; } static bool save_wav24_stereo(const char* path, const std::vector& L, const std::vector& R, int sr) { FILE* f = fopen(path, "wb"); if (!f) return false; int ch = 2, bits = 24; size_t n = std::min(L.size(), R.size()); int data = (int)(n * ch * (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 < n; i++) { int32_t vl = (int32_t)(std::max(-1.0f, std::min(1.0f, L[i])) * 8388607.0f); int32_t vr = (int32_t)(std::max(-1.0f, std::min(1.0f, R[i])) * 8388607.0f); unsigned char b0 = vl & 0xFF, b1 = (vl >> 8) & 0xFF, b2 = (vl >> 16) & 0xFF; fwrite(&b0, 1, 1, f); fwrite(&b1, 1, 1, f); fwrite(&b2, 1, 1, f); b0 = vr & 0xFF; b1 = (vr >> 8) & 0xFF; b2 = (vr >> 16) & 0xFF; fwrite(&b0, 1, 1, f); fwrite(&b1, 1, 1, f); fwrite(&b2, 1, 1, f); } fclose(f); return true; } static std::vector resample_mono(const std::vector& in, int src_sr, int dst_sr) { double frac = (double)dst_sr / src_sr; int out_len = (int)(in.size() * frac) + 16; std::vector 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; } static std::vector resample_stereo(const std::vector& in, int src_sr, int dst_sr) { size_t n = in.size() / 2; double frac = (double)dst_sr / src_sr; int out_len = (int)(n * frac) + 16; std::vector buf(out_len * 2); SRC_DATA sd; sd.data_in = in.data(); sd.input_frames = (long)n; 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, 2); if (err != 0) { fprintf(stderr, "resample err %d\n", err); return {}; } buf.resize(sd.output_frames_gen * 2); 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 x; int sr, channels; if (!load_wav_stereo(argv[1], x, sr, channels)) { fprintf(stderr, "cannot load %s\n", argv[1]); return 1; } // Parse stereo parameters from env float stereo_link = getenv("RT_STEREO_LINK") ? atof(getenv("RT_STEREO_LINK")) : 1.0f; float stereo_balance = getenv("RT_STEREO_BALANCE") ? atof(getenv("RT_STEREO_BALANCE")) : 0.284f; float depth = getenv("RT_DEPTH") ? atof(getenv("RT_DEPTH")) : 1.0f; float mix = getenv("RT_MIX") ? atof(getenv("RT_MIX")) : 1.0f; // Parse bands std::vector 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; bands.push_back(b); } 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; // 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. SpectralProcessor procL(4096, 1024, 48000.0f); SpectralProcessor procR(4096, 1024, 48000.0f); procL.setDetectorParams(bands); procR.setDetectorParams(bands); std::vector L_out(n), R_out(n); const size_t BLK = 1 << 16; std::vector 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(), x48.data() + 2*s, blk * sizeof(float)); for (size_t i = blk; i < BLK; i++) inb[i] = 0.0f; 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(), x48.data() + 2*s + 1, blk * sizeof(float)); for (size_t i = blk; i < BLK; i++) inb[i] = 0.0f; procR.processBlock(inb.data(), outb.data(), BLK, 1); for (size_t i = 0; i < blk; i++) R_out[s+i] = outb[i]; } // Apply balance and mix std::vector L_final(n), R_final(n); for (size_t i = 0; i < n; i++) { 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(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), balance=%.3f depth=%.3f\n", x.size(), channels, x48.size(), out_len, stereo_balance, depth); return 0; }