-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalign.py
More file actions
500 lines (408 loc) · 13.3 KB
/
Copy pathalign.py
File metadata and controls
500 lines (408 loc) · 13.3 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import gc
import os
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Any
os.environ.setdefault("OMP_NUM_THREADS", "2")
os.environ.setdefault("MKL_NUM_THREADS", "2")
os.environ.setdefault("OPENBLAS_NUM_THREADS", "2")
os.environ.setdefault("NUMEXPR_NUM_THREADS", "2")
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
import av
import numpy as np
import torch
from lrc_export import save_lrc
APP_NAME = "CTCLRC"
SAMPLE_RATE = 16000
DEFAULT_LANGUAGE = "jpn"
CTC_ALIGNMENT_MODEL = "MahmoudAshraf/mms-300m-1130-forced-aligner"
LOCAL_CTC_MODEL_DIR = "models/mms-300m-1130-forced-aligner"
DEFAULT_LRC_LEAD_IN_SEC = 0.345
CPU_THREADS = 2
if torch.cuda.is_available():
DEVICE = "cuda"
else:
DEVICE = "cpu"
torch.set_num_threads(CPU_THREADS)
torch.set_num_interop_threads(1)
if getattr(sys, "frozen", False):
BASE_DIR = Path(sys._MEIPASS)
APP_DIR = Path(sys.executable).resolve().parent
else:
BASE_DIR = Path(__file__).resolve().parent
APP_DIR = BASE_DIR
@dataclass(frozen=True)
class AlignmentBundle:
model: Any
tokenizer: Any
dtype: Any
model_path: str
def get_ctc_alignment_model_path() -> str:
for base_dir in (APP_DIR, BASE_DIR):
local_model = base_dir / LOCAL_CTC_MODEL_DIR
if local_model.exists():
return str(local_model)
return CTC_ALIGNMENT_MODEL
def load_alignment_bundle() -> AlignmentBundle:
try:
from ctc_forced_aligner import load_alignment_model
except ImportError as exc:
raise RuntimeError(
"ctc-forced-aligner is not installed. Install it with requirements.txt first."
) from exc
dtype = torch.float16 if DEVICE == "cuda" else torch.float32
model_path = get_ctc_alignment_model_path()
print(f"Loading CTC model: {model_path}")
print(f"Device: {DEVICE}")
model, tokenizer = load_alignment_model(
DEVICE,
model_path,
None,
dtype,
)
return AlignmentBundle(model=model, tokenizer=tokenizer, dtype=dtype, model_path=model_path)
def load_lyrics(path: str) -> list[str]:
lines, _ = load_lyrics_with_sections(path)
return lines
def load_lyrics_with_sections(path: str, keep_sections: bool = True) -> tuple[list[str], list[int]]:
with open(path, "r", encoding="utf-8-sig") as f:
return parse_lyrics_text(f.read(), keep_sections=keep_sections)
def load_lyrics_text(text: str, keep_sections: bool = True) -> tuple[list[str], list[int]]:
return parse_lyrics_text(text, keep_sections=keep_sections)
def parse_lyrics_text(text: str, keep_sections: bool = True) -> tuple[list[str], list[int]]:
lines: list[str] = []
section_breaks: set[int] = set()
pending_section_break = False
for raw_line in text.splitlines():
line = raw_line.strip()
if not line:
if keep_sections and lines:
pending_section_break = True
continue
if keep_sections and pending_section_break and lines:
section_breaks.add(len(lines) - 1)
pending_section_break = False
lines.append(line)
return lines, sorted(section_breaks) if keep_sections else []
def resolve_lyrics_source(
audio_file: str | Path,
lyric_file: str | Path | None = None,
lyric_text: str | None = None,
keep_sections: bool = True,
) -> tuple[list[str], list[int]]:
if lyric_text is not None and lyric_text.strip():
return load_lyrics_text(lyric_text, keep_sections=keep_sections)
if lyric_file:
lyric_path = Path(lyric_file)
if lyric_path.exists():
return load_lyrics_with_sections(str(lyric_path), keep_sections=keep_sections)
audio_path = Path(audio_file)
candidate = audio_path.with_suffix(".txt")
if candidate.exists():
return load_lyrics_with_sections(str(candidate), keep_sections=keep_sections)
raise FileNotFoundError(
"Lyrics were not provided. Supply a lyrics file, paste lyrics text, or place a .txt file next to the audio."
)
def decode_audio(audio_path: str) -> np.ndarray:
resampler = av.audio.resampler.AudioResampler(
format="s16",
layout="mono",
rate=SAMPLE_RATE,
)
chunks = []
with av.open(audio_path, mode="r", metadata_errors="ignore") as container:
for frame in container.decode(audio=0):
for resampled in resampler.resample(frame):
chunks.append(resampled.to_ndarray().reshape(-1))
for resampled in resampler.resample(None):
chunks.append(resampled.to_ndarray().reshape(-1))
if not chunks:
raise ValueError(f"No audio stream found: {audio_path}")
audio_i16 = np.concatenate(chunks).astype(np.int16, copy=False)
return audio_i16.astype(np.float32) / 32768.0
def normalize_text(text: str) -> str:
remove_chars = {
" ",
"\t",
"\u3000",
"\n",
"\r",
",",
".",
"!",
"?",
"!",
"?",
"、",
"。",
"・",
"…",
"「",
"」",
"『",
"』",
"(",
")",
"(",
")",
"[",
"]",
"【",
"】",
'"',
"'",
":",
";",
":",
";",
}
return "".join(ch for ch in text.strip() if ch not in remove_chars)
def average_ctc_score(items: list[dict]) -> float:
scores = [float(item.get("score", 0.0)) for item in items]
if not scores:
return 0.0
return round(max(0.0, sum(scores) / len(scores)), 1)
def distribute_chars(chars: list[str], start: float, end: float) -> list[dict]:
if not chars:
return []
if end <= start:
end = start + 0.05 * len(chars)
span = end - start
return [
{
"text": char,
"start": start + span * index / len(chars),
"end": start + span * (index + 1) / len(chars),
}
for index, char in enumerate(chars)
]
def enforce_monotonic_times(line_result: list[dict], word_result: list[dict]) -> None:
previous = 0.0
for line, words in zip(line_result, word_result):
start = max(float(line["start"]), previous)
end = max(float(line["end"]), start + 0.05)
line["start"] = start
line["end"] = end
words["start"] = start
words["end"] = end
previous = start + 0.02
def apply_lrc_lead_in(line_result: list[dict], word_result: list[dict], seconds: float) -> None:
if seconds <= 0:
return
for line, words in zip(line_result, word_result):
line["start"] = max(0.0, float(line["start"]) - seconds)
line["end"] = max(line["start"] + 0.05, float(line["end"]) - seconds)
words["start"] = line["start"]
words["end"] = line["end"]
for word in words.get("words", []):
word["start"] = max(0.0, float(word.get("start", 0.0)) - seconds)
word["end"] = max(
word["start"] + 0.01,
float(word.get("end", word["start"])) - seconds,
)
def ctc_align_lyrics(
audio: np.ndarray,
lyrics: list[str],
language: str,
lead_in: float,
bundle: AlignmentBundle | None = None,
) -> tuple[list[dict], list[dict]]:
try:
from ctc_forced_aligner import (
generate_emissions,
get_alignments,
get_spans,
postprocess_results,
preprocess_text,
)
except ImportError as exc:
raise RuntimeError(
"ctc-forced-aligner is not installed. Install it with requirements.txt first."
) from exc
owned_bundle = bundle is None
if bundle is None:
bundle = load_alignment_bundle()
prepared_lines = [normalize_text(line) for line in lyrics]
full_text = "".join(prepared_lines)
if not full_text:
raise ValueError("Lyrics contain no alignable text.")
print(f"Language: {language}")
try:
audio_waveform = torch.as_tensor(audio, dtype=bundle.dtype, device=DEVICE)
emissions, stride = generate_emissions(
bundle.model,
audio_waveform,
window_length=20,
context_length=2,
batch_size=1,
)
tokens_starred, text_starred = preprocess_text(
full_text,
romanize=True,
language=language,
split_size="char",
star_frequency="edges",
)
segments, scores, blank_token = get_alignments(
emissions,
tokens_starred,
bundle.tokenizer,
)
spans = get_spans(tokens_starred, segments, blank_token)
char_results = postprocess_results(
text_starred,
spans,
stride,
scores,
merge_threshold=0.0,
)
finally:
if owned_bundle:
del bundle
gc.collect()
if DEVICE == "cuda":
torch.cuda.empty_cache()
line_result = []
word_result = []
cursor = 0
for lyric, prepared in zip(lyrics, prepared_lines):
count = len(prepared)
chars = char_results[cursor:cursor + count]
cursor += count
timed_chars = [
item for item in chars
if item.get("start") is not None and item.get("end") is not None
]
if timed_chars:
start = float(timed_chars[0]["start"])
end = float(timed_chars[-1]["end"])
score = average_ctc_score(timed_chars)
else:
start = 0.0 if not line_result else line_result[-1]["end"]
end = start + 0.05 * max(count, 1)
score = 0.0
words = [
{
"text": prepared[index],
"start": float(item.get("start", start)),
"end": float(item.get("end", end)),
}
for index, item in enumerate(chars[:count])
if index < len(prepared)
]
if not words:
words = distribute_chars(list(prepared), start, end)
line_result.append(
{
"lyric": lyric,
"recognized": "",
"start": start,
"end": max(end, start + 0.05),
"score": score,
}
)
word_result.append(
{
"lyric": lyric,
"start": start,
"end": max(end, start + 0.05),
"words": words,
}
)
enforce_monotonic_times(line_result, word_result)
apply_lrc_lead_in(line_result, word_result, lead_in)
return line_result, word_result
def annotate_section_breaks(lines: list[dict], break_after_indices: list[int] | None) -> None:
if not break_after_indices:
return
breaks = set(break_after_indices)
for index, item in enumerate(lines):
if index in breaks:
item["section_break_after"] = True
def print_line_alignment(lines: list[dict]) -> None:
print()
print("=" * 60)
print("Line Alignment")
print("=" * 60)
for item in lines:
print(f"[{item['start']:.2f}] {item['lyric']}")
if item.get("section_break_after"):
print()
def print_word_alignment(lines: list[dict]) -> None:
print()
print("=" * 60)
print("Word Alignment")
print("=" * 60)
for line in lines:
print()
print(line["lyric"])
for word in line["words"]:
print(f" {word['start']:.2f} {word['text']}")
def generate_lrc(
audio_file,
lyric_file=None,
lyric_text=None,
model=None,
language=DEFAULT_LANGUAGE,
lead_in=DEFAULT_LRC_LEAD_IN_SEC,
line_mode=True,
word_mode=False,
output_path=None,
progress_callback=None,
keep_sections=True,
):
print("=" * 60)
print(APP_NAME)
print("=" * 60)
audio_file = Path(audio_file)
if not audio_file.exists():
raise FileNotFoundError(audio_file)
if progress_callback:
progress_callback(5)
print("Load lyrics...")
lyrics, section_breaks = resolve_lyrics_source(audio_file, lyric_file, lyric_text, keep_sections=keep_sections)
if not lyrics:
raise ValueError("Lyrics are empty.")
print(f"{len(lyrics)} lyric lines")
if progress_callback:
progress_callback(15)
print("Load audio...")
audio = decode_audio(str(audio_file))
if progress_callback:
progress_callback(30)
print("CTC forced alignment...")
bundle = model if isinstance(model, AlignmentBundle) else None
line_result, word_result = ctc_align_lyrics(audio, lyrics, language, lead_in, bundle=bundle)
annotate_section_breaks(line_result, section_breaks)
annotate_section_breaks(word_result, section_breaks)
print(f"LRC lead-in: -{lead_in:.2f}s")
if progress_callback:
progress_callback(90)
print_line_alignment(line_result)
if word_mode:
print_word_alignment(word_result)
save_lrc(
str(audio_file),
line_result,
word_result,
line_mode=line_mode,
word_mode=word_mode,
output_path=output_path,
)
if progress_callback:
progress_callback(100)
print()
print("Done.")
return {
"lines": line_result,
"words": word_result,
}
if __name__ == "__main__":
generate_lrc(
"song.flac",
"lyrics.txt",
language=DEFAULT_LANGUAGE,
line_mode=True,
word_mode=False,
)