diff --git a/pyproject.toml b/pyproject.toml index a10027a..3b36be0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,6 @@ dev = [ "tensorflow==2.20.0", "tensorflow-io==0.37.1", "tensorflow-hub==0.16.1", - "pydub==0.25.1", "librosa==0.11.0" ] test = [ diff --git a/tests/unit/nightingale/test_audio_preprocessor.py b/tests/unit/nightingale/test_audio_preprocessor.py new file mode 100644 index 0000000..2f71958 --- /dev/null +++ b/tests/unit/nightingale/test_audio_preprocessor.py @@ -0,0 +1,57 @@ +import os +from pathlib import Path + +import numpy as np +import soundfile as sf +import pytest + +from nightingale.data_pipeline.audio_preprocessor import AudioPreprocessor + +def _write_test_wav(path: Path, sr: int = 16000, channels: int = 1, duration=0.5): + t = np.linspace(0, duration, int(sr * duration), endpoint=False) + tone = 0.1 * np.sin(2 * np.pi * 440 * t) + if channels == 1: + data = tone.astype(np.float32) + else: + data = np.stack([tone, tone], axis=-1).astype(np.float32) + path.parent.mkdir(parents=True, exist_ok=True) + sf.write(str(path), data, sr) + +def test_is_yamnet_ready_true_for_matching_wav(tmp_path): + ap = AudioPreprocessor(target_sr=16000, mono=True) + p = tmp_path / "mono_16k.wav" + _write_test_wav(p, sr=16000, channels=1) + assert ap.is_yamnet_ready(str(p)) is True + +def test_load_audio_resamples_and_converts_to_mono(tmp_path): + ap = AudioPreprocessor(target_sr=16000, mono=True) + p = tmp_path / "stereo_22k.wav" + _write_test_wav(p, sr=22050, channels=2) + wav, sr = ap.load_audio(str(p)) + assert sr == ap.target_sr + assert wav.dtype == np.float32 + assert wav.ndim == 1 # mono + +def test_process_file_creates_output_and_is_ready(tmp_path): + ap = AudioPreprocessor(target_sr=16000, mono=True) + inp = tmp_path / "in" / "stereo_src.wav" + out = tmp_path / "out" / "converted.wav" + _write_test_wav(inp, sr=22050, channels=2) + # ensure output does not exist initially + assert not out.exists() + ap.process_file(str(inp), str(out)) + assert out.exists() + assert ap.is_yamnet_ready(str(out)) is True + +def test_process_folder_creates_mirrored_structure(tmp_path): + ap = AudioPreprocessor(target_sr=16000, mono=True) + input_root = tmp_path / "input_root" + subfolder = input_root / "sub1" + file_in = subfolder / "sound.wav" + _write_test_wav(file_in, sr=22050, channels=2) + + output_root = tmp_path / "output_root" + ap.process_folder(str(input_root), str(output_root)) + expected_out = output_root / "sub1" / "sound.wav" + assert expected_out.exists() + assert ap.is_yamnet_ready(str(expected_out)) is True \ No newline at end of file diff --git a/uv.lock b/uv.lock index 3536712..53c39a0 100644 --- a/uv.lock +++ b/uv.lock @@ -1807,7 +1807,6 @@ dev = [ { name = "librosa" }, { name = "matplotlib" }, { name = "numpy" }, - { name = "pydub" }, { name = "scikit-learn" }, { name = "seaborn" }, { name = "tensorflow" }, @@ -1840,7 +1839,6 @@ dev = [ { name = "librosa", specifier = "==0.11.0" }, { name = "matplotlib", specifier = "==3.10.6" }, { name = "numpy", specifier = "==2.3.3" }, - { name = "pydub", specifier = "==0.25.1" }, { name = "scikit-learn", specifier = "==1.7.2" }, { name = "seaborn", specifier = "==0.13.2" }, { name = "tensorflow", specifier = "==2.20.0" }, @@ -2334,15 +2332,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/48/f7/925f65d930802e3ea2eb4d5afa4cb8730c8dc0d2cb89a59dc4ed2fcb2d74/pydantic_core-2.41.4-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:c173ddcd86afd2535e2b695217e82191580663a1d1928239f877f5a1649ef39f", size = 2147775, upload-time = "2025-10-14T10:23:45.406Z" }, ] -[[package]] -name = "pydub" -version = "0.25.1" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/fe/9a/e6bca0eed82db26562c73b5076539a4a08d3cffd19c3cc5913a3e61145fd/pydub-0.25.1.tar.gz", hash = "sha256:980a33ce9949cab2a569606b65674d748ecbca4f0796887fd6f46173a7b0d30f", size = 38326, upload-time = "2021-03-10T02:09:54.659Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/a6/53/d78dc063216e62fc55f6b2eebb447f6a4b0a59f55c8406376f76bf959b08/pydub-0.25.1-py2.py3-none-any.whl", hash = "sha256:65617e33033874b59d87db603aa1ed450633288aefead953b30bded59cb599a6", size = 32327, upload-time = "2021-03-10T02:09:53.503Z" }, -] - [[package]] name = "pygments" version = "2.19.2"