From 909d04f973d123a891e6879cb57c536ef0f69ef7 Mon Sep 17 00:00:00 2001 From: skal Date: Wed, 18 Feb 2026 21:19:20 +0100 Subject: refactor(mq_editor): extract refreshPartialsView() and playOriginal() helpers - refreshPartialsView(selectIdx) consolidates editor+viewer sync (5 call sites) - playOriginal() replaces inline play button logic; Digit2 calls it directly - autoSpreadAll() now called after extractPartials via named function handoff(Claude): factored common button actions in app.js --- tools/mq_editor/app.js | 59 +++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) (limited to 'tools') diff --git a/tools/mq_editor/app.js b/tools/mq_editor/app.js index bd9a2b3..9b05789 100644 --- a/tools/mq_editor/app.js +++ b/tools/mq_editor/app.js @@ -87,14 +87,18 @@ function pushUndo() { _updateUndoRedoBtns(); } -function _applySnapshot(snap) { - extractedPartials = snap; - editor.setPartials(snap); +function refreshPartialsView(selectIdx = -1) { + editor.setPartials(extractedPartials); if (viewer) { - viewer.setPartials(snap); - viewer.setKeepCount(snap.length > 0 ? getKeepCount() : 0); - viewer.selectPartial(-1); + viewer.setPartials(extractedPartials); + viewer.setKeepCount(extractedPartials && extractedPartials.length > 0 ? getKeepCount() : 0); + viewer.selectPartial(selectIdx); } +} + +function _applySnapshot(snap) { + extractedPartials = snap; + refreshPartialsView(); _updateUndoRedoBtns(); } @@ -217,10 +221,7 @@ function loadAudioBuffer(buffer, label) { const {spread_above, spread_below} = autodetectSpread(partial, stftCache, fftSize, audioBuffer.sampleRate); partial.replicas = { ...partial.replicas, spread_above, spread_below }; extractedPartials.unshift(partial); - editor.setPartials(extractedPartials); - viewer.setPartials(extractedPartials); - viewer.setKeepCount(getKeepCount()); - viewer.selectPartial(0); + refreshPartialsView(0); setStatus(`${exploreMode}: added partial (${extractedPartials.length} total)`, 'info'); }; }, 10); @@ -292,12 +293,10 @@ function runExtraction() { }); extractedPartials = result.partials; - editor.setPartials(result.partials); + autoSpreadAll(); viewer.setFrames(result.frames); setStatus(`Extracted ${result.partials.length} partials`, 'info'); - viewer.setPartials(result.partials); - viewer.setKeepCount(getKeepCount()); - viewer.selectPartial(-1); + refreshPartialsView(); } catch (err) { setStatus('Extraction error: ' + err.message, 'error'); @@ -334,24 +333,14 @@ function createNewPartial() { replicas: { decay_alpha: 0.1, jitter: 0.05, spread_above: 0.02, spread_below: 0.02 }, }; extractedPartials.unshift(newPartial); - editor.setPartials(extractedPartials); - if (viewer) { - viewer.setPartials(extractedPartials); - viewer.setKeepCount(getKeepCount()); - viewer.selectPartial(0); - } + refreshPartialsView(0); } function clearAllPartials() { if (!extractedPartials || extractedPartials.length === 0) return; pushUndo(); extractedPartials = []; - editor.setPartials([]); - if (viewer) { - viewer.setPartials([]); - viewer.setKeepCount(0); - viewer.selectPartial(-1); - } + refreshPartialsView(); } document.getElementById('newPartialBtn').addEventListener('click', createNewPartial); @@ -361,7 +350,7 @@ document.getElementById('contourBtn').addEventListener('click', () => setExplore document.getElementById('undoBtn').addEventListener('click', undo); document.getElementById('redoBtn').addEventListener('click', redo); -autoSpreadAllBtn.addEventListener('click', () => { +function autoSpreadAll() { if (!extractedPartials || !stftCache) return; const fs = stftCache.fftSize; const sr = audioBuffer.sampleRate; @@ -376,7 +365,9 @@ autoSpreadAllBtn.addEventListener('click', () => { const sel = viewer ? viewer.selectedPartial : -1; if (sel >= 0) editor.onPartialSelect(sel); setStatus(`Auto-spread applied to ${extractedPartials.length} partials`, 'info'); -}); +} + +autoSpreadAllBtn.addEventListener('click', autoSpreadAll); threshold.addEventListener('change', () => { if (stftCache) runExtraction(); @@ -425,12 +416,14 @@ function stopAudio() { setStatus('Stopped', 'info'); } -// Play audio -playBtn.addEventListener('click', () => { +function playOriginal() { if (!audioBuffer || !audioContext) return; stopAudio(); playAudioBuffer(audioBuffer, 'Playing...'); -}); +} + +// Play audio +playBtn.addEventListener('click', playOriginal); // Stop audio stopBtn.addEventListener('click', () => { @@ -522,9 +515,7 @@ document.addEventListener('keydown', (e) => { playSynthesized(); } else if (e.code === 'Digit2') { e.preventDefault(); - if (!playBtn.disabled) { - playBtn.click(); - } + if (!playBtn.disabled) playOriginal(); } else if (e.code === 'Digit3') { e.preventDefault(); const sel = viewer ? viewer.selectedPartial : -1; -- cgit v1.2.3