45 lines
1.9 KiB
Python
45 lines
1.9 KiB
Python
import tempfile
|
|||
|
|
import unittest
|
||
|
|
import wave
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from prepare_validation import write_wav
|
||
|
|
|
||
|
|
|
||
|
|
class PreparationTests(unittest.TestCase):
|
||
|
|
def test_known_padding_does_not_change_source_samples(self):
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
path = Path(directory) / "clip.wav"
|
||
|
|
samples = b"\x01\x00" * 16000
|
||
|
|
write_wav(path, samples, 5000)
|
||
|
|
with wave.open(str(path), "rb") as clip:
|
||
|
|
self.assertEqual(clip.getnframes(), 96000)
|
||
|
|
self.assertEqual(clip.readframes(80000), bytes(160000))
|
||
|
|
self.assertEqual(clip.readframes(16000), samples)
|
||
|
|
|
||
|
|
def test_full_source_over_budget_is_rejected(self):
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
with self.assertRaises(ValueError):
|
||
|
|
write_wav(Path(directory) / "clip.wav", bytes(960000), 5000)
|
||
|
|
|
||
|
|
def test_existing_preparation_cannot_be_overwritten(self):
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
path = Path(directory) / "clip.wav"
|
||
|
|
path.write_bytes(b"original")
|
||
|
|
with self.assertRaises(ValueError):
|
||
|
|
write_wav(path, bytes(32000))
|
||
|
|
self.assertEqual(path.read_bytes(), b"original")
|
||
|
|
|
||
|
|
def test_odd_pcm_is_rejected(self):
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
with self.assertRaises(ValueError):
|
||
|
|
write_wav(Path(directory) / "clip.wav", b"\x00")
|
||
|
|
|
||
|
|
def test_explicit_seam_budget_is_bounded(self):
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
path = Path(directory) / "seam.wav"
|
||
|
|
write_wav(path, bytes(32000 * 35), maximum_ms=45000)
|
||
|
|
with wave.open(str(path), "rb") as clip:
|
||
|
|
self.assertEqual(clip.getnframes(), 16000 * 35)
|
||
|
|
with self.assertRaises(ValueError):
|
||
|
|
write_wav(Path(directory) / "oversized.wav", bytes(32000), maximum_ms=45001)
|