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.2 KiB

import random
import pyttsx3
from utils import settings
2 years ago
class pyttsx:
def __init__(self):
self.max_chars = 5000
self.voices = []
def run(
self,
2 years ago
text: str,
filepath: str,
random_voice=False,
):
2 years ago
voice_id = settings.config["settings"]["tts"]["python_voice"]
voice_num = settings.config["settings"]["tts"]["py_voice_num"]
if voice_id == "" or voice_num == "":
voice_id = 2
voice_num = 3
raise ValueError("set pyttsx values to a valid value, switching to defaults")
else:
voice_id = int(voice_id)
voice_num = int(voice_num)
for i in range(voice_num):
self.voices.append(i)
2 years ago
i = +1
if random_voice:
2 years ago
voice_id = self.randomvoice()
engine = pyttsx3.init()
2 years ago
voices = engine.getProperty("voices")
engine.setProperty(
"voice", voices[voice_id].id
) # changing index changes voices but ony 0 and 1 are working here
engine.save_to_file(text, f"{filepath}")
engine.runAndWait()
2 years ago
def randomvoice(self):
return random.choice(self.voices)