MUSX Document Model
Loading...
Searching...
No Matches
music_theory.hpp
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 // Do not use `#pragma once` here, because the file may be included in multiple projects
24#ifndef MUSIC_THEORY_HPP
25#define MUSIC_THEORY_HPP
26
27#include <array>
28#include <vector>
29#include <cmath>
30#include <algorithm>
31#include <optional>
32#include <stdexcept>
33#include <string>
34
35/*
36This header-only library has no dependencies and can be shared into any other library merely
37by including it.
38*/
39
42namespace music_theory {
43
45constexpr int STANDARD_DIATONIC_STEPS = 7;
46constexpr int STANDARD_12EDO_STEPS = 12;
47
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 };
50
54constexpr std::array<std::array<int, 2>, 7> DIATONIC_INTERVAL_ADJUSTMENTS = { {
55 { 0, 0 }, // unison
56 { 2, -1 }, // second
57 { 4, -2 }, // third
58 {-1, 1 }, // fourth
59 { 1, 0 }, // fifth
60 { 3, -1 }, // sixth
61 { 5, -2 } // seventh
62}};
63
65enum class NoteName : int
66{
67 C = 0,
68 D = 1,
69 E = 2,
70 F = 3,
71 G = 4,
72 A = 5,
73 B = 6
74};
75
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
78};
79
84enum class DiatonicMode : int
85{
86 Ionian = 0,
87 Dorian = 1,
88 Phrygian = 2,
89 Lydian = 3,
90 Mixolydian = 4,
91 Aeolian = 5,
92 Locrian = 6
93};
94
97enum class ClefType
98{
99 Unknown,
100 G,
101 C,
102 F,
105 Tab,
106 TabSerif
107};
108
113inline int calcDisplacement(int pitchClass, int octave)
114{
115 pitchClass %= STANDARD_DIATONIC_STEPS;
116 const int relativeOctave = octave - 4;
117
118 return pitchClass + (STANDARD_DIATONIC_STEPS * relativeOctave);
119}
120
124template <typename T>
125constexpr inline T sign(T n)
126{
127 static_assert(std::is_arithmetic_v<T>, "sign requires a numeric type");
128 return n < T(0) ? T(-1) : T(1);
129}
130
136template <typename T>
137constexpr T signedModulus(T n, T d)
138{
139 static_assert(std::is_integral_v<T>, "signedModulus requires an integer type");
140 return sign(n) * (std::abs(n) % d);
141}
142
149template <typename T>
150constexpr T positiveModulus(T n, T d, T* q = nullptr)
151{
152 static_assert(std::is_integral_v<T>, "positiveModulus requires an integer type");
153 if (q) *q = n / d;
154 T result = signedModulus(n, d);
155 if (result < 0) {
156 result += d;
157 if (q) --(*q);
158 }
159 return result;
160}
161
166inline int calc12EdoHalfstepsInInterval(int interval, int chromaticAlteration)
167{
168 int octaves{};
169 int diatonic = positiveModulus(interval, STANDARD_DIATONIC_STEPS, &octaves);
170 return MAJOR_KEYMAP[diatonic] + (octaves * STANDARD_12EDO_STEPS) + chromaticAlteration;
171}
172
177inline int calcAlterationFrom12EdoHalfsteps(int interval, int halfsteps)
178{
179 int octaves{};
180 int diatonic = positiveModulus(interval, STANDARD_DIATONIC_STEPS, &octaves);
181 int expectedHalfsteps = MAJOR_KEYMAP[diatonic] + (octaves * STANDARD_12EDO_STEPS);
182 return halfsteps - expectedHalfsteps;
183}
184
189inline int calcAlterationFromKeySigChange(int interval, int keySigChange)
190{
191 int diatonic = positiveModulus(interval, STANDARD_DIATONIC_STEPS);
192 int expectedKeyChange = DIATONIC_INTERVAL_ADJUSTMENTS[diatonic][0];
193 if (interval < 0) {
194 if (std::abs(expectedKeyChange) > 1) { // imperfect intervals
195 expectedKeyChange -= STANDARD_DIATONIC_STEPS;
196 }
197 }
198 int alteration = (keySigChange - expectedKeyChange) / STANDARD_DIATONIC_STEPS;
199 return alteration;
200}
201
210{
211private:
212 int m_displacement;
213 int m_alteration; // alteration from key signature
214 int m_numberOfEdoDivisions; // number of divisions in the EDO (default 12)
215 std::vector<int> m_keyMap; // step map for the EDO
216
217public:
227 bool isMinor = false, int numberOfEdoDivisions = STANDARD_12EDO_STEPS,
228 const std::optional <std::vector<int>>& keyMap = std::nullopt)
229 : m_displacement(displacement), m_alteration(alteration), m_numberOfEdoDivisions(numberOfEdoDivisions)
230 {
231 if (keyMap) {
232 if (keyMap.value().size() != STANDARD_DIATONIC_STEPS) {
233 throw std::invalid_argument("The Transposer class only supports key map arrays of " + std::to_string(STANDARD_DIATONIC_STEPS) + " elements");
234 }
235 m_keyMap = keyMap.value();
236 } else if (isMinor) {
237 m_keyMap.assign(MINOR_KEYMAP.begin(), MINOR_KEYMAP.end());
238 } else {
239 m_keyMap.assign(MAJOR_KEYMAP.begin(), MAJOR_KEYMAP.end());
240 }
241 }
242
244 int displacement() const { return m_displacement; }
245
247 int alteration() const { return m_alteration; }
248
251 void diatonicTranspose(int interval)
252 {
253 m_displacement += interval;
254 }
255
258 void enharmonicTranspose(int direction)
259 {
260 const int keyStepEnharmonic = calcStepsBetweenScaleDegrees(m_displacement, m_displacement + sign(direction));
261 diatonicTranspose(sign(direction));
262 m_alteration -= sign(direction) * keyStepEnharmonic;
263 }
264
286 void chromaticTranspose(int interval, int chromaticAlteration)
287 {
288 const int intervalNormalized = signedModulus(interval, STANDARD_DIATONIC_STEPS);
289 const int stepsInAlteration = calcStepsInAlteration(interval, chromaticAlteration);
290 const int stepsInInterval = calcStepsInNormalizedInterval(intervalNormalized);
291 const int stepsInDiatonicInterval = calcStepsBetweenScaleDegrees(m_displacement, m_displacement + intervalNormalized);
292
293 const int effectiveAlteration = stepsInAlteration + stepsInInterval - sign(interval) * stepsInDiatonicInterval;
294
295 diatonicTranspose(interval);
296 m_alteration += effectiveAlteration;
297 }
298
306 {
307 while (std::abs(m_alteration) > 0) {
308 const int currSign = sign(m_alteration);
309 const int currAbsDisp = std::abs(m_alteration);
310 enharmonicTranspose(currSign);
311 if (std::abs(m_alteration) >= currAbsDisp) {
312 enharmonicTranspose(-currSign);
313 return;
314 }
315 if (currSign != sign(m_alteration)) {
316 break;
317 }
318 }
319 }
320
330 void stepwiseTranspose(int numberOfEdoDivisions)
331 {
332 m_alteration += numberOfEdoDivisions;
334 }
335
347 return calcAbsoluteDivision(displacement, alteration) == calcAbsoluteDivision(m_displacement, m_alteration);
348 }
349
350private:
351 int calcFifthSteps() const
352 {
353 // std::log(3.0 / 2.0) / std::log(2.0) is 0.5849625007211562.
354 static constexpr double kFifthsMultiplier = 0.5849625007211562;
355 return static_cast<int>(std::floor(m_numberOfEdoDivisions * kFifthsMultiplier) + 0.5);
356 }
357
358 int calcScaleDegree(int interval) const
359 { return positiveModulus(interval, int(m_keyMap.size())); }
360
361 int calcStepsBetweenScaleDegrees(int firstDisplacement, int secondDisplacement) const
362 {
363 const int firstScaleDegree = calcScaleDegree(firstDisplacement);
364 const int secondScaleDegree = calcScaleDegree(secondDisplacement);
365 int result = sign(secondDisplacement - firstDisplacement) * (m_keyMap[secondScaleDegree] - m_keyMap[firstScaleDegree]);
366 if (result < 0) {
367 result += m_numberOfEdoDivisions;
368 }
369 return result;
370 }
371
372 int calcStepsInAlteration(int interval, int alteration) const
373 {
374 const int fifthSteps = calcFifthSteps();
375 const int plusFifths = sign(interval) * alteration * 7; // number of fifths to add for a chromatic halfstep alteration (in any EDO)
376 const int minusOctaves = sign(interval) * alteration * -4; // number of octaves to subtract for a chromatic halfstep alteration (in any EDO)
377 const int result = sign(interval) * ((plusFifths * fifthSteps) + (minusOctaves * m_numberOfEdoDivisions));
378 return result;
379 }
380
381 int calcStepsInNormalizedInterval(int intervalNormalized) const
382 {
383 const int fifthSteps = calcFifthSteps();
384 const int index = std::abs(intervalNormalized);
385 const int plusFifths = DIATONIC_INTERVAL_ADJUSTMENTS[index][0]; // number of fifths
386 const int minusOctaves = DIATONIC_INTERVAL_ADJUSTMENTS[index][1]; // number of octaves
387
388 return sign(intervalNormalized) * ((plusFifths * fifthSteps) + (minusOctaves * m_numberOfEdoDivisions));
389 }
390
391 int calcAbsoluteDivision(int displacement, int alteration) const {
392 const int scaleDegree = calcScaleDegree(displacement); // 0..6
393 const int baseStep = m_keyMap[scaleDegree];
394
395 const int octaveCount = (displacement < 0 && displacement % STANDARD_DIATONIC_STEPS != 0)
398 const int octaveSteps = octaveCount * m_numberOfEdoDivisions;
399 const int chromaticSteps = calcStepsInAlteration(/*interval=*/+1, alteration);
400
401 return baseStep + chromaticSteps + octaveSteps;
402 }
403};
404
405} // namespace music_theory
406
407#endif // MUSIC_THEORY_HPP
Provides dependency-free transposition utilities that work with any scale that has 7 diatonic steps a...
Definition music_theory.hpp:210
int displacement() const
Return the current displacement value.
Definition music_theory.hpp:244
void chromaticTranspose(int interval, int chromaticAlteration)
Chromatically transposes by a specified chromatic interval.
Definition music_theory.hpp:286
void stepwiseTranspose(int numberOfEdoDivisions)
Transposes by the given number of EDO divisions and simplifies the spelling.
Definition music_theory.hpp:330
int alteration() const
Return the current chromatic alteration value.
Definition music_theory.hpp:247
void simplifySpelling()
Simplifies the spelling by reducing its alteration while preserving pitch.
Definition music_theory.hpp:305
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:226
void diatonicTranspose(int interval)
Transposes the displacement by the specified interval.
Definition music_theory.hpp:251
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:346
void enharmonicTranspose(int direction)
Transposes enharmonically relative to the current values.
Definition music_theory.hpp:258
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
int calc12EdoHalfstepsInInterval(int interval, int chromaticAlteration)
Calculates the number of 12-EDO chromatic halfsteps in the specified interval.
Definition music_theory.hpp:166
constexpr int STANDARD_NUMBER_OF_STAFFLINES
The standard number of lines on a staff.
Definition music_theory.hpp:44
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
int calcDisplacement(int pitchClass, int octave)
Calculates the displacement value for a given absolute pitch class and octave.
Definition music_theory.hpp:113
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...
int calcAlterationFrom12EdoHalfsteps(int interval, int halfsteps)
Calculates the alteration in chromatic halfsteps for the specified interval/halfsteps combination.
Definition music_theory.hpp:177
constexpr int STANDARD_12EDO_STEPS
this can be overriden when constructing a Transposer instance.
Definition music_theory.hpp:46
constexpr std::array< std::array< int, 2 >, 7 > DIATONIC_INTERVAL_ADJUSTMENTS
Array of chromatic intervals. Each member array contains.
Definition music_theory.hpp:54
constexpr int STANDARD_DIATONIC_STEPS
currently this is the only supported number of diatonic steps.
Definition music_theory.hpp:45
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
NoteName
The available note names in array order.
Definition music_theory.hpp:66
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