You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RedditVideoMakerBot/TTS/pyttsx.py

43 lines
1.3 KiB

import random
import pyttsx3
from utils import settings
max_chars = 0
# Uses the system voices, significantly faster than other tts
class Pyttsx:
def __init__(self):
self.max_chars = 0
self.voices = []
def run(
self,
text: str = "Python Text to Speech",
filepath: str = "assets/temp/mp3",
random_voice=False,
):
if (settings.config["settings"]["tts"]["python_voice"] == '' or settings.config["settings"]["tts"]["python_voice"] == ""):
voice_id = 0
voice_num = 3
else:
voice_id = int(settings.config["settings"]["tts"]["python_voice"])
voice_num = int(settings.config["settings"]["tts"]["python_voice"])
for i in range(voice_num - 1):
self.voices.append(i)
i = +1
if random_voice:
voice_id = self.randomvoice()
else:
voice = voice_id
engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty(
"voice", voices[voice].id
) # changing index changes voices but ony 0 and 1 are working here
engine.save_to_file(text, f"{filepath}")
engine.runAndWait()
def randomvoice(self):
return random.choice(self.voices)