|
| 1 | +// Copyright (c) ktsu.dev |
| 2 | +// All rights reserved. |
| 3 | +// Licensed under the MIT license. |
| 4 | + |
| 5 | +namespace ktsu.Semantics.Music; |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | + |
| 11 | +/// <summary>The structural form of a piece: its section letter-pattern and any recognized named form.</summary> |
| 12 | +public sealed record Form |
| 13 | +{ |
| 14 | + private static readonly int[][] BluesTemplates = |
| 15 | + [ |
| 16 | + [1, 1, 1, 1, 4, 4, 1, 1, 5, 4, 1, 1], |
| 17 | + [1, 1, 1, 1, 4, 4, 1, 1, 5, 4, 1, 5], |
| 18 | + [1, 4, 1, 1, 4, 4, 1, 1, 5, 4, 1, 1], |
| 19 | + [1, 4, 1, 1, 4, 4, 1, 1, 5, 4, 1, 5], |
| 20 | + ]; |
| 21 | + |
| 22 | + /// <summary>Gets the section letter-pattern (e.g. "AABA").</summary> |
| 23 | + public string Pattern { get; private init; } = ""; |
| 24 | + |
| 25 | + /// <summary>Gets the per-section letters, aligned to arrangement order.</summary> |
| 26 | + public IReadOnlyList<char> Letters { get; private init; } = []; |
| 27 | + |
| 28 | + /// <summary>Gets the recognized named form, or null.</summary> |
| 29 | + public NamedForm? Name { get; private init; } |
| 30 | + |
| 31 | + /// <summary>Derives the form of an arrangement from its sections.</summary> |
| 32 | + /// <param name="arrangement">The arrangement to analyze.</param> |
| 33 | + /// <returns>The derived form.</returns> |
| 34 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="arrangement"/> is null.</exception> |
| 35 | + public static Form Of(Arrangement arrangement) |
| 36 | + { |
| 37 | + Ensure.NotNull(arrangement); |
| 38 | + IReadOnlyList<Section> sections = arrangement.Sections; |
| 39 | + |
| 40 | + List<char> letters = []; |
| 41 | + List<Section> representatives = []; |
| 42 | + foreach (Section section in sections) |
| 43 | + { |
| 44 | + int index = representatives.FindIndex(representative => representative.IsSameStructure(section)); |
| 45 | + if (index < 0) |
| 46 | + { |
| 47 | + representatives.Add(section); |
| 48 | + index = representatives.Count - 1; |
| 49 | + } |
| 50 | + |
| 51 | + letters.Add((char)('A' + index)); |
| 52 | + } |
| 53 | + |
| 54 | + string pattern = new([.. letters]); |
| 55 | + NamedForm name = sections.Any(section => IsTwelveBarBlues(section, section.Key ?? arrangement.Key)) |
| 56 | + ? NamedForm.TwelveBarBlues |
| 57 | + : RecognizePattern(pattern); |
| 58 | + |
| 59 | + return new() { Pattern = pattern, Letters = letters, Name = name }; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary>Builds a form directly from a letter-pattern string, applying letter recognition only.</summary> |
| 63 | + /// <param name="pattern">A pattern of letters A-Z (e.g. "ABACA").</param> |
| 64 | + /// <returns>The form for the pattern.</returns> |
| 65 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="pattern"/> is null.</exception> |
| 66 | + /// <exception cref="FormatException">Thrown when the pattern is empty or contains a non-letter.</exception> |
| 67 | + public static Form FromPattern(string pattern) |
| 68 | + { |
| 69 | + Ensure.NotNull(pattern); |
| 70 | + if (pattern.Length == 0) |
| 71 | + { |
| 72 | + throw new FormatException("Form pattern is empty."); |
| 73 | + } |
| 74 | + |
| 75 | + if (!pattern.All(c => c is >= 'A' and <= 'Z')) |
| 76 | + { |
| 77 | + throw new FormatException($"Form pattern '{pattern}' must contain only upper-case letters."); |
| 78 | + } |
| 79 | + |
| 80 | + return new() { Pattern = pattern, Letters = [.. pattern], Name = RecognizePattern(pattern) }; |
| 81 | + } |
| 82 | + |
| 83 | + private static NamedForm RecognizePattern(string pattern) => pattern switch |
| 84 | + { |
| 85 | + "AABA" => NamedForm.ThirtyTwoBarAABA, |
| 86 | + "AB" => NamedForm.Binary, |
| 87 | + "ABA" => NamedForm.Ternary, |
| 88 | + _ when IsRondo(pattern) => NamedForm.Rondo, |
| 89 | + _ when pattern.Length > 1 && pattern.All(letter => letter == pattern[0]) => NamedForm.Strophic, |
| 90 | + _ when pattern.Distinct().Count() == pattern.Length => NamedForm.ThroughComposed, |
| 91 | + _ => NamedForm.Unknown, |
| 92 | + }; |
| 93 | + |
| 94 | + private static bool IsRondo(string pattern) |
| 95 | + { |
| 96 | + if (pattern.Length < 5 || pattern.Length % 2 == 0) |
| 97 | + { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + for (int i = 0; i < pattern.Length; i++) |
| 102 | + { |
| 103 | + bool refrainPosition = i % 2 == 0; |
| 104 | + if (refrainPosition != (pattern[i] == 'A')) |
| 105 | + { |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + private static bool IsTwelveBarBlues(Section section, Key key) |
| 114 | + { |
| 115 | + IReadOnlyList<ChordEvent> chords = section.Progression.Chords; |
| 116 | + if (chords.Count != 12) |
| 117 | + { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + int[] degrees = new int[12]; |
| 122 | + for (int i = 0; i < 12; i++) |
| 123 | + { |
| 124 | + ScaleDegree degree = key.FunctionOf(chords[i].Chord.Root); |
| 125 | + if (degree.Alteration != 0) |
| 126 | + { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + degrees[i] = degree.Degree; |
| 131 | + } |
| 132 | + |
| 133 | + return BluesTemplates.Any(template => template.SequenceEqual(degrees)); |
| 134 | + } |
| 135 | +} |
0 commit comments