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
6#include "utils/utf8_string.h"
7
8#include <nlohmann/json.hpp>
9
10namespace zrythm::dsp
11{
12Q_NAMESPACE
13
14enum class MusicalNote : std::uint8_t
15{
16 C = 0,
17 CSharp,
18 D,
19 DSharp,
20 E,
21 F,
22 FSharp,
23 G,
24 GSharp,
25 A,
26 ASharp,
27 B
28};
29Q_ENUM_NS (MusicalNote)
30
31
34enum class ChordType : std::uint8_t
35{
36 None,
37 Major,
38 Minor,
39 Diminished,
40 SuspendedFourth,
41 SuspendedSecond,
42 Augmented,
43 Custom,
44};
45Q_ENUM_NS (ChordType)
46
47
50enum class ChordAccent : std::uint8_t
51{
52 None,
55 Seventh,
57 MajorSeventh,
58 /* NOTE: all accents below assume Seventh */
60 FlatNinth,
62 Ninth,
64 SharpNinth,
66 Eleventh,
68 FlatFifthSharpEleventh,
70 SharpFifthFlatThirteenth,
72 SixthThirteenth,
73};
74Q_ENUM_NS (ChordAccent)
75
76
82class ChordDescriptor
83{
84public:
85 static constexpr size_t MAX_NOTES = 48;
86
87public:
88 ChordDescriptor () = default;
89
94 MusicalNote root,
95 bool has_bass,
96 MusicalNote bass,
97 ChordType type,
98 ChordAccent accent,
99 int inversion)
100 : has_bass_ (has_bass), root_note_ (root), bass_note_ (bass),
101 type_ (type), accent_ (accent), inversion_ (inversion)
102 {
103 update_notes ();
104 }
105
106 int get_max_inversion () const
107 {
108 int max_inv = 2;
109 switch (accent_)
110 {
111 case ChordAccent::None:
112 break;
113 case ChordAccent::Seventh:
114 case ChordAccent::MajorSeventh:
115 case ChordAccent::FlatNinth:
116 case ChordAccent::Ninth:
117 case ChordAccent::SharpNinth:
118 case ChordAccent::Eleventh:
119 max_inv = 3;
120 break;
121 case ChordAccent::FlatFifthSharpEleventh:
122 case ChordAccent::SharpFifthFlatThirteenth:
123 case ChordAccent::SixthThirteenth:
124 max_inv = 4;
125 break;
126 default:
127 break;
128 }
129
130 return max_inv;
131 }
132
133 int get_min_inversion () const { return -get_max_inversion (); }
134
141 bool is_key_in_chord (MusicalNote key) const;
142
148 bool is_key_bass (MusicalNote key) const;
149
154
158 static utils::Utf8String chord_accent_to_string (ChordAccent accent);
159
163 static utils::Utf8String note_to_string (MusicalNote note);
164
169
175
176 friend bool
177 operator== (const ChordDescriptor &lhs, const ChordDescriptor &rhs)
178 {
179 return lhs.has_bass_ == rhs.has_bass_ && lhs.root_note_ == rhs.root_note_
180 && lhs.bass_note_ == rhs.bass_note_ && lhs.type_ == rhs.type_
181 && lhs.notes_ == rhs.notes_ && lhs.inversion_ == rhs.inversion_;
182 }
183
184 NLOHMANN_DEFINE_TYPE_INTRUSIVE (
186 has_bass_,
187 root_note_,
188 bass_note_,
189 type_,
190 accent_,
191 notes_,
192 inversion_)
193
194public:
196 bool has_bass_ = false;
197
199 MusicalNote root_note_ = MusicalNote::C;
200
202 MusicalNote bass_note_ = MusicalNote::C;
203
205 ChordType type_ = ChordType::None;
206
212 ChordAccent accent_ = ChordAccent::None;
213
222 std::array<bool, MAX_NOTES> notes_{};
223
229 int inversion_ = 0;
230};
231
232} // 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.
NLOHMANN_DEFINE_TYPE_INTRUSIVE(ChordDescriptor, has_bass_, root_note_, bass_note_, type_, accent_, notes_, inversion_) public MusicalNote root_note_
Has bass note or not.
ChordAccent accent_
Chord accent.
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.
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:38