fix: prevent infinite loop in waveform rendering

- 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 <noreply@anthropic.com>
This commit is contained in:
2025-11-20 11:24:31 +01:00
parent dd8d46795a
commit d9bd8246c9

View File

@@ -341,16 +341,17 @@ export function Track({
} }
// Calculate how much of the canvas width this track's duration occupies // Calculate how much of the canvas width this track's duration occupies
const trackDurationRatio = buffer.duration / duration; // If duration is 0 or invalid, use full width (first track scenario)
const trackWidth = width * trackDurationRatio; const trackDurationRatio = duration > 0 ? buffer.duration / duration : 1;
const samplesPerPixel = buffer.length / trackWidth; const trackWidth = Math.min(width * trackDurationRatio, width);
const samplesPerPixel = trackWidth > 0 ? buffer.length / trackWidth : 0;
// Draw waveform // Draw waveform
ctx.fillStyle = track.color; ctx.fillStyle = track.color;
ctx.strokeStyle = track.color; ctx.strokeStyle = track.color;
ctx.lineWidth = 1; 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 startSample = Math.floor(x * samplesPerPixel);
const endSample = Math.floor((x + 1) * samplesPerPixel); const endSample = Math.floor((x + 1) * samplesPerPixel);