Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/audio/IRLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
20 changes: 18 additions & 2 deletions src/audio/NAMProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ void NAMProcessor::releaseResources()
void NAMProcessor::processBlock(juce::AudioBuffer<float>& 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;

Expand Down Expand Up @@ -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"))
Expand Down
3 changes: 3 additions & 0 deletions src/audio/NoteVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -391,6 +392,7 @@ void NoteVerifier::run()
{
onsetLog[(size_t) best].claimed = true;
when = onsetLog[(size_t) best].songTime;
timed = true;
}
}
st.detectedSongTime = when;
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/audio/NoteVerifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
5 changes: 4 additions & 1 deletion src/audio/addon/DeviceBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions tests/tone-load-no-stall.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
Loading