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 <array>
7#include <cstdint>
8
9#include <QObject>
10#include <QtQmlIntegration/qqmlintegration.h>
11
12#include <nlohmann/json_fwd.hpp>
13
15{
16class Utf8String;
17}
18
19namespace zrythm::dsp::chords
20{
21Q_NAMESPACE
22QML_ELEMENT
23
24enum class MusicalNote : std::uint8_t
25{
26 C = 0,
27 CSharp,
28 D,
29 DSharp,
30 E,
31 F,
32 FSharp,
33 G,
34 GSharp,
35 A,
36 ASharp,
37 B
38};
39Q_ENUM_NS (MusicalNote)
40
41
44enum class ChordType : std::uint8_t
45{
46 None,
47 Major,
48 Minor,
49 Diminished,
50 SuspendedFourth,
51 SuspendedSecond,
52 Augmented,
53 Custom,
54};
55Q_ENUM_NS (ChordType)
56
57
60enum class ChordAccent : std::uint8_t
61{
62 None,
65 Seventh,
67 MajorSeventh,
68 /* NOTE: all accents below assume Seventh */
70 FlatNinth,
72 Ninth,
74 SharpNinth,
76 Eleventh,
78 FlatFifthSharpEleventh,
80 SharpFifthFlatThirteenth,
82 SixthThirteenth,
83};
84Q_ENUM_NS (ChordAccent)
85
86} // namespace zrythm::dsp::chords
87
88namespace zrythm::dsp
89{
90
91using MusicalNote = chords::MusicalNote;
92using ChordType = chords::ChordType;
93using ChordAccent = chords::ChordAccent;
94
101class ChordDescriptor
102{
103public:
104 static constexpr size_t MAX_NOTES = 48;
105
106public:
107 ChordDescriptor () = default;
108
113 MusicalNote root,
114 bool has_bass,
115 MusicalNote bass,
116 ChordType type,
117 ChordAccent accent,
118 int inversion)
119 : has_bass_ (has_bass), root_note_ (root), bass_note_ (bass),
120 type_ (type), accent_ (accent), inversion_ (inversion)
121 {
122 update_notes ();
123 }
124
125 int get_max_inversion () const
126 {
127 int max_inv = 2;
128 switch (accent_)
129 {
130 case ChordAccent::None:
131 break;
132 case ChordAccent::Seventh:
133 case ChordAccent::MajorSeventh:
134 case ChordAccent::FlatNinth:
135 case ChordAccent::Ninth:
136 case ChordAccent::SharpNinth:
137 case ChordAccent::Eleventh:
138 max_inv = 3;
139 break;
140 case ChordAccent::FlatFifthSharpEleventh:
141 case ChordAccent::SharpFifthFlatThirteenth:
142 case ChordAccent::SixthThirteenth:
143 max_inv = 4;
144 break;
145 default:
146 break;
147 }
148
149 return max_inv;
150 }
151
152 int get_min_inversion () const { return -get_max_inversion (); }
153
160 bool is_key_in_chord (MusicalNote key) const;
161
167 bool is_key_bass (MusicalNote key) const;
168
173
177 static utils::Utf8String chord_accent_to_string (ChordAccent accent);
178
182 static utils::Utf8String note_to_string (MusicalNote note);
183
188
194
195 friend bool
196 operator== (const ChordDescriptor &lhs, const ChordDescriptor &rhs)
197 {
198 return lhs.has_bass_ == rhs.has_bass_ && lhs.root_note_ == rhs.root_note_
199 && lhs.bass_note_ == rhs.bass_note_ && lhs.type_ == rhs.type_
200 && lhs.notes_ == rhs.notes_ && lhs.inversion_ == rhs.inversion_;
201 }
202
203private:
204 friend void to_json (nlohmann::json &j, const ChordDescriptor &c);
205 friend void from_json (const nlohmann::json &j, ChordDescriptor &c);
206
207public:
209 bool has_bass_ = false;
210
212 MusicalNote root_note_ = MusicalNote::C;
213
215 MusicalNote bass_note_ = MusicalNote::C;
216
218 ChordType type_ = ChordType::None;
219
225 ChordAccent accent_ = ChordAccent::None;
226
235 std::array<bool, MAX_NOTES> notes_{};
236
242 int inversion_ = 0;
243};
244
245} // 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.