41 constexpr std::array<std::array<std::string_view, 5>, 7> names{{
42 {{
"Cbb",
"Cb",
"C",
"C#",
"Cx"}},
43 {{
"Dbb",
"Db",
"D",
"D#",
"Dx"}},
44 {{
"Ebb",
"Eb",
"E",
"E#",
"Ex"}},
45 {{
"Fbb",
"Fb",
"F",
"F#",
"Fx"}},
46 {{
"Gbb",
"Gb",
"G",
"G#",
"Gx"}},
47 {{
"Abb",
"Ab",
"A",
"A#",
"Ax"}},
48 {{
"Bbb",
"Bb",
"B",
"B#",
"Bx"}},
50 const auto letter =
static_cast<std::size_t
>(spelling.letter);
51 const int accidental = spelling.accidental;
52 if (letter >= names.size() || accidental < -2 || accidental > 2)
54 return names[letter][
static_cast<std::size_t
>(accidental + 2)];
59 if (!
is_valid(pitch_class) ||
static_cast<unsigned>(policy) > 2)
61 constexpr std::array<NoteLetter, 12> sharp_letters{
64 constexpr std::array<std::int8_t, 12> sharp_accidentals{0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0};
65 constexpr std::array<NoteLetter, 12> flat_letters{
68 constexpr std::array<std::int8_t, 12> flat_accidentals{0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0};
69 const auto index =
static_cast<std::size_t
>(pitch_class);
72 flats ? flat_accidentals[index] : sharp_accidentals[index],
78 constexpr std::size_t
size() const noexcept {
82 constexpr std::optional<SpelledPitchClass>
note(std::size_t index)
const noexcept {
94 std::array<SpelledPitchClass, ChordFormula::kMaxNotes> notes_{};
95 std::uint8_t count_ = 0;
101 if (!spelled_root || formula.size() == 0)
104 constexpr std::array<int, 7> natural_pitch_classes{0, 2, 4, 5, 7, 9, 11};
105 constexpr std::array<int, 12> diatonic_steps{0, 1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 6};
107 result.count_ =
static_cast<std::uint8_t
>(formula.size());
108 const int root_letter =
static_cast<int>(spelled_root->letter);
110 for (std::size_t
note = 0;
note < formula.size(); ++
note) {
111 const int interval = *formula.interval(
note);
114 int letter_steps = octaves * 7 + diatonic_steps[within_octave];
117 else if (interval == 18)
119 else if (interval == 20)
121 const auto letter =
static_cast<NoteLetter>((root_letter + letter_steps) % 7);
122 const auto pitch_class =
transpose(root, interval);
124 static_cast<int>(pitch_class) - natural_pitch_classes[
static_cast<std::size_t
>(letter)];
125 while (accidental > 6)
127 while (accidental < -6)
129 if (accidental < -2 || accidental > 2)
131 result.notes_[
note] =
139 return spell_chord(chord.root_pitch_class(), chord.formula(), policy);
Definition spelling.hpp:76
constexpr std::size_t size() const noexcept
Definition spelling.hpp:78
constexpr std::optional< SpelledPitchClass > note(std::size_t index) const noexcept
Definition spelling.hpp:82
constexpr auto operator<=>(const SpelledChord &) const =default
friend constexpr std::optional< SpelledChord > spell_chord(PitchClass, const ChordFormula &, AccidentalPolicy) noexcept
Definition spelling.hpp:98
constexpr bool is_valid(PitchClass pitch_class) noexcept
Definition pitch.hpp:34
constexpr std::optional< SpelledChord > spell_chord(PitchClass root, const ChordFormula &formula, AccidentalPolicy policy) noexcept
Definition spelling.hpp:98
NoteLetter
Definition spelling.hpp:14
@ a
Definition spelling.hpp:20
@ c
Definition spelling.hpp:15
@ d
Definition spelling.hpp:16
@ f
Definition spelling.hpp:18
@ b
Definition spelling.hpp:21
@ g
Definition spelling.hpp:19
@ e
Definition spelling.hpp:17
constexpr PitchClass transpose(PitchClass pitch_class, int semitones) noexcept
Definition pitch.hpp:51
constexpr int kPitchClassesPerOctave
Definition pitch.hpp:16
PitchClass
Definition pitch.hpp:19
@ a
Definition pitch.hpp:29
@ c
Definition pitch.hpp:20
@ d
Definition pitch.hpp:22
@ f
Definition pitch.hpp:25
@ b
Definition pitch.hpp:31
@ g
Definition pitch.hpp:27
@ e
Definition pitch.hpp:24
AccidentalPolicy
Definition spelling.hpp:24
@ prefer_flats
Definition spelling.hpp:26
@ minimize_accidentals
Definition spelling.hpp:29
@ prefer_sharps
Definition spelling.hpp:25
constexpr std::string_view spelling_name(SpelledPitchClass spelling) noexcept
Definition spelling.hpp:40
constexpr std::optional< SpelledPitchClass > spell_pitch_class(PitchClass pitch_class, AccidentalPolicy policy) noexcept
Definition spelling.hpp:57
Definition spelling.hpp:32
NoteLetter letter
Definition spelling.hpp:33
constexpr auto operator<=>(const SpelledPitchClass &) const =default
std::int8_t accidental
Definition spelling.hpp:34
PitchClass pitch_class
Definition spelling.hpp:35