-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
58 lines (49 loc) · 2.41 KB
/
utils.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
55
56
57
58
from synthesizer.utils.model import get_model as load_synthesizer_model
from resgrad.utils import load_model as load_resgrad_model
from vocoder.utils import get_vocoder
from synthesizer.utils.tools import plot_mel
import os
import json
from scipy.io import wavfile
from matplotlib import pyplot as plt
import yaml
import time
def save_result(mel_prediction, wav, pitch, energy, preprocess_config, result_dir, file_name):
with open(os.path.join(preprocess_config["path"]["preprocessed_path"], "stats.json")) as f:
stats = json.load(f)
stats = stats["pitch"] + stats["energy"][:2]
fig = plot_mel([(mel_prediction.cpu().numpy(), pitch.cpu().numpy(), energy.cpu().numpy())], stats, ["Synthetized Spectrogram"])
plt.savefig(os.path.join(result_dir, "{}.png".format(file_name)))
plt.close()
sampling_rate = preprocess_config["preprocessing"]["audio"]["sampling_rate"]
wavfile.write(os.path.join(result_dir, "{}.wav".format(file_name)), sampling_rate, wav)
def load_models(all_restore_step, config):
synthesizer_model, resgrad_model, vocoder_model = None, None, None
device = config['main']['device']
if all_restore_step['synthesizer'] not in [None, 0]:
synthesizer_model = load_synthesizer_model(all_restore_step['synthesizer'], config).to(device)
if all_restore_step['vocoder'] not in [None, 0]:
vocoder_model = get_vocoder(config['vocoder'], device).to(device)
if all_restore_step['resgrad'] not in [None, 0]:
resgrad_model = load_resgrad_model(config['resgrad'], train=False, restore_model_step=all_restore_step['resgrad'], device=device).to(device)
return synthesizer_model, resgrad_model, vocoder_model
def load_yaml_file(path):
## define custom tag handler
def join(loader, node):
seq = loader.construct_sequence(node)
return str(os.path.join(*[str(i) for i in seq]))
## register the tag handler
yaml.add_constructor('!join', join)
data = yaml.load(open(path, "r"), Loader=yaml.FullLoader)
return data
def get_file_name(args):
file_name_parts = []
if args.result_file_name:
file_name_parts.append(args.result_file_name)
if args.speaker_id:
file_name_parts.append("spk" + str(args.speaker_id))
if len(file_name_parts) == 0:
file_name_parts.append(str(time.time()).replace('.', '_'))
file_name_parts.append("FastSpeech")
file_name = "_".join(file_name_parts)
return file_name