main.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. if not files:
  12. exit("WARNING! There is no txt files in folder 'files'. Please create \
  13. one and run again")
  14. output_audio_filename = input("Type output file name: ") or "audio"
  15. while True:
  16. speaker = input(f"Select voice. Type one of: {'/'.join(info.speakers)}: ") \
  17. or info.speakers[0]
  18. if speaker in info.speakers:
  19. break
  20. else:
  21. print('WARNING! Incorrect voice name. Try again')
  22. print(f"Selected voice - {speaker}")
  23. for i, file in enumerate(files):
  24. try:
  25. paragraphs = get_separated_text_from_file(file)
  26. except Exception as e:
  27. print("Aborting program cause file can't be opened")
  28. exit()
  29. if len(files) == 1 and output_audio_filename:
  30. audio_filename = f"files/{output_audio_filename}-{speaker}.wav"
  31. else:
  32. filename = ''.join(file.split('/')[-1].split('.')[:-1])
  33. audio_filename = f"files/{''.join(file.split('/')[-1].split('.')[:-1])}-{speaker}-{output_audio_filename}.wav"
  34. for j, p in enumerate(paragraphs):
  35. # push("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  36. print("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  37. try:
  38. tts.text2speech(p, "files/temp.wav", speaker=speaker)
  39. except Exception as e:
  40. print(e)
  41. combine_two_files([audio_filename, "files/temp.wav"], audio_filename)
  42. print(f"File {audio_filename} was finished")
  43. print("Al files were finished")