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

Dev.ap/demo formats #105

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions fs2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def forward(self, batch, control=InferenceControl(), inference=False):
"energy_target": variance_adaptor_out["energy_target"],
"pitch_prediction": variance_adaptor_out["pitch_prediction"],
"pitch_target": variance_adaptor_out["pitch_target"],
"text_input": text_inputs,
}

def check_and_upgrade_checkpoint(self, checkpoint):
Expand Down
132 changes: 76 additions & 56 deletions fs2/prediction_writing_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Optional, Sequence

import numpy as np
import numpy.typing as npt
import torch
from everyvoice.model.vocoder.HiFiGAN_iSTFT_lightning.hfgl.config import HiFiGANConfig
from everyvoice.model.vocoder.HiFiGAN_iSTFT_lightning.hfgl.model import HiFiGAN
Expand All @@ -21,6 +22,72 @@
from .type_definitions import SynthesizeOutputFormats


def frames_to_seconds(frames: int, fft_hop_size: int, sampling_rate: int) -> float:
return (frames * fft_hop_size) / sampling_rate


def get_tokens_from_duration_and_labels(
duration_predictions: torch.Tensor,
text: npt.NDArray[np.float32],
raw_text: str,
text_processor: TextProcessor,
config: FastSpeech2Config,
):
# Get all durations in frames
duration_frames = (
torch.clamp(torch.round(torch.exp(duration_predictions) - 1), min=0)
.int()
.tolist()
)
# Get all input labels
tokens: list[int] = text.tolist()
text_labels = text_processor.decode_tokens(tokens, join_character=None)
assert len(duration_frames) == len(
text_labels
), f"can't synthesize {raw_text} because the number of predicted duration steps ({len(duration_frames)}) doesn't equal the number of input text labels ({len(text_labels)})"
# get the duration of the audio: (sum_of_frames * hop_size) / sample_rate
xmax_seconds = frames_to_seconds(
sum(duration_frames),
config.preprocessing.audio.fft_hop_size,
config.preprocessing.audio.output_sampling_rate,
)
# create the tiers
words: list[tuple[float, float, str]] = []
phones: list[tuple[float, float, str]] = []
raw_text_words = raw_text.split()
current_word_duration = 0.0
last_phone_end = 0.0
last_word_end = 0.0
# skip padding
text_labels_no_padding = [tl for tl in text_labels if tl != "\x80"]
duration_frames_no_padding = duration_frames[: len(text_labels_no_padding)]
for label, duration in zip(text_labels_no_padding, duration_frames_no_padding):
# add phone label
phone_duration = frames_to_seconds(
duration,
config.preprocessing.audio.fft_hop_size,
config.preprocessing.audio.output_sampling_rate,
)
current_phone_end = last_phone_end + phone_duration
interval = (last_phone_end, current_phone_end, label)
phones.append(interval)
last_phone_end = current_phone_end
# accumulate phone to word label
current_word_duration += phone_duration
# if label is space or the last phone, add the word and recount
if label == " " or len(phones) == len(text_labels_no_padding):
current_word_end = last_word_end + current_word_duration
interval = (
last_word_end,
current_word_end,
raw_text_words[len(words)],
)
words.append(interval)
last_word_end = current_word_end
current_word_duration = 0
return xmax_seconds, phones, words


def get_synthesis_output_callbacks(
output_type: Sequence[SynthesizeOutputFormats],
output_dir: Path,
Expand Down Expand Up @@ -111,13 +178,15 @@ def __init__(
file_extension: str,
global_step: int,
save_dir: Path,
include_global_step_in_filename: bool = False,
) -> None:
super().__init__()
self.config = config
self.file_extension = file_extension
self.global_step = f"ckpt={global_step}"
self.save_dir = save_dir
self.sep = "--"
self.include_global_step_in_filename = include_global_step_in_filename

self.save_dir.mkdir(parents=True, exist_ok=True)

Expand All @@ -126,12 +195,11 @@ def get_filename(
basename: str,
speaker: str,
language: str,
include_global_step: bool = False,
) -> str:
# We don't truncate or alter the filename here because the basename is
# already truncated/cleaned in cli/synthesize.py
name_parts = [basename, speaker, language, self.file_extension]
if include_global_step:
if self.include_global_step_in_filename:
name_parts.insert(-1, self.global_step)
path = self.save_dir / self.sep.join(name_parts)
# synthesizing spec allows nested outputs so we may need to make subdirs
Expand Down Expand Up @@ -225,11 +293,6 @@ def save_aligned_text_to_file(
in the desired format."""
raise NotImplementedError

def frames_to_seconds(self, frames: int) -> float:
return (
frames * self.config.preprocessing.audio.fft_hop_size
) / self.config.preprocessing.audio.output_sampling_rate

def on_predict_batch_end( # pyright: ignore [reportIncompatibleMethodOverride]
self,
_trainer,
Expand All @@ -252,50 +315,10 @@ def on_predict_batch_end( # pyright: ignore [reportIncompatibleMethodOverride]
batch["text"], # type: ignore
outputs["duration_prediction"],
):
# Get all durations in frames
duration_frames = (
torch.clamp(torch.round(torch.exp(duration) - 1), min=0).int().tolist()
# Get the phone/word alignment tokens
xmax_seconds, phones, words = get_tokens_from_duration_and_labels(
duration, text, raw_text, self.text_processor, self.config
)
# Get all input labels
tokens: list[int] = text.tolist()
text_labels = self.text_processor.decode_tokens(tokens, join_character=None)
assert len(duration_frames) == len(
text_labels
), f"can't synthesize {raw_text} because the number of predicted duration steps ({len(duration_frames)}) doesn't equal the number of input text labels ({len(text_labels)})"
# get the duration of the audio: (sum_of_frames * hop_size) / sample_rate
xmax_seconds = self.frames_to_seconds(sum(duration_frames))
# create the tiers
words: list[tuple[float, float, str]] = []
phones: list[tuple[float, float, str]] = []
raw_text_words = raw_text.split()
current_word_duration = 0.0
last_phone_end = 0.0
last_word_end = 0.0
# skip padding
text_labels_no_padding = [tl for tl in text_labels if tl != "\x80"]
duration_frames_no_padding = duration_frames[: len(text_labels_no_padding)]
for label, duration in zip(
text_labels_no_padding, duration_frames_no_padding
):
# add phone label
phone_duration = self.frames_to_seconds(duration)
current_phone_end = last_phone_end + phone_duration
interval = (last_phone_end, current_phone_end, label)
phones.append(interval)
last_phone_end = current_phone_end
# accumulate phone to word label
current_word_duration += phone_duration
# if label is space or the last phone, add the word and recount
if label == " " or len(phones) == len(text_labels_no_padding):
current_word_end = last_word_end + current_word_duration
interval = (
last_word_end,
current_word_end,
raw_text_words[len(words)],
)
words.append(interval)
last_word_end = current_word_end
current_word_duration = 0

# Save the output (the subclass has to implement this)
self.save_aligned_text_to_file(
Expand Down Expand Up @@ -441,9 +464,7 @@ def save_aligned_text_to_file(
ras_tokens.append(Token(text=" ", is_word=False))
ras_tokens.append(Token(text=label, time=start, dur=end - start))

wav_file_name = self.wav_callback.get_filename(
basename, speaker, language, include_global_step=True
)
wav_file_name = self.wav_callback.get_filename(basename, speaker, language)
readalong_html, _readalong_xml = convert_prealigned_text_to_offline_html(
[ras_tokens],
wav_file_name,
Expand Down Expand Up @@ -476,6 +497,7 @@ def __init__(
file_extension="pred.wav",
global_step=global_step,
save_dir=output_dir / "wav",
include_global_step_in_filename=True,
)

self.output_key = output_key
Expand Down Expand Up @@ -547,9 +569,7 @@ def on_predict_batch_end( # pyright: ignore [reportIncompatibleMethodOverride]
outputs["tgt_lens"],
):
torchaudio.save(
self.get_filename(
basename, speaker, language, include_global_step=True
),
self.get_filename(basename, speaker, language),
# the vocoder output includes padding so we have to remove that
wav[:, : (unmasked_len * self.output_hop_size)],
sr,
Expand Down
Loading