MCC
Loading...
Searching...
No Matches
Tuning.h
1#ifndef MCC_TUNING_TUNING_H
2#define MCC_TUNING_TUNING_H
3
4#include <float.h>
5
6#include <MCC/Pitch/Pitch.h>
7
8namespace MCC {
9
18class Tuning {
19 MCC::Pitch _reference;
20 float _frequency;
21
22public:
24 constexpr Tuning() noexcept : _reference(), _frequency(0.0f) {}
25
31 constexpr Tuning(MCC::Pitch reference, float frequency) noexcept
32 : _reference(IsUsable(reference, frequency)
33 ? reference
35 _frequency(IsUsable(reference, frequency) ? frequency : 0.0f) {}
36
38 static constexpr Tuning Standard() noexcept {
39 return Tuning(MCC::Pitch(MCC::Letter::A, 4), 440.0f);
40 }
41
43 static constexpr Tuning Invalid() noexcept { return Tuning(); }
44
46 constexpr bool IsValid() const noexcept { return _reference.IsValid(); }
47
49 constexpr MCC::Pitch Reference() const noexcept { return _reference; }
50
55 constexpr float ReferenceFrequency() const noexcept { return _frequency; }
56
61 friend constexpr bool operator==(Tuning a, Tuning b) noexcept {
62 return a._reference == b._reference && a._frequency == b._frequency;
63 }
64
65 friend constexpr bool operator!=(Tuning a, Tuning b) noexcept {
66 return !(a == b);
67 }
68
69private:
70 static constexpr bool IsUsable(MCC::Pitch reference, float frequency) noexcept {
71 // NaN fails both comparisons; infinity fails the upper bound.
72 return reference.IsValid() && frequency > 0.0f && frequency <= FLT_MAX;
73 }
74};
75
76} // namespace MCC
77
78#endif // MCC_TUNING_TUNING_H
Written absolute pitch: a note name plus an octave.
Definition Pitch.h:35
static constexpr Pitch Invalid() noexcept
Returns the invalid pitch.
Definition Pitch.h:80
Tuning reference: a written pitch and its frequency in hertz.
Definition Tuning.h:18
static constexpr Tuning Invalid() noexcept
Returns the invalid tuning.
Definition Tuning.h:43
constexpr bool IsValid() const noexcept
Returns true unless this is the invalid tuning.
Definition Tuning.h:46
friend constexpr bool operator==(Tuning a, Tuning b) noexcept
Equality of reference pitch and frequency. All invalid tunings are equal (SPEC-ERR-6).
Definition Tuning.h:61
constexpr Tuning() noexcept
Creates the invalid tuning (SPEC-ERR-2).
Definition Tuning.h:24
constexpr Tuning(MCC::Pitch reference, float frequency) noexcept
Creates a tuning where reference sounds at frequency Hz. An invalid pitch, or a frequency that is zer...
Definition Tuning.h:31
constexpr MCC::Pitch Reference() const noexcept
Returns the reference pitch; invalid for the invalid tuning.
Definition Tuning.h:49
constexpr float ReferenceFrequency() const noexcept
Returns the reference frequency in hertz, or 0 for the invalid tuning (SPEC-ERR-7).
Definition Tuning.h:55
static constexpr Tuning Standard() noexcept
Returns the standard tuning, A4 = 440 Hz (SPEC-OCT-2).
Definition Tuning.h:38
Music Composition Core public namespace.
Definition Version.h:4