main.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from audio import combine_two_files
  2. from files import get_txt_files
  3. from models import SpeakerModel, Device, SampleRate
  4. from text import get_separated_text_from_file
  5. tts = SpeakerModel()
  6. # Can provide language, device, sample rate and model id
  7. # Default 'ru' 'cpu' 48000 v3_1_ru
  8. tts.specify_model()
  9. info = tts.get_model_info()
  10. files = get_txt_files()
  11. output_audio_filename = input("Type output file name: ") or "audio"
  12. speaker = input(f"Select voice. Type one of: {'/'.join(info.speakers)}: ") or info.speakers[0]
  13. print(f"Selected voice - {speaker}")
  14. for i, file in enumerate(files):
  15. try:
  16. paragraphs = get_separated_text_from_file(file)
  17. except Exception as e:
  18. print("Aborting program cause file can't be opened")
  19. exit()
  20. if len(files) == 1 and output_audio_filename:
  21. audio_filename = f"files/{output_audio_filename}-{speaker}.wav"
  22. else:
  23. audio_filename = f"files/{''.join(file.split('/')[-1].split('.')[:-1])}-{speaker}-{output_audio_filename}.wav"
  24. for j, p in enumerate(paragraphs):
  25. # push("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  26. print("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  27. try:
  28. tts.text2speech(p, "files/temp.wav", speaker=speaker)
  29. except Exception as e:
  30. print(e)
  31. combine_two_files([audio_filename, "files/temp.wav"], audio_filename)
  32. print("Process was finished")