main.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from audio import combine_two_files
  2. from models import SpeakerModel, Device, SampleRate
  3. import easygui
  4. import os
  5. from text import get_separated_text_from_file
  6. from push import push
  7. tts = SpeakerModel()
  8. # Can provide language, device, sample rate and model id
  9. # Default 'ru' 'cpu' 48000 v3_1_ru
  10. tts.specify_model()
  11. info = tts.get_model_info()
  12. def get_files() -> list:
  13. choice = easygui.buttonbox("Pick an item", "", ["File", "Folder"])
  14. if choice == "File":
  15. p = easygui.fileopenbox(filetypes=['*.txt'])
  16. files = [p]
  17. else:
  18. p = easygui.diropenbox(msg="Hello", title="Filessss")
  19. dir_listing = os.listdir(p)
  20. files = [f"{p}/{f}" for f in dir_listing if f.endswith(".txt")]
  21. return files
  22. files = get_files()
  23. output_audio_filename = easygui.enterbox("Specify outpyt file name") or ""
  24. speaker = easygui.choicebox("Select voice", "", info.speakers)
  25. for i, file in enumerate(files):
  26. try:
  27. paragraphs = get_separated_text_from_file(file)
  28. except Exception as e:
  29. print("Aborting program couse file can't be opened")
  30. exit()
  31. if len(files) == 1 and output_audio_filename:
  32. audio_filename = f"{output_audio_filename}.wav"
  33. else:
  34. audio_filename = f"{''.join(file.split('/')[-1].split('.')[:-1])}-{output_audio_filename}.wav"
  35. for j, p in enumerate(paragraphs):
  36. # push("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  37. print("Processing...", f"File {i+1}/{len(files)} paragraph {j+1}/{len(paragraphs)}")
  38. try:
  39. tts.text2speech(p, "temp.wav", speaker=speaker)
  40. except Exception as e:
  41. print(e)
  42. combine_two_files([audio_filename, "temp.wav"], audio_filename)
  43. easygui.msgbox("Process was finished", "Text-to-speech")