From 6302ec01469aa2196d6a4cfb4e56df5b686d4417 Mon Sep 17 00:00:00 2001 From: Sanan507 <227714367+Sanan507@users.noreply.github.com> Date: Fri, 11 Sep 2026 15:40:33 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20useSound=20detune=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced indexOf lookup with a standard index-based loop in playChord to avoid redundant O(N) array scans during high-frequency audio generation. --- frontend/src/hooks/useSound.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frontend/src/hooks/useSound.ts b/frontend/src/hooks/useSound.ts index 3cfcec0..dc6bf64 100644 --- a/frontend/src/hooks/useSound.ts +++ b/frontend/src/hooks/useSound.ts @@ -153,12 +153,13 @@ function playChord( // Exponential decay to simulate vibraphone resonance gainNode.gain.exponentialRampToValueAtTime(0.0001, startTime + durationSec); - for (const note of chord.notes) { + for (let i = 0; i < chord.notes.length; i++) { + const note = chord.notes[i]; const osc = ctx.createOscillator(); osc.type = chord.type; osc.frequency.setValueAtTime(midiToHz(note), startTime); // Slight detune per note for vibraphone width - osc.detune.setValueAtTime((chord.notes.indexOf(note) - 1) * 2, startTime); + osc.detune.setValueAtTime((i - 1) * 2, startTime); osc.connect(gainNode); osc.start(startTime); osc.stop(startTime + durationSec + 0.05);