2018-06-05 12:15:48 +00:00
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import librosa
|
|
|
|
import torch
|
2018-06-05 14:15:57 +00:00
|
|
|
import scipy
|
2018-06-05 12:15:48 +00:00
|
|
|
import numpy as np
|
2018-06-05 14:15:57 +00:00
|
|
|
import soundfile as sf
|
2018-11-19 14:27:22 +00:00
|
|
|
from utils.text import text_to_sequence
|
|
|
|
from utils.generic_utils import load_config
|
|
|
|
from utils.audio import AudioProcessor
|
|
|
|
from models.tacotron import Tacotron
|
2018-06-05 12:15:48 +00:00
|
|
|
from matplotlib import pylab as plt
|
|
|
|
|
|
|
|
|
|
|
|
class Synthesizer(object):
|
|
|
|
def load_model(self, model_path, model_name, model_config, use_cuda):
|
|
|
|
model_config = os.path.join(model_path, model_config)
|
2018-08-02 14:34:17 +00:00
|
|
|
self.model_file = os.path.join(model_path, model_name)
|
2018-06-05 12:15:48 +00:00
|
|
|
print(" > Loading model ...")
|
|
|
|
print(" | > model config: ", model_config)
|
|
|
|
print(" | > model file: ", self.model_file)
|
|
|
|
config = load_config(model_config)
|
|
|
|
self.config = config
|
|
|
|
self.use_cuda = use_cuda
|
2018-11-19 14:27:22 +00:00
|
|
|
self.ap = AudioProcessor(**config.audio)
|
|
|
|
self.model = Tacotron(config.embedding_size, self.ap.num_freq, self.ap.num_mels, config.r)
|
2018-06-05 12:15:48 +00:00
|
|
|
# load model state
|
|
|
|
if use_cuda:
|
|
|
|
cp = torch.load(self.model_file)
|
|
|
|
else:
|
2018-08-02 14:34:17 +00:00
|
|
|
cp = torch.load(
|
|
|
|
self.model_file, map_location=lambda storage, loc: storage)
|
2018-06-05 12:15:48 +00:00
|
|
|
# load the model
|
|
|
|
self.model.load_state_dict(cp['model'])
|
|
|
|
if use_cuda:
|
|
|
|
self.model.cuda()
|
2018-08-02 14:34:17 +00:00
|
|
|
self.model.eval()
|
|
|
|
|
2018-06-05 12:15:48 +00:00
|
|
|
def save_wav(self, wav, path):
|
2018-11-19 14:27:22 +00:00
|
|
|
# wav *= 32767 / max(1e-8, np.max(np.abs(wav)))
|
|
|
|
self.ap.save_wav(wav, path)
|
2018-06-05 12:15:48 +00:00
|
|
|
|
|
|
|
def tts(self, text):
|
|
|
|
text_cleaner = [self.config.text_cleaner]
|
|
|
|
wavs = []
|
|
|
|
for sen in text.split('.'):
|
|
|
|
if len(sen) < 3:
|
|
|
|
continue
|
2018-06-06 14:30:45 +00:00
|
|
|
sen = sen.strip()
|
2018-08-02 14:34:17 +00:00
|
|
|
sen += '.'
|
2018-06-05 14:15:57 +00:00
|
|
|
print(sen)
|
2018-06-05 12:15:48 +00:00
|
|
|
sen = sen.strip()
|
|
|
|
seq = np.array(text_to_sequence(text, text_cleaner))
|
2018-08-02 14:34:17 +00:00
|
|
|
chars_var = torch.from_numpy(seq).unsqueeze(0).long()
|
2018-06-05 12:15:48 +00:00
|
|
|
if self.use_cuda:
|
|
|
|
chars_var = chars_var.cuda()
|
2018-08-02 14:34:17 +00:00
|
|
|
mel_out, linear_out, alignments, stop_tokens = self.model.forward(
|
|
|
|
chars_var)
|
2018-06-05 12:15:48 +00:00
|
|
|
linear_out = linear_out[0].data.cpu().numpy()
|
|
|
|
wav = self.ap.inv_spectrogram(linear_out.T)
|
|
|
|
out = io.BytesIO()
|
|
|
|
wavs.append(wav)
|
|
|
|
wavs.append(np.zeros(10000))
|
|
|
|
self.save_wav(wav, out)
|
2018-06-06 14:30:45 +00:00
|
|
|
return out
|