mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
feat(medley): customer chord charts + static-gain master (v0.278.5) (#1183)
## Summary Two follow-ups on the medley tooling, both from the Fiverr medley customer's feedback. - **Fixes the robotic artifact at the start of the karaoke audio.** The final single-pass `loudnorm` ran in dynamic mode. On a song that opens a cappella (HSLHT sings from 0.4s, band at 3s), the instrumental's first 3s is near-silent separation residue at -70 dB, and `loudnorm` boosted it by about 50 dB into a loud warble. The master is now one static gain to -14 LUFS plus a 4×-oversampled limiter at -1 dB (`master_filter`). After the fix, the first 3s measure -61 dB (were -15 dB). Sync is sample-identical and true peak is -0.9 dB. - **Imports the customer's own chord chart.** `scripts/medley/chord_chart.py` plus a new `make_medley.py chart` step read a chords-over-lyrics chart (.txt or .docx in Courier, capo shapes, `Capo N` / `Key change Capo 1` lines). They align it word-by-word to the reviewed lyrics, so chords come out timed on the words the chart puts them on. Detected chords fill intros, breaks and spoken verses, transposed into the capo shapes. - **Shows the capo in the chord box.** New `ChordChange.capo` field, shown as "capo N" under the chord. - **Docs:** `docs/MEDLEYS.md` gets a new section on importing a customer chart, and the mastering guidance now says never to use single-pass `loudnorm`. `docs/README.md` has an entry. ## Testing - Unit tests: - New: `tests/unit/test_chord_chart.py` (9). - Added: capo tests in `test_guitar_chords.py`, plus master and capo tests in `test_make_medley.py`. - Full `tests/unit` suite: 2100 passed, 65 skipped (run before the last CodeRabbit fix; only the 9 chord-chart tests were re-run after it). - Real data: the customer's 4-song chart against the medley. The lyrics-with-chords output matched the chart line by line, and 4K frames rendered with capo labels. - A local CodeRabbit review found 1 issue (two chord groups after the same word could come out in the wrong order). It is fixed and has a test. @coderabbitai ignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01QV2TQwyC7pwVkGPCk98Bqg --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9a44a30368
commit
03ee693f28
10 files changed
+692
-27
No files matched your search
+60
-11
@@ -96,9 +96,15 @@ This writes `out/medley_{instrumental_with_backing,instrumental_clean,guide_with
|
||||
and `out/timeline.json` (each section's source and medley times).
|
||||
|
||||
- Each section's gain is set so the section itself measures -18 LUFS. That's measured on the
|
||||
section, not the whole song, which matters for quiet intros. The full medley is then run
|
||||
through `loudnorm` to -14 LUFS with peaks no higher than -1 dBTP. In the example, all five
|
||||
sections ended up within 1.6 LU of each other.
|
||||
section, not the whole song, which matters for quiet intros. The full medley is then
|
||||
measured and given **one static gain** to -14 LUFS, with a 4×-oversampled peak limiter at
|
||||
-1 dB (`master_filter`). In the example, all five sections ended up within 1.6 LU of each
|
||||
other.
|
||||
- **Never master with single-pass `loudnorm`.** Its dynamic mode rides the gain over a 3s
|
||||
window. When a song opens a cappella, the instrumental is near-silent separation residue
|
||||
(-70 dB), and `loudnorm` boosted it ~50 dB into a loud robotic warble. The customer heard
|
||||
it at 0:08–0:12 of the v2 karaoke video (HSLHT opens with an unaccompanied line). Fixed
|
||||
in v0.278.5 by the static-gain master above.
|
||||
- WAV is 24-bit / 44.1 kHz. MP3 is 320k.
|
||||
- **Listen to the guide (`guide_with_vocals`) first.** Bad transitions are much easier to hear
|
||||
with vocals in.
|
||||
@@ -166,10 +172,51 @@ python scripts/medley/make_medley.py video spec.json # 4K final
|
||||
verse, one on an `INSTRUMENTAL` screen (the corner box should show chords there), one in a
|
||||
chorus.
|
||||
- **Fixing a chord.** Edit `work/<job_id>/chords.json` (list of
|
||||
`{"start", "end", "label"}` in that song's own source seconds) and re-run `video`. If a
|
||||
customer sends their own chord chart, enter it the same way: their chords replace the
|
||||
detected ones, and nothing else changes.
|
||||
- **Deliver both versions** (with and without chords), and say the chords are auto-detected.
|
||||
`{"start", "end", "label", "capo"}` in that song's own source seconds; `capo` optional)
|
||||
and re-run `video`.
|
||||
- **Deliver both versions** (with and without chords), and say the chords are auto-detected
|
||||
(unless they came from the customer's chart, below).
|
||||
|
||||
#### The customer's own chord chart (`chart` step)
|
||||
|
||||
When a customer sends a chart (chords written over lyrics, usually a Word doc), use it
|
||||
instead of detection: their chart says *which* chord and *where in the lyrics*, gen's
|
||||
reviewed word timings say *when*. First done in October 2026 for the country medley
|
||||
(customer chart in Courier New, capo shapes).
|
||||
|
||||
```bash
|
||||
# after `chords` (detected chords still fill intros and breaks), per job:
|
||||
# work/<job_id>/chart.txt (or chart.docx) - that song's part of the chart, in song order
|
||||
python scripts/medley/make_medley.py chart spec.json --wrap 56 # rewrites each chords.json
|
||||
python scripts/medley/make_medley.py video spec.json
|
||||
```
|
||||
|
||||
- **Making `chart.txt`.** `scripts/medley/chord_chart.py` reads .docx directly
|
||||
(`docx_text`). For a whole-medley doc, split it per song (a song used twice, like an opener
|
||||
plus finale, gets one file with both parts in order). Wrap stray headings mid-file in
|
||||
`[brackets]`. Titles and notes before the first chord/capo line, `[...]`/`(...)` lines and
|
||||
section labels (`Chorus:`, `Verse 2`, `Instrumental`) are ignored.
|
||||
- **Format it understands.** A chord line (only chord symbols) puts each chord on the lyric
|
||||
word under its first column (up to 2 blank lines between them are fine). Chords past the
|
||||
end of the line, or a chord line with no lyric under it (`C Em F G C` after
|
||||
"Instrumental"), are played in the break after the previous lyric: they're timed to the
|
||||
matching detected changes there, else spaced evenly. `Capo 2`, `No Capo` and
|
||||
`Key change Capo 1` lines set the capo from there on.
|
||||
- **`--wrap N` = page width in monospaced columns.** Word soft-wraps long paragraphs; a
|
||||
chord line glued to the end of a lyric paragraph is really on the next row. For Courier
|
||||
New 16pt on Letter with 0.5" margins it's 56 (7.5" × 72 / 9.6pt per char). Check one
|
||||
known chord after a wrapped line.
|
||||
- **Spelling is kept exactly** ("G/Em" means G or Em in this customer's charts, not a slash
|
||||
chord). Labels are capo **shapes**; the chord box shows "capo N" under the chord when
|
||||
capo > 0. Gaps the chart doesn't cover (intros, breaks, outros, spoken verses) get the
|
||||
detected chords transposed down by the capo into shapes (triads + dominant 7ths), using
|
||||
whichever neighbouring capo the chord fits (so a key-change pivot reads in the new
|
||||
capo). Fills that repeat the ringing chord, or anticipate the chart's next chord by up to
|
||||
2.5s, are dropped.
|
||||
- **Check it.** Print each song as lyrics with inline `[chord]`s against the chart. Lyrics
|
||||
are aligned word-by-word (`difflib`), so differences such as "nineteen-sixty-two" vs
|
||||
"1962" or a missing word are fine. A chart heading read as lyrics shifts the alignment;
|
||||
bracket it.
|
||||
|
||||
#### A single song, not a medley
|
||||
|
||||
@@ -216,8 +263,8 @@ PyTorch's safe `weights_only=True`; the third bundled checkpoint
|
||||
#### How rendering works (in gen)
|
||||
|
||||
`karaoke_gen/lyrics_transcriber/output/ass/chords.py`, used via
|
||||
`OutputGenerator.generate_outputs(chords=[ChordChange(start, end, label)])`. With no chords,
|
||||
output is byte-identical to before.
|
||||
`OutputGenerator.generate_outputs(chords=[ChordChange(start, end, label, capo=0)])`. With no
|
||||
chords, output is byte-identical to before.
|
||||
|
||||
- **Layout.** Each lyric slot gets a chord row above the line: the slot grows by 1.05 ×
|
||||
the chord font size, and lines per screen are capped at 4. It's the same mechanism as
|
||||
@@ -262,8 +309,10 @@ and a chord editor in review.
|
||||
or from a karaoke track. In the example, the brief asked for an "instrumental intro → verse 1
|
||||
→ chorus 1" on a song whose original starts singing at 0.4s and has no chorus until the third
|
||||
verse. Measure, then confirm the interpretation with the customer.
|
||||
- **Gain-match per section, then normalise once.** Don't run `loudnorm` on each section
|
||||
separately (it pumps); set static per-section gains, then process the whole medley once.
|
||||
- **Gain-match per section, then normalise once, statically.** Don't run `loudnorm` on each
|
||||
section (it pumps) or on the whole medley in its default dynamic mode (it boosts
|
||||
near-silent stretches, see step 4); set static per-section gains, then one static gain
|
||||
plus a limiter for the whole medley.
|
||||
|
||||
## Gotchas
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@
|
||||
## Recent Changes
|
||||
|
||||
- **Guitar chords in karaoke videos** (2026-10-16, v0.277.0). `OutputGenerator.generate_outputs(chords=[ChordChange(...)])` draws each chord above the word it lands on (it lights up when played) in a row reserved above every line, plus a "now / next" chord box in the top-right corner that also covers intros and instrumental breaks. Chords come from `scripts/medley/detect_chords.py`, an ensemble of ChordMini models over the mix, instrumental and harmonic stems. It's used by the medley script for now (`"chords": true`); there's no job/UI option yet. Theme keys: `chord_color`, `chord_font_size`/`chord_font_ratio`, `chord_max_visible_lines`. See [MEDLEYS.md § Guitar chords](MEDLEYS.md#guitar-chords-optional).
|
||||
- **Customer chord charts + medley mastering fix** (2026-10-17, v0.278.5). `make_medley.py chart` turns a customer's chords-over-lyrics chart (.txt or .docx, capo shapes kept, `ChordChange.capo` shown in the chord box) into timed chords on the reviewed lyrics, with detected chords filling intros and breaks (`scripts/medley/chord_chart.py`). The medley master is now one static gain + limiter instead of single-pass `loudnorm`, which had boosted a near-silent a cappella intro into robotic artifacts. See [MEDLEYS.md](MEDLEYS.md#the-customers-own-chord-chart-chart-step).
|
||||
- **Local E2E stack + email-code sign-in** (2026-10-15, v0.275.0). Plan: [archive/2026-10-15-local-stack-e2e-plan.md](archive/2026-10-15-local-stack-e2e-plan.md).
|
||||
- **Local stack.** `scripts/local-stack.sh up --frontend` runs the real API on the Firestore + GCS emulators with `FAKE_WORKERS=1`. In-process fake workers (`backend/services/fake_workers/`) move jobs through their real states in seconds with small real media; the search, download, Stripe Checkout, email, Secret Manager and encoding-VM seams are faked or skipped. The `/api/test/*` router (only mounted in that mode; startup refuses it in production) seeds scenarios, tunes the fakes per user, exposes captured email and serves a fake checkout page.
|
||||
- **New suite.** `frontend/e2e/local-stack` covers sign-up, make, review, instrumental, download, buying credits, the duration-confirm and failure states, and code sign-in through the real UI and API in about 1 minute. It runs in CI as the required `local-stack-e2e` job. Mobile Maestro flows will use the same stack. See [TESTING.md § Local stack](TESTING.md#local-stack-real-backend-fake-workers).
|
||||
|
||||
@@ -8,7 +8,7 @@ chord at all times, which covers intros and instrumental breaks where there's no
|
||||
line to sit a chord above.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, replace
|
||||
from typing import Callable, Dict, List, Optional, Sequence
|
||||
|
||||
from karaoke_gen.lyrics_transcriber.types import LyricsSegment
|
||||
@@ -25,6 +25,7 @@ class ChordChange:
|
||||
start: float
|
||||
end: float
|
||||
label: str
|
||||
capo: int = 0 # fret the label's shape is played at (shown in the chord box when > 0)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -40,10 +41,10 @@ def clean_chords(chords: Sequence[ChordChange]) -> List[ChordChange]:
|
||||
for c in sorted(chords, key=lambda c: c.start):
|
||||
if c.label.strip() in NO_CHORD_LABELS or c.end <= c.start:
|
||||
continue
|
||||
if out and out[-1].label == c.label and c.start - out[-1].end < 0.05:
|
||||
out[-1] = ChordChange(out[-1].start, c.end, c.label)
|
||||
if out and out[-1].label == c.label.strip() and out[-1].capo == c.capo and c.start - out[-1].end < 0.05:
|
||||
out[-1] = replace(out[-1], end=c.end)
|
||||
else:
|
||||
out.append(ChordChange(c.start, c.end, c.label.strip()))
|
||||
out.append(replace(c, label=c.label.strip()))
|
||||
return out
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import copy
|
||||
import dataclasses
|
||||
import os
|
||||
import logging
|
||||
from typing import List, Optional, Tuple, Union
|
||||
@@ -175,7 +176,7 @@ class SubtitlesGenerator:
|
||||
self.chords = clean_chords(chords or [])
|
||||
if self.chords and self.subtitle_offset_ms:
|
||||
off = self.subtitle_offset_ms / 1000.0
|
||||
self.chords = [ChordChange(c.start + off, c.end + off, c.label) for c in self.chords]
|
||||
self.chords = [dataclasses.replace(c, start=c.start + off, end=c.end + off) for c in self.chords]
|
||||
output_path = self._get_output_path(f"{output_prefix} (Karaoke)", "ass")
|
||||
|
||||
try:
|
||||
@@ -274,6 +275,8 @@ class SubtitlesGenerator:
|
||||
text = f"{{\\an9}}{{\\pos({x},{y})}}{{\\1c{now_colour}}}{chord.label}"
|
||||
if nxt:
|
||||
text += f"\\N{{\\fs{next_size}}}{{\\1c{next_colour}}}next: {nxt.label}"
|
||||
if chord.capo:
|
||||
text += f"\\N{{\\fs{next_size}}}{{\\1c{next_colour}}}capo {chord.capo}"
|
||||
event.Text = text
|
||||
events.append(event)
|
||||
return events
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "karaoke-gen"
|
||||
version = "0.278.4"
|
||||
version = "0.278.5"
|
||||
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
|
||||
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
|
||||
license = "MIT"
|
||||
|
||||
@@ -0,0 +1,369 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Turn a customer's chord chart (chords written over lyrics) into timed chords for one song.
|
||||
|
||||
python scripts/medley/chord_chart.py --chart chart.txt --lyrics corrections.json \\
|
||||
[--lyrics-updated corrections_updated.json] --detected chords_detected.json \\
|
||||
--out chords.json [--wrap 56]
|
||||
|
||||
`make_medley.py chart spec.json` runs this for every job that has a `chart.txt` (or
|
||||
`chart.docx`) in its work dir. See docs/MEDLEYS.md § Guitar chords.
|
||||
|
||||
The chart is the source of truth for WHICH chord and WHERE in the lyrics; gen's reviewed
|
||||
word timings say WHEN. Chart format (plain text in a monospaced layout, or a .docx of one):
|
||||
|
||||
Capo 2 <- "Capo N" / "No Capo" (alone or e.g. "Key change Capo 1")
|
||||
C Em F C <- chord line: only chord symbols
|
||||
Amarillo by mornin', up from San Antone <- lyric line: each chord goes on the word under it
|
||||
C Em F G C <- chord line with no lyric under it: played in the
|
||||
break after the previous lyric
|
||||
[Chorus] (Talking) # note Verse 2: <- ignored, as is everything before the first
|
||||
chord or capo line (title, notes)
|
||||
|
||||
Chords keep the chart's spelling (capo shapes, "G/Em"). Intros, breaks and outros the
|
||||
chart doesn't cover are filled from the detected chords (concert pitch), moved down by
|
||||
the capo so they read in the same shapes. Each output chord carries its "capo".
|
||||
"""
|
||||
import argparse
|
||||
import difflib
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
import zipfile
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
from xml.etree import ElementTree
|
||||
|
||||
CHORD_RE = re.compile(
|
||||
r"^(?:N\.?C\.?|[A-G][#b]?(?:maj|min|m|M|dim|aug|sus|add)?\d*(?:sus\d*|add\d+|[b#]\d+)*"
|
||||
r"(?:/[A-G][#b]?(?:maj|min|m)?\d*)?)$")
|
||||
CAPO_RE = re.compile(r"\b(?:no\s+capo|capo\s*(\d+))\b", re.I)
|
||||
COMMENT_RE = re.compile(r"^\s*(?:#.*|\[.*\]|\(.*\))\s*$")
|
||||
SECTION_RE = re.compile(r"^\s*(?:pre-?chorus|chorus|verse|bridge|intro|outro|instrumental|interlude|solo|"
|
||||
r"refrain|tag|ending)\b[\s\d:.)(-]*$", re.I) # "Chorus:", "Verse 2", "Instrumental"
|
||||
NAMES = ["C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B"] # guitar-shape spelling
|
||||
PITCH = {"C": 0, "C#": 1, "Db": 1, "D": 2, "D#": 3, "Eb": 3, "E": 4, "F": 5, "F#": 6, "Gb": 6, "G": 7,
|
||||
"G#": 8, "Ab": 8, "A": 9, "A#": 10, "Bb": 10, "B": 11}
|
||||
# A detected change this soon before the chart's next chord (same root) is that chord, early
|
||||
ANTICIPATION_SECONDS = 2.5
|
||||
# A chord this close before a line's first word belongs to that line (gen's CHORD_LINE_LEAD_SECONDS)
|
||||
LINE_LEAD_SECONDS = 0.6
|
||||
FALLBACK_CHORD_SECONDS = 2.0
|
||||
MAX_BLANKS_UNDER_CHORDS = 2
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChartChord:
|
||||
label: str
|
||||
capo: int
|
||||
word: Optional[int] = None # chart word index the chord sits over
|
||||
after: Optional[int] = None # free chord: played after this chart word (-1 = before the first)
|
||||
group: int = 0 # free chords from the same spot form a group
|
||||
time: Optional[float] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Chart:
|
||||
words: list = field(default_factory=list) # chart lyric words, in order
|
||||
chords: list = field(default_factory=list) # ChartChord
|
||||
lines: list = field(default_factory=list) # (first word, last word) of lyric lines that have chords
|
||||
|
||||
|
||||
# --- Reading -------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def docx_text(path: str) -> str:
|
||||
"""Paragraph text of a .docx (line breaks and tabs kept), without python-docx."""
|
||||
ns = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
|
||||
with zipfile.ZipFile(path) as z:
|
||||
root = ElementTree.fromstring(z.read("word/document.xml"))
|
||||
paras = []
|
||||
for p in root.iter(f"{ns}p"):
|
||||
text = ""
|
||||
# Only run content: <w:tabs> in the paragraph properties are tab stops, not tabs
|
||||
for el in (child for run in p.iter(f"{ns}r") for child in run):
|
||||
if el.tag == f"{ns}t":
|
||||
text += el.text or ""
|
||||
elif el.tag == f"{ns}tab":
|
||||
text += "\t"
|
||||
elif el.tag in (f"{ns}br", f"{ns}cr"):
|
||||
text += "\n"
|
||||
paras.append(text)
|
||||
return "\n".join(paras)
|
||||
|
||||
|
||||
def chart_lines(text: str, wrap: Optional[int] = None) -> list[str]:
|
||||
"""Lines as they appear on the page: soft breaks split, tabs expanded, trailing space
|
||||
dropped, and (wrap=N) paragraphs longer than N monospaced columns broken like Word does."""
|
||||
out = []
|
||||
for line in text.replace("
", "\n").replace("\r", "").split("\n"):
|
||||
line = line.expandtabs(8).rstrip()
|
||||
while wrap and len(line) > wrap:
|
||||
out.append(line[:wrap].rstrip())
|
||||
line = line[wrap:]
|
||||
out.append(line)
|
||||
return out
|
||||
|
||||
|
||||
def is_chord_line(line: str) -> bool:
|
||||
tokens = line.split()
|
||||
return bool(tokens) and all(CHORD_RE.match(t) for t in tokens)
|
||||
|
||||
|
||||
def is_comment(line: str) -> bool:
|
||||
return bool(COMMENT_RE.match(line) or SECTION_RE.match(line))
|
||||
|
||||
|
||||
def capo_directive(line: str) -> Optional[int]:
|
||||
"""Capo for 'Capo 2' / 'No Capo' / 'Key change Capo 1' lines, else None."""
|
||||
m = CAPO_RE.search(line)
|
||||
if not m or len(line.split()) > 5:
|
||||
return None
|
||||
return int(m.group(1)) if m.group(1) else 0
|
||||
|
||||
|
||||
def norm_word(w: str) -> str:
|
||||
return re.sub(r"[^a-z0-9']", "", w.lower().replace("’", "'"))
|
||||
|
||||
|
||||
def parse_chart(text: str, wrap: Optional[int] = None) -> Chart:
|
||||
chart, capo, group = Chart(), 0, 0
|
||||
lines = chart_lines(text, wrap)
|
||||
i = 0
|
||||
started = False # lines before the first chord / capo line are the title and notes
|
||||
while i < len(lines):
|
||||
line = lines[i]
|
||||
i += 1
|
||||
started = started or is_chord_line(line) or capo_directive(line) is not None
|
||||
if not started or not line.strip() or is_comment(line):
|
||||
continue
|
||||
directive = capo_directive(line)
|
||||
if directive is not None:
|
||||
capo = directive
|
||||
continue
|
||||
if is_chord_line(line):
|
||||
chords = [(m.start(), m.group()) for m in re.finditer(r"\S+", line)]
|
||||
j = i
|
||||
while j < len(lines) and j - i < MAX_BLANKS_UNDER_CHORDS and not lines[j].strip():
|
||||
j += 1 # Word charts often have an empty paragraph between chords and lyric
|
||||
lyric = lines[j] if j < len(lines) else ""
|
||||
if lyric.strip() and not is_chord_line(lyric) and capo_directive(lyric) is None \
|
||||
and not is_comment(lyric):
|
||||
i = j + 1
|
||||
_add_line(chart, chords, lyric, capo, group)
|
||||
else:
|
||||
for _, label in chords:
|
||||
chart.chords.append(ChartChord(label, capo, after=len(chart.words) - 1, group=group))
|
||||
group += 1
|
||||
continue
|
||||
_add_line(chart, [], line, capo, group)
|
||||
return chart
|
||||
|
||||
|
||||
def _add_line(chart: Chart, chords, lyric: str, capo: int, group: int):
|
||||
"""Lyric words, with each chord on the word under its first column (or the next word)."""
|
||||
spans = [(m.start(), m.end(), m.group()) for m in re.finditer(r"\S+", lyric)]
|
||||
base = len(chart.words)
|
||||
chart.words += [norm_word(w) for _, _, w in spans]
|
||||
if chords and spans:
|
||||
chart.lines.append((base, base + len(spans) - 1))
|
||||
used = set()
|
||||
for col, label in chords:
|
||||
target = next((k for k, (s, e, _) in enumerate(spans) if col < e), None)
|
||||
if target is None or target in used: # past the end of the line, or a 2nd chord on a word
|
||||
after = base + (target if target is not None else len(spans) - 1)
|
||||
chart.chords.append(ChartChord(label, capo, after=after, group=group))
|
||||
else:
|
||||
used.add(target)
|
||||
chart.chords.append(ChartChord(label, capo, word=base + target))
|
||||
|
||||
|
||||
# --- Timing --------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def root_pitches(label: str) -> set:
|
||||
"""Pitch classes a label can be matched on: root, plus the part after '/' ("G/Em" = G or Em)."""
|
||||
out = set()
|
||||
for part in label.split("/"):
|
||||
m = re.match(r"[A-G][#b]?", part)
|
||||
if m:
|
||||
out.add(PITCH[m.group()])
|
||||
return out
|
||||
|
||||
|
||||
def to_shape(label: str, capo: int) -> str:
|
||||
"""Concert-pitch detected chord -> the shape played with this capo, simplified to the
|
||||
chart's vocabulary (triads, plus dominant 7ths)."""
|
||||
m = re.match(r"([A-G][#b]?)(.*)", label)
|
||||
if not m:
|
||||
return label
|
||||
quality = m.group(2).split("/")[0]
|
||||
quality = {"m7": "m", "maj7": "", "sus2": "", "sus4": "", "m7b5": "dim", "dim7": "dim"}.get(quality, quality)
|
||||
return NAMES[(PITCH[m.group(1)] - capo) % 12] + quality
|
||||
|
||||
|
||||
def align_words(chart_words: list[str], sung: list[str]) -> dict:
|
||||
"""chart word index -> sung word index, from matching runs (missed words interpolated)."""
|
||||
matcher = difflib.SequenceMatcher(a=chart_words, b=sung, autojunk=False)
|
||||
exact = {}
|
||||
for a, b, size in matcher.get_matching_blocks():
|
||||
for k in range(size):
|
||||
exact[a + k] = b + k
|
||||
if not exact:
|
||||
raise ValueError("chart lyrics don't match the song's lyrics at all")
|
||||
keys = sorted(exact)
|
||||
out = {}
|
||||
for i in range(len(chart_words)):
|
||||
if i in exact:
|
||||
out[i] = exact[i]
|
||||
continue
|
||||
lo = max((k for k in keys if k < i), default=None)
|
||||
hi = min((k for k in keys if k > i), default=None)
|
||||
if lo is None:
|
||||
out[i] = max(0, exact[hi] - (hi - i))
|
||||
elif hi is None:
|
||||
out[i] = min(len(sung) - 1, exact[lo] + (i - lo))
|
||||
else:
|
||||
out[i] = round(exact[lo] + (exact[hi] - exact[lo]) * (i - lo) / (hi - lo))
|
||||
return out
|
||||
|
||||
|
||||
def chart_to_chords(chart: Chart, sung: list[tuple], detected: list[dict], duration: float) -> list[dict]:
|
||||
"""Timed chords. sung = [(text, start, end)] for every reviewed lyric word, in order."""
|
||||
if not chart.chords:
|
||||
raise ValueError("no chords found in the chart")
|
||||
mapping = align_words(chart.words, [norm_word(w[0]) for w in sung])
|
||||
chords = [ChartChord(c.label, c.capo, c.word, c.after, c.group) for c in chart.chords]
|
||||
for c in chords:
|
||||
if c.word is not None:
|
||||
c.time = sung[mapping[c.word]][1]
|
||||
for group in sorted({c.group for c in chords if c.word is None}):
|
||||
_time_free_group([c for c in chords if c.word is None and c.group == group], chords, sung, mapping,
|
||||
detected, duration)
|
||||
out = [{"start": c.time, "label": c.label, "capo": c.capo} for c in chords]
|
||||
out += _fill_gaps(chart, chords, sung, mapping, detected)
|
||||
out.sort(key=lambda c: c["start"])
|
||||
for a, b in zip(out, out[1:]):
|
||||
a["end"] = b["start"]
|
||||
if out:
|
||||
last_detected = max((d["end"] for d in detected), default=0.0)
|
||||
out[-1]["end"] = min(duration, max(last_detected, out[-1]["start"] + FALLBACK_CHORD_SECONDS))
|
||||
return [{"start": round(c["start"], 3), "end": round(c["end"], 3), "label": c["label"], "capo": c["capo"]}
|
||||
for c in out if c["end"] - c["start"] > 0.05]
|
||||
|
||||
|
||||
def _time_free_group(group, chords, sung, mapping, detected, duration):
|
||||
"""Chords played after a lyric (no word under them): matched in order to detected changes
|
||||
between that word and the next timed chord / sung word, else spaced evenly."""
|
||||
after = group[0].after
|
||||
w = mapping.get(after) if after is not None and after >= 0 else None
|
||||
t0 = sung[w][2] if w is not None else 0.0
|
||||
# Earlier groups after the same word (chords past the line end, then a chords-only
|
||||
# break line) are played first: start after them
|
||||
prior = [c.time for c in chords if c.word is None and c.after == after and c.time is not None]
|
||||
if prior:
|
||||
t0 = max(prior)
|
||||
nxt_word = sung[w + 1][1] if w is not None and w + 1 < len(sung) else (sung[0][1] if w is None else duration)
|
||||
later = [c.time for c in chords if c.time is not None and c.time > t0]
|
||||
t1 = min([nxt_word] + later)
|
||||
window = [d for d in detected if t0 - 0.3 <= d["start"] < t1 - 0.3]
|
||||
cursor = t0 if prior else t0 - 0.3
|
||||
for c in group:
|
||||
hit = next((d for d in window if d["start"] > cursor and
|
||||
root_pitches(to_shape(d["label"], c.capo)) & root_pitches(c.label)), None)
|
||||
if hit:
|
||||
c.time = cursor = hit["start"]
|
||||
prev = t0
|
||||
for k, c in enumerate(group): # the rest: evenly between their timed neighbours
|
||||
if c.time is None:
|
||||
j = next((j for j in range(k + 1, len(group)) if group[j].time is not None), None)
|
||||
missing = (j if j is not None else len(group)) - k
|
||||
if j is not None:
|
||||
step = (group[j].time - prev) / (missing + 1)
|
||||
else:
|
||||
step = min(FALLBACK_CHORD_SECONDS, (t1 - prev) / (missing + 1))
|
||||
c.time = prev + step
|
||||
prev = c.time
|
||||
|
||||
|
||||
def _fill_gaps(chart, chords, sung, mapping, detected) -> list[dict]:
|
||||
"""Detected chords wherever no charted line is being sung: intros, breaks, outros, the
|
||||
gaps between lines and uncharted lines (e.g. a spoken verse). Dropped when they only
|
||||
repeat the chord already ringing (same root) or anticipate the chart's next chord; moved into the
|
||||
shapes of whichever neighbouring capo they fit (that capo's chart chords)."""
|
||||
sung_lines = [(sung[mapping[a]][1] - 0.3, sung[mapping[b]][2]) for a, b in chart.lines]
|
||||
timed = sorted((c for c in chords if c.time is not None), key=lambda c: c.time)
|
||||
free = {}
|
||||
for c in chords:
|
||||
if c.word is None and c.time is not None:
|
||||
free.setdefault(c.group, []).append(c.time)
|
||||
charted = sung_lines + [(min(ts) - 0.3, max(ts) + 0.3) for ts in free.values()]
|
||||
vocab = {}
|
||||
for c in chords:
|
||||
vocab.setdefault(c.capo, set()).update(root_pitches(c.label))
|
||||
out = []
|
||||
for d in sorted(detected, key=lambda d: d["start"]):
|
||||
t = d["start"]
|
||||
if any(a <= t <= b for a, b in charted):
|
||||
continue
|
||||
before = [c for c in timed if c.time <= t][-1:]
|
||||
after = [c for c in timed if c.time > t][:1]
|
||||
capos = [x.capo for x in before + after]
|
||||
fits = [cp for cp in capos if root_pitches(to_shape(d["label"], cp)) & vocab.get(cp, set())]
|
||||
capo = fits[-1] if fits else (capos[-1] if capos else 0)
|
||||
label = to_shape(d["label"], capo)
|
||||
ringing = out[-1] if out and (not before or out[-1]["start"] > before[0].time) else (
|
||||
{"label": before[0].label, "capo": before[0].capo} if before else None)
|
||||
if ringing and ringing["capo"] == capo and root_pitches(label) & root_pitches(ringing["label"]):
|
||||
continue # same chord (D7 under a charted D, Em under "G/Em")
|
||||
if after and after[0].time - t < ANTICIPATION_SECONDS and after[0].capo == capo \
|
||||
and root_pitches(label) & root_pitches(after[0].label):
|
||||
continue
|
||||
out.append({"start": t, "label": label, "capo": capo})
|
||||
return out
|
||||
|
||||
|
||||
# --- CLI -----------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def read_chart(path: str, wrap: Optional[int]) -> Chart:
|
||||
text = docx_text(path) if path.lower().endswith(".docx") else open(path, encoding="utf-8").read()
|
||||
return parse_chart(text, wrap)
|
||||
|
||||
|
||||
def sung_words(corrections: str, updated: Optional[str] = None) -> list[tuple]:
|
||||
"""(text, start, end) of every reviewed word, via gen's own correction loading."""
|
||||
from karaoke_gen.lyrics_transcriber.correction.operations import CorrectionOperations
|
||||
from karaoke_gen.lyrics_transcriber.types import CorrectionResult
|
||||
with open(corrections) as f:
|
||||
result = CorrectionResult.from_dict(json.load(f))
|
||||
if updated:
|
||||
with open(updated) as f:
|
||||
data = json.load(f)
|
||||
if isinstance(data, dict) and "corrections" in data:
|
||||
result = CorrectionOperations.update_correction_result_with_data(result, data)
|
||||
return [(w.text, w.start_time, w.end_time) for seg in result.corrected_segments for w in seg.words]
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
p.add_argument("--chart", required=True, help="chord chart: .txt or .docx")
|
||||
p.add_argument("--lyrics", required=True, help="the job's corrections.json")
|
||||
p.add_argument("--lyrics-updated", help="the job's corrections_updated.json (reviewed edits)")
|
||||
p.add_argument("--detected", required=True, help="detected chords (detect_chords.py output)")
|
||||
p.add_argument("--duration", type=float, help="song length (default: end of the last detected chord)")
|
||||
p.add_argument("--wrap", type=int, help="page width in columns, to undo Word's soft wrapping of long lines")
|
||||
p.add_argument("--out", required=True)
|
||||
args = p.parse_args(argv)
|
||||
with open(args.detected) as f:
|
||||
detected = json.load(f)
|
||||
duration = args.duration or max(d["end"] for d in detected)
|
||||
chords = chart_to_chords(read_chart(args.chart, args.wrap), sung_words(args.lyrics, args.lyrics_updated),
|
||||
detected, duration)
|
||||
with open(args.out, "w") as f:
|
||||
json.dump(chords, f, indent=1)
|
||||
print(f"{len(chords)} chord changes -> {args.out}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -15,6 +15,8 @@ Usage (from the repo root, in an env where karaoke_gen imports):
|
||||
python scripts/medley/make_medley.py video spec.json [--resolution 4k] # karaoke + guide MP4s
|
||||
python scripts/medley/make_medley.py chords spec.json # optional: detect guitar chords per song
|
||||
# (needs MEDLEY_CHORDMINI; see docs)
|
||||
python scripts/medley/make_medley.py chart spec.json [--wrap 56] # optional: the customer's own
|
||||
# chord chart(s) instead (after `chords`)
|
||||
|
||||
Requires Firestore read access (ADC; the claude-readonly SA is enough), the gcloud CLI for GCS
|
||||
downloads (set MEDLEY_GCLOUD_ACCOUNT to pick an account, e.g. the read-only SA when your user
|
||||
@@ -27,6 +29,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
@@ -37,7 +40,7 @@ BUCKET = "karaoke-gen-storage-nomadkaraoke"
|
||||
DEFAULT_CROSSFADE = 2.0
|
||||
SEGMENT_TARGET_LUFS = -18.0
|
||||
FINAL_LUFS = -14.0
|
||||
FINAL_TRUE_PEAK = -1.0
|
||||
FINAL_PEAK_DB = -1.0
|
||||
# Audio variants built by `audio`: name -> stem file (None = the job's original input audio)
|
||||
VARIANTS = {
|
||||
"instrumental_with_backing": "instrumental_with_backing.flac",
|
||||
@@ -136,7 +139,7 @@ def shift_chords(chords: list[dict], src_start: float, src_end: float, offset: f
|
||||
for c in chords:
|
||||
start, end = max(c["start"], lo), min(c["end"], hi)
|
||||
if end - start > 0.05:
|
||||
out.append({"start": start + offset, "end": end + offset, "label": c["label"]})
|
||||
out.append({**c, "start": start + offset, "end": end + offset})
|
||||
return out
|
||||
|
||||
|
||||
@@ -153,7 +156,10 @@ def medley_chords(sections: list[Section], durations: list[float], offsets: list
|
||||
|
||||
|
||||
def build_filtergraph(sections: list[Section], durations: list[float], gains_db: list[float]) -> str:
|
||||
"""ffmpeg filter_complex joining inputs 0..n-1 (one per section, already trimmed) into [out]."""
|
||||
"""ffmpeg filter_complex joining inputs 0..n-1 (one per section, already trimmed) into [out].
|
||||
|
||||
Un-mastered: `master_filter` brings the result to FINAL_LUFS in a second pass.
|
||||
"""
|
||||
chains = []
|
||||
for i, (sec, dur, gain) in enumerate(zip(sections, durations, gains_db)):
|
||||
f = f"[{i}:a]aformat=sample_rates=44100:channel_layouts=stereo,volume={gain:.2f}dB"
|
||||
@@ -174,10 +180,22 @@ def build_filtergraph(sections: list[Section], durations: list[float], gains_db:
|
||||
else:
|
||||
chains.append(f"[{cur}][s{i}]concat=n=2:v=0:a=1[{out}]")
|
||||
cur = out
|
||||
chains.append(f"[{cur}]loudnorm=I={FINAL_LUFS}:TP={FINAL_TRUE_PEAK}:LRA=11,aresample=44100[out]")
|
||||
chains.append(f"[{cur}]anull[out]")
|
||||
return ";".join(chains)
|
||||
|
||||
|
||||
def master_filter(measured_lufs: float) -> str:
|
||||
"""One static gain to FINAL_LUFS, then a peak limiter (4x oversampled ~ true peak).
|
||||
|
||||
Never single-pass loudnorm: its dynamic mode rides the gain on a 3s window, so a
|
||||
near-silent stretch (an a cappella pickup, where the instrumental is just separation
|
||||
residue) gets boosted ~50 dB into loud robotic artifacts.
|
||||
"""
|
||||
limit = 10 ** (FINAL_PEAK_DB / 20)
|
||||
return (f"volume={FINAL_LUFS - measured_lufs:.2f}dB,aresample=176400,"
|
||||
f"alimiter=limit={limit:.4f}:attack=5:release=50:level=false:latency=true,aresample=44100")
|
||||
|
||||
|
||||
# --- I/O helpers ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -285,8 +303,12 @@ def cmd_audio(spec: dict, workdir: str, outdir: str):
|
||||
gains.append(SEGMENT_TARGET_LUFS - measure_lufs(p, sec.start, sec.end))
|
||||
inputs += ["-ss", str(sec.start)] + (["-to", str(sec.end)] if sec.end is not None else []) + ["-i", p]
|
||||
wav = os.path.join(outdir, f"medley_{variant}.wav")
|
||||
raw = wav[:-4] + ".unmastered.wav"
|
||||
_run(["ffmpeg", "-y", "-hide_banner", "-loglevel", "error"] + inputs +
|
||||
["-filter_complex", build_filtergraph(sections, durations, gains), "-map", "[out]", "-c:a", "pcm_s24le", wav])
|
||||
["-filter_complex", build_filtergraph(sections, durations, gains), "-map", "[out]", "-c:a", "pcm_f32le", raw])
|
||||
_run(["ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-i", raw,
|
||||
"-af", master_filter(measure_lufs(raw, 0.0, None)), "-c:a", "pcm_s24le", wav])
|
||||
os.remove(raw)
|
||||
_run(["ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-i", wav, "-c:a", "libmp3lame", "-b:a", "320k",
|
||||
wav[:-4] + ".mp3"])
|
||||
log.info("%s: %.1fs", wav, probe_duration(wav))
|
||||
@@ -315,10 +337,37 @@ def cmd_chords(spec: dict, workdir: str):
|
||||
harm = _harmonic_stem(d, variant_path(workdir, job_id, GUIDE_VARIANT))
|
||||
if harm:
|
||||
inputs.append(f"harm={harm}")
|
||||
args = [python, detect, "--chordmini", chordmini, "--out", os.path.join(d, "chords.json")]
|
||||
args = [python, detect, "--chordmini", chordmini, "--out", os.path.join(d, "chords_detected.json")]
|
||||
for i in inputs:
|
||||
args += ["--input", i]
|
||||
_run(args)
|
||||
shutil.copyfile(os.path.join(d, "chords_detected.json"), os.path.join(d, "chords.json"))
|
||||
|
||||
|
||||
def cmd_chart(spec: dict, workdir: str, wrap: Optional[int]):
|
||||
"""chords.json from the customer's chord chart (<job>/chart.txt or chart.docx), timed on the
|
||||
reviewed lyrics; chords_detected.json (from `chords`) fills intros and breaks."""
|
||||
import chord_chart
|
||||
for job_id in dict.fromkeys(s["job_id"] for s in spec["sections"]):
|
||||
d = job_dir(workdir, job_id)
|
||||
chart = next((os.path.join(d, n) for n in ("chart.txt", "chart.docx") if os.path.exists(os.path.join(d, n))), None)
|
||||
if chart is None:
|
||||
log.info("%s: no chart, keeping its chords.json", job_id)
|
||||
continue
|
||||
detected = os.path.join(d, "chords_detected.json")
|
||||
if not os.path.exists(detected):
|
||||
if not os.path.exists(os.path.join(d, "chords.json")):
|
||||
raise SystemExit(f"{job_id}: run the `chords` step first (breaks are filled from detected chords)")
|
||||
shutil.copyfile(os.path.join(d, "chords.json"), detected) # chords.json from before `chart` existed
|
||||
with open(detected) as f:
|
||||
detected_chords = json.load(f)
|
||||
updated = os.path.join(d, "corrections_updated.json")
|
||||
sung = chord_chart.sung_words(os.path.join(d, "corrections.json"), updated if os.path.exists(updated) else None)
|
||||
chords = chord_chart.chart_to_chords(chord_chart.read_chart(chart, wrap), sung, detected_chords,
|
||||
probe_duration(variant_path(workdir, job_id, GUIDE_VARIANT)))
|
||||
with open(os.path.join(d, "chords.json"), "w") as f:
|
||||
json.dump(chords, f, indent=1)
|
||||
log.info("%s: %d chord changes from %s", job_id, len(chords), os.path.basename(chart))
|
||||
|
||||
|
||||
def _harmonic_stem(d: str, source: str) -> Optional[str]:
|
||||
@@ -409,7 +458,7 @@ def cmd_video(spec: dict, workdir: str, outdir: str, resolution: str):
|
||||
raise SystemExit(f"no chords for {job_id}: run the `chords` step first")
|
||||
with open(path) as f:
|
||||
by_job[job_id] = json.load(f)
|
||||
chords = [ChordChange(c["start"] + pad, c["end"] + pad, c["label"])
|
||||
chords = [ChordChange(c["start"] + pad, c["end"] + pad, c["label"], c.get("capo", 0))
|
||||
for c in medley_chords(sections, durations, offsets, by_job)]
|
||||
log.info("guitar chords: %d changes", len(chords))
|
||||
|
||||
@@ -452,9 +501,10 @@ def cmd_video(spec: dict, workdir: str, outdir: str, resolution: str):
|
||||
|
||||
def main(argv=None):
|
||||
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
p.add_argument("command", choices=["fetch", "audio", "video", "chords"])
|
||||
p.add_argument("command", choices=["fetch", "audio", "video", "chords", "chart"])
|
||||
p.add_argument("spec")
|
||||
p.add_argument("--resolution", default="4k", choices=["4k", "1080p", "720p", "360p"])
|
||||
p.add_argument("--wrap", type=int, help="chart: page width in columns (undoes Word's soft wrapping)")
|
||||
args = p.parse_args(argv)
|
||||
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
||||
with open(args.spec) as f:
|
||||
@@ -467,6 +517,8 @@ def main(argv=None):
|
||||
cmd_fetch(spec, workdir)
|
||||
elif args.command == "chords":
|
||||
cmd_chords(spec, workdir)
|
||||
elif args.command == "chart":
|
||||
cmd_chart(spec, workdir, args.wrap)
|
||||
elif args.command == "audio":
|
||||
cmd_audio(spec, workdir, outdir)
|
||||
else:
|
||||
|
||||
@@ -168,3 +168,16 @@ def test_chord_colour_comes_from_theme(tmp_path):
|
||||
ass = generate(tmp_path, segments, [ChordChange(5.5, 9, "C")], {"chord_color": "10, 20, 30"})
|
||||
style = next(line for line in ass.splitlines() if line.startswith("Style: Karaoke.Chord,"))
|
||||
assert "&H001E140A" in style # secondary (upcoming) colour = theme chord_color (BGR)
|
||||
|
||||
|
||||
def test_clean_chords_keeps_capo_and_splits_on_capo_change():
|
||||
chords = clean_chords([ChordChange(0, 2, "G", 0), ChordChange(2, 4, "G", 1), ChordChange(4, 6, "G", 1)])
|
||||
assert [(c.label, c.capo, c.start, c.end) for c in chords] == [("G", 0, 0, 2), ("G", 1, 2, 6)]
|
||||
|
||||
|
||||
def test_chord_box_shows_capo(tmp_path):
|
||||
segments = [seg("hello there world", 5.0, "a")]
|
||||
ass = generate(tmp_path, segments, [ChordChange(0, 5.5, "G"), ChordChange(5.5, 9, "C", 2)])
|
||||
box = dialogues(ass, "Karaoke.ChordBox")
|
||||
assert "capo" not in box[0] # capo 0 = nothing to say
|
||||
assert box[1].endswith("capo 2")
|
||||
@@ -0,0 +1,162 @@
|
||||
"""Unit tests for importing a customer's chord chart (scripts/medley/chord_chart.py)."""
|
||||
|
||||
import importlib.util
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
_SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "medley" / "chord_chart.py"
|
||||
_spec = importlib.util.spec_from_file_location("chord_chart", _SCRIPT)
|
||||
cc = importlib.util.module_from_spec(_spec)
|
||||
sys.modules[_spec.name] = cc
|
||||
_spec.loader.exec_module(cc)
|
||||
|
||||
|
||||
def sung_line(text: str, start: float, step: float = 0.5):
|
||||
"""(text, start, end) per word, back to back."""
|
||||
return [(w, start + i * step, start + (i + 1) * step - 0.05) for i, w in enumerate(text.split())]
|
||||
|
||||
|
||||
# ---- reading ----
|
||||
|
||||
|
||||
def test_docx_text_keeps_spacing_and_ignores_tab_stops(tmp_path):
|
||||
ns = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"'
|
||||
body = (f'<w:document {ns}><w:body>'
|
||||
'<w:p><w:pPr><w:tabs><w:tab w:val="left" w:pos="916"/></w:tabs></w:pPr>'
|
||||
'<w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>G</w:t></w:r></w:p>'
|
||||
'<w:p><w:r><w:t>He said</w:t><w:br/><w:t>hi</w:t><w:tab/><w:t>x</w:t></w:r></w:p>'
|
||||
'</w:body></w:document>')
|
||||
path = tmp_path / "chart.docx"
|
||||
with zipfile.ZipFile(path, "w") as z:
|
||||
z.writestr("word/document.xml", body)
|
||||
assert cc.docx_text(str(path)) == " G\nHe said\nhi\tx"
|
||||
|
||||
|
||||
def test_chart_lines_undo_soft_wrapping():
|
||||
long = "Everything I got on" + " " * 41 + "F G" # F at column 60 = 3rd row of 30
|
||||
assert cc.chart_lines(long + "\nWhen that sun", wrap=30) == [
|
||||
"Everything I got on", "", "F G", "When that sun"]
|
||||
assert cc.chart_lines("C
Some words") == ["C", "Some words"] # Word soft line break
|
||||
|
||||
|
||||
def test_chord_and_capo_recognition():
|
||||
assert cc.is_chord_line("G/Em C D7 F#m Bbmaj7 Asus4 N.C.")
|
||||
assert not cc.is_chord_line("Amarillo by mornin'")
|
||||
assert cc.capo_directive("Capo 2") == 2
|
||||
assert cc.capo_directive("No Capo ") == 0
|
||||
assert cc.capo_directive("Key change Capo 1") == 1
|
||||
assert cc.capo_directive("He put a capo on and played it all night long") is None
|
||||
assert cc.is_comment("Chorus:") and cc.is_comment("[Chorus]") and cc.is_comment("(Talking)")
|
||||
assert cc.is_comment("Instrumental") and cc.is_comment("Verse 2")
|
||||
assert not cc.is_comment("hearts:") # a lyric that happens to end in a colon
|
||||
|
||||
|
||||
# ---- parsing ----
|
||||
|
||||
|
||||
CHART = """\
|
||||
"My Song" (Full intro)
|
||||
Capo 2
|
||||
C Em
|
||||
Amarillo by mornin'
|
||||
|
||||
G/Em C D
|
||||
Remember when.
|
||||
|
||||
Instrumental
|
||||
C Em F
|
||||
Key change Capo 3
|
||||
G
|
||||
He stopped loving her
|
||||
"""
|
||||
|
||||
|
||||
def test_parse_chart():
|
||||
chart = cc.parse_chart(CHART)
|
||||
assert chart.words == ["amarillo", "by", "mornin'", "remember", "when", "he", "stopped", "loving", "her"]
|
||||
got = [(c.label, c.capo, c.word, c.after) for c in chart.chords]
|
||||
assert got == [
|
||||
("C", 2, 0, None), ("Em", 2, 2, None), # over "Amarillo" and "mornin'"
|
||||
("G/Em", 2, 4, None), # over "when." (blank line in between is fine)
|
||||
("C", 2, None, 4), ("D", 2, None, 4), # past the end of the line: played after it
|
||||
("C", 2, None, 4), ("Em", 2, None, 4), ("F", 2, None, 4), # chords-only line: the break
|
||||
("G", 3, 7, None), # col 14 = "loving", after the capo change
|
||||
]
|
||||
assert chart.lines == [(0, 2), (3, 4), (5, 8)]
|
||||
assert len({c.group for c in chart.chords if c.word is None}) == 2
|
||||
|
||||
|
||||
# ---- timing ----
|
||||
|
||||
|
||||
def test_to_shape_and_roots():
|
||||
assert cc.to_shape("D", 2) == "C" and cc.to_shape("F#m7", 2) == "Em" and cc.to_shape("Ab", 1) == "G"
|
||||
assert cc.to_shape("Eb7", 1) == "D7" and cc.to_shape("Gmaj7", 0) == "G"
|
||||
assert cc.root_pitches("G/Em") == {7, 4}
|
||||
|
||||
|
||||
def test_align_words_interpolates_misses():
|
||||
mapping = cc.align_words(["dated", "nineteensixtytwo", "he"], ["dated", "1962", "he"])
|
||||
assert mapping == {0: 0, 1: 1, 2: 2}
|
||||
with pytest.raises(ValueError):
|
||||
cc.align_words(["la"], ["nothing", "alike"])
|
||||
|
||||
|
||||
def test_chart_to_chords_end_to_end():
|
||||
chart = cc.parse_chart("""\
|
||||
No Capo
|
||||
G
|
||||
He said I'll love you til die
|
||||
C
|
||||
Hoping she'd come back
|
||||
Key change Capo 1
|
||||
G
|
||||
Kept some letters
|
||||
""")
|
||||
sung = sung_line("He said I'll love you til die", 10.0) + sung_line("Hoping she'd come back", 15.0) \
|
||||
+ sung_line("Kept some letters", 25.0)
|
||||
detected = [
|
||||
{"start": 2.0, "end": 6.0, "label": "C"}, # intro (uncharted): kept, no capo
|
||||
{"start": 6.0, "end": 13.0, "label": "G"}, # intro: kept
|
||||
{"start": 13.0, "end": 17.0, "label": "G"}, # mid-line: the chart rules
|
||||
{"start": 17.0, "end": 21.0, "label": "C"}, # break: same chord as the one ringing -> dropped
|
||||
{"start": 21.0, "end": 24.0, "label": "Eb"}, # break: pivot into the new key -> D, capo 1
|
||||
{"start": 24.0, "end": 26.0, "label": "Ab"}, # just before the line, anticipates its G -> dropped
|
||||
{"start": 26.0, "end": 30.0, "label": "Ab"},
|
||||
]
|
||||
chords = cc.chart_to_chords(chart, sung, detected, duration=32.0)
|
||||
got = [(c["start"], c["label"], c["capo"]) for c in chords]
|
||||
assert got == [(2.0, "C", 0), (6.0, "G", 0), (13.0, "G", 0), (16.0, "C", 0), (21.0, "D", 1), (26.0, "G", 1)]
|
||||
assert chords[-1]["end"] == 30.0 # last detected chord's end
|
||||
assert all(a["end"] == b["start"] for a, b in zip(chords, chords[1:]))
|
||||
|
||||
|
||||
def test_free_chords_follow_detected_changes_or_spread_evenly():
|
||||
chart = cc.parse_chart("""\
|
||||
G C D
|
||||
Remember when.
|
||||
G
|
||||
Again
|
||||
""")
|
||||
sung = sung_line("Remember when.", 10.0) + sung_line("Again", 20.0)
|
||||
detected = [{"start": 12.0, "end": 14.0, "label": "C"}] # C heard at 12; D not detected
|
||||
chords = cc.chart_to_chords(chart, sung, detected, duration=25.0)
|
||||
(g, g_at), (c, c_at), (d, d_at), (g2, g2_at) = [(c["label"], c["start"]) for c in chords]
|
||||
assert (g, c, d, g2) == ("G", "C", "D", "G")
|
||||
assert g_at == 10.5 # on "when."
|
||||
assert c_at == 12.0 # matched to the detected change
|
||||
assert 12.0 < d_at <= 14.0 # spread after it
|
||||
assert g2_at == 20.0
|
||||
|
||||
|
||||
def test_break_line_after_trailing_chords_keeps_chart_order():
|
||||
chart = cc.parse_chart(CHART)
|
||||
sung = sung_line("Amarillo by mornin'", 10.0) + sung_line("Remember when.", 14.0) \
|
||||
+ sung_line("He stopped loving her", 40.0)
|
||||
chords = cc.chart_to_chords(chart, sung, [], duration=45.0)
|
||||
labels = [c["label"] for c in chords]
|
||||
assert labels == ["C", "Em", "G/Em", "C", "D", "C", "Em", "F", "G"]
|
||||
assert all(a["start"] < b["start"] for a, b in zip(chords, chords[1:]))
|
||||
@@ -106,7 +106,8 @@ class TestBuildFiltergraph:
|
||||
assert "apad=pad_dur=0.700" in graph # silence before the gap join
|
||||
assert "afade=t=in:st=0:d=0.500" in graph # pot in on the finale
|
||||
assert "[x3][s4]concat=n=2:v=0:a=1[x4]" in graph
|
||||
assert graph.endswith("[x4]loudnorm=I=-14.0:TP=-1.0:LRA=11,aresample=44100[out]")
|
||||
assert graph.endswith("[x4]anull[out]")
|
||||
assert "loudnorm" not in graph
|
||||
|
||||
|
||||
class TestMedleyChords:
|
||||
@@ -131,6 +132,10 @@ class TestMedleyChords:
|
||||
# the finale's chords start at the finale (source 132.2 -> medley 239.7)
|
||||
assert labels[4][0] == pytest.approx(239.7) and labels[4][2] == "Ab"
|
||||
|
||||
def test_shift_chords_keeps_capo(self):
|
||||
out = medley.shift_chords([{"start": 0.0, "end": 5.0, "label": "C", "capo": 2}], 0.0, 10.0, 100.0)
|
||||
assert out == [{"start": 100.0, "end": 105.0, "label": "C", "capo": 2}]
|
||||
|
||||
def test_shift_chords_drops_slivers(self):
|
||||
out = medley.shift_chords([{"start": 0, "end": 10.02, "label": "G"}, {"start": 10.02, "end": 20, "label": "C"}],
|
||||
10.0, 20.0, 100.0)
|
||||
@@ -143,6 +148,16 @@ class TestSingleSong:
|
||||
assert medley.compute_offsets(sections, [200.0]) == [0.0]
|
||||
graph = medley.build_filtergraph(sections, [200.0], [1.5])
|
||||
assert "acrossfade" not in graph and "concat" not in graph
|
||||
assert graph.endswith("[s0]loudnorm=I=-14.0:TP=-1.0:LRA=11,aresample=44100[out]")
|
||||
assert graph.endswith("[s0]anull[out]")
|
||||
chords = medley.medley_chords(sections, [200.0], [0.0], {"a": [{"start": 0, "end": 200, "label": "G"}]})
|
||||
assert chords == [{"start": 0.0, "end": 200.0, "label": "G"}]
|
||||
|
||||
|
||||
class TestMaster:
|
||||
def test_static_gain_to_target_then_limiter(self):
|
||||
f = medley.master_filter(-18.5)
|
||||
assert f.startswith("volume=4.50dB,")
|
||||
assert "alimiter=limit=0.8913" in f and "level=false" in f and "latency=true" in f
|
||||
assert "loudnorm" not in f # dynamic loudnorm boosted near-silent intros into artifacts
|
||||
assert f.endswith("aresample=44100")
|
||||
|
||||
Reference in new issue
Block a user