Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement missing speed functions along with durable speech rate / speed changer function. #239

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TTS/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def tts(
language_name=language,
speaker_wav=speaker_wav,
split_sentences=split_sentences,
speed=speed,
**kwargs,
)
return wav
Expand Down Expand Up @@ -362,13 +363,13 @@ def tts_to_file(
Additional arguments for the model.
"""
self._check_arguments(speaker=speaker, language=language, speaker_wav=speaker_wav, **kwargs)

wav = self.tts(
text=text,
speaker=speaker,
language=language,
speaker_wav=speaker_wav,
split_sentences=split_sentences,
speed=speed,
**kwargs,
)
self.synthesizer.save_wav(wav=wav, path=file_path, pipe_out=pipe_out)
Expand Down
28 changes: 28 additions & 0 deletions TTS/tts/models/base_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import torch
import torch.distributed as dist
import torch.nn.functional as F
from coqpit import Coqpit
from torch import nn
from torch.utils.data import DataLoader
Expand Down Expand Up @@ -79,6 +80,33 @@ def _set_model_args(self, config: Coqpit):
else:
raise ValueError("config must be either a *Config or *Args")

def adjust_speech_rate(self, gpt_latents, length_scale):
if abs(length_scale - 1.0) < 1e-6:
return gpt_latents

B, L, D = gpt_latents.shape
target_length = int(L * length_scale)

assert target_length > 0, f"Invalid target length: {target_length}"

try:
resized = F.interpolate(
gpt_latents.transpose(1, 2),
size=target_length,
mode="linear",
align_corners=True
).transpose(1, 2)

if torch.isnan(resized).any():
print("Warning: NaN values detected on adjust speech rate")
return gpt_latents

return resized

except RuntimeError as e:
print(f"Interpolation failed: {e}")
return gpt_latents

def init_multispeaker(self, config: Coqpit, data: List = None):
"""Set up for multi-speaker TTS.

Expand Down
25 changes: 12 additions & 13 deletions TTS/tts/models/xtts.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def get_conditioning_latents(

return gpt_cond_latents, speaker_embedding

def synthesize(self, text, config, speaker_wav, language, speaker_id=None, **kwargs):
def synthesize(self, text, config, speaker_wav, language, speaker_id=None, speed: float = 1.0, **kwargs):
"""Synthesize speech with the given input text.

Args:
Expand Down Expand Up @@ -398,16 +398,16 @@ def synthesize(self, text, config, speaker_wav, language, speaker_id=None, **kwa
settings.update(kwargs) # allow overriding of preset settings with kwargs
if speaker_id is not None:
gpt_cond_latent, speaker_embedding = self.speaker_manager.speakers[speaker_id].values()
return self.inference(text, language, gpt_cond_latent, speaker_embedding, **settings)
return self.inference(text, language, gpt_cond_latent, speaker_embedding, speed=speed, **settings)
settings.update(
{
"gpt_cond_len": config.gpt_cond_len,
"gpt_cond_chunk_len": config.gpt_cond_chunk_len,
"max_ref_len": config.max_ref_len,
"sound_norm_refs": config.sound_norm_refs,
}
{
"gpt_cond_len": config.gpt_cond_len,
"gpt_cond_chunk_len": config.gpt_cond_chunk_len,
"max_ref_len": config.max_ref_len,
"sound_norm_refs": config.sound_norm_refs,
}
)
return self.full_inference(text, speaker_wav, language, **settings)
return self.full_inference(text, speaker_wav, language, speed=speed, **settings)

@torch.inference_mode()
def full_inference(
Expand All @@ -427,6 +427,7 @@ def full_inference(
gpt_cond_chunk_len=6,
max_ref_len=10,
sound_norm_refs=False,
speed: float = 1.0,
**hf_generate_kwargs,
):
"""
Expand Down Expand Up @@ -487,6 +488,7 @@ def full_inference(
top_k=top_k,
top_p=top_p,
do_sample=do_sample,
speed=speed,
**hf_generate_kwargs,
)

Expand Down Expand Up @@ -560,10 +562,7 @@ def inference(
)

if length_scale != 1.0:
gpt_latents = F.interpolate(
gpt_latents.transpose(1, 2), scale_factor=length_scale, mode="linear"
).transpose(1, 2)

gpt_latents = self.adjust_speech_rate(gpt_latents, length_scale)
gpt_latents_list.append(gpt_latents.cpu())
wavs.append(self.hifigan_decoder(gpt_latents, g=speaker_embedding).cpu().squeeze())

Expand Down
Loading