File size: 14,683 Bytes
a8e5e01 | 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | """Offline phoneme-level forced alignment. Annotation time only.
A pretrained multilingual phoneme recogniser (wav2vec 2.0 fine-tuned with CTC on
eSpeak phoneme labels, ``facebook/wav2vec2-lv-60-espeak-cv-ft``) labels the
existing recordings. It is used ONLY to annotate training data: none of its
weights, features or outputs enter the TTS model, which never sees it.
Why this replaces the learned Gaussian aligner: that objective normalised scores
over the token positions of one utterance, so a frequent token whose key matched
the average spectrum (first BOS, later the word spaces) won the softmax on most
frames and every other token was left with single frames. Here each frame is
scored against phoneme classes shared by the whole language, and the transcript
only constrains their order.
Per utterance:
1. CTC Viterbi alignment of the eSpeak phoneme sequence (20 ms frames).
2. This recogniser emits a label once, at the START of the phone (measured on
our recordings against pitch-tracker voicing flips: a phone begins about
19 ms before the start of its first emission frame, for both voices; see
``scripts/calibrate_aligner_timing.py``). A phone therefore lasts from its
own onset to the next phone's onset.
3. Silence is found from signal energy at acoustic resolution and given to the
explicit pause-capable token at that position (BOS, EOS, punctuation, word
boundary). Tokens without a pause get zero frames.
4. Durations are counted on the acoustic frame grid, so they sum to the frame
count exactly and every phoneme has at least one frame.
``refine.py`` can then sharpen the boundaries with a classifier trained from
scratch on these labels.
"""
from dataclasses import dataclass
import math
import numpy as np
from .text import PHONE_SET, PUNCTUATION, SP, UNK, VOICELESS, VOWEL_SET
ALIGNER_MODEL = "facebook/wav2vec2-lv-60-espeak-cv-ft"
ALIGNER_RATE = 16000
ALIGNER_STRIDE = 320 # samples at 16 kHz: 20 ms
ALIGNER_CENTER = 200 # a frame's centre: 12.5 ms after its start
ALIGNMENT_VERSION = 2
PAUSING = (",", ".", "?", "!", ";", ":", "—", "…")
# Every inventory phone is a label of the recogniser (checked when it loads);
# only the unknown-phone token needs a stand-in.
ALIGNER_LABEL_FALLBACK = {UNK: "<unk>"}
@dataclass(frozen=True)
class AlignmentSettings:
onset_offset_seconds: float = -0.019 # phone onset relative to the start of its first emission frame
min_pause_frames: int = 7 # ~75 ms of silence: less stays with the neighbouring phones (stop closures)
silence_below_peak_db: float = 45.0
silence_above_floor_db: float = 8.0
silence_ceiling_below_peak_db: float = 30.0
energy_window: int = 512
def to_dict(self):
return {"alignment_version": ALIGNMENT_VERSION, "aligner": ALIGNER_MODEL, **self.__dict__}
def ctc_viterbi(log_probs, labels, blank=0):
"""Best CTC path. Returns per-frame extended-state indices (token k is state 2k+1).
Plain numpy reference, also used when torchaudio's implementation is absent.
"""
frames, count = log_probs.shape[0], len(labels)
if count < 1:
raise ValueError("no labels to align")
states = 2 * count + 1
extended = np.full(states, blank, dtype=np.int64)
extended[1::2] = labels
skip = np.zeros(states, dtype=bool)
skip[3::2] = extended[3::2] != extended[1:-2:2]
score = np.full(states, -np.inf)
score[0] = log_probs[0, blank]
score[1] = log_probs[0, extended[1]]
back = np.zeros((frames, states), dtype=np.int8)
for t in range(1, frames):
one = np.concatenate(([-np.inf], score[:-1]))
two = np.concatenate(([-np.inf, -np.inf], score[:-2]))
two[~skip] = -np.inf
options = np.stack((score, one, two))
choice = options.argmax(0)
score = options[choice, np.arange(states)] + log_probs[t, extended]
back[t] = choice
state = states - 1 if score[states - 1] >= score[states - 2] else states - 2
if not np.isfinite(score[state]):
raise ValueError("audio is too short for this phoneme sequence")
path = np.empty(frames, dtype=np.int64)
for t in range(frames - 1, -1, -1):
path[t] = state
state -= back[t, state]
return path
def spans_from_states(states, count):
spans = []
for index in range(count):
where = np.flatnonzero(states == 2 * index + 1)
if not where.size:
raise ValueError("forced alignment did not visit every phoneme")
spans.append((int(where[0]), int(where[-1])))
return spans
def token_spans(log_probs, labels, blank=0):
"""Inclusive ``(first, last)`` emission frames of every label on the best CTC path."""
count = len(labels)
repeats = sum(1 for a, b in zip(labels[:-1], labels[1:]) if a == b)
if log_probs.shape[0] < count + repeats:
raise ValueError("audio is too short for this phoneme sequence")
try:
import torch
from torchaudio.functional import forced_align
except ImportError:
return spans_from_states(ctc_viterbi(log_probs, np.asarray(labels), blank), count)
path, _ = forced_align(torch.from_numpy(np.ascontiguousarray(log_probs, dtype=np.float32))[None],
torch.tensor([list(labels)], dtype=torch.long), blank=blank)
spans, previous = [], blank
for frame, label in enumerate(path[0].tolist()):
if label != blank and (previous == blank or label != previous):
spans.append([frame, frame])
elif label != blank:
spans[-1][1] = frame
previous = label
if len(spans) != count:
raise ValueError("forced alignment did not visit every phoneme")
return [(a, b) for a, b in spans]
def frame_energy_db(wave, hop, window=512):
"""RMS level per acoustic frame, window centred on the frame centre i*hop."""
frames = len(wave) // hop
padded = np.pad(np.asarray(wave, dtype=np.float64), (window // 2, window // 2))
squares = np.concatenate(([0.0], np.cumsum(padded ** 2)))
starts = np.arange(frames) * hop
mean = (squares[starts + window] - squares[starts]) / window
return 10 * np.log10(np.maximum(mean, 1e-12))
def silence_mask(level_db, settings):
loud, floor = np.percentile(level_db, 95), np.percentile(level_db, 2)
threshold = min(loud - settings.silence_ceiling_below_peak_db,
max(loud - settings.silence_below_peak_db, floor + settings.silence_above_floor_db))
return level_db < threshold, float(threshold)
def pause_carrier(symbols, group):
"""Index of the token that receives a pause inside a run of non-phone tokens."""
for wanted in (PAUSING, PUNCTUATION, (SP,)):
for index in group:
if symbols[index] in wanted:
return index
return group[0]
def assemble(symbols, spans, frames, silent, hop, sample_rate, settings):
"""Durations in acoustic frames for every token (sums to ``frames``).
Acoustic frame i is centred on sample ``i * hop`` (the convention of the
stored mel/pitch features and of the synthesis frames).
"""
phones = [i for i, s in enumerate(symbols) if s in PHONE_SET or s == UNK]
count = len(phones)
if count != len(spans) or count < 1:
raise ValueError("phoneme count differs from alignment")
stride = ALIGNER_STRIDE / ALIGNER_RATE
emission = np.array([first for first, _ in spans], dtype=np.float64) * stride
seconds = np.arange(frames) * hop / sample_rate
# One protected frame per phone at its emission centre; phones cannot lose it.
keep = np.clip(np.round((emission + ALIGNER_CENTER / ALIGNER_RATE) * sample_rate / hop).astype(np.int64), 0, frames - 1)
for k in range(1, count):
keep[k] = max(keep[k], keep[k - 1] + 1)
if keep[-1] > frames - 1:
raise ValueError("audio is too short for this phoneme sequence")
starts = np.minimum(np.searchsorted(seconds, emission + settings.onset_offset_seconds, side="left"), keep)
starts[0] = 0
for k in range(1, count):
starts[k] = max(starts[k], keep[k - 1] + 1)
token = np.empty(frames, dtype=np.int64)
bounds = np.append(starts, frames)
for k in range(count):
token[bounds[k]:bounds[k + 1]] = phones[k]
# A pause runs from the first to the last silent frame between two protected
# frames. Whatever lies between them (a breath, a stop release) is not part
# of either phone's steady sound and stays with the pause.
quiet = np.flatnonzero(silent[:keep[0]])
if quiet.size: # leading silence -> BOS
token[:quiet[-1] + 1] = 0
quiet = np.flatnonzero(silent[keep[-1] + 1:]) + keep[-1] + 1
if quiet.size: # trailing silence -> EOS
token[quiet[0]:] = len(symbols) - 1
for k in range(count - 1): # interior pauses -> pause-capable token
left, right = phones[k], phones[k + 1]
if right == left + 1:
continue
quiet = np.flatnonzero(silent[keep[k] + 1:keep[k + 1]]) + keep[k] + 1
if quiet.size < settings.min_pause_frames:
continue
token[keep[k] + 1:quiet[0]] = left
token[quiet[0]:quiet[-1] + 1] = pause_carrier(symbols, range(left + 1, right))
token[quiet[-1] + 1:keep[k + 1]] = right
if np.any(np.diff(token) < 0):
raise ValueError("alignment is not monotonic")
durations = np.bincount(token, minlength=len(symbols)).astype(np.int64)
if durations.sum() != frames or np.any(durations[phones] < 1):
raise ValueError("durations do not cover the acoustic frames")
return durations
def quality(symbols, durations, spans, log_probs, labels, voiced, level_db, hop, sample_rate):
"""Label-free checks of one alignment against the recording itself."""
durations = np.asarray(durations)
ends = np.cumsum(durations)
starts = ends - durations
token_probability = np.array([float(np.exp(log_probs[a:b + 1, label]).mean()) for (a, b), label in zip(spans, labels)])
phone_frames = np.array([d for s, d in zip(symbols, durations) if s in PHONE_SET or s == UNK])
def voiced_share(wanted):
total = hits = 0
for symbol, start, end in zip(symbols, starts, ends):
if wanted(symbol) and end - start >= 3: # interior frames only
total += end - start - 2
hits += float(voiced[start + 1:end - 1].sum())
return round(hits / total, 4) if total else None
pause = np.zeros(len(voiced), dtype=bool)
for symbol, start, end in zip(symbols, starts, ends):
if symbol not in PHONE_SET and symbol != UNK:
pause[start:end] = True
speech_seconds = float((~pause).sum() * hop / sample_rate)
return {
"token_probability_mean": round(float(token_probability.mean()), 4),
"weak_phone_fraction": round(float((token_probability < 0.1).mean()), 4),
"max_phone_frames": int(phone_frames.max()),
"one_frame_phone_fraction": round(float((phone_frames == 1).mean()), 4),
"phones_per_second": round(len(phone_frames) / max(speech_seconds, 1e-6), 2),
"pause_frames": int(pause.sum()),
"leading_frames": int(durations[0]),
"trailing_frames": int(durations[-1]),
"vowel_voiced": voiced_share(lambda s: s in VOWEL_SET),
"voiceless_voiced": voiced_share(lambda s: s in VOICELESS),
"pause_voiced": round(float(voiced[pause].mean()), 4) if pause.any() else None,
"pause_level_db": round(float(level_db[pause].mean()), 1) if pause.any() else None,
"speech_level_db": round(float(np.percentile(level_db[~pause], 95)), 1) if (~pause).any() else None,
}
class PhoneRecognizer:
"""The pretrained annotation model. Loaded from the local HF cache only."""
def __init__(self, device="cuda", name=ALIGNER_MODEL, local_files_only=True):
import json
from pathlib import Path
import torch
from huggingface_hub import hf_hub_download
from transformers import Wav2Vec2ForCTC
self.torch, self.device = torch, torch.device(device)
self.model = Wav2Vec2ForCTC.from_pretrained(name, local_files_only=local_files_only).to(self.device).eval()
if self.device.type == "cuda":
self.model = self.model.half()
vocabulary = Path(hf_hub_download(name, "vocab.json", local_files_only=local_files_only))
self.vocabulary = json.loads(vocabulary.read_text(encoding="utf-8"))
self.blank = int(self.model.config.pad_token_id)
self.name = name
self.revision = vocabulary.parent.name # snapshot directory = commit hash
missing = [p for p in sorted(PHONE_SET) if p not in self.vocabulary and p not in ALIGNER_LABEL_FALLBACK]
if missing:
raise ValueError(f"recogniser has no label for inventory phones {missing}")
def label(self, symbol):
symbol = symbol if symbol in self.vocabulary else ALIGNER_LABEL_FALLBACK[symbol]
return int(self.vocabulary[symbol])
def log_probs(self, wave, sample_rate):
from scipy.signal import resample_poly
torch = self.torch
factor = math.gcd(sample_rate, ALIGNER_RATE)
audio = resample_poly(np.asarray(wave, dtype=np.float64), ALIGNER_RATE // factor, sample_rate // factor)
audio = (audio - audio.mean()) / np.sqrt(audio.var() + 1e-7)
with torch.inference_mode():
values = torch.from_numpy(audio.astype(np.float32))[None].to(self.device, self.model.dtype)
return self.model(values).logits[0].float().log_softmax(-1).cpu().numpy()
def align_utterance(recognizer, phonemized, wave, voiced, config, settings):
"""Return ``(durations, qc)`` for one recording or raise ``ValueError``."""
symbols = phonemized.symbols
frames = len(wave) // config.hop
if len(wave) != frames * config.hop or len(voiced) != frames:
raise ValueError("waveform length must be frames * hop")
labels = [recognizer.label(s) for s in symbols if s in PHONE_SET or s == UNK]
log_probs = recognizer.log_probs(wave, config.sample_rate)
spans = token_spans(log_probs, labels, recognizer.blank)
level = frame_energy_db(wave, config.hop, settings.energy_window)
silent, threshold = silence_mask(level, settings)
durations = assemble(symbols, spans, frames, silent, config.hop, config.sample_rate, settings)
qc = quality(symbols, durations, spans, log_probs, labels, np.asarray(voiced) > 0.5, level, config.hop, config.sample_rate)
qc["silence_threshold_db"] = round(threshold, 1)
return durations.tolist(), qc
|