-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts.py
More file actions
75 lines (62 loc) · 2.03 KB
/
Copy pathtts.py
File metadata and controls
75 lines (62 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import subprocess
import json
from playsound import playsound
import os
import logging
from colorlog import ColoredFormatter
handler = logging.StreamHandler()
handler.setFormatter(ColoredFormatter(
"%(log_color)s[%(levelname)s] [%(funcName)s] %(message)s",
log_colors={
'INFO': 'white'
}
))
logger = logging.getLogger()
logger.setLevel(logging.INFO) # zmienić INFO na DEBUG żeby tts dawał więcej logów
logger.addHandler(handler)
logger.propagate = False
def tts(text: str, language: str = None, filename: str =None ):
if not language or not filename:
if not os.path.exists(text):
raise ValueError("Missing parameters: either provide 'language' and 'filename', or pass a path to a JSON file.")
with open(text, 'r', encoding='utf-8') as f:
params = json.load(f)
code = tts(**params)
return code
model = "./piper/voices/"
language = language.lower()
match language:
case "en":
model = model + "en_GB-alba-medium.onnx"
case "pl":
model = model + "pl_PL-gosia-medium.onnx"
case _:
model = model + "en_GB-alba-medium.onnx"
filename = ".\\"+filename+".wav"
code = tts_run_model(text, model, filename)
if code == 0:
playsound(filename)
else:
print("TTS failed. Error code:", code)
return code
def tts_run_model(text, model, filename):
try:
process = subprocess.Popen(
[
"piper", "-m", model,
"-f", filename,"--debug"
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8"
)
except Exception as e:
logging.exception("TTS failed, unhandled exception:", e)
stdout, stderr = process.communicate(input=text)
code = process.returncode
logging.debug("TTS logs: %s", stderr)
logging.info("Output: %s", stdout)
logging.info("Return code: %s", code)
return code