24#ifndef MUSIC_THEORY_HPP
25#define MUSIC_THEORY_HPP
48constexpr std::array<int, STANDARD_DIATONIC_STEPS>
MAJOR_KEYMAP = { 0, 2, 4, 5, 7, 9, 11 };
49constexpr std::array<int, STANDARD_DIATONIC_STEPS>
MINOR_KEYMAP = { 0, 2, 3, 5, 7, 8, 10 };
91 constexpr Pitch(
NoteName pitchName,
int pitchOctave,
int pitchAlteration = 0)
101static constexpr std::array<music_theory::NoteName, music_theory::STANDARD_DIATONIC_STEPS> noteNames = {
102 NoteName::C, NoteName::D, NoteName::E, NoteName::F, NoteName::G, NoteName::A, NoteName::B
140 const int relativeOctave = pitch.
octave - 4;
151 static_assert(std::is_arithmetic_v<T>,
"sign requires a numeric type");
152 return n < T(0) ? T(-1) : T(1);
163 static_assert(std::is_integral_v<T>,
"signedModulus requires an integer type");
164 return sign(n) * (std::abs(n) % d);
176 static_assert(std::is_integral_v<T>,
"positiveModulus requires an integer type");
206 return halfsteps - expectedHalfsteps;
218 if (std::abs(expectedKeyChange) > 1) {
237 if (std::abs(expectedKeyChange) > 1) {
264 int m_numberOfEdoDivisions;
265 std::vector<int> m_keyMap;
285 const std::optional <std::vector<int>>& keyMap = std::nullopt)
290 throw std::invalid_argument(
"The Transposer class only supports key map arrays of " + std::to_string(
STANDARD_DIATONIC_STEPS) +
" elements");
292 m_keyMap = keyMap.value();
293 }
else if (isMinor) {
310 m_displacement += interval;
317 const int stepSign =
sign(diatonicSteps);
318 for (
int i = 0; i < std::abs(diatonicSteps); ++i) {
319 const int keyStepEnharmonic = calcStepsBetweenScaleDegrees(m_displacement, m_displacement + stepSign);
321 m_alteration -= stepSign * keyStepEnharmonic;
349 const int stepsInAlteration = calcStepsInAlteration(interval, chromaticAlteration);
350 const int stepsInInterval = calcStepsInNormalizedInterval(intervalNormalized);
351 const int stepsInDiatonicInterval = calcStepsBetweenScaleDegrees(m_displacement, m_displacement + intervalNormalized);
353 const int effectiveAlteration = stepsInAlteration + stepsInInterval -
sign(interval) * stepsInDiatonicInterval;
356 m_alteration += effectiveAlteration;
367 while (std::abs(m_alteration) > 0) {
368 const int currSign =
sign(m_alteration);
369 const int currAbsDisp = std::abs(m_alteration);
371 if (std::abs(m_alteration) >= currAbsDisp) {
375 if (currSign !=
sign(m_alteration)) {
392 m_alteration += numberOfEdoDivisions;
407 return calcAbsoluteDivision(
displacement,
alteration) == calcAbsoluteDivision(m_displacement, m_alteration);
411 int calcFifthSteps()
const
414 static constexpr double kFifthsMultiplier = 0.5849625007211562;
415 return static_cast<int>(std::floor(m_numberOfEdoDivisions * kFifthsMultiplier) + 0.5);
418 int calcScaleDegree(
int interval)
const
421 int calcStepsBetweenScaleDegrees(
int firstDisplacement,
int secondDisplacement)
const
423 const int firstScaleDegree = calcScaleDegree(firstDisplacement);
424 const int secondScaleDegree = calcScaleDegree(secondDisplacement);
425 int result =
sign(secondDisplacement - firstDisplacement) * (m_keyMap[secondScaleDegree] - m_keyMap[firstScaleDegree]);
427 result += m_numberOfEdoDivisions;
432 int calcStepsInAlteration(
int interval,
int alteration)
const
434 const int fifthSteps = calcFifthSteps();
437 const int result =
sign(interval) * ((plusFifths * fifthSteps) + (minusOctaves * m_numberOfEdoDivisions));
441 int calcStepsInNormalizedInterval(
int intervalNormalized)
const
443 const int fifthSteps = calcFifthSteps();
444 const int index = std::abs(intervalNormalized);
448 return sign(intervalNormalized) * ((plusFifths * fifthSteps) + (minusOctaves * m_numberOfEdoDivisions));
453 const int baseStep = m_keyMap[scaleDegree];
458 const int octaveSteps = octaveCount * m_numberOfEdoDivisions;
459 const int chromaticSteps = calcStepsInAlteration(+1,
alteration);
461 return baseStep + chromaticSteps + octaveSteps;
Provides dependency-free transposition utilities that work with any scale that has 7 diatonic steps a...
Definition music_theory.hpp:260
int displacement() const
Return the current displacement value.
Definition music_theory.hpp:301
void chromaticTranspose(int interval, int chromaticAlteration)
Chromatically transposes by a specified chromatic interval.
Definition music_theory.hpp:346
void stepwiseTranspose(int numberOfEdoDivisions)
Transposes by the given number of EDO divisions and simplifies the spelling.
Definition music_theory.hpp:390
int alteration() const
Return the current chromatic alteration value.
Definition music_theory.hpp:304
Transposer(const Pitch &pitch)
Constructs a 12-EDO major-scale transposer for a spelled pitch.
Definition music_theory.hpp:270
void simplifySpelling()
Simplifies the spelling by reducing its alteration while preserving pitch.
Definition music_theory.hpp:365
Transposer(int displacement, int alteration, bool isMinor=false, int numberOfEdoDivisions=STANDARD_12EDO_STEPS, const std::optional< std::vector< int > > &keyMap=std::nullopt)
Constructor function.
Definition music_theory.hpp:283
void diatonicTranspose(int interval)
Transposes the displacement by the specified interval.
Definition music_theory.hpp:308
bool isEnharmonicEquivalent(int displacement, int alteration) const
Determines if the given displacement and alteration refer to the same pitch as the current state.
Definition music_theory.hpp:406
void enharmonicTranspose(int diatonicSteps)
Transposes enharmonically relative to the current values.
Definition music_theory.hpp:315
A dependency-free, header-only collection of useful functions for music theory.
DiatonicMode
Represents the seven standard diatonic musical modes.
Definition music_theory.hpp:110
@ Phrygian
minor with flat 2
@ Locrian
diminished with flat 2 and 5
@ Lydian
major with raised 4
@ Dorian
minor with raised 6
@ Mixolydian
major with flat 7
constexpr T signedModulus(T n, T d)
Calculates the modulus of positive and negative numbers in a predictable manner.
Definition music_theory.hpp:161
constexpr int STANDARD_NUMBER_OF_STAFFLINES
The standard number of lines on a staff.
Definition music_theory.hpp:44
constexpr int calcDisplacement(const Pitch &pitch)
Calculates the displacement value for a spelled pitch.
Definition music_theory.hpp:137
constexpr int calcAlterationFrom12EdoHalfsteps(int interval, int halfsteps)
Calculates the alteration in chromatic halfsteps for the specified interval/halfsteps combination.
Definition music_theory.hpp:201
constexpr std::array< int, STANDARD_DIATONIC_STEPS > MAJOR_KEYMAP
keymap for 12-EDO major keys
Definition music_theory.hpp:48
constexpr T sign(T n)
Calculates the sign of an integer.
Definition music_theory.hpp:149
ClefType
Represents the possible types of clef, irrespective of octave transposition.
Definition music_theory.hpp:123
@ TabSerif
Tablature clef (TAB) with serif font.
@ Percussion2
Narrow rectangle centered on middle staff line (corresponds to SMuFL glyph unpitchedPercussionClef2)
@ Tab
Tablature clef (TAB) with non-serif font.
@ Unknown
Unknown clef type (default value with {} initializer)
@ Percussion1
2 thick vertical lines centered on middle staff line (corresponds to SMuFL glyph unpitchedPercussionC...
constexpr int STANDARD_12EDO_STEPS
this can be overriden when constructing a Transposer instance.
Definition music_theory.hpp:46
constexpr int calcKeySigChangeFromInterval(int interval, int chromaticAlteration)
Calculates the resulting key signature change (sharps/flats) produced by a diatonic interval and chro...
Definition music_theory.hpp:232
constexpr int calcAlterationFromKeySigChange(int interval, int keySigChange)
Determines the chromatic alteration needed for a diatonic interval to produce a desired key signature...
Definition music_theory.hpp:213
constexpr bool calcTranspositionIsOctave(int displacement, int alteration)
Determines if the transposition values result in trasposing by one or more octaves.
Definition music_theory.hpp:247
constexpr int STANDARD_DIATONIC_STEPS
currently this is the only supported number of diatonic steps.
Definition music_theory.hpp:45
NoteName
The available note names in array order.
Definition music_theory.hpp:66
constexpr std::array< std::array< int, 2 >, STANDARD_DIATONIC_STEPS > DIATONIC_INTERVAL_ADJUSTMENTS
Array of chromatic intervals. Each member array contains.
Definition music_theory.hpp:54
constexpr int calc12EdoHalfstepsInInterval(int interval, int chromaticAlteration)
Calculates the number of 12-EDO chromatic halfsteps in the specified interval.
Definition music_theory.hpp:190
constexpr T positiveModulus(T n, T d, T *q=nullptr)
Calculates a positive modulus in the range [0, d-1], even for negative dividends.
Definition music_theory.hpp:174
constexpr std::array< int, STANDARD_DIATONIC_STEPS > MINOR_KEYMAP
keymap for 12-EDO minor keys
Definition music_theory.hpp:49
A spelled pitch, expressed relative to C4.
Definition music_theory.hpp:83
NoteName noteName
The diatonic note name.
Definition music_theory.hpp:96
constexpr Pitch()=default
Creates an unspecified pitch.
int octave
The octave number, where C4 is middle C.
Definition music_theory.hpp:97
constexpr Pitch(NoteName pitchName, int pitchOctave, int pitchAlteration=0)
Creates a spelled pitch.
Definition music_theory.hpp:91
int alteration
The alteration relative to the natural note name, in EDO divisions.
Definition music_theory.hpp:98