MCC
Loading...
Searching...
No Matches
KeySignature.h
1#ifndef MCC_KEY_KEY_SIGNATURE_H
2#define MCC_KEY_KEY_SIGNATURE_H
3
4#include <MCC_BuildSettings.h>
5
6#include <stdint.h>
7
8#include <MCC/Pitch/Accidental.h>
9#include <MCC/Pitch/Letter.h>
10
11namespace MCC {
12
22 static constexpr int8_t InvalidValue = -128;
23
24 int8_t _fifths;
25
26 // Position of a letter in the order of sharps: F C G D A E B.
27 static constexpr int32_t SharpOrder(Letter letter) noexcept {
28 return (DiatonicIndex(letter) * 2 + 1) % 7;
29 }
30
31public:
33 constexpr KeySignature() noexcept : _fifths(InvalidValue) {}
34
36 constexpr explicit KeySignature(int32_t fifths) noexcept
37 : _fifths(fifths >= -7 && fifths <= 7 ? static_cast<int8_t>(fifths) : InvalidValue) {}
38
40 static constexpr KeySignature Invalid() noexcept { return KeySignature(); }
41
43 constexpr bool IsValid() const noexcept { return _fifths != InvalidValue; }
44
46 constexpr int8_t Fifths() const noexcept { return IsValid() ? _fifths : static_cast<int8_t>(0); }
47
49 constexpr uint8_t Sharps() const noexcept {
50 return static_cast<uint8_t>(Fifths() > 0 ? Fifths() : 0);
51 }
52
54 constexpr uint8_t Flats() const noexcept {
55 return static_cast<uint8_t>(Fifths() < 0 ? -Fifths() : 0);
56 }
57
62 MCC_CONSTEXPR14 MCC::Accidental AccidentalOf(Letter letter) const noexcept {
63 if (!IsValid()) {
65 }
66 const int32_t order = SharpOrder(letter);
67 if (order < Sharps()) {
69 }
70 if (6 - order < Flats()) {
71 return MCC::Accidental::Flat();
72 }
74 }
75
77 friend constexpr bool operator==(KeySignature a, KeySignature b) noexcept {
78 return a._fifths == b._fifths;
79 }
80
82 friend constexpr bool operator!=(KeySignature a, KeySignature b) noexcept {
83 return !(a == b);
84 }
85};
86
87} // namespace MCC
88
89#endif // MCC_KEY_KEY_SIGNATURE_H
Chromatic alteration of a letter, from -4 (quadruple flat) to +4 (quadruple sharp).
Definition Accidental.h:20
static constexpr Accidental Natural() noexcept
Returns the natural (0).
Definition Accidental.h:66
static constexpr Accidental Invalid() noexcept
Returns the invalid accidental.
Definition Accidental.h:51
static constexpr Accidental Sharp() noexcept
Returns the sharp (+1).
Definition Accidental.h:69
static constexpr Accidental Flat() noexcept
Returns the flat (-1).
Definition Accidental.h:63
constexpr bool IsValid() const noexcept
Returns true unless this is the invalid signature.
Definition KeySignature.h:43
constexpr KeySignature(int32_t fifths) noexcept
Creates a signature of fifths sharps (positive) or flats (negative).
Definition KeySignature.h:36
MCC_CONSTEXPR14 MCC::Accidental AccidentalOf(Letter letter) const noexcept
Returns the accidental the signature applies to letter: sharp, flat or natural; invalid for the inval...
Definition KeySignature.h:62
static constexpr KeySignature Invalid() noexcept
Returns the invalid signature.
Definition KeySignature.h:40
friend constexpr bool operator==(KeySignature a, KeySignature b) noexcept
Compares signatures.
Definition KeySignature.h:77
constexpr int8_t Fifths() const noexcept
Returns sharps as positive and flats as negative; 0 when invalid.
Definition KeySignature.h:46
constexpr uint8_t Sharps() const noexcept
Returns the number of sharps.
Definition KeySignature.h:49
constexpr uint8_t Flats() const noexcept
Returns the number of flats.
Definition KeySignature.h:54
constexpr KeySignature() noexcept
Creates the invalid signature (SPEC-ERR-2).
Definition KeySignature.h:33
friend constexpr bool operator!=(KeySignature a, KeySignature b) noexcept
Negation of operator==.
Definition KeySignature.h:82
constexpr uint8_t DiatonicIndex(Letter letter) noexcept
Returns the diatonic index of letter: C = 0 ... B = 6.
Definition Letter.h:49
Letter
Diatonic letter name.
Definition Letter.h:22
Music Composition Core public namespace.
Definition Version.h:4