MCC
Loading...
Searching...
No Matches
Scale.h
1#ifndef MCC_SCALE_SCALE_H
2#define MCC_SCALE_SCALE_H
3
4#include <MCC_BuildSettings.h>
5
6#include <stdint.h>
7
8#include <MCC/Interval/Transposition.h>
9#include <MCC/Pitch/NoteName.h>
10#include <MCC/Pitch/Pitch.h>
11#include <MCC/Pitch/PitchClass.h>
12#include <MCC/Scale/ScalePattern.h>
13
14namespace MCC {
15
28class Scale {
29 NoteName _root;
30 ScalePattern _pattern;
31
32public:
34 constexpr Scale() noexcept : _root(), _pattern() {}
35
39 constexpr Scale(NoteName root, ScalePattern pattern) noexcept
40 : _root(root.IsValid() && pattern.IsValid() ? root : NoteName::Invalid()),
41 _pattern(root.IsValid() && pattern.IsValid() ? pattern : ScalePattern::Invalid()) {}
42
44 static constexpr Scale Invalid() noexcept { return Scale(); }
45
47 constexpr bool IsValid() const noexcept { return _pattern.IsValid(); }
48
50 constexpr NoteName Root() const noexcept { return _root; }
51
53 constexpr ScalePattern Pattern() const noexcept { return _pattern; }
54
56 MCC_CONSTEXPR14 uint8_t DegreeCount() const noexcept { return _pattern.DegreeCount(); }
57
62 MCC_CONSTEXPR14 NoteName NoteAt(int32_t degree) const noexcept {
63 return _root + _pattern.DegreeInterval(degree);
64 }
65
70 MCC_CONSTEXPR14 MCC::Pitch PitchAt(int32_t degree, int32_t rootOctave) const noexcept {
71 return MCC::Pitch(_root, rootOctave) + _pattern.DegreeInterval(degree);
72 }
73
78 MCC_CONSTEXPR14 uint8_t DegreeOf(NoteName noteName) const noexcept {
79 if (!noteName.IsValid()) {
80 return 0;
81 }
82 const uint8_t count = DegreeCount();
83 for (uint8_t degree = 1; degree <= count; ++degree) {
84 if (NoteAt(degree) == noteName) {
85 return degree;
86 }
87 }
88 return 0;
89 }
90
92 MCC_CONSTEXPR14 bool Contains(NoteName noteName) const noexcept {
93 return DegreeOf(noteName) != 0;
94 }
95
100 constexpr bool ContainsPitchClass(MCC::PitchClass pitchClass) const noexcept {
101 return IsValid() && pitchClass.IsValid() &&
102 _pattern.ContainsSemitone(
103 static_cast<int32_t>(pitchClass.Value()) - _root.PitchClass().Value());
104 }
105
107 friend constexpr bool operator==(const Scale& a, const Scale& b) noexcept {
108 return a._root == b._root && a._pattern == b._pattern;
109 }
110
112 friend constexpr bool operator!=(const Scale& a, const Scale& b) noexcept {
113 return !(a == b);
114 }
115};
116
117} // namespace MCC
118
119#endif // MCC_SCALE_SCALE_H
Written note name: a letter plus an accidental, without octave.
Definition NoteName.h:29
static constexpr NoteName Invalid() noexcept
Returns the invalid note name.
Definition NoteName.h:59
Pitch class 0-11 with spelling discarded (C = 0).
Definition PitchClass.h:21
Written absolute pitch: a note name plus an octave.
Definition Pitch.h:35
Root-independent scale structure: up to twelve ascending degrees.
Definition ScalePattern.h:32
static constexpr ScalePattern Invalid() noexcept
Returns the invalid pattern.
Definition ScalePattern.h:78
constexpr Scale() noexcept
Creates the invalid scale (SPEC-ERR-2).
Definition Scale.h:34
friend constexpr bool operator==(const Scale &a, const Scale &b) noexcept
Compares roots by spelling and patterns by degrees.
Definition Scale.h:107
constexpr bool ContainsPitchClass(MCC::PitchClass pitchClass) const noexcept
Returns true when any degree sounds as pitchClass, regardless of spelling (explicit enharmonic member...
Definition Scale.h:100
MCC_CONSTEXPR14 uint8_t DegreeOf(NoteName noteName) const noexcept
Returns the 1-based degree written exactly as noteName, or 0 when no degree has that spelling.
Definition Scale.h:78
constexpr Scale(NoteName root, ScalePattern pattern) noexcept
Spells pattern from root; invalid when either is invalid.
Definition Scale.h:39
MCC_CONSTEXPR14 NoteName NoteAt(int32_t degree) const noexcept
Returns the note name of degree (1-based), or the invalid note name outside the scale or when it cann...
Definition Scale.h:62
MCC_CONSTEXPR14 bool Contains(NoteName noteName) const noexcept
Returns true when a degree is written exactly as noteName.
Definition Scale.h:92
constexpr NoteName Root() const noexcept
Returns the root note name.
Definition Scale.h:50
MCC_CONSTEXPR14 uint8_t DegreeCount() const noexcept
Returns the number of degrees, or 0 for the invalid scale.
Definition Scale.h:56
constexpr bool IsValid() const noexcept
Returns true unless this is the invalid scale.
Definition Scale.h:47
constexpr ScalePattern Pattern() const noexcept
Returns the root-independent pattern.
Definition Scale.h:53
friend constexpr bool operator!=(const Scale &a, const Scale &b) noexcept
Negation of operator==.
Definition Scale.h:112
MCC_CONSTEXPR14 MCC::Pitch PitchAt(int32_t degree, int32_t rootOctave) const noexcept
Returns the pitch of degree when the root is placed in rootOctave; degrees above the root may fall in...
Definition Scale.h:70
static constexpr Scale Invalid() noexcept
Returns the invalid scale.
Definition Scale.h:44
Music Composition Core public namespace.
Definition Version.h:4