Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
chord_descriptor.h
1// SPDX-FileCopyrightText: © 2018-2022, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6namespace zrythm::utils
7{
8class Utf8String;
9}
10
11#include <QObject>
12
13#include <nlohmann/json_fwd.hpp>
14
15namespace zrythm::dsp
16{
17Q_NAMESPACE
18
19enum class MusicalNote : std::uint8_t
20{
21 C = 0,
22 CSharp,
23 D,
24 DSharp,
25 E,
26 F,
27 FSharp,
28 G,
29 GSharp,
30 A,
31 ASharp,
32 B
33};
34Q_ENUM_NS (MusicalNote)
35
36
39enum class ChordType : std::uint8_t
40{
41 None,
42 Major,
43 Minor,
44 Diminished,
45 SuspendedFourth,
46 SuspendedSecond,
47 Augmented,
48 Custom,
49};
50Q_ENUM_NS (ChordType)
51
52
55enum class ChordAccent : std::uint8_t
56{
57 None,
60 Seventh,
62 MajorSeventh,
63 /* NOTE: all accents below assume Seventh */
65 FlatNinth,
67 Ninth,
69 SharpNinth,
71 Eleventh,
73 FlatFifthSharpEleventh,
75 SharpFifthFlatThirteenth,
77 SixthThirteenth,
78};
79Q_ENUM_NS (ChordAccent)
80
81
87class ChordDescriptor
88{
89public:
90 static constexpr size_t MAX_NOTES = 48;
91
92public:
93 ChordDescriptor () = default;
94
99 MusicalNote root,
100 bool has_bass,
101 MusicalNote bass,
102 ChordType type,
103 ChordAccent accent,
104 int inversion)
105 : has_bass_ (has_bass), root_note_ (root), bass_note_ (bass),
106 type_ (type), accent_ (accent), inversion_ (inversion)
107 {
108 update_notes ();
109 }
110
111 int get_max_inversion () const
112 {
113 int max_inv = 2;
114 switch (accent_)
115 {
116 case ChordAccent::None:
117 break;
118 case ChordAccent::Seventh:
119 case ChordAccent::MajorSeventh:
120 case ChordAccent::FlatNinth:
121 case ChordAccent::Ninth:
122 case ChordAccent::SharpNinth:
123 case ChordAccent::Eleventh:
124 max_inv = 3;
125 break;
126 case ChordAccent::FlatFifthSharpEleventh:
127 case ChordAccent::SharpFifthFlatThirteenth:
128 case ChordAccent::SixthThirteenth:
129 max_inv = 4;
130 break;
131 default:
132 break;
133 }
134
135 return max_inv;
136 }
137
138 int get_min_inversion () const { return -get_max_inversion (); }
139
146 bool is_key_in_chord (MusicalNote key) const;
147
153 bool is_key_bass (MusicalNote key) const;
154
159
163 static utils::Utf8String chord_accent_to_string (ChordAccent accent);
164
168 static utils::Utf8String note_to_string (MusicalNote note);
169
174
180
181 friend bool
182 operator== (const ChordDescriptor &lhs, const ChordDescriptor &rhs)
183 {
184 return lhs.has_bass_ == rhs.has_bass_ && lhs.root_note_ == rhs.root_note_
185 && lhs.bass_note_ == rhs.bass_note_ && lhs.type_ == rhs.type_
186 && lhs.notes_ == rhs.notes_ && lhs.inversion_ == rhs.inversion_;
187 }
188
189private:
190 friend void to_json (nlohmann::json &j, const ChordDescriptor &c);
191 friend void from_json (const nlohmann::json &j, ChordDescriptor &c);
192
193public:
195 bool has_bass_ = false;
196
198 MusicalNote root_note_ = MusicalNote::C;
199
201 MusicalNote bass_note_ = MusicalNote::C;
202
204 ChordType type_ = ChordType::None;
205
211 ChordAccent accent_ = ChordAccent::None;
212
221 std::array<bool, MAX_NOTES> notes_{};
222
228 int inversion_ = 0;
229};
230
231} // namespace zrythm::dsp
A ChordDescriptor describes a chord and is not linked to any specific object by itself.
void update_notes()
Updates the notes array based on the current settings.
ChordAccent accent_
Chord accent.
MusicalNote root_note_
Root note.
int inversion_
0 no inversion, less than 0 highest note(s) drop an octave, greater than 0 lowest note(s) receive an ...
utils::Utf8String to_string() const
Returns the chord in human readable string.
ChordDescriptor(MusicalNote root, bool has_bass, MusicalNote bass, ChordType type, ChordAccent accent, int inversion)
Creates a ChordDescriptor.
static utils::Utf8String chord_type_to_string(ChordType type)
Returns the chord type as a string (eg.
std::array< bool, MAX_NOTES > notes_
Only used if custom chord.
static utils::Utf8String note_to_string(MusicalNote note)
Returns the musical note as a string (eg.
MusicalNote bass_note_
Bass note 1 octave below.
bool is_key_bass(MusicalNote key) const
Returns if key is the bass or root note of chord.
bool is_key_in_chord(MusicalNote key) const
Returns if the given key is in the chord represented by the given ChordDescriptor.
static utils::Utf8String chord_accent_to_string(ChordAccent accent)
Returns the chord accent as a string (eg.
bool has_bass_
Has bass note or not.
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:37
String utilities.