This commit is contained in:
Claudio Santini 2025-01-31 14:13:37 +01:00
parent 37dd30933a
commit 0ae54a734f
2 changed files with 14 additions and 7 deletions

View file

@ -27,7 +27,7 @@ from voices import voices, available_voices_str
sample_rate = 24000
def main(pipeline, file_path, voice, pick_manually, speed):
def main(file_path, voice, pick_manually, speed):
filename = Path(file_path).name
warnings.simplefilter("ignore")
book = epub.read_epub(file_path)
@ -78,7 +78,7 @@ def main(pipeline, file_path, voice, pick_manually, speed):
text = intro + '.\n\n' + text
start_time = time.time()
audio_segments = gen_audio_segments(pipeline, text, voice, speed)
audio_segments = gen_audio_segments(text, voice, speed)
if audio_segments:
final_audio = np.concatenate(audio_segments)
soundfile.write(chapter_filename, final_audio, sample_rate)
@ -100,7 +100,8 @@ def main(pipeline, file_path, voice, pick_manually, speed):
create_m4b(chapter_mp3_files, filename, cover_image)
def gen_audio_segments(pipeline, text, voice, speed):
def gen_audio_segments(text, voice, speed):
pipeline = KPipeline(lang_code=voice[0]) # a for american or b for british etc.
audio_segments = []
for gs, ps, audio in pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+'):
audio_segments.append(audio)
@ -250,8 +251,7 @@ def cli_main():
else:
print('CUDA GPU not available. Defaulting to CPU')
pipeline = KPipeline(lang_code=args.voice[0]) # a for american or b for british
main(pipeline, args.epub_file_path, args.voice, args.pick, args.speed)
main(args.epub_file_path, args.voice, args.pick, args.speed)
if __name__ == '__main__':

View file

@ -10,8 +10,9 @@ from audiblez import main
class MainTest(unittest.TestCase):
def base(self, file_path, **kwargs):
os.system('rm mini_chapter_*.wav')
pipeline = KPipeline(lang_code='a')
main(pipeline, file_path=file_path, voice='af_sky', pick_manually=False, speed=1, **kwargs)
merged_args = dict(voice='af_sky', pick_manually=False, speed=1.0)
merged_args.update(kwargs)
main(file_path, **merged_args)
def test_1_allan_poe(self):
Path('poe.m4b').unlink(missing_ok=True)
@ -33,3 +34,9 @@ class MainTest(unittest.TestCase):
for i in range(8):
self.assertTrue(Path(f'orwell_chapter_{i}.wav').exists())
self.assertTrue(Path(f'orwell_chapter_{i}.wav').stat().st_size > 300 * 1024, 'file should be larger than 300KB, surely failed')
def test_0_manzoni(self):
Path('manzoni.m4b').unlink(missing_ok=True)
os.system('rm manzoni_chapter_*.wav')
self.base(file_path='../epub/manzoni.epub', voice='im_nicola')
self.assertTrue(Path('manzoni.m4b').exists())