test_voice_chunker.py 1.7 KB
import tempfile
import unittest
from pathlib import Path

import test_bootstrap

import numpy as np
import soundfile as sf

from src.data.voice_chunker import detect_voiced_intervals, chunk_intervals, voice_to_chunks


class VoiceChunkerTests(unittest.TestCase):
    def test_detect_voiced_intervals_filters_short_segments(self):
        sr = 16000
        y = np.concatenate([
            np.zeros(sr),
            0.2 * np.sin(2 * np.pi * 440 * np.linspace(0, 3, sr * 3, endpoint=False)),
            np.zeros(sr // 2),
        ]).astype(np.float32)
        intervals = detect_voiced_intervals(y, sr=sr, top_db=30, min_voiced_sec=2.0)
        self.assertEqual(len(intervals), 1)

    def test_chunk_intervals_handles_short_and_long_regions(self):
        sr = 16000
        chunks = chunk_intervals([(0, sr * 3), (sr * 5, sr * 15)], sr=sr, target_chunk_sec=8.0, stride_sec=4.0)
        self.assertTrue(any(padded for _, _, padded in chunks))
        self.assertGreaterEqual(len(chunks), 2)

    def test_voice_to_chunks_writes_chunk_files(self):
        sr = 16000
        with tempfile.TemporaryDirectory() as tmp:
            src = Path(tmp) / 'hum.wav'
            out = Path(tmp) / 'chunks'
            y = np.concatenate([
                np.zeros(sr),
                0.2 * np.sin(2 * np.pi * 330 * np.linspace(0, 4, sr * 4, endpoint=False)),
                np.zeros(sr),
            ]).astype(np.float32)
            sf.write(src, y, sr)
            chunks = voice_to_chunks(str(src), str(out), target_chunk_sec=3.0, stride_sec=2.0, min_voiced_sec=2.0, sr=sr)
            self.assertGreaterEqual(len(chunks), 1)
            self.assertTrue(Path(chunks[0]['audio_path']).exists())


if __name__ == '__main__':
    unittest.main()