refactor: cap waveform decode length + guard frombuffer + tighten test

This commit is contained in:
2026-07-02 15:00:34 +02:00
parent 12eaa944a7
commit 7501c4729b
3 changed files with 11 additions and 2 deletions
+3 -1
View File
@@ -32,4 +32,6 @@ def load_region_samples(path: str, start: float, duration: float,
return np.zeros(0, dtype="float32")
if proc.returncode != 0 or not proc.stdout:
return np.zeros(0, dtype="float32")
return np.frombuffer(proc.stdout, dtype="float32")
buf = proc.stdout
buf = buf[: len(buf) - (len(buf) % 4)]
return np.frombuffer(buf, dtype="float32")
+6 -1
View File
@@ -6511,12 +6511,17 @@ class MainWindow(QMainWindow):
from core.waveform import load_region_samples, peaks
start = self._cursor
dur = self._spn_audio_len.value()
preview = min(dur, 120.0) # cap decode; the strip is 128 bars regardless
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
try:
samples = load_region_samples(self._file_path, start, dur)
samples = load_region_samples(self._file_path, start, preview)
finally:
QApplication.restoreOverrideCursor()
self._wave.set_peaks(peaks(samples))
if samples.size == 0:
self._show_status("Waveform: no audio decoded", 3000)
elif preview < dur:
self._show_status(f"Waveform shows first {preview:.0f}s of {dur:.0f}s", 4000)
def _on_extract_audio(self) -> None:
"""Extract an exact-length audio slice starting at the playhead and
+2
View File
@@ -551,6 +551,8 @@ def test_peaks_downsamples_to_bucket_count():
p = peaks(samples, buckets=64)
assert len(p) == 64
assert all(0.0 <= v <= 1.0 for v in p)
import pytest
assert max(p) == pytest.approx(1.0)
def test_peaks_empty_returns_zeros():