File size: 2,081 Bytes
d09f52e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The gold gate: dual-derivation verification.

For each chord we derive pitch content two independent ways and require they
agree exactly (pitch-class set + bass):

    figure  -> music21 roman engine   -> pitches      (the label's meaning)
    symbol  -> music21 chord-symbol parser -> pitches  (what the model sees)

If they disagree, the Roman-numeral label and the printed chord symbol denote
different chords, so the training pair would be wrong. Such items are dropped
and surfaced in the verification report. This is what makes the synthetic gold
trustworthy without hand-labelling.
"""
from __future__ import annotations

from dataclasses import dataclass

from music21 import key

from .vocabulary import (
    Analysis,
    bass_pc_from_figure,
    bass_pc_from_symbol,
    pitch_classes_from_figure,
    pitch_classes_from_symbol,
)

# music21 returns this string when it cannot name a chord.
_UNIDENTIFIED = "Chord Symbol Cannot Be Identified"


@dataclass(frozen=True)
class ChordCheck:
    ok: bool
    figure: str
    symbol: str
    figure_pcs: frozenset[int]
    symbol_pcs: frozenset[int]
    reason: str = ""


def verify_chord(analysis: Analysis, symbol: str, key_obj: key.Key) -> ChordCheck:
    figure = analysis.music21_figure()
    fig_pcs = pitch_classes_from_figure(figure, key_obj)

    if not symbol or symbol == _UNIDENTIFIED:
        return ChordCheck(False, figure, symbol, fig_pcs, frozenset(), "unidentified chord symbol")

    try:
        sym_pcs = pitch_classes_from_symbol(symbol)
        sym_bass = bass_pc_from_symbol(symbol)
    except Exception as exc:  # music21 raises a variety of parse errors
        return ChordCheck(False, figure, symbol, fig_pcs, frozenset(), f"symbol parse error: {exc}")

    if fig_pcs != sym_pcs:
        return ChordCheck(False, figure, symbol, fig_pcs, sym_pcs, "pitch-class set mismatch")

    if bass_pc_from_figure(figure, key_obj) != sym_bass:
        return ChordCheck(False, figure, symbol, fig_pcs, sym_pcs, "bass mismatch (inversion)")

    return ChordCheck(True, figure, symbol, fig_pcs, sym_pcs)