|
| 1 | +"""Turn a page of lyrics into a .krn skeleton, so only the pitches are left. |
| 2 | +
|
| 3 | +Hand-writing Humdrum is the boring half of contributing a song: three tab |
| 4 | +separated spines, one character per row, jyutping on every one. This does that |
| 5 | +part and looks up the jyutping, leaving you to replace each placeholder pitch |
| 6 | +with the note actually sung. |
| 7 | +
|
| 8 | +Input is plain text, one phrase per line. A line beginning with # marks a |
| 9 | +section: |
| 10 | +
|
| 11 | + # verse |
| 12 | + 今日天氣真係好 |
| 13 | + 我哋一齊去食飯 |
| 14 | + # chorus |
| 15 | + 佢話唔記得帶錢 |
| 16 | +
|
| 17 | +The output carries a SKELETON marker, and scripts/check_krn.py refuses any file |
| 18 | +that still has it, so an untranscribed draft cannot reach the corpus by |
| 19 | +accident. Delete that line once the pitches are real. |
| 20 | +
|
| 21 | +Usage: |
| 22 | + python scripts/scaffold_krn.py LYRICS.txt --id X0001 --title 歌名 \\ |
| 23 | + --singer 歌手 --composer 作曲 --lyricist 作詞 --year 2026 > corpus/X0001.krn |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse |
| 27 | +import os |
| 28 | +import sys |
| 29 | + |
| 30 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 31 | + |
| 32 | +from cantojam.jyutping import Lexicon, is_han # noqa: E402 |
| 33 | + |
| 34 | +SKELETON = "!!!ONB: SKELETON, pitches not yet transcribed" |
| 35 | +PLACEHOLDER = "4c" |
| 36 | +SECTIONS = {"verse", "prechorus", "chorus", "bridge", "coda", "intro", |
| 37 | + "outro", "interlude", "refrain"} |
| 38 | + |
| 39 | + |
| 40 | +def main(): |
| 41 | + parser = argparse.ArgumentParser( |
| 42 | + description=__doc__, |
| 43 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 44 | + parser.add_argument("lyrics", help="plain text, one phrase per line") |
| 45 | + parser.add_argument("--id", required=True, help="song ID, e.g. X0001") |
| 46 | + parser.add_argument("--title", required=True) |
| 47 | + parser.add_argument("--singer", default="") |
| 48 | + parser.add_argument("--composer", default="") |
| 49 | + parser.add_argument("--lyricist", default="") |
| 50 | + parser.add_argument("--arranger", default="") |
| 51 | + parser.add_argument("--year", default="") |
| 52 | + parser.add_argument("--key", default="*k[b-]\t*\t*", |
| 53 | + help="Humdrum key signature line") |
| 54 | + parser.add_argument("--tonic", default="*F:") |
| 55 | + parser.add_argument("--meter", default="*M4/4") |
| 56 | + parser.add_argument("--tempo", default="*MM72") |
| 57 | + parser.add_argument("--override", action="append", metavar="字=jyutping", |
| 58 | + help="pin a reading for this song") |
| 59 | + args = parser.parse_args() |
| 60 | + |
| 61 | + overrides = {} |
| 62 | + for pair in args.override or []: |
| 63 | + char, _, reading = pair.partition("=") |
| 64 | + if not reading: |
| 65 | + sys.exit(f"bad override {pair!r}, expected 字=jyutping") |
| 66 | + overrides[char.strip()] = reading.strip() |
| 67 | + |
| 68 | + lexicon = Lexicon(overrides) |
| 69 | + with open(args.lyrics, encoding="utf-8") as handle: |
| 70 | + lines = [line.rstrip("\n") for line in handle] |
| 71 | + |
| 72 | + out = [SKELETON, f"!!!OTL: {args.title}", f"!!!OTA: {args.id}"] |
| 73 | + for tag, value in (("RRD", args.year), ("MGN", args.singer), |
| 74 | + ("COM", args.composer), ("LYR", args.lyricist), |
| 75 | + ("LAR", args.arranger)): |
| 76 | + if value: |
| 77 | + out.append(f"!!!{tag}: {value}") |
| 78 | + |
| 79 | + out += ["**kern\t**text\t**jyutping", "*clefGv2\t*\t*", args.key, |
| 80 | + f"{args.tonic}\t*\t*", f"{args.meter}\t*\t*", |
| 81 | + f"{args.tempo}\t*\t*"] |
| 82 | + |
| 83 | + unknown, ambiguous, bar, syllables = [], [], 0, 0 |
| 84 | + for line in lines: |
| 85 | + stripped = line.strip() |
| 86 | + if not stripped: |
| 87 | + continue |
| 88 | + if stripped.startswith("#"): |
| 89 | + name = stripped.lstrip("#").strip() |
| 90 | + if name and name not in SECTIONS: |
| 91 | + print(f"warning: unrecognised section {name!r}", |
| 92 | + file=sys.stderr) |
| 93 | + out.append(f"*>{name}\t*>{name}\t*>{name}") |
| 94 | + continue |
| 95 | + |
| 96 | + out.append(f"!! {stripped}") # the phrase, for your eyes only |
| 97 | + for char in stripped: |
| 98 | + if not is_han(char): |
| 99 | + continue |
| 100 | + reading = lexicon.lookup(char) |
| 101 | + if reading is None: |
| 102 | + unknown.append(char) |
| 103 | + reading = "TODO" |
| 104 | + elif lexicon.is_ambiguous(char): |
| 105 | + ambiguous.append(f"{char}({'/'.join(lexicon.readings(char))})") |
| 106 | + out.append(f"{PLACEHOLDER}\t{char}\t{reading}") |
| 107 | + syllables += 1 |
| 108 | + bar += 1 |
| 109 | + out.append(f"={bar}\t={bar}\t={bar}") |
| 110 | + |
| 111 | + out.append("*-\t*-\t*-") |
| 112 | + print("\n".join(out)) |
| 113 | + |
| 114 | + print(f"\n{syllables} syllables over {bar} phrases", file=sys.stderr) |
| 115 | + if unknown: |
| 116 | + print(f"jyutping missing for {len(set(unknown))} character(s), marked " |
| 117 | + f"TODO: {''.join(sorted(set(unknown)))}", file=sys.stderr) |
| 118 | + print(" add them to cantojam/data/colloquial.json, or fill by hand", |
| 119 | + file=sys.stderr) |
| 120 | + if ambiguous: |
| 121 | + shown = ", ".join(sorted(set(ambiguous))[:8]) |
| 122 | + print(f"{len(set(ambiguous))} polyphone(s), defaulted to the corpus's " |
| 123 | + f"most frequent reading. Check these: {shown}", file=sys.stderr) |
| 124 | + print(f"\nnow replace every {PLACEHOLDER} with the pitch actually sung, " |
| 125 | + f"then delete the SKELETON line.", file=sys.stderr) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments