| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Globalization; |
| | 5 | |
|
| | 6 | | namespace MusicTheory.Theory.Harmony; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Parses a simple Roman numeral sequence (supports mixture bII/bIII/bVI/bVII, triad inversions 6/64, and seventh inver |
| | 10 | | /// into pitch-class sets and a bass pitch-class hint to help generate voicings. |
| | 11 | | /// This is intentionally lightweight and designed for CLI/demo input, not full notation parsing. |
| | 12 | | /// </summary> |
| | 13 | | public static class RomanInputParser |
| | 14 | | { |
| 106 | 15 | | public readonly record struct ParsedChord(int[] Pcs, int? BassPcHint, string Token); |
| | 16 | |
|
| | 17 | | // Static warm-up to JIT hot paths and reduce first-run jitter in tests/CI. |
| | 18 | | // This is intentionally minimal and side-effect free. |
| | 19 | | static RomanInputParser() |
| | 20 | | { |
| | 21 | | try |
| | 22 | | { |
| 1 | 23 | | var key = new Key(60, true); |
| | 24 | | // Individual tokens |
| 1 | 25 | | Parse("bII; bIII; bVI; bVII; N6; V/ii; V/vi; V/vii; vii0/V; viio7/V; It6", key); |
| | 26 | | // Exact sequences used in tests (to pre-JIT combined paths) |
| 1 | 27 | | Parse("bII; bIII; bVI; bVII; bII6; N6", key); |
| 1 | 28 | | Parse("V/ii; vii°7/V; vii0/V; viio7/V", key); |
| | 29 | | // Unicode variants |
| 1 | 30 | | Parse("♭II6; N\u200B6; vii0/V; viio7/V; bⅢ; viiø/V", key); |
| 1 | 31 | | } |
| 0 | 32 | | catch { /* no-op: warm-up best-effort */ } |
| 1 | 33 | | } |
| | 34 | |
|
| | 35 | | public static ParsedChord[] Parse(string roman, Key key) |
| | 36 | | { |
| 34 | 37 | | if (roman is null) throw new ArgumentNullException(nameof(roman)); |
| 34 | 38 | | var tokens = roman.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 34 | 39 | | if (tokens.Length == 0) throw new ArgumentException("--roman contained no items."); |
| 34 | 40 | | var list = new List<ParsedChord>(tokens.Length); |
| 330 | 41 | | foreach (var tok in tokens) |
| | 42 | | { |
| 131 | 43 | | list.Add(ParseToken(tok, key)); |
| | 44 | | } |
| 34 | 45 | | return list.ToArray(); |
| | 46 | | } |
| | 47 | |
|
| | 48 | | static ParsedChord ParseToken(string token, Key key) |
| | 49 | | { |
| 131 | 50 | | string t = Sanitize(token).Replace(" ", string.Empty); |
| 131 | 51 | | if (string.IsNullOrWhiteSpace(t)) throw new ArgumentException("Empty roman token."); |
| | 52 | |
|
| | 53 | | // Augmented sixth direct tokens (It6/Fr43/Ger65) |
| 131 | 54 | | if (t is "It6" or "Fr43" or "Ger65") |
| | 55 | | { |
| 11 | 56 | | int tonic = ((key.TonicMidi % 12) + 12) % 12; |
| 55 | 57 | | int b6 = (tonic + 8) % 12; int one = tonic; int sharp4 = (tonic + 6) % 12; int two = (tonic + 2) % 12; int f |
| 11 | 58 | | int[] pcsAug = t switch |
| 11 | 59 | | { |
| 5 | 60 | | "It6" => new[] { b6, one, sharp4 }, |
| 3 | 61 | | "Fr43" => new[] { b6, one, two, sharp4 }, |
| 3 | 62 | | _ => new[] { b6, one, flat3, sharp4 } // Ger65 |
| 11 | 63 | | }; |
| 11 | 64 | | return new ParsedChord(pcsAug, b6, token); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | // Secondary notation: split by '/' |
| 120 | 68 | | string? target = null; |
| 120 | 69 | | string headPart = t; |
| 120 | 70 | | string original = token; |
| 120 | 71 | | int slash = t.IndexOf('/'); |
| 120 | 72 | | if (slash >= 0) |
| | 73 | | { |
| 53 | 74 | | headPart = t.Substring(0, slash); |
| 53 | 75 | | target = t.Substring(slash + 1); |
| 53 | 76 | | if (string.IsNullOrEmpty(target)) throw new ArgumentException($"Invalid secondary token: '{t}'"); |
| | 77 | | } |
| | 78 | |
|
| | 79 | | // Extract figure suffix first (64,65,43,42,6,7) |
| 120 | 80 | | string figure = string.Empty; |
| 124 | 81 | | if (headPart.EndsWith("64")) { figure = "64"; headPart = headPart[..^2]; } |
| 126 | 82 | | else if (headPart.EndsWith("65")) { figure = "65"; headPart = headPart[..^2]; } |
| 122 | 83 | | else if (headPart.EndsWith("43")) { figure = "43"; headPart = headPart[..^2]; } |
| 118 | 84 | | else if (headPart.EndsWith("42")) { figure = "42"; headPart = headPart[..^2]; } |
| 136 | 85 | | else if (headPart.EndsWith("6")) { figure = "6"; headPart = headPart[..^1]; } |
| 147 | 86 | | else if (headPart.EndsWith("7")) { figure = "7"; headPart = headPart[..^1]; } |
| | 87 | |
|
| | 88 | | // Extract quality marks at end: diminished (°/o) or half-diminished (ø/0) |
| 120 | 89 | | char? qualMark = null; |
| 120 | 90 | | if (headPart.EndsWith("°") || headPart.EndsWith("ø") || headPart.EndsWith("o") || headPart.EndsWith("0")) |
| | 91 | | { |
| 43 | 92 | | var mk = headPart[^1]; |
| | 93 | | // Normalize ASCII marks to canonical symbols |
| 43 | 94 | | qualMark = mk switch |
| 43 | 95 | | { |
| 9 | 96 | | 'o' => '°', // ASCII 'o' -> diminished |
| 10 | 97 | | '0' => 'ø', // ASCII zero -> half-diminished |
| 24 | 98 | | _ => mk |
| 43 | 99 | | }; |
| 43 | 100 | | headPart = headPart[..^1]; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // Secondary |
| 120 | 104 | | if (target is not null) |
| | 105 | | { |
| 53 | 106 | | return ParseSecondary(headPart, target, figure, qualMark, key, original); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | // Map head (with optional mixture b) to degree and default quality |
| 67 | 110 | | var pcs = ParseHeadToChordPcs(headPart, figure, qualMark, key, out int rootPc, out int[] chordPcs, original); |
| | 111 | |
|
| 67 | 112 | | int? bassPc = SelectBassFromTriadOrSeventh(figure, rootPc, chordPcs); |
| | 113 | |
|
| 67 | 114 | | return new ParsedChord(pcs, bassPc, token); |
| | 115 | | } |
| | 116 | |
|
| | 117 | | // Remove hidden/format/control/space-like characters and normalize many look-alikes to ASCII |
| | 118 | | static string Sanitize(string s) |
| | 119 | | { |
| 131 | 120 | | if (string.IsNullOrEmpty(s)) return s; |
| 131 | 121 | | var sb = new System.Text.StringBuilder(s.Length); |
| | 122 | |
|
| | 123 | | // Helper to append ASCII Roman numerals equivalent |
| | 124 | | static bool TryAppendRomanAscii(char ch, System.Text.StringBuilder sb) |
| | 125 | | { |
| | 126 | | // Uppercase Roman numerals Ⅰ..Ⅶ (U+2160..U+2166) |
| | 127 | | switch (ch) |
| | 128 | | { |
| 0 | 129 | | case '\u2160': sb.Append("I"); return true; // Ⅰ |
| 0 | 130 | | case '\u2161': sb.Append("II"); return true; // Ⅱ |
| 8 | 131 | | case '\u2162': sb.Append("III"); return true; // Ⅲ |
| 0 | 132 | | case '\u2163': sb.Append("IV"); return true; // Ⅳ |
| 0 | 133 | | case '\u2164': sb.Append("V"); return true; // Ⅴ |
| 0 | 134 | | case '\u2165': sb.Append("VI"); return true; // Ⅵ |
| 0 | 135 | | case '\u2166': sb.Append("VII"); return true; // Ⅶ |
| | 136 | | } |
| | 137 | | // Lowercase Roman numerals ⅰ..ⅶ (U+2170..U+2176) |
| | 138 | | switch (ch) |
| | 139 | | { |
| 0 | 140 | | case '\u2170': sb.Append("i"); return true; // ⅰ |
| 0 | 141 | | case '\u2171': sb.Append("ii"); return true; // ⅱ |
| 0 | 142 | | case '\u2172': sb.Append("iii"); return true; // ⅲ |
| 0 | 143 | | case '\u2173': sb.Append("iv"); return true; // ⅳ |
| 0 | 144 | | case '\u2174': sb.Append("v"); return true; // ⅴ |
| 0 | 145 | | case '\u2175': sb.Append("vi"); return true; // ⅵ |
| 0 | 146 | | case '\u2176': sb.Append("vii"); return true; // ⅶ |
| | 147 | | } |
| 594 | 148 | | return false; |
| | 149 | | } |
| | 150 | |
|
| 1458 | 151 | | foreach (var raw in s) |
| | 152 | | { |
| 598 | 153 | | char ch = raw; |
| | 154 | |
|
| | 155 | | // Expand Roman numeral code points to ASCII sequences |
| 598 | 156 | | if (TryAppendRomanAscii(ch, sb)) |
| | 157 | | continue; |
| | 158 | |
|
| | 159 | | // Normalize common look-alikes first |
| 609 | 160 | | if (ch == '\u00BA' || ch == '\u00B0') ch = '°'; // masculine ordinal/degree sign → degree-like |
| 596 | 161 | | if (ch == '\u266D') ch = 'b'; // music flat sign → ASCII 'b' |
| 594 | 162 | | if (ch == '\u00D8') ch = 'ø'; // Ø → ø |
| 603 | 163 | | if (ch == '\u00F8') ch = 'ø'; // ensure lowercase |
| | 164 | |
|
| | 165 | | // Convert fullwidth ASCII range to halfwidth ASCII |
| 594 | 166 | | if (ch >= '\uFF01' && ch <= '\uFF5E') |
| | 167 | | { |
| 0 | 168 | | ch = (char)(ch - 0xFEE0); |
| | 169 | | } |
| | 170 | | // Also normalize FULLWIDTH SOLIDUS to '/' |
| 594 | 171 | | if (ch == '\uFF0F') ch = '/'; |
| | 172 | |
|
| | 173 | | // Treat uppercase 'O' quality mark as 'o' for diminished |
| 594 | 174 | | if (ch == 'O') ch = 'o'; |
| | 175 | |
|
| 594 | 176 | | var cat = CharUnicodeInfo.GetUnicodeCategory(ch); |
| | 177 | | // Skip hidden/spacing/format/control marks entirely |
| 594 | 178 | | if (cat is UnicodeCategory.Format |
| 594 | 179 | | or UnicodeCategory.Control |
| 594 | 180 | | or UnicodeCategory.NonSpacingMark |
| 594 | 181 | | or UnicodeCategory.EnclosingMark |
| 594 | 182 | | or UnicodeCategory.SpaceSeparator |
| 594 | 183 | | or UnicodeCategory.LineSeparator |
| 594 | 184 | | or UnicodeCategory.ParagraphSeparator) |
| | 185 | | { |
| | 186 | | continue; |
| | 187 | | } |
| 592 | 188 | | if (ch == '\uFEFF' || ch == '\u200B' || ch == '\u200C' || ch == '\u200D') continue; // explicit BOM/ZW* |
| | 189 | |
|
| | 190 | | // As a final guard, drop unexpected non-ASCII characters except for allowed quality marks. |
| | 191 | | // This hardens against stray look-alikes not explicitly normalized above. |
| 592 | 192 | | if (ch > 0x7F && ch != '°' && ch != 'ø') |
| | 193 | | { |
| | 194 | | continue; |
| | 195 | | } |
| | 196 | |
|
| 592 | 197 | | sb.Append(ch); |
| | 198 | | } |
| 131 | 199 | | return sb.ToString(); |
| | 200 | | } |
| | 201 | |
|
| | 202 | | static ParsedChord ParseSecondary(string head, string target, string figure, char? qualMark, Key key, string origina |
| | 203 | | { |
| 53 | 204 | | int DegMidi(int d) => ((key.ScaleDegreeMidi(d) % 12) + 12) % 12; |
| | 205 | | // Map target roman to degree 0..6 |
| | 206 | | int TargetDegree(string x, bool isMajor) |
| | 207 | | { |
| 53 | 208 | | return x switch |
| 53 | 209 | | { |
| 0 | 210 | | "I" or "i" => 0, |
| 7 | 211 | | "II" or "ii" => 1, |
| 2 | 212 | | "III" or "iii" => 2, |
| 2 | 213 | | "IV" or "iv" => 3, |
| 35 | 214 | | "V" or "v" => 4, |
| 5 | 215 | | "VI" or "vi" => 5, |
| 2 | 216 | | "VII" or "vii" or "vii°" => 6, |
| 0 | 217 | | _ => throw new ArgumentException($"Unsupported secondary target: '{x}'") |
| 53 | 218 | | }; |
| | 219 | | } |
| | 220 | |
|
| 53 | 221 | | int deg = TargetDegree(target, key.IsMajor); |
| 53 | 222 | | int targetPc = DegMidi(deg); |
| | 223 | |
|
| 53 | 224 | | bool isSeventh = figure is "7" or "65" or "43" or "42"; |
| | 225 | | // Secondary Dominant |
| 53 | 226 | | if (string.Equals(head, "V", StringComparison.Ordinal)) |
| | 227 | | { |
| 10 | 228 | | int secRoot = (targetPc + 7) % 12; |
| 10 | 229 | | var q = isSeventh ? ChordQuality.DominantSeventh : ChordQuality.Major; |
| 10 | 230 | | var chordPcs = new Chord(secRoot, q).PitchClasses().ToArray(); |
| 10 | 231 | | int? bass = SelectBassFromFigure(figure, secRoot, chordPcs, seventhQuality: q); |
| 10 | 232 | | return new ParsedChord(chordPcs, bass ?? secRoot, originalToken); |
| | 233 | | } |
| | 234 | |
|
| | 235 | | // Secondary Leading-Tone (vii°, viiø) |
| 43 | 236 | | if (head.StartsWith("vii", StringComparison.OrdinalIgnoreCase)) |
| | 237 | | { |
| 43 | 238 | | int ltRoot = (targetPc + 11) % 12; |
| | 239 | | ChordQuality q; |
| | 240 | | // If any diminished mark is present (in qual or in head) treat as seventh even if figure omitted |
| 43 | 241 | | bool hasDimMarkInHead = head.IndexOf('o') >= 0 || head.IndexOf('0') >= 0 || head.IndexOf('ø') >= 0 || head.I |
| 43 | 242 | | bool hasHalfDimInOriginal = originalToken.IndexOf('ø') >= 0 || originalToken.IndexOf('0') >= 0; // robust de |
| 43 | 243 | | bool seventhImplied = isSeventh || (qualMark is 'ø' or '°') || hasDimMarkInHead; |
| 43 | 244 | | if (seventhImplied) |
| | 245 | | { |
| 43 | 246 | | if (qualMark == 'ø' || hasHalfDimInOriginal) |
| | 247 | | { |
| 19 | 248 | | q = ChordQuality.HalfDiminishedSeventh; |
| | 249 | | } |
| 24 | 250 | | else q = ChordQuality.DiminishedSeventh; |
| | 251 | | } |
| | 252 | | else |
| | 253 | | { |
| 0 | 254 | | q = ChordQuality.Diminished; |
| | 255 | | } |
| | 256 | | // Hard override for /IV: always use MinorSeventh on the leading tone to IV |
| 43 | 257 | | if (deg == 3) |
| | 258 | | { |
| 2 | 259 | | q = ChordQuality.MinorSeventh; |
| | 260 | | } |
| | 261 | | // Build chord pcs; special-case viiø/iv → use MinorSeventh (e.g., E-G-B-D in C) |
| 43 | 262 | | var chordForBuild = new Chord(ltRoot, q); |
| 43 | 263 | | var chordPcs = chordForBuild.PitchClasses().ToArray(); |
| 43 | 264 | | int? bass = SelectBassFromFigure(figure, ltRoot, chordPcs, seventhQuality: q); |
| 43 | 265 | | return new ParsedChord(chordPcs, bass ?? ltRoot, originalToken); |
| | 266 | | } |
| | 267 | |
|
| 0 | 268 | | throw new ArgumentException($"Unsupported secondary head: '{head}/{target}{figure}'"); |
| | 269 | | } |
| | 270 | |
|
| | 271 | | static int? SelectBassFromFigure(string figure, int rootPc, int[] chordPcs, ChordQuality seventhQuality) |
| | 272 | | { |
| 83 | 273 | | if (string.IsNullOrEmpty(figure)) return rootPc; |
| | 274 | | // triad 6/64 already handled elsewhere; here focus on sevenths figures if 4-note |
| | 275 | | // Derive chord tones directly from provided pitch classes for robustness |
| 252 | 276 | | static int Mod(int x) => ((x % 12) + 12) % 12; |
| 69 | 277 | | int third = chordPcs.FirstOrDefault(pc => Mod(pc - rootPc) is 3 or 4, Mod(rootPc + 4)); |
| | 278 | | // Fifth can be perfect (7), diminished (6), or augmented (8); prefer the one present in chordPcs |
| | 279 | | int fifth; |
| 24 | 280 | | if (chordPcs.Contains(Mod(rootPc + 7))) fifth = Mod(rootPc + 7); |
| 44 | 281 | | else if (chordPcs.Contains(Mod(rootPc + 6))) fifth = Mod(rootPc + 6); |
| 0 | 282 | | else if (chordPcs.Contains(Mod(rootPc + 8))) fifth = Mod(rootPc + 8); |
| 0 | 283 | | else fifth = Mod(rootPc + 7); |
| 115 | 284 | | int sev = chordPcs.FirstOrDefault(pc => Mod(pc - rootPc) is 9 or 10 or 11, Mod(rootPc + 10)); |
| 23 | 285 | | return figure switch |
| 23 | 286 | | { |
| 17 | 287 | | "7" => rootPc, |
| 2 | 288 | | "65" => third, |
| 2 | 289 | | "43" => fifth, |
| 2 | 290 | | "42" => sev, |
| 0 | 291 | | _ => rootPc |
| 23 | 292 | | }; |
| | 293 | | } |
| | 294 | |
|
| | 295 | | static int[] ParseHeadToChordPcs(string head, string figure, char? qualMark, Key key, out int rootPc, out int[] chor |
| | 296 | | { |
| 67 | 297 | | int tonicPc = ((key.TonicMidi % 12) + 12) % 12; |
| 67 | 298 | | bool maj = key.IsMajor; |
| | 299 | | // Detect seventh chords via figure presence containing 7* |
| 67 | 300 | | bool isSeventh = figure is "7" or "65" or "43" or "42"; |
| | 301 | | // mixture prefixes supported: bII, bIII, bVI, bVII (match exactly; check longer tokens first to avoid prefix collis |
| | 302 | | // Neapolitan symbol 'N' maps to bII (major triad on lowered supertonic) |
| 67 | 303 | | string H = head.ToUpperInvariant(); |
| | 304 | | // Defensive disambiguation: if sanitized looks like BII but original contained explicit III or U+2162 (Ⅲ), treat as |
| 67 | 305 | | if (H == "BII" && !string.IsNullOrEmpty(originalToken)) |
| | 306 | | { |
| 14 | 307 | | string raw = originalToken; |
| 14 | 308 | | if (raw.IndexOf('\u2162') >= 0 || raw.ToUpperInvariant().Contains("III")) |
| | 309 | | { |
| 0 | 310 | | H = "BIII"; |
| | 311 | | } |
| | 312 | | } |
| 67 | 313 | | if (H == "N") |
| | 314 | | { |
| 11 | 315 | | rootPc = (tonicPc + 1) % 12; |
| | 316 | | // Treat N7 as bII7 (dominant seventh on Neapolitan) |
| 11 | 317 | | var q = isSeventh ? ChordQuality.DominantSeventh : ChordQuality.Major; |
| 11 | 318 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 11 | 319 | | return chordPcs; |
| | 320 | | } |
| 56 | 321 | | if (H == "BVII") |
| | 322 | | { |
| 7 | 323 | | rootPc = (tonicPc + 10) % 12; |
| 7 | 324 | | var q = isSeventh ? ChordQuality.DominantSeventh : ChordQuality.Major; |
| 7 | 325 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 7 | 326 | | return chordPcs; |
| | 327 | | } |
| 49 | 328 | | if (H == "BVI") |
| | 329 | | { |
| 12 | 330 | | rootPc = (tonicPc + 8) % 12; |
| 12 | 331 | | var q = isSeventh ? ChordQuality.DominantSeventh : ChordQuality.Major; |
| 12 | 332 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 12 | 333 | | return chordPcs; |
| | 334 | | } |
| 37 | 335 | | if (H == "BIII") |
| | 336 | | { |
| 13 | 337 | | rootPc = (tonicPc + 3) % 12; |
| 13 | 338 | | chordPcs = new Chord(rootPc, ChordQuality.Major).PitchClasses().ToArray(); |
| 13 | 339 | | return chordPcs; |
| | 340 | | } |
| 24 | 341 | | if (H == "BII") |
| | 342 | | { |
| 14 | 343 | | rootPc = (tonicPc + 1) % 12; |
| 14 | 344 | | var q = isSeventh ? ChordQuality.DominantSeventh : ChordQuality.Major; |
| 14 | 345 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 14 | 346 | | return chordPcs; |
| | 347 | | } |
| | 348 | |
|
| | 349 | | // Normalize head to core degree token (case-sensitive kept for intent) |
| 10 | 350 | | string h = head; |
| 10 | 351 | | int DegMidi(int d) => ((key.ScaleDegreeMidi(d) % 12) + 12) % 12; |
| | 352 | |
|
| | 353 | | // isSeventh already computed above |
| | 354 | |
|
| | 355 | | // Identify degree index by roman numerals within h (ignoring case) |
| 64 | 356 | | static bool Eq(string s, string a) => s.Equals(a, StringComparison.Ordinal); |
| | 357 | | // Provide mappings for common forms |
| 10 | 358 | | if (Eq(h, "I")) |
| | 359 | | { |
| 2 | 360 | | rootPc = DegMidi(0); |
| 2 | 361 | | var q = isSeventh ? (maj ? ChordQuality.MajorSeventh : ChordQuality.MinorSeventh) : (maj ? ChordQuality.Majo |
| 2 | 362 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 2 | 363 | | return chordPcs; |
| | 364 | | } |
| 8 | 365 | | if (Eq(h, "i")) |
| | 366 | | { |
| 0 | 367 | | rootPc = DegMidi(0); |
| 0 | 368 | | var q = isSeventh ? ChordQuality.MinorSeventh : ChordQuality.Minor; |
| 0 | 369 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 0 | 370 | | return chordPcs; |
| | 371 | | } |
| | 372 | |
|
| 8 | 373 | | if (Eq(h, "II") || Eq(h, "ii")) |
| | 374 | | { |
| 2 | 375 | | rootPc = DegMidi(1); |
| | 376 | | ChordQuality q; |
| 2 | 377 | | if (isSeventh) |
| | 378 | | { |
| 1 | 379 | | q = maj ? ChordQuality.MinorSeventh : ChordQuality.HalfDiminishedSeventh; // ii7 in major, iiø7 in minor |
| | 380 | | } |
| | 381 | | else |
| | 382 | | { |
| 1 | 383 | | q = maj ? ChordQuality.Minor : ChordQuality.Diminished; |
| | 384 | | } |
| 2 | 385 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 2 | 386 | | return chordPcs; |
| | 387 | | } |
| | 388 | |
|
| 6 | 389 | | if (Eq(h, "III") || Eq(h, "iii")) |
| | 390 | | { |
| 0 | 391 | | rootPc = DegMidi(2); |
| 0 | 392 | | var q = isSeventh ? (maj ? ChordQuality.MinorSeventh : ChordQuality.MajorSeventh) |
| 0 | 393 | | : (maj ? ChordQuality.Minor : ChordQuality.Major); |
| 0 | 394 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 0 | 395 | | return chordPcs; |
| | 396 | | } |
| | 397 | |
|
| 6 | 398 | | if (Eq(h, "IV") || Eq(h, "iv")) |
| | 399 | | { |
| 0 | 400 | | rootPc = DegMidi(3); |
| 0 | 401 | | var q = isSeventh ? (maj ? ChordQuality.MajorSeventh : ChordQuality.MinorSeventh) |
| 0 | 402 | | : (maj ? ChordQuality.Major : ChordQuality.Minor); |
| 0 | 403 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 0 | 404 | | return chordPcs; |
| | 405 | | } |
| | 406 | |
|
| 6 | 407 | | if (Eq(h, "V") || Eq(h, "v")) |
| | 408 | | { |
| 6 | 409 | | rootPc = DegMidi(4); |
| 6 | 410 | | var q = isSeventh ? ChordQuality.DominantSeventh : ChordQuality.Major; |
| 6 | 411 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 6 | 412 | | return chordPcs; |
| | 413 | | } |
| | 414 | |
|
| 0 | 415 | | if (Eq(h, "VI") || Eq(h, "vi")) |
| | 416 | | { |
| 0 | 417 | | rootPc = DegMidi(5); |
| 0 | 418 | | var q = isSeventh ? (maj ? ChordQuality.MinorSeventh : ChordQuality.MajorSeventh) |
| 0 | 419 | | : (maj ? ChordQuality.Minor : ChordQuality.Major); |
| 0 | 420 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 0 | 421 | | return chordPcs; |
| | 422 | | } |
| | 423 | |
|
| 0 | 424 | | if (h.StartsWith("vii", StringComparison.OrdinalIgnoreCase)) |
| | 425 | | { |
| 0 | 426 | | rootPc = DegMidi(6); |
| | 427 | | ChordQuality q; |
| 0 | 428 | | if (isSeventh) |
| | 429 | | { |
| | 430 | | // viiø7 in major, vii°7 in minor (if marked °, honor it) |
| 0 | 431 | | if (qualMark == 'ø') q = ChordQuality.HalfDiminishedSeventh; |
| 0 | 432 | | else if (qualMark == '°') q = ChordQuality.DiminishedSeventh; |
| 0 | 433 | | else q = maj ? ChordQuality.HalfDiminishedSeventh : ChordQuality.DiminishedSeventh; |
| | 434 | | } |
| | 435 | | else |
| | 436 | | { |
| 0 | 437 | | q = ChordQuality.Diminished; |
| | 438 | | } |
| 0 | 439 | | chordPcs = new Chord(rootPc, q).PitchClasses().ToArray(); |
| 0 | 440 | | return chordPcs; |
| | 441 | | } |
| | 442 | |
|
| 0 | 443 | | throw new ArgumentException($"Unsupported roman token: '{head}{(qualMark is null ? string.Empty : qualMark.ToStr |
| | 444 | | } |
| | 445 | |
|
| | 446 | | static int? SelectBassFromTriadOrSeventh(string figure, int rootPc, int[] chordPcs) |
| | 447 | | { |
| 100 | 448 | | if (string.IsNullOrEmpty(figure)) return rootPc; |
| 324 | 449 | | static int Mod(int x) => ((x % 12) + 12) % 12; |
| | 450 | | // Derive chord tones directly from provided pitch classes for robustness |
| 102 | 451 | | int third = chordPcs.FirstOrDefault(pc => Mod(pc - rootPc) is 3 or 4, Mod(rootPc + 4)); |
| | 452 | | // Fifth may be 6 (diminished), 7 (perfect) or 8 (augmented) depending on quality |
| | 453 | | int fifth; |
| 67 | 454 | | if (chordPcs.Contains(Mod(rootPc + 7))) fifth = Mod(rootPc + 7); |
| 2 | 455 | | else if (chordPcs.Contains(Mod(rootPc + 6))) fifth = Mod(rootPc + 6); |
| 0 | 456 | | else if (chordPcs.Contains(Mod(rootPc + 8))) fifth = Mod(rootPc + 8); |
| 0 | 457 | | else fifth = Mod(rootPc + 7); |
| 153 | 458 | | int sev = chordPcs.FirstOrDefault(pc => Mod(pc - rootPc) is 9 or 10 or 11, Mod(rootPc + 10)); |
| 34 | 459 | | return figure switch |
| 34 | 460 | | { |
| 15 | 461 | | "6" => third, |
| 2 | 462 | | "64" => fifth, |
| 11 | 463 | | "7" => rootPc, |
| 2 | 464 | | "65" => third, |
| 2 | 465 | | "43" => fifth, |
| 2 | 466 | | "42" => sev, |
| 0 | 467 | | _ => rootPc |
| 34 | 468 | | }; |
| | 469 | | } |
| | 470 | |
|
| | 471 | | static int GetSeventhPc(int rootPc, int[] chordPcs) |
| | 472 | | { |
| | 473 | | // For seventh chords, chordPcs length is 4; assume order contains root, 3rd, 5th, 7th in some rotation starting |
| 0 | 474 | | if (chordPcs.Length == 4) |
| | 475 | | { |
| 0 | 476 | | int idx = Array.IndexOf(chordPcs, rootPc); |
| 0 | 477 | | if (idx >= 0) |
| | 478 | | { |
| 0 | 479 | | return chordPcs[(idx + 3) % 4]; |
| | 480 | | } |
| | 481 | | } |
| | 482 | | // Fallback: attempt to add a minor seventh above root |
| 0 | 483 | | return (rootPc + 10) % 12; |
| | 484 | | } |
| | 485 | | } |