| | 1 | | using System.Collections.Generic; |
| | 2 | |
|
| | 3 | | namespace MusicTheory.Theory.Harmony; |
| | 4 | |
|
| | 5 | | public readonly record struct ProgressionResult( |
| | 6 | | List<HarmonyAnalysisResult> Chords, |
| | 7 | | List<(int indexFrom, CadenceType cadence)> Cadences |
| | 8 | | ); |
| | 9 | |
|
| | 10 | | public static class ProgressionAnalyzer |
| | 11 | | { |
| | 12 | | public static ProgressionResult Analyze(IReadOnlyList<int[]> pcsList, Key key, IReadOnlyList<FourPartVoicing?>? voic |
| | 13 | | { |
| 1 | 14 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 1 | 15 | | var cads = new List<(int, CadenceType)>(); |
| | 16 | |
|
| 1 | 17 | | FourPartVoicing? prevV = null; |
| 1 | 18 | | RomanNumeral? prevRn = null; |
| 10 | 19 | | for (int i = 0; i < pcsList.Count; i++) |
| | 20 | | { |
| 4 | 21 | | var pcs = pcsList[i]; |
| 4 | 22 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 4 | 23 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcs, key, v, prevV); |
| 4 | 24 | | chords.Add(res); |
| 4 | 25 | | if (prevRn != null && res.Roman != null) |
| | 26 | | { |
| 3 | 27 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, key.IsMajor); |
| 3 | 28 | | if (cad != CadenceType.None) |
| 2 | 29 | | cads.Add((i - 1, cad)); |
| | 30 | | } |
| 4 | 31 | | prevV = v; |
| 4 | 32 | | prevRn = res.Roman; |
| | 33 | | } |
| | 34 | |
|
| 1 | 35 | | return new ProgressionResult(chords, cads); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | // Overload: HarmonyOptions を指定して解析(既定は HarmonyOptions.Default と同等) |
| | 39 | | public static ProgressionResult Analyze(IReadOnlyList<int[]> pcsList, Key key, HarmonyOptions options, IReadOnlyList |
| | 40 | | { |
| 1 | 41 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 1 | 42 | | var cads = new List<(int, CadenceType)>(); |
| | 43 | |
|
| 1 | 44 | | FourPartVoicing? prevV = null; |
| 1 | 45 | | RomanNumeral? prevRn = null; |
| 8 | 46 | | for (int i = 0; i < pcsList.Count; i++) |
| | 47 | | { |
| 3 | 48 | | var pcs = pcsList[i]; |
| 3 | 49 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 3 | 50 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcs, key, options, v, prevV); |
| 3 | 51 | | chords.Add(res); |
| 3 | 52 | | if (prevRn != null && res.Roman != null) |
| | 53 | | { |
| 2 | 54 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, key.IsMajor); |
| 2 | 55 | | if (cad != CadenceType.None) |
| 2 | 56 | | cads.Add((i - 1, cad)); |
| | 57 | | } |
| 3 | 58 | | prevV = v; |
| 3 | 59 | | prevRn = res.Roman; |
| | 60 | | } |
| | 61 | |
|
| 1 | 62 | | return new ProgressionResult(chords, cads); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Analyze a progression and return per-chord harmony results along with detailed cadence diagnostics. |
| | 67 | | /// </summary> |
| | 68 | | /// <remarks> |
| | 69 | | /// Cadence details include: |
| | 70 | | /// - Cadence type (Authentic/Plagal/Half/Deceptive) |
| | 71 | | /// - PAC/IAC approximation flag |
| | 72 | | /// - Cadential 6-4 flag and generic 6-4 classification (<see cref="SixFourType"/>) |
| | 73 | | /// |
| | 74 | | /// Half cadence is suppressed when the previous chord is I64 and the current chord is V (I64→V). |
| | 75 | | /// In that case, a non-cadential entry is returned with <c>SixFour=Cadential</c>, and the following step will emit |
| | 76 | | /// </remarks> |
| | 77 | | /// <param name="pcsList">Sequence of pitch-class sets (0..11), per chord.</param> |
| | 78 | | /// <param name="key">Tonic and mode used for analysis.</param> |
| | 79 | | /// <param name="voicings">Optional four-part voicings aligned to <paramref name="pcsList"/> for inversion/6-4 detec |
| | 80 | | /// <returns>Tuple of <see cref="ProgressionResult"/> and a list of <see cref="CadenceInfo"/>.</returns> |
| | 81 | | public static (ProgressionResult result, List<CadenceInfo> cadences) AnalyzeWithDetailedCadences( |
| | 82 | | IReadOnlyList<int[]> pcsList, Key key, IReadOnlyList<FourPartVoicing?>? voicings = null) |
| | 83 | | { |
| 39 | 84 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 39 | 85 | | var cadInfos = new List<CadenceInfo>(); |
| | 86 | |
|
| | 87 | | // vMinus1: 直前 (i-1) のボイシング, vMinus2: さらに一つ前 (i-2) |
| 78 | 88 | | FourPartVoicing? vMinus1 = null; FourPartVoicing? vMinus2 = null; |
| 117 | 89 | | RomanNumeral? prevRn = null; string? prevTxt = null; string? prevPrevTxt = null; |
| | 90 | | static string Head(string? txt) |
| | 91 | | { |
| 28 | 92 | | if (string.IsNullOrEmpty(txt)) return string.Empty; |
| 28 | 93 | | int i = 0; |
| 82 | 94 | | while (i < txt!.Length) |
| | 95 | | { |
| 62 | 96 | | char ch = txt[i]; |
| 62 | 97 | | bool ok = ch == 'b' || ch == '#' || ch == 'i' || ch == 'v' || ch == 'I' || ch == 'V' || ch == '/'; |
| 62 | 98 | | if (!ok) break; |
| 54 | 99 | | i++; |
| | 100 | | } |
| 28 | 101 | | if (i == 0) return txt!; |
| 28 | 102 | | return txt!.Substring(0, i); |
| | 103 | | } |
| 274 | 104 | | for (int i = 0; i < pcsList.Count; i++) |
| | 105 | | { |
| 98 | 106 | | var pcs = pcsList[i]; |
| 98 | 107 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 98 | 108 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcs, key, v, vMinus1); |
| 98 | 109 | | chords.Add(res); |
| 98 | 110 | | if (prevRn != null && res.Roman != null) |
| | 111 | | { |
| 59 | 112 | | var info = CadenceAnalyzer.DetectDetailed(i - 1, prevRn, res.Roman, key.IsMajor, key.TonicMidi % 12, pre |
| 59 | 113 | | if (info.Type != CadenceType.None) |
| | 114 | | { |
| | 115 | | // Ensure non-cadential 6-4 labels (Passing/Pedal) are not attached to cadence entries |
| 25 | 116 | | var info2 = info; |
| 25 | 117 | | if (info2.SixFour != SixFourType.None && info2.SixFour != SixFourType.Cadential) |
| 0 | 118 | | info2 = new CadenceInfo(info2.IndexFrom, info2.Type, info2.IsPerfectAuthentic, info2.HasCadentia |
| 25 | 119 | | cadInfos.Add(info2); |
| | 120 | | } |
| | 121 | | else |
| | 122 | | { |
| | 123 | | // Prefer voicing-based classification around x64 when neighbors share the same harmony |
| 34 | 124 | | bool around64 = v != null && vMinus1 != null && vMinus2 != null && !string.IsNullOrEmpty(prevTxt) && |
| 34 | 125 | | if (around64) |
| | 126 | | { |
| 13 | 127 | | var hPrevPrev = Head(prevPrevTxt); // (i-2) の Roman |
| 13 | 128 | | var hCurr = Head(res.RomanText); |
| 13 | 129 | | if (!string.IsNullOrEmpty(hPrevPrev) && hPrevPrev == hCurr) |
| | 130 | | { |
| 12 | 131 | | int bPrevPrev = vMinus2!.Value.B % 12; |
| 12 | 132 | | int bCurr = v!.Value.B % 12; |
| 12 | 133 | | var six = (bPrevPrev == bCurr) ? SixFourType.Pedal : SixFourType.Passing; |
| 12 | 134 | | cadInfos.Add(new CadenceInfo(info.IndexFrom, info.Type, info.IsPerfectAuthentic, info.HasCad |
| 12 | 135 | | goto NextChord1; |
| | 136 | | } |
| | 137 | | } |
| | 138 | | // Fallback: if neighbors share the same harmony and voicings available, decide Pedal vs Passing by |
| 22 | 139 | | if (v != null && vMinus1 != null && vMinus2 != null) |
| | 140 | | { |
| 1 | 141 | | var hPrevPrev2 = Head(prevPrevTxt); |
| 1 | 142 | | var hCurr2 = Head(res.RomanText); |
| 1 | 143 | | if (!string.IsNullOrEmpty(hPrevPrev2) && hPrevPrev2 == hCurr2) |
| | 144 | | { |
| 0 | 145 | | int bPrevPrev = vMinus2!.Value.B % 12; |
| 0 | 146 | | int bCurr = v!.Value.B % 12; |
| 0 | 147 | | var six = (bPrevPrev == bCurr) ? SixFourType.Pedal : SixFourType.Passing; |
| 0 | 148 | | cadInfos.Add(new CadenceInfo(info.IndexFrom, info.Type, info.IsPerfectAuthentic, info.HasCad |
| 0 | 149 | | goto NextChord1; |
| | 150 | | } |
| | 151 | | } |
| 22 | 152 | | if (info.SixFour != SixFourType.None && info.SixFour != SixFourType.Cadential) |
| 0 | 153 | | cadInfos.Add(info); |
| | 154 | | } |
| | 155 | | NextChord1: ; |
| | 156 | | } |
| 196 | 157 | | vMinus2 = vMinus1; vMinus1 = v; |
| 98 | 158 | | prevPrevTxt = prevTxt; |
| 98 | 159 | | prevRn = res.Roman; |
| 98 | 160 | | prevTxt = res.RomanText; |
| | 161 | | } |
| | 162 | |
|
| 39 | 163 | | return (new ProgressionResult(chords, new List<(int, CadenceType)>()), cadInfos); |
| | 164 | | } |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// Analyze a progression and return detailed cadence diagnostics with <see cref="HarmonyOptions"/>. |
| | 168 | | /// </summary> |
| | 169 | | /// <remarks> |
| | 170 | | /// The <see cref="HarmonyOptions.ShowNonCadentialSixFour"/> option controls whether non-cadential 6-4 entries |
| | 171 | | /// (Passing/Pedal with <c>Type==None</c>) are included in the returned cadence list. When set to <c>false</c>, |
| | 172 | | /// such entries are suppressed; cadential 6-4 information attached to a proper cadence (e.g., I64→V→I → Authentic) |
| | 173 | | /// remains part of that cadence entry. |
| | 174 | | /// </remarks> |
| | 175 | | /// <param name="pcsList">Sequence of pitch-class sets (0..11), per chord.</param> |
| | 176 | | /// <param name="key">Tonic and mode used for analysis.</param> |
| | 177 | | /// <param name="options">Harmony options affecting recognition and display of details.</param> |
| | 178 | | /// <param name="voicings">Optional four-part voicings aligned to <paramref name="pcsList"/>.</param> |
| | 179 | | /// <returns>Tuple of <see cref="ProgressionResult"/> and a list of <see cref="CadenceInfo"/>.</returns> |
| | 180 | | public static (ProgressionResult result, List<CadenceInfo> cadences) AnalyzeWithDetailedCadences( |
| | 181 | | IReadOnlyList<int[]> pcsList, Key key, HarmonyOptions options, IReadOnlyList<FourPartVoicing?>? voicings = null) |
| | 182 | | { |
| 22 | 183 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 22 | 184 | | var cadInfos = new List<CadenceInfo>(); |
| | 185 | |
|
| 44 | 186 | | FourPartVoicing? vMinus1 = null; FourPartVoicing? vMinus2 = null; |
| 66 | 187 | | RomanNumeral? prevRn = null; string? prevTxt = null; string? prevPrevTxt = null; |
| | 188 | | static string Head(string? txt) |
| | 189 | | { |
| 2 | 190 | | if (string.IsNullOrEmpty(txt)) return string.Empty; |
| 2 | 191 | | int i = 0; |
| 6 | 192 | | while (i < txt!.Length) |
| | 193 | | { |
| 5 | 194 | | char ch = txt[i]; |
| 5 | 195 | | bool ok = ch == 'b' || ch == '#' || ch == 'i' || ch == 'v' || ch == 'I' || ch == 'V' || ch == '/'; |
| 5 | 196 | | if (!ok) break; |
| 4 | 197 | | i++; |
| | 198 | | } |
| 2 | 199 | | if (i == 0) return txt!; |
| 2 | 200 | | return txt!.Substring(0, i); |
| | 201 | | } |
| 162 | 202 | | for (int i = 0; i < pcsList.Count; i++) |
| | 203 | | { |
| 59 | 204 | | var pcs = pcsList[i]; |
| 59 | 205 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 59 | 206 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcs, key, options, v, vMinus1); |
| 59 | 207 | | chords.Add(res); |
| 59 | 208 | | if (prevRn != null && res.Roman != null) |
| | 209 | | { |
| 37 | 210 | | var info = CadenceAnalyzer.DetectDetailed(i - 1, prevRn, res.Roman, key.IsMajor, key.TonicMidi % 12, pre |
| 37 | 211 | | if (info.Type != CadenceType.None) |
| | 212 | | { |
| 19 | 213 | | var info2 = info; |
| 19 | 214 | | if (info2.SixFour != SixFourType.None && info2.SixFour != SixFourType.Cadential) |
| 0 | 215 | | info2 = new CadenceInfo(info2.IndexFrom, info2.Type, info2.IsPerfectAuthentic, info2.HasCadentia |
| | 216 | |
|
| | 217 | | // Relabel cadential 6-4 (I64) as V64-53 when preferred |
| 19 | 218 | | if (options.PreferCadentialSixFourAsDominant && info2.HasCadentialSixFour) |
| | 219 | | { |
| 3 | 220 | | int idxI64 = i - 2; |
| 3 | 221 | | if (idxI64 >= 0 && idxI64 < chords.Count) |
| | 222 | | { |
| 3 | 223 | | var c = chords[idxI64]; |
| 3 | 224 | | string? txt = c.RomanText; |
| 3 | 225 | | bool looksI64 = !string.IsNullOrEmpty(txt) && txt!.EndsWith("64"); |
| | 226 | | // Only relabel when it looks like a 6-4 triad on tonic (heuristic: startsWith I/i) |
| 3 | 227 | | if (looksI64 && (txt!.StartsWith("I") || txt!.StartsWith("i"))) |
| | 228 | | { |
| 3 | 229 | | var relabeled = new HarmonyAnalysisResult( |
| 3 | 230 | | c.Success, |
| 3 | 231 | | RomanNumeral.V, |
| 3 | 232 | | TonalFunction.Dominant, |
| 3 | 233 | | "V64-53", |
| 3 | 234 | | c.Warnings, |
| 3 | 235 | | c.Errors |
| 3 | 236 | | ); |
| 3 | 237 | | chords[idxI64] = relabeled; |
| | 238 | | } |
| | 239 | | } |
| | 240 | | } |
| 19 | 241 | | cadInfos.Add(info2); |
| | 242 | | } |
| | 243 | | else |
| | 244 | | { |
| | 245 | | // Prefer voicing-based classification when allowed |
| 18 | 246 | | if (options.ShowNonCadentialSixFour) |
| | 247 | | { |
| 4 | 248 | | bool around64 = v != null && vMinus1 != null && vMinus2 != null && !string.IsNullOrEmpty(prevTxt |
| 4 | 249 | | if (around64) |
| | 250 | | { |
| 1 | 251 | | var hPrevPrev = Head(prevPrevTxt); |
| 1 | 252 | | var hCurr = Head(res.RomanText); |
| 1 | 253 | | if (!string.IsNullOrEmpty(hPrevPrev) && hPrevPrev == hCurr) |
| | 254 | | { |
| 1 | 255 | | int bPrevPrev = vMinus2!.Value.B % 12; |
| 1 | 256 | | int bCurr = v!.Value.B % 12; |
| 1 | 257 | | var six = (bPrevPrev == bCurr) ? SixFourType.Pedal : SixFourType.Passing; |
| 1 | 258 | | cadInfos.Add(new CadenceInfo(info.IndexFrom, info.Type, info.IsPerfectAuthentic, info.Ha |
| 1 | 259 | | goto NextChord2; |
| | 260 | | } |
| | 261 | | } |
| | 262 | | // Fallback: neighbors share harmony + voicings, decide by bass equality |
| 3 | 263 | | if (v != null && vMinus1 != null && vMinus2 != null) |
| | 264 | | { |
| 0 | 265 | | var hPrevPrev2 = Head(prevPrevTxt); |
| 0 | 266 | | var hCurr2 = Head(res.RomanText); |
| 0 | 267 | | if (!string.IsNullOrEmpty(hPrevPrev2) && hPrevPrev2 == hCurr2) |
| | 268 | | { |
| 0 | 269 | | int bPrevPrev = vMinus2!.Value.B % 12; |
| 0 | 270 | | int bCurr = v!.Value.B % 12; |
| 0 | 271 | | var six = (bPrevPrev == bCurr) ? SixFourType.Pedal : SixFourType.Passing; |
| 0 | 272 | | cadInfos.Add(new CadenceInfo(info.IndexFrom, info.Type, info.IsPerfectAuthentic, info.Ha |
| 0 | 273 | | goto NextChord2; |
| | 274 | | } |
| | 275 | | } |
| 3 | 276 | | if (info.SixFour != SixFourType.None && info.SixFour != SixFourType.Cadential) |
| 0 | 277 | | cadInfos.Add(info); |
| | 278 | | } |
| | 279 | | } |
| | 280 | | NextChord2: ; |
| | 281 | | } |
| 118 | 282 | | vMinus2 = vMinus1; vMinus1 = v; |
| 59 | 283 | | prevPrevTxt = prevTxt; |
| 59 | 284 | | prevRn = res.Roman; |
| 59 | 285 | | prevTxt = res.RomanText; |
| | 286 | | } |
| | 287 | |
|
| 22 | 288 | | return (new ProgressionResult(chords, new List<(int, CadenceType)>()), cadInfos); |
| | 289 | | } |
| | 290 | |
|
| | 291 | | public static (ProgressionResult result, List<(int start, int end, Key key)> segments) AnalyzeWithKeyEstimate( |
| | 292 | | IReadOnlyList<int[]> pcsList, Key initialKey, IReadOnlyList<FourPartVoicing?>? voicings = null, int window = 2) |
| | 293 | | { |
| 6 | 294 | | var keys = KeyEstimator.EstimatePerChord(pcsList, initialKey, window); |
| 6 | 295 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 6 | 296 | | var cads = new List<(int, CadenceType)>(); |
| | 297 | |
|
| 18 | 298 | | FourPartVoicing? prevV = null; RomanNumeral? prevRn = null; Key currKey = initialKey; |
| 56 | 299 | | for (int i = 0; i < pcsList.Count; i++) |
| | 300 | | { |
| 22 | 301 | | currKey = keys[i]; |
| 22 | 302 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 22 | 303 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcsList[i], currKey, v, prevV); |
| 22 | 304 | | chords.Add(res); |
| 22 | 305 | | if (prevRn != null && res.Roman != null) |
| | 306 | | { |
| 16 | 307 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, currKey.IsMajor); |
| 29 | 308 | | if (cad != CadenceType.None) cads.Add((i - 1, cad)); |
| | 309 | | } |
| 44 | 310 | | prevV = v; prevRn = res.Roman; |
| | 311 | | } |
| | 312 | |
|
| | 313 | | // Build key segments |
| 6 | 314 | | var segs = new List<(int start, int end, Key key)>(); |
| 6 | 315 | | if (keys.Count > 0) |
| | 316 | | { |
| 6 | 317 | | int s = 0; |
| 44 | 318 | | for (int i = 1; i < keys.Count; i++) |
| | 319 | | { |
| 16 | 320 | | if (!SameKey(keys[i - 1], keys[i])) |
| | 321 | | { |
| 10 | 322 | | segs.Add((s, i - 1, keys[i - 1])); |
| 10 | 323 | | s = i; |
| | 324 | | } |
| | 325 | | } |
| 6 | 326 | | segs.Add((s, keys.Count - 1, keys[^1])); |
| | 327 | | } |
| | 328 | |
|
| 6 | 329 | | return (new ProgressionResult(chords, cads), segs); |
| | 330 | | } |
| | 331 | |
|
| | 332 | | // Overload: HarmonyOptions を併用したキー推定 + 和音解析 |
| | 333 | | public static (ProgressionResult result, List<(int start, int end, Key key)> segments) AnalyzeWithKeyEstimate( |
| | 334 | | IReadOnlyList<int[]> pcsList, Key initialKey, HarmonyOptions options, IReadOnlyList<FourPartVoicing?>? voicings |
| | 335 | | { |
| 0 | 336 | | var keys = KeyEstimator.EstimatePerChord(pcsList, initialKey, window); |
| 0 | 337 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 0 | 338 | | var cads = new List<(int, CadenceType)>(); |
| | 339 | |
|
| 0 | 340 | | FourPartVoicing? prevV = null; RomanNumeral? prevRn = null; Key currKey = initialKey; |
| 0 | 341 | | for (int i = 0; i < pcsList.Count; i++) |
| | 342 | | { |
| 0 | 343 | | currKey = keys[i]; |
| 0 | 344 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 0 | 345 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcsList[i], currKey, options, v, prevV); |
| 0 | 346 | | chords.Add(res); |
| 0 | 347 | | if (prevRn != null && res.Roman != null) |
| | 348 | | { |
| 0 | 349 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, currKey.IsMajor); |
| 0 | 350 | | if (cad != CadenceType.None) cads.Add((i - 1, cad)); |
| | 351 | | } |
| 0 | 352 | | prevV = v; prevRn = res.Roman; |
| | 353 | | } |
| | 354 | |
|
| | 355 | | // Build key segments |
| 0 | 356 | | var segs = new List<(int start, int end, Key key)>(); |
| 0 | 357 | | if (keys.Count > 0) |
| | 358 | | { |
| 0 | 359 | | int s = 0; |
| 0 | 360 | | for (int i = 1; i < keys.Count; i++) |
| | 361 | | { |
| 0 | 362 | | if (!SameKey(keys[i - 1], keys[i])) |
| | 363 | | { |
| 0 | 364 | | segs.Add((s, i - 1, keys[i - 1])); |
| 0 | 365 | | s = i; |
| | 366 | | } |
| | 367 | | } |
| 0 | 368 | | segs.Add((s, keys.Count - 1, keys[^1])); |
| | 369 | | } |
| | 370 | |
|
| 0 | 371 | | return (new ProgressionResult(chords, cads), segs); |
| | 372 | | } |
| | 373 | |
|
| | 374 | | // Overload exposing the per-chord key trace and allowing estimator Options. |
| | 375 | | public static (ProgressionResult result, List<(int start, int end, Key key)> segments, List<Key> keys) AnalyzeWithKe |
| | 376 | | IReadOnlyList<int[]> pcsList, Key initialKey, KeyEstimator.Options options, IReadOnlyList<FourPartVoicing?>? voi |
| | 377 | | { |
| 0 | 378 | | var keys = KeyEstimator.EstimatePerChord(pcsList, initialKey, options); |
| | 379 | | // Enforce MinSwitchIndex: force staying on previous key before this index |
| 0 | 380 | | if (options.MinSwitchIndex > 0 && keys.Count > 1) |
| | 381 | | { |
| 0 | 382 | | if (options.MinSwitchIndex >= keys.Count) |
| | 383 | | { |
| | 384 | | // Full lock: keep entire sequence on the initial key |
| 0 | 385 | | for (int i = 0; i < keys.Count; i++) keys[i] = initialKey; |
| | 386 | | } |
| | 387 | | else |
| | 388 | | { |
| 0 | 389 | | int limit = Math.Min(options.MinSwitchIndex, keys.Count); |
| 0 | 390 | | for (int i = 1; i < limit; i++) keys[i] = keys[i - 1]; |
| | 391 | | } |
| | 392 | | } |
| 0 | 393 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 0 | 394 | | var cads = new List<(int, CadenceType)>(); |
| | 395 | |
|
| 0 | 396 | | FourPartVoicing? prevV = null; RomanNumeral? prevRn = null; Key currKey = initialKey; |
| 0 | 397 | | for (int i = 0; i < pcsList.Count; i++) |
| | 398 | | { |
| 0 | 399 | | currKey = keys[i]; |
| 0 | 400 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 0 | 401 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcsList[i], currKey, v, prevV); |
| 0 | 402 | | chords.Add(res); |
| 0 | 403 | | if (prevRn != null && res.Roman != null) |
| | 404 | | { |
| 0 | 405 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, currKey.IsMajor); |
| 0 | 406 | | if (cad != CadenceType.None) cads.Add((i - 1, cad)); |
| | 407 | | } |
| 0 | 408 | | prevV = v; prevRn = res.Roman; |
| | 409 | | } |
| | 410 | |
|
| 0 | 411 | | var segs = new List<(int start, int end, Key key)>(); |
| 0 | 412 | | if (keys.Count > 0) |
| | 413 | | { |
| 0 | 414 | | int s = 0; |
| 0 | 415 | | for (int i = 1; i < keys.Count; i++) |
| | 416 | | { |
| 0 | 417 | | if (!SameKey(keys[i - 1], keys[i])) |
| | 418 | | { |
| 0 | 419 | | segs.Add((s, i - 1, keys[i - 1])); |
| 0 | 420 | | s = i; |
| | 421 | | } |
| | 422 | | } |
| 0 | 423 | | segs.Add((s, keys.Count - 1, keys[^1])); |
| | 424 | | } |
| | 425 | |
|
| 0 | 426 | | return (new ProgressionResult(chords, cads), segs, keys); |
| | 427 | | } |
| | 428 | |
|
| | 429 | | // Overload: KeyEstimator.Options + HarmonyOptions(トレース無し) |
| | 430 | | public static (ProgressionResult result, List<(int start, int end, Key key)> segments, List<Key> keys) AnalyzeWithKe |
| | 431 | | IReadOnlyList<int[]> pcsList, |
| | 432 | | Key initialKey, |
| | 433 | | KeyEstimator.Options options, |
| | 434 | | HarmonyOptions harmonyOptions, |
| | 435 | | IReadOnlyList<FourPartVoicing?>? voicings = null) |
| | 436 | | { |
| 0 | 437 | | var keys = KeyEstimator.EstimatePerChord(pcsList, initialKey, options); |
| | 438 | | // Enforce MinSwitchIndex: force staying on previous key before this index |
| 0 | 439 | | if (options.MinSwitchIndex > 0 && keys.Count > 1) |
| | 440 | | { |
| 0 | 441 | | if (options.MinSwitchIndex >= keys.Count) |
| | 442 | | { |
| 0 | 443 | | for (int i = 0; i < keys.Count; i++) keys[i] = initialKey; |
| | 444 | | } |
| | 445 | | else |
| | 446 | | { |
| 0 | 447 | | int limit = Math.Min(options.MinSwitchIndex, keys.Count); |
| 0 | 448 | | for (int i = 1; i < limit; i++) keys[i] = keys[i - 1]; |
| | 449 | | } |
| | 450 | | } |
| 0 | 451 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 0 | 452 | | var cads = new List<(int, CadenceType)>(); |
| | 453 | |
|
| 0 | 454 | | FourPartVoicing? prevV = null; RomanNumeral? prevRn = null; Key currKey = initialKey; |
| 0 | 455 | | for (int i = 0; i < pcsList.Count; i++) |
| | 456 | | { |
| 0 | 457 | | currKey = keys[i]; |
| 0 | 458 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 0 | 459 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcsList[i], currKey, harmonyOptions, v, prevV); |
| 0 | 460 | | chords.Add(res); |
| 0 | 461 | | if (prevRn != null && res.Roman != null) |
| | 462 | | { |
| 0 | 463 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, currKey.IsMajor); |
| 0 | 464 | | if (cad != CadenceType.None) cads.Add((i - 1, cad)); |
| | 465 | | } |
| 0 | 466 | | prevV = v; prevRn = res.Roman; |
| | 467 | | } |
| | 468 | |
|
| 0 | 469 | | var segs = new List<(int start, int end, Key key)>(); |
| 0 | 470 | | if (keys.Count > 0) |
| | 471 | | { |
| 0 | 472 | | int s = 0; |
| 0 | 473 | | for (int i = 1; i < keys.Count; i++) |
| | 474 | | { |
| 0 | 475 | | if (!SameKey(keys[i - 1], keys[i])) |
| | 476 | | { |
| 0 | 477 | | segs.Add((s, i - 1, keys[i - 1])); |
| 0 | 478 | | s = i; |
| | 479 | | } |
| | 480 | | } |
| 0 | 481 | | segs.Add((s, keys.Count - 1, keys[^1])); |
| | 482 | | } |
| | 483 | |
|
| 0 | 484 | | return (new ProgressionResult(chords, cads), segs, keys); |
| | 485 | | } |
| | 486 | |
|
| | 487 | | // New overload: expose per-chord key-estimation trace via out parameter. |
| | 488 | | public static (ProgressionResult result, List<(int start, int end, Key key)> segments, List<Key> keys) AnalyzeWithKe |
| | 489 | | IReadOnlyList<int[]> pcsList, |
| | 490 | | Key initialKey, |
| | 491 | | KeyEstimator.Options options, |
| | 492 | | out List<KeyEstimator.TraceEntry> trace, |
| | 493 | | IReadOnlyList<FourPartVoicing?>? voicings = null) |
| | 494 | | { |
| 8 | 495 | | var keys = KeyEstimator.EstimatePerChord(pcsList, initialKey, options, out trace, voicings); |
| | 496 | | // Enforce MinSwitchIndex: force staying on previous key before this index |
| 8 | 497 | | if (options.MinSwitchIndex > 0 && keys.Count > 1) |
| | 498 | | { |
| 1 | 499 | | if (options.MinSwitchIndex >= keys.Count) |
| | 500 | | { |
| 11 | 501 | | for (int i = 0; i < keys.Count; i++) keys[i] = initialKey; |
| | 502 | | } |
| | 503 | | else |
| | 504 | | { |
| 0 | 505 | | int limit = Math.Min(options.MinSwitchIndex, keys.Count); |
| 0 | 506 | | for (int i = 1; i < limit; i++) keys[i] = keys[i - 1]; |
| | 507 | | } |
| | 508 | | } |
| 8 | 509 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 8 | 510 | | var cads = new List<(int, CadenceType)>(); |
| | 511 | |
|
| 24 | 512 | | FourPartVoicing? prevV = null; RomanNumeral? prevRn = null; Key currKey = initialKey; |
| 86 | 513 | | for (int i = 0; i < pcsList.Count; i++) |
| | 514 | | { |
| 35 | 515 | | currKey = keys[i]; |
| 35 | 516 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 35 | 517 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcsList[i], currKey, v, prevV); |
| 35 | 518 | | chords.Add(res); |
| 35 | 519 | | if (prevRn != null && res.Roman != null) |
| | 520 | | { |
| 26 | 521 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, currKey.IsMajor); |
| 46 | 522 | | if (cad != CadenceType.None) cads.Add((i - 1, cad)); |
| | 523 | | } |
| 70 | 524 | | prevV = v; prevRn = res.Roman; |
| | 525 | | } |
| | 526 | |
|
| 8 | 527 | | var segs = new List<(int start, int end, Key key)>(); |
| 8 | 528 | | if (keys.Count > 0) |
| | 529 | | { |
| 8 | 530 | | int s = 0; |
| 70 | 531 | | for (int i = 1; i < keys.Count; i++) |
| | 532 | | { |
| 27 | 533 | | if (!SameKey(keys[i - 1], keys[i])) |
| | 534 | | { |
| 2 | 535 | | segs.Add((s, i - 1, keys[i - 1])); |
| 2 | 536 | | s = i; |
| | 537 | | } |
| | 538 | | } |
| 8 | 539 | | segs.Add((s, keys.Count - 1, keys[^1])); |
| | 540 | | } |
| | 541 | |
|
| 8 | 542 | | return (new ProgressionResult(chords, cads), segs, keys); |
| | 543 | | } |
| | 544 | |
|
| | 545 | | // Overload: KeyEstimator.Options + HarmonyOptions(トレース有り) |
| | 546 | | public static (ProgressionResult result, List<(int start, int end, Key key)> segments, List<Key> keys) AnalyzeWithKe |
| | 547 | | IReadOnlyList<int[]> pcsList, |
| | 548 | | Key initialKey, |
| | 549 | | KeyEstimator.Options options, |
| | 550 | | HarmonyOptions harmonyOptions, |
| | 551 | | out List<KeyEstimator.TraceEntry> trace, |
| | 552 | | IReadOnlyList<FourPartVoicing?>? voicings = null) |
| | 553 | | { |
| 6 | 554 | | var keys = KeyEstimator.EstimatePerChord(pcsList, initialKey, options, out trace, voicings); |
| | 555 | | // Enforce MinSwitchIndex: force staying on previous key before this index |
| 6 | 556 | | if (options.MinSwitchIndex > 0 && keys.Count > 1) |
| | 557 | | { |
| 2 | 558 | | if (options.MinSwitchIndex >= keys.Count) |
| | 559 | | { |
| 28 | 560 | | for (int i = 0; i < keys.Count; i++) keys[i] = initialKey; |
| | 561 | | } |
| | 562 | | else |
| | 563 | | { |
| 0 | 564 | | int limit = Math.Min(options.MinSwitchIndex, keys.Count); |
| 0 | 565 | | for (int i = 1; i < limit; i++) keys[i] = keys[i - 1]; |
| | 566 | | } |
| | 567 | | } |
| 6 | 568 | | var chords = new List<HarmonyAnalysisResult>(pcsList.Count); |
| 6 | 569 | | var cads = new List<(int, CadenceType)>(); |
| | 570 | |
|
| 18 | 571 | | FourPartVoicing? prevV = null; RomanNumeral? prevRn = null; Key currKey = initialKey; |
| 44 | 572 | | for (int i = 0; i < pcsList.Count; i++) |
| | 573 | | { |
| 16 | 574 | | currKey = keys[i]; |
| 16 | 575 | | var v = voicings != null && i < voicings.Count ? voicings[i] : null; |
| 16 | 576 | | var res = HarmonyAnalyzer.AnalyzeTriad(pcsList[i], currKey, harmonyOptions, v, prevV); |
| 16 | 577 | | chords.Add(res); |
| 16 | 578 | | if (prevRn != null && res.Roman != null) |
| | 579 | | { |
| 9 | 580 | | var cad = CadenceAnalyzer.Detect(prevRn, res.Roman, currKey.IsMajor); |
| 17 | 581 | | if (cad != CadenceType.None) cads.Add((i - 1, cad)); |
| | 582 | | } |
| 32 | 583 | | prevV = v; prevRn = res.Roman; |
| | 584 | | } |
| | 585 | |
|
| 6 | 586 | | var segs = new List<(int start, int end, Key key)>(); |
| 6 | 587 | | if (keys.Count > 0) |
| | 588 | | { |
| 6 | 589 | | int s = 0; |
| 32 | 590 | | for (int i = 1; i < keys.Count; i++) |
| | 591 | | { |
| 10 | 592 | | if (!SameKey(keys[i - 1], keys[i])) |
| | 593 | | { |
| 2 | 594 | | segs.Add((s, i - 1, keys[i - 1])); |
| 2 | 595 | | s = i; |
| | 596 | | } |
| | 597 | | } |
| 6 | 598 | | segs.Add((s, keys.Count - 1, keys[^1])); |
| | 599 | | } |
| | 600 | |
|
| 6 | 601 | | return (new ProgressionResult(chords, cads), segs, keys); |
| | 602 | | } |
| | 603 | |
|
| | 604 | | // Overload: also compute a simple confidence for each key segment (0..1) |
| | 605 | | // confidence is derived from per-chord trace: avg of (max - secondBest) / max within the segment (where max>0) |
| | 606 | | public static (ProgressionResult result, List<(int start, int end, Key key, double confidence)> segments, List<Key> |
| | 607 | | IReadOnlyList<int[]> pcsList, |
| | 608 | | Key initialKey, |
| | 609 | | KeyEstimator.Options options, |
| | 610 | | out List<KeyEstimator.TraceEntry> trace, |
| | 611 | | IReadOnlyList<FourPartVoicing?>? voicings, |
| | 612 | | bool withConfidence) |
| | 613 | | { |
| 5 | 614 | | var (res, segs, keys) = AnalyzeWithKeyEstimate(pcsList, initialKey, options, out trace, voicings); |
| 5 | 615 | | var segsWithConf = new List<(int start, int end, Key key, double confidence)>(segs.Count); |
| 24 | 616 | | foreach (var s in segs) |
| | 617 | | { |
| 14 | 618 | | double sum = 0; int count = 0; |
| 66 | 619 | | for (int i = s.start; i <= s.end && i < trace.Count; i++) |
| | 620 | | { |
| 26 | 621 | | int max = Math.Max(0, trace[i].MaxScore); |
| 26 | 622 | | int second = Math.Max(0, trace[i].SecondBestScore); |
| 26 | 623 | | if (max > 0) |
| | 624 | | { |
| 21 | 625 | | sum += Math.Clamp((max - second) / (double)max, 0.0, 1.0); |
| 21 | 626 | | count++; |
| | 627 | | } |
| | 628 | | } |
| 7 | 629 | | double conf = count > 0 ? sum / count : 0.0; |
| 7 | 630 | | segsWithConf.Add((s.start, s.end, s.key, conf)); |
| | 631 | | } |
| 5 | 632 | | return (res, segsWithConf, keys); |
| | 633 | | } |
| | 634 | |
|
| | 635 | | // Overload: KeyEstimator.Options + HarmonyOptions(トレース有り + 信頼度) |
| | 636 | | public static (ProgressionResult result, List<(int start, int end, Key key, double confidence)> segments, List<Key> |
| | 637 | | IReadOnlyList<int[]> pcsList, |
| | 638 | | Key initialKey, |
| | 639 | | KeyEstimator.Options options, |
| | 640 | | HarmonyOptions harmonyOptions, |
| | 641 | | out List<KeyEstimator.TraceEntry> trace, |
| | 642 | | IReadOnlyList<FourPartVoicing?>? voicings, |
| | 643 | | bool withConfidence) |
| | 644 | | { |
| 1 | 645 | | var (res, segs, keys) = AnalyzeWithKeyEstimate(pcsList, initialKey, options, harmonyOptions, out trace, voicings |
| 1 | 646 | | var segsWithConf = new List<(int start, int end, Key key, double confidence)>(segs.Count); |
| 4 | 647 | | foreach (var s in segs) |
| | 648 | | { |
| 2 | 649 | | double sum = 0; int count = 0; |
| 12 | 650 | | for (int i = s.start; i <= s.end && i < trace.Count; i++) |
| | 651 | | { |
| 5 | 652 | | int max = Math.Max(0, trace[i].MaxScore); |
| 5 | 653 | | int second = Math.Max(0, trace[i].SecondBestScore); |
| 5 | 654 | | if (max > 0) |
| | 655 | | { |
| 0 | 656 | | sum += Math.Clamp((max - second) / (double)max, 0.0, 1.0); |
| 0 | 657 | | count++; |
| | 658 | | } |
| | 659 | | } |
| 1 | 660 | | double conf = count > 0 ? sum / count : 0.0; |
| 1 | 661 | | segsWithConf.Add((s.start, s.end, s.key, conf)); |
| | 662 | | } |
| 1 | 663 | | return (res, segsWithConf, keys); |
| | 664 | | } |
| | 665 | |
|
| | 666 | | // New: Modulation-aware segments post-filtering |
| | 667 | | // - minLength: require at least N chords for a new key segment |
| | 668 | | // - minConfidence: require average normalized margin >= threshold (0..1) for the segment |
| | 669 | | public static (ProgressionResult result, List<(int start, int end, Key key, double confidence)> segments, List<Key> |
| | 670 | | IReadOnlyList<int[]> pcsList, |
| | 671 | | Key initialKey, |
| | 672 | | KeyEstimator.Options options, |
| | 673 | | out List<KeyEstimator.TraceEntry> trace, |
| | 674 | | IReadOnlyList<FourPartVoicing?>? voicings, |
| | 675 | | int minLength = 2, |
| | 676 | | double minConfidence = 0.2) |
| | 677 | | { |
| 4 | 678 | | var (res, segsWithConf, keys) = AnalyzeWithKeyEstimate(pcsList, initialKey, options, out trace, voicings, withCo |
| 4 | 679 | | var filtered = new List<(int start, int end, Key key, double confidence)>(); |
| 20 | 680 | | foreach (var s in segsWithConf) |
| | 681 | | { |
| 6 | 682 | | int len = s.end - s.start + 1; |
| 6 | 683 | | if (len >= minLength && s.confidence >= minConfidence) |
| | 684 | | { |
| 4 | 685 | | filtered.Add(s); |
| | 686 | | } |
| | 687 | | } |
| | 688 | | // Safety: ensure at least one segment is returned to avoid empty results in borderline cases. |
| | 689 | | // If all segments were filtered out by thresholds, return the first segment as a fallback. |
| 4 | 690 | | if (filtered.Count == 0 && segsWithConf.Count > 0) |
| | 691 | | { |
| 2 | 692 | | filtered.Add(segsWithConf[0]); |
| | 693 | | } |
| 4 | 694 | | return (res, filtered, keys); |
| | 695 | | } |
| | 696 | |
|
| 53 | 697 | | private static bool SameKey(Key a, Key b) => a.IsMajor == b.IsMajor && ((a.TonicMidi % 12 + 12) % 12) == ((b.TonicMi |
| | 698 | | } |