From d9bd8246c9364f1332998de7c541120928059514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Thu, 20 Nov 2025 11:24:31 +0100 Subject: [PATCH] fix: prevent infinite loop in waveform rendering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add safety check for duration === 0 (first track scenario) - Ensure trackWidth doesn't exceed canvas width - Use Math.floor for integer loop iterations - Prevent division by zero in samplesPerPixel calculation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- components/tracks/Track.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/tracks/Track.tsx b/components/tracks/Track.tsx index 77011d6..5d56bea 100644 --- a/components/tracks/Track.tsx +++ b/components/tracks/Track.tsx @@ -341,16 +341,17 @@ export function Track({ } // Calculate how much of the canvas width this track's duration occupies - const trackDurationRatio = buffer.duration / duration; - const trackWidth = width * trackDurationRatio; - const samplesPerPixel = buffer.length / trackWidth; + // If duration is 0 or invalid, use full width (first track scenario) + const trackDurationRatio = duration > 0 ? buffer.duration / duration : 1; + const trackWidth = Math.min(width * trackDurationRatio, width); + const samplesPerPixel = trackWidth > 0 ? buffer.length / trackWidth : 0; // Draw waveform ctx.fillStyle = track.color; ctx.strokeStyle = track.color; ctx.lineWidth = 1; - for (let x = 0; x < trackWidth; x++) { + for (let x = 0; x < Math.floor(trackWidth); x++) { const startSample = Math.floor(x * samplesPerPixel); const endSample = Math.floor((x + 1) * samplesPerPixel);