Ψ-W2-T2 remediation: atomic temp+rename collapse write, honest unknown-channel fallback, [verify — DAW] markers, corrected+filed bake-collapse deferral, observable collapse message, quiet-NaN test

This commit is contained in:
2026-08-01 22:08:39 -04:00
parent 4fa3c1dd15
commit 8b191e3379
12 changed files with 151 additions and 31 deletions
+42 -16
View File
@@ -1,5 +1,5 @@
// REAPER-facing offline-render backend (OfflineRenderBackend) plus the shared
// backend helpers (makeUniqueTag / stampCaptureSample).
// backend helpers (makeUniqueTag / collapseCapturedFileToMono / stampCaptureSample).
//
// Includes reaper_plugin_functions.h WITHOUT REAPERAPI_IMPLEMENT — main.cpp is
// the one TU that defines the API pointers; here they are extern.
@@ -31,7 +31,7 @@
#include <vector>
#include "core/capture/capture_paths.h"
#include "core/capture/wav_codec.h" // hashWavContent — the one WAV/RIFF owner
#include "core/capture/wav_codec.h" // hashWavContent / collapseToMono — the one WAV/RIFF owner
#include "core/util/file_bytes.h"
#include "core/capture/render_settings.h"
#include "core/capture/render_window.h" // frameCountFor — the exact-bounds number
@@ -201,29 +201,54 @@ std::string makeUniqueTag(const std::string& prefix) {
std::to_string(++counter);
}
void collapseCapturedFileToMono(const std::string& absolutePath) {
bool collapseCapturedFileToMono(const std::string& absolutePath) {
const std::vector<std::uint8_t> bytes = util::readFileBytes(absolutePath);
if (bytes.empty()) return;
if (bytes.empty()) return false;
const MonoCollapse collapse = collapseToMono(bytes);
if (!collapse.collapsed) return;
if (!collapse.collapsed) return false;
// One truncating write the same shape, and the same accepted mid-write residual,
// as the realtime Auto-tail trim (capture_realtime_finalize.cpp).
std::ofstream out(absolutePath, std::ios::binary | std::ios::trunc);
if (!out) return;
out.write(reinterpret_cast<const char*>(collapse.bytes.data()),
static_cast<std::streamsize>(collapse.bytes.size()));
// Sibling temp + rename, NOT an in-place truncating write: this runs unconditionally
// on the deterministic offline path (which never reopened its render for write before
// this step existed), so a mid-write failure here must not land a truncated file that
// stampCaptureSample then hashes as a false CaptureStatus::Ok. rename() replaces the
// destination in one step, so the original bytes are never destroyed until the
// replacement is known-complete; a failed write or rename leaves the original file
// untouched and self-cleans the temp rather than littering it.
const std::string tempPath = absolutePath + ".moncollapse.tmp";
{
std::ofstream out(tempPath, std::ios::binary | std::ios::trunc);
if (!out) return false;
out.write(reinterpret_cast<const char*>(collapse.bytes.data()),
static_cast<std::streamsize>(collapse.bytes.size()));
const bool wroteOk = static_cast<bool>(out);
out.close();
if (!wroteOk) {
std::error_code ec;
std::filesystem::remove(tempPath, ec);
return false;
}
}
std::error_code ec;
std::filesystem::rename(tempPath, absolutePath, ec);
if (ec) {
std::filesystem::remove(tempPath, ec); // don't leave litter on a failed rename
return false;
}
return true;
}
void stampCaptureSample(Sample& s, const CaptureRequest& req,
ReaProject* rateProj, ReaProject* timeSigProj,
const std::string& absolutePath) {
// Track GUIDs echoed from the request (the caller resolved the selection; the
// backends stay source-agnostic). channelCount starts at the request value only
// as the fallback for an unparseable file — the produced FILE overrides it below.
// backends stay source-agnostic). channelCount starts at 0 (unknown, the same
// sentinel bank_model already uses for a pre-field entry) rather than the
// request's value — the request always asks for 2, so echoing it would claim a
// measurement that never happened for the unparseable-file case below. The
// produced FILE overrides it below whenever it parses.
s.trackGuids = req.trackGuids;
s.channelCount = req.channelCount;
s.channelCount = 0;
// PROJECT_SRATE can read 0 on a project that never pinned a rate — stays 0
// (honest "unknown") rather than a bogus literal.
@@ -473,7 +498,7 @@ CaptureResult OfflineRenderBackend::capture(const CaptureRequest& request) {
// renderer's file rather than one this step had already rewritten. The collapse
// preserves the frame count, so the two are order-independent in outcome — only
// in what each is measuring.
collapseCapturedFileToMono(expectedPath);
const bool collapsedToMono = collapseCapturedFileToMono(expectedPath);
// Record the request's own bounds (exact) rather than re-measuring the file.
Sample s;
@@ -501,7 +526,8 @@ CaptureResult OfflineRenderBackend::capture(const CaptureRequest& request) {
result.message = "Captured [" +
std::to_string(request.startSeconds) + "s, " +
std::to_string(request.endSeconds) + "s] -> " +
paths.relativePath;
paths.relativePath +
(collapsedToMono ? " (collapsed to mono)" : "");
return result;
}