forked from sovaai/sova-tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handler.py
54 lines (40 loc) · 1.49 KB
/
file_handler.py
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
import time
import logging
from models import models, ALL_MODELS
WAVES_FOLDER = "data/waves/"
class FileHandler:
@staticmethod
def get_synthesized_audio(text, model_type, **options):
try:
results = FileHandler.get_models_results(text, model_type, **options)
return 0, results
except Exception as e:
logging.exception(e)
return 1, str(e)
@staticmethod
def get_models_results(text, model_type, **options):
if model_type == ALL_MODELS:
current_models = {key: val for key, val in models.items() if val is not None}
else:
current_models = {model_type: models[model_type]}
results = []
for model_name, model in current_models.items():
start = time.time()
audio = model.synthesize(text, **options)
filename = model.save(audio, WAVES_FOLDER)
with open(filename, "rb") as f:
audio_bytes = f.read()
end = time.time()
sample_rate = model.sample_rate
duration = len(audio) / sample_rate
results.append(
{
"voice": model_name,
"sample_rate": sample_rate,
"duration_s": round(duration, 3),
"synthesis_time": round(end - start, 3),
"filename": filename,
"response_audio": audio_bytes
}
)
return results