diff --git a/src/audio/IRLoader.cpp b/src/audio/IRLoader.cpp index 1c8d8b4..5aecf85 100644 --- a/src/audio/IRLoader.cpp +++ b/src/audio/IRLoader.cpp @@ -94,8 +94,11 @@ void IRLoader::setStateInformation(const void* data, int sizeInBytes) if (auto* obj = parsed.getDynamicObject()) { + // Same as NAMProcessor::setStateInformation: the preset already loaded + // this slot's IR by path, so only reload when the state names another. auto path = obj->getProperty("irPath").toString(); - if (path.isNotEmpty()) + if (path.isNotEmpty() + && ! (irLoaded.load() && juce::File(path) == juce::File(currentIRPath))) loadIR(juce::File(path)); if (obj->hasProperty("mix")) diff --git a/src/audio/NAMProcessor.cpp b/src/audio/NAMProcessor.cpp index 9ba8657..f7b555c 100644 --- a/src/audio/NAMProcessor.cpp +++ b/src/audio/NAMProcessor.cpp @@ -73,7 +73,16 @@ void NAMProcessor::releaseResources() void NAMProcessor::processBlock(juce::AudioBuffer& buffer, juce::MidiBuffer&) { #if SLOPSMITH_NAM_SUPPORT - const juce::ScopedLock sl(modelLock); + // Never block the audio thread on a model swap: a control thread holding + // modelLock (loadModel's swap, prepareToPlay's Reset + prewarm) would stall + // the whole device callback — guitar, song bus and all. Skip the block + // (silence) instead; a swap only ever holds the lock briefly. + const juce::ScopedTryLock sl(modelLock); + if (!sl.isLocked()) + { + buffer.clear(); + return; + } if (!model) return; @@ -150,8 +159,15 @@ void NAMProcessor::setStateInformation(const void* data, int sizeInBytes) if (auto* obj = parsed.getDynamicObject()) { + // A preset restores each NAM slot by path FIRST (LoadPreset), then + // applies this state — whose modelPath is normally that same file. + // Reloading it re-parsed and re-prewarmed the model a second time while + // the slot was already live in the chain, and the audio callback froze + // for ~0.75 s on every tone switch (measured on ASIO: the renderer bus + // backed up to ~37k frames, then dropped). Only load a DIFFERENT model. auto path = obj->getProperty("modelPath").toString(); - if (path.isNotEmpty()) + if (path.isNotEmpty() + && ! (modelLoaded.load() && juce::File(path) == juce::File(currentModelPath))) loadModel(juce::File(path)); if (obj->hasProperty("inputLevel")) diff --git a/src/audio/NoteVerifier.cpp b/src/audio/NoteVerifier.cpp index f79bdb5..a871087 100644 --- a/src/audio/NoteVerifier.cpp +++ b/src/audio/NoteVerifier.cpp @@ -377,6 +377,7 @@ void NoteVerifier::run() st.snr = st.bestSnr; double when = cn.t; + bool timed = false; if (! cn.ho && ! cn.po) { int best = -1; @@ -391,6 +392,7 @@ void NoteVerifier::run() { onsetLog[(size_t) best].claimed = true; when = onsetLog[(size_t) best].songTime; + timed = true; } } st.detectedSongTime = when; @@ -399,6 +401,7 @@ void NoteVerifier::run() v.id = cn.id; v.detected = true; v.detectedSongTime = when; + v.timed = timed; v.centsError = st.bestCents; v.snr = st.bestSnr; pending.push_back(v); diff --git a/src/audio/NoteVerifier.h b/src/audio/NoteVerifier.h index 421bc6b..f3c07be 100644 --- a/src/audio/NoteVerifier.h +++ b/src/audio/NoteVerifier.h @@ -87,6 +87,12 @@ class NoteVerifier std::string id; bool detected = false; double detectedSongTime = 0.0; // playhead at which the note was scored + // True only when detectedSongTime came from a real pick attack (an + // onset claimed from the onset log). A hit with no matching attack + // (legato, or an attack the flux detector missed: palm mutes, fast + // runs) is stamped at the chart time — "on time" by construction, not + // by measurement — so timing consumers (stats, Sync Lab) must skip it. + bool timed = false; float centsError = 0.0f; float snr = 0.0f; }; diff --git a/src/audio/addon/DeviceBindings.cpp b/src/audio/addon/DeviceBindings.cpp index 63f6656..827fb01 100644 --- a/src/audio/addon/DeviceBindings.cpp +++ b/src/audio/addon/DeviceBindings.cpp @@ -642,6 +642,7 @@ Napi::Value GetSourceNoteVerdicts(const Napi::CallbackInfo& info) entry.Set("id", v.id); entry.Set("detected", v.detected); entry.Set("detectedSongTime", v.detectedSongTime); + entry.Set("timed", v.timed); entry.Set("centsError", v.centsError); entry.Set("snr", v.snr); arr.Set((uint32_t) i, entry); @@ -812,7 +813,8 @@ Napi::Value SetSourceChart(const Napi::CallbackInfo& info) } // Drain the verdicts the NoteVerifier thread has finalized since the last -// call. Returns an array of { id, detected, detectedSongTime, centsError, snr }. +// call. Returns an array of { id, detected, detectedSongTime, timed, centsError, snr } +// (timed: detectedSongTime is a measured pick attack, not the chart time). // // Optionally also pushes the renderer's playhead: getNoteVerdicts(songTime, // playing). The plugin calls this once per detect tick, so folding the push in @@ -846,6 +848,7 @@ Napi::Value GetNoteVerdicts(const Napi::CallbackInfo& info) entry.Set("id", v.id); entry.Set("detected", v.detected); entry.Set("detectedSongTime", v.detectedSongTime); + entry.Set("timed", v.timed); entry.Set("centsError", v.centsError); entry.Set("snr", v.snr); arr.Set((uint32_t) i, entry); diff --git a/tests/tone-load-no-stall.test.js b/tests/tone-load-no-stall.test.js new file mode 100644 index 0000000..31e907c --- /dev/null +++ b/tests/tone-load-no-stall.test.js @@ -0,0 +1,51 @@ +// A tone switch froze the whole audio callback for ~0.75 s (measured on ASIO: the +// renderer bus backed up to ~37k frames, then dropped — heard as a hard glitch and a +// sync jump). LoadPreset loads each NAM/IR slot by path, then applies the slot's saved +// state, which re-loaded the SAME model a second time while the slot was live. These +// pin the fix (skip a same-file reload) and the audio-thread hardening (never block on +// the NAM model lock), plus the verdict `timed` flag timing consumers rely on. + +'use strict'; + +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); + +const read = (p) => fs.readFileSync(path.join(__dirname, '..', p), 'utf8').replace(/\r\n/g, '\n'); + +function fnBody(src, signature) { + const start = src.indexOf(signature); + assert.ok(start !== -1, `${signature} not found`); + let i = src.indexOf('{', start + signature.length) + 1, depth = 1; + while (i < src.length && depth > 0) { if (src[i] === '{') depth++; else if (src[i] === '}') depth--; i++; } + return src.slice(start, i); +} + +test('NAM state restore skips reloading the model that is already loaded', () => { + const body = fnBody(read('src/audio/NAMProcessor.cpp'), 'void NAMProcessor::setStateInformation('); + assert.match(body, /modelLoaded\.load\(\) && juce::File\(path\) == juce::File\(currentModelPath\)/); + assert.match(body, /!\s*\(modelLoaded[\s\S]*?\)\)\s*loadModel\(/); +}); + +test('IR state restore skips reloading the IR that is already loaded', () => { + const body = fnBody(read('src/audio/IRLoader.cpp'), 'void IRLoader::setStateInformation('); + assert.match(body, /irLoaded\.load\(\) && juce::File\(path\) == juce::File\(currentIRPath\)/); +}); + +test('the NAM audio path never blocks on the model lock', () => { + const body = fnBody(read('src/audio/NAMProcessor.cpp'), 'void NAMProcessor::processBlock('); + assert.match(body, /ScopedTryLock sl\(modelLock\)/); + assert.doesNotMatch(body, /const juce::ScopedLock sl\(modelLock\)/); + assert.match(body, /if \(!sl\.isLocked\(\)\)\s*\{\s*buffer\.clear\(\);\s*return;/); +}); + +test('verdicts carry `timed`, true only when a pick attack was claimed', () => { + assert.match(read('src/audio/NoteVerifier.h'), /bool timed = false;/); + const cpp = read('src/audio/NoteVerifier.cpp'); + assert.match(cpp, /when = onsetLog\[\(size_t\) best\]\.songTime;\s*timed = true;/); + assert.match(cpp, /v\.timed = timed;/); + const bindings = read('src/audio/addon/DeviceBindings.cpp'); + assert.equal((bindings.match(/entry\.Set\("timed", v\.timed\);/g) || []).length, 2, + 'both getNoteVerdicts and getSourceNoteVerdicts expose it'); +});