From 261b6046838f829c27607c1cec3fc85016233f0c Mon Sep 17 00:00:00 2001 From: Josef Haupt Date: Tue, 1 Sep 2026 14:40:37 +0200 Subject: [PATCH] Replace reampy with soxr --- birdnet_analyzer/audio.py | 4 ++-- tests/test_audio.py | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/test_audio.py diff --git a/birdnet_analyzer/audio.py b/birdnet_analyzer/audio.py index ea2e338d9..b5eb1cf88 100644 --- a/birdnet_analyzer/audio.py +++ b/birdnet_analyzer/audio.py @@ -48,7 +48,7 @@ def open_audio_file( offset=offset, duration=duration, mono=True, - res_type="kaiser_fast", + res_type="soxr_hq", ) else: @@ -61,7 +61,7 @@ def open_audio_file( sig, orig_sr=int(rate * speed), target_sr=sample_rate, - res_type="kaiser_fast", + res_type="soxr_hq", ) rate = sample_rate diff --git a/tests/test_audio.py b/tests/test_audio.py new file mode 100644 index 000000000..0d325a786 --- /dev/null +++ b/tests/test_audio.py @@ -0,0 +1,41 @@ +import numpy as np +import pytest +import soundfile as sf + +from birdnet_analyzer import audio + + +def _write_sine_wav(path, sample_rate, duration=1.0, freq=440.0): + t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) + sf.write(path, np.sin(2 * np.pi * freq * t).astype(np.float32), sample_rate) + + +def test_open_audio_file_resamples_non_native_rate(tmp_path): + path = str(tmp_path / "44k.wav") + _write_sine_wav(path, 44100) + + sig, rate = audio.open_audio_file(path) + + assert rate == 48000 + assert len(sig) == pytest.approx(48000, abs=2) + + +@pytest.mark.parametrize("speed", [0.5, 2.0]) +def test_open_audio_file_speed_scales_duration(tmp_path, speed): + path = str(tmp_path / "48k.wav") + _write_sine_wav(path, 48000) + + sig, rate = audio.open_audio_file(path, speed=speed) + + assert rate == 48000 + assert len(sig) == pytest.approx(48000 / speed, abs=2) + + +def test_open_audio_file_speed_with_non_native_rate(tmp_path): + path = str(tmp_path / "44k.wav") + _write_sine_wav(path, 44100) + + sig, rate = audio.open_audio_file(path, speed=2.0) + + assert rate == 48000 + assert len(sig) == pytest.approx(24000, abs=2)