32 lines
1.6 KiB
Python
32 lines
1.6 KiB
Python
"""Run with the installed TTS Python to exercise its actual NumPy PCM format."""
|
|
import tempfile,unittest
|
|
from pathlib import Path
|
|
from tts_worker import streaming_wave
|
|
class WorkerPCMTests(unittest.TestCase):
|
|
def setUp(self):
|
|
try:import numpy
|
|
except ImportError:self.skipTest('Run with the installed TTS runtime (NumPy required).')
|
|
self.np=numpy
|
|
def test_pcm_is_published_before_complete_wave_and_clips_correctly(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
np=self.np;root=Path(d)
|
|
class Model:
|
|
def generate_custom_voice_streaming(self,**kwargs):
|
|
assert kwargs['chunk_size']==4
|
|
yield np.array([-2.,0.,2.]),24000,{}
|
|
assert (root/'chunk-000000.pcm').is_file()
|
|
assert not (root/'result.wav').exists()
|
|
yield np.array([.5]),24000,{}
|
|
wave,rate=streaming_wave(Model(),dict(output=d,text='Test',language='German',speaker='Serena'))
|
|
self.assertEqual(rate,24000);self.assertEqual(len(wave),4)
|
|
self.assertEqual(np.frombuffer((root/'chunk-000000.pcm').read_bytes(),dtype='<i2').tolist(),[-32767,0,32767])
|
|
self.assertFalse((root/'chunk.tmp').exists())
|
|
def test_wrong_rate_and_invalid_samples_fail_without_publishing(self):
|
|
for audio,rate in ((self.np.array([.1]),22050),(self.np.array([float('nan')]),24000)):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
class Model:
|
|
def generate_custom_voice_streaming(self,**kwargs):yield audio,rate,{}
|
|
with self.assertRaises(ValueError):streaming_wave(Model(),dict(output=d,text='Test',language='German',speaker='Serena'))
|
|
self.assertFalse(list(Path(d).glob('chunk-*.pcm')))
|
|
if __name__=='__main__':unittest.main()
|