Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/TTS/pyttsx.py
327 views
1
import random
2
3
import pyttsx3
4
5
from utils import settings
6
7
8
class pyttsx:
9
def __init__(self):
10
self.max_chars = 5000
11
self.voices = []
12
13
def run(
14
self,
15
text: str,
16
filepath: str,
17
random_voice=False,
18
):
19
voice_id = settings.config["settings"]["tts"]["python_voice"]
20
voice_num = settings.config["settings"]["tts"]["py_voice_num"]
21
if voice_id == "" or voice_num == "":
22
voice_id = 2
23
voice_num = 3
24
raise ValueError("set pyttsx values to a valid value, switching to defaults")
25
else:
26
voice_id = int(voice_id)
27
voice_num = int(voice_num)
28
for i in range(voice_num):
29
self.voices.append(i)
30
i = +1
31
if random_voice:
32
voice_id = self.randomvoice()
33
engine = pyttsx3.init()
34
voices = engine.getProperty("voices")
35
engine.setProperty(
36
"voice", voices[voice_id].id
37
) # changing index changes voices but ony 0 and 1 are working here
38
engine.save_to_file(text, f"{filepath}")
39
engine.runAndWait()
40
41
def randomvoice(self):
42
return random.choice(self.voices)
43
44