2018-11-02 15:13:51 +00:00
|
|
|
import io
|
|
|
|
import time
|
|
|
|
import librosa
|
|
|
|
import torch
|
|
|
|
import numpy as np
|
2019-02-25 16:20:05 +00:00
|
|
|
from .text import text_to_sequence, phoneme_to_sequence, sequence_to_phoneme
|
2018-11-02 15:13:51 +00:00
|
|
|
from .visual import visualize
|
|
|
|
from matplotlib import pylab as plt
|
|
|
|
|
|
|
|
|
2019-06-12 10:12:22 +00:00
|
|
|
def text_to_seqvec(text, CONFIG, use_cuda):
|
|
|
|
text_cleaner = [CONFIG.text_cleaner]
|
2019-06-14 14:18:49 +00:00
|
|
|
# text ot phonemes to sequence vector
|
2019-06-12 10:12:22 +00:00
|
|
|
if CONFIG.use_phonemes:
|
|
|
|
seq = np.asarray(
|
2019-06-14 14:18:49 +00:00
|
|
|
phoneme_to_sequence(text, text_cleaner, CONFIG.phoneme_language,
|
|
|
|
CONFIG.enable_eos_bos_chars),
|
2019-06-12 10:12:22 +00:00
|
|
|
dtype=np.int32)
|
|
|
|
else:
|
|
|
|
seq = np.asarray(text_to_sequence(text, text_cleaner), dtype=np.int32)
|
2019-06-14 14:18:49 +00:00
|
|
|
# torch tensor
|
2019-06-12 10:12:22 +00:00
|
|
|
chars_var = torch.from_numpy(seq).unsqueeze(0)
|
|
|
|
if use_cuda:
|
|
|
|
chars_var = chars_var.cuda()
|
|
|
|
return chars_var.long()
|
|
|
|
|
|
|
|
|
2019-06-14 14:18:49 +00:00
|
|
|
def compute_style_mel(style_wav, ap, use_cuda):
|
|
|
|
print(style_wav)
|
|
|
|
style_mel = torch.FloatTensor(ap.melspectrogram(
|
|
|
|
ap.load_wav(style_wav))).unsqueeze(0)
|
|
|
|
if use_cuda:
|
|
|
|
return style_mel.cuda()
|
|
|
|
else:
|
|
|
|
return style_mel
|
|
|
|
|
|
|
|
|
2019-06-26 11:31:16 +00:00
|
|
|
def run_model(model, inputs, speaker_id, CONFIG, truncated, style_mel=None):
|
2019-06-14 14:18:49 +00:00
|
|
|
if CONFIG.model == "TacotronGST" and style_mel is not None:
|
|
|
|
decoder_output, postnet_output, alignments, stop_tokens = model.inference(
|
2019-06-26 11:31:16 +00:00
|
|
|
inputs, style_mel, speaker_id)
|
2019-06-14 14:18:49 +00:00
|
|
|
else:
|
|
|
|
if truncated:
|
|
|
|
decoder_output, postnet_output, alignments, stop_tokens = model.inference_truncated(
|
2019-06-26 11:31:16 +00:00
|
|
|
inputs, speaker_id)
|
2019-06-14 14:18:49 +00:00
|
|
|
else:
|
|
|
|
decoder_output, postnet_output, alignments, stop_tokens = model.inference(
|
2019-06-26 11:31:16 +00:00
|
|
|
inputs, speaker_id)
|
2019-06-14 14:18:49 +00:00
|
|
|
return decoder_output, postnet_output, alignments, stop_tokens
|
2019-06-12 10:12:22 +00:00
|
|
|
|
|
|
|
|
2019-06-14 14:18:49 +00:00
|
|
|
def parse_outputs(postnet_output, decoder_output, alignments):
|
|
|
|
postnet_output = postnet_output[0].data.cpu().numpy()
|
|
|
|
decoder_output = decoder_output[0].data.cpu().numpy()
|
|
|
|
alignment = alignments[0].cpu().data.numpy()
|
|
|
|
return postnet_output, decoder_output, alignment
|
2019-06-12 10:12:22 +00:00
|
|
|
|
|
|
|
|
2019-06-14 14:18:49 +00:00
|
|
|
def trim_silence(wav):
|
|
|
|
return wav[:ap.find_endpoint(wav)]
|
2019-06-12 10:12:22 +00:00
|
|
|
|
|
|
|
|
2019-06-14 14:18:49 +00:00
|
|
|
def inv_spectrogram(postnet_output, ap, CONFIG):
|
|
|
|
if CONFIG.model in ["Tacotron", "TacotronGST"]:
|
|
|
|
wav = ap.inv_spectrogram(postnet_output.T)
|
|
|
|
else:
|
|
|
|
wav = ap.inv_mel_spectrogram(postnet_output.T)
|
|
|
|
return wav
|
2019-06-12 10:12:22 +00:00
|
|
|
|
|
|
|
|
2019-06-14 14:18:49 +00:00
|
|
|
def synthesis(model,
|
|
|
|
text,
|
2019-06-26 10:59:14 +00:00
|
|
|
speaker_id,
|
2019-06-14 14:18:49 +00:00
|
|
|
CONFIG,
|
|
|
|
use_cuda,
|
|
|
|
ap,
|
|
|
|
style_wav=None,
|
|
|
|
truncated=False,
|
|
|
|
enable_eos_bos_chars=False,
|
|
|
|
trim_silence=False):
|
2019-03-11 16:40:09 +00:00
|
|
|
"""Synthesize voice for the given text.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
model (TTS.models): model to synthesize.
|
|
|
|
text (str): target text
|
2019-06-26 10:59:14 +00:00
|
|
|
speaker_id (int): id of speaker
|
2019-03-11 16:40:09 +00:00
|
|
|
CONFIG (dict): config dictionary to be loaded from config.json.
|
|
|
|
use_cuda (bool): enable cuda.
|
|
|
|
ap (TTS.utils.audio.AudioProcessor): audio processor to process
|
|
|
|
model outputs.
|
2019-06-12 10:12:01 +00:00
|
|
|
style_wav (str): Uses for style embedding of GST.
|
2019-03-11 16:40:09 +00:00
|
|
|
truncated (bool): keep model states after inference. It can be used
|
|
|
|
for continuous inference at long texts.
|
2019-04-12 14:12:15 +00:00
|
|
|
enable_eos_bos_chars (bool): enable special chars for end of sentence and start of sentence.
|
|
|
|
trim_silence (bool): trim silence after synthesis.
|
2019-03-11 16:40:09 +00:00
|
|
|
"""
|
2019-06-12 10:12:01 +00:00
|
|
|
# GST processing
|
2019-06-14 23:22:27 +00:00
|
|
|
style_mel = None
|
2019-06-12 10:12:01 +00:00
|
|
|
if CONFIG.model == "TacotronGST" and style_wav is not None:
|
2019-06-14 14:18:49 +00:00
|
|
|
style_mel = compute_style_mel(style_wav, ap, use_cuda)
|
2019-04-12 14:12:15 +00:00
|
|
|
# preprocess the given text
|
2019-06-14 14:18:49 +00:00
|
|
|
inputs = text_to_seqvec(text, CONFIG, use_cuda)
|
2019-06-26 11:31:16 +00:00
|
|
|
speaker_id = np.asarray(speaker_id)
|
|
|
|
speaker_id = torch.from_numpy(speaker_id).unsqueeze(0)
|
2019-06-26 10:59:14 +00:00
|
|
|
if use_cuda:
|
|
|
|
speaker_id.cuda()
|
2019-04-12 14:12:15 +00:00
|
|
|
# synthesize voice
|
2019-06-14 14:18:49 +00:00
|
|
|
decoder_output, postnet_output, alignments, stop_tokens = run_model(
|
2019-06-26 11:31:16 +00:00
|
|
|
model, inputs, speaker_id, CONFIG, truncated, style_mel)
|
2019-04-12 14:12:15 +00:00
|
|
|
# convert outputs to numpy
|
2019-06-14 14:18:49 +00:00
|
|
|
postnet_output, decoder_output, alignment = parse_outputs(
|
|
|
|
postnet_output, decoder_output, alignments)
|
2019-04-12 14:12:15 +00:00
|
|
|
# plot results
|
2019-06-14 14:18:49 +00:00
|
|
|
wav = inv_spectrogram(postnet_output, ap, CONFIG)
|
2019-04-12 14:12:15 +00:00
|
|
|
# trim silence
|
|
|
|
if trim_silence:
|
2019-06-14 14:18:49 +00:00
|
|
|
wav = trim_silence(wav)
|
2019-03-06 12:11:46 +00:00
|
|
|
return wav, alignment, decoder_output, postnet_output, stop_tokens
|