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 };
76static constexpr std::array<music_theory::NoteName, music_theory::STANDARD_DIATONIC_STEPS> noteNames = {
77 NoteName::C, NoteName::D, NoteName::E, NoteName::F, NoteName::G, NoteName::A, NoteName::B
116 const int relativeOctave = octave - 4;
127 static_assert(std::is_arithmetic_v<T>,
"sign requires a numeric type");
128 return n < T(0) ? T(-1) : T(1);
139 static_assert(std::is_integral_v<T>,
"signedModulus requires an integer type");
140 return sign(n) * (std::abs(n) % d);
152 static_assert(std::is_integral_v<T>,
"positiveModulus requires an integer type");
182 return halfsteps - expectedHalfsteps;
194 if (std::abs(expectedKeyChange) > 1) {
222 int m_numberOfEdoDivisions;
223 std::vector<int> m_keyMap;
236 const std::optional <std::vector<int>>& keyMap = std::nullopt)
241 throw std::invalid_argument(
"The Transposer class only supports key map arrays of " + std::to_string(
STANDARD_DIATONIC_STEPS) +
" elements");
243 m_keyMap = keyMap.value();
244 }
else if (isMinor) {
261 m_displacement += interval;
268 const int keyStepEnharmonic = calcStepsBetweenScaleDegrees(m_displacement, m_displacement +
sign(direction));
270 m_alteration -=
sign(direction) * keyStepEnharmonic;
297 const int stepsInAlteration = calcStepsInAlteration(interval, chromaticAlteration);
298 const int stepsInInterval = calcStepsInNormalizedInterval(intervalNormalized);
299 const int stepsInDiatonicInterval = calcStepsBetweenScaleDegrees(m_displacement, m_displacement + intervalNormalized);
301 const int effectiveAlteration = stepsInAlteration + stepsInInterval -
sign(interval) * stepsInDiatonicInterval;
304 m_alteration += effectiveAlteration;
315 while (std::abs(m_alteration) > 0) {
316 const int currSign =
sign(m_alteration);
317 const int currAbsDisp = std::abs(m_alteration);
319 if (std::abs(m_alteration) >= currAbsDisp) {
323 if (currSign !=
sign(m_alteration)) {
340 m_alteration += numberOfEdoDivisions;
355 return calcAbsoluteDivision(
displacement,
alteration) == calcAbsoluteDivision(m_displacement, m_alteration);
359 int calcFifthSteps()
const
362 static constexpr double kFifthsMultiplier = 0.5849625007211562;
363 return static_cast<int>(std::floor(m_numberOfEdoDivisions * kFifthsMultiplier) + 0.5);
366 int calcScaleDegree(
int interval)
const
369 int calcStepsBetweenScaleDegrees(
int firstDisplacement,
int secondDisplacement)
const
371 const int firstScaleDegree = calcScaleDegree(firstDisplacement);
372 const int secondScaleDegree = calcScaleDegree(secondDisplacement);
373 int result =
sign(secondDisplacement - firstDisplacement) * (m_keyMap[secondScaleDegree] - m_keyMap[firstScaleDegree]);
375 result += m_numberOfEdoDivisions;
380 int calcStepsInAlteration(
int interval,
int alteration)
const
382 const int fifthSteps = calcFifthSteps();
385 const int result =
sign(interval) * ((plusFifths * fifthSteps) + (minusOctaves * m_numberOfEdoDivisions));
389 int calcStepsInNormalizedInterval(
int intervalNormalized)
const
391 const int fifthSteps = calcFifthSteps();
392 const int index = std::abs(intervalNormalized);
396 return sign(intervalNormalized) * ((plusFifths * fifthSteps) + (minusOctaves * m_numberOfEdoDivisions));
401 const int baseStep = m_keyMap[scaleDegree];
406 const int octaveSteps = octaveCount * m_numberOfEdoDivisions;
407 const int chromaticSteps = calcStepsInAlteration(+1,
alteration);
409 return baseStep + chromaticSteps + octaveSteps;
Provides dependency-free transposition utilities that work with any scale that has 7 diatonic steps a...
Definition music_theory.hpp:218
int displacement() const
Return the current displacement value.
Definition music_theory.hpp:252
void chromaticTranspose(int interval, int chromaticAlteration)
Chromatically transposes by a specified chromatic interval.
Definition music_theory.hpp:294
void stepwiseTranspose(int numberOfEdoDivisions)
Transposes by the given number of EDO divisions and simplifies the spelling.
Definition music_theory.hpp:338
int alteration() const
Return the current chromatic alteration value.
Definition music_theory.hpp:255
void simplifySpelling()
Simplifies the spelling by reducing its alteration while preserving pitch.
Definition music_theory.hpp:313
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:234
void diatonicTranspose(int interval)
Transposes the displacement by the specified interval.
Definition music_theory.hpp:259
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:354
void enharmonicTranspose(int direction)
Transposes enharmonically relative to the current values.
Definition music_theory.hpp:266
A dependency-free, header-only collection of useful functions for music theory.
DiatonicMode
Represents the seven standard diatonic musical modes.
Definition music_theory.hpp:85
@ 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:137
constexpr int STANDARD_NUMBER_OF_STAFFLINES
The standard number of lines on a staff.
Definition music_theory.hpp:44
constexpr int calcAlterationFrom12EdoHalfsteps(int interval, int halfsteps)
Calculates the alteration in chromatic halfsteps for the specified interval/halfsteps combination.
Definition music_theory.hpp:177
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:125
ClefType
Represents the possible types of clef, irrespective of octave transposition.
Definition music_theory.hpp:98
@ 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 calcDisplacement(int pitchClass, int octave)
Calculates the displacement value for a given absolute pitch class and octave.
Definition music_theory.hpp:113
constexpr int STANDARD_12EDO_STEPS
this can be overriden when constructing a Transposer instance.
Definition music_theory.hpp:46
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:189
constexpr bool calcTranspositionIsOctave(int displacement, int alteration)
Determines if the transposition values result in trasposing by one or more octaves.
Definition music_theory.hpp:205
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:166
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:150
constexpr std::array< int, STANDARD_DIATONIC_STEPS > MINOR_KEYMAP
keymap for 12-EDO minor keys
Definition music_theory.hpp:49