MUSX Document Model
Loading...
Searching...
No Matches
music_theory Namespace Reference

A dependency-free, header-only collection of useful functions for music theory. More...

Classes

class  Transposer
 Provides dependency-free transposition utilities that work with any scale that has 7 diatonic steps and an equal number of divisions of the octave (EDO). The most common Western scales use 12 divisions of the octave, .i.e., 12-EDO. More...
 

Enumerations

enum class  NoteName : int {
  C = 0 , D = 1 , E = 2 , F = 3 ,
  G = 4 , A = 5 , B = 6
}
 The available note names in array order.
 
enum class  DiatonicMode : int {
  Ionian = 0 , Dorian = 1 , Phrygian = 2 , Lydian = 3 ,
  Mixolydian = 4 , Aeolian = 5 , Locrian = 6
}
 Represents the seven standard diatonic musical modes. More...
 
enum class  ClefType {
  Unknown , G , C , F ,
  Percussion1 , Percussion2 , Tab , TabSerif
}
 Represents the possible types of clef, irrespective of octave transposition. More...
 

Functions

int calcDisplacement (int pitchClass, int octave)
 Calculates the displacement value for a given absolute pitch class and octave.
 
template<typename T >
constexpr T sign (T n)
 Calculates the sign of an integer.
 
template<typename T >
constexpr T signedModulus (T n, T d)
 Calculates the modulus of positive and negative numbers in a predictable manner.
 
template<typename T >
constexpr T positiveModulus (T n, T d, T *q=nullptr)
 Calculates a positive modulus in the range [0, d-1], even for negative dividends.
 
int calc12EdoHalfstepsInInterval (int interval, int chromaticAlteration)
 Calculates the number of 12-EDO chromatic halfsteps in the specified interval.
 
int calcAlterationFrom12EdoHalfsteps (int interval, int halfsteps)
 Calculates the alteration in chromatic halfsteps for the specified interval/halfsteps combination.
 
int calcAlterationFromKeySigChange (int interval, int keySigChange)
 Determines the chromatic alteration needed for a diatonic interval to produce a desired key signature change.
 

Variables

constexpr int STANDARD_NUMBER_OF_STAFFLINES = 5
 The standard number of lines on a staff.
 
constexpr int STANDARD_DIATONIC_STEPS = 7
 currently this is the only supported number of diatonic steps.
 
constexpr int STANDARD_12EDO_STEPS = 12
 this can be overriden when constructing a Transposer instance.
 
constexpr std::array< int, STANDARD_DIATONIC_STEPSMAJOR_KEYMAP = { 0, 2, 4, 5, 7, 9, 11 }
 keymap for 12-EDO major keys
 
constexpr std::array< int, STANDARD_DIATONIC_STEPSMINOR_KEYMAP = { 0, 2, 3, 5, 7, 8, 10 }
 keymap for 12-EDO minor keys
 
constexpr std::array< std::array< int, 2 >, 7 > DIATONIC_INTERVAL_ADJUSTMENTS
 Array of diatonic intervals. Each member array contains.
 

Detailed Description

A dependency-free, header-only collection of useful functions for music theory.

Enumeration Type Documentation

◆ ClefType

enum class music_theory::ClefType
strong

Represents the possible types of clef, irrespective of octave transposition.

Enumerator
Unknown 

Unknown clef type (default value with {} initializer)

Treble clef.

C clef.

Bass clef.

Percussion1 

2 thick vertical lines centered on middle staff line (corresponds to SMuFL glyph unpitchedPercussionClef1)

Percussion2 

Narrow rectangle centered on middle staff line (corresponds to SMuFL glyph unpitchedPercussionClef2)

Tab 

Tablature clef (TAB) with non-serif font.

TabSerif 

Tablature clef (TAB) with serif font.

◆ DiatonicMode

enum class music_theory::DiatonicMode : int
strong

Represents the seven standard diatonic musical modes.

Values correspond to their roots with no sharps or flats C (0) through B (6).

Enumerator
Ionian 

major

Dorian 

minor with raised 6

Phrygian 

minor with flat 2

Lydian 

major with raised 4

Mixolydian 

major with flat 7

Aeolian 

natural minor

Locrian 

diminished with flat 2 and 5

Function Documentation

◆ calc12EdoHalfstepsInInterval()

int music_theory::calc12EdoHalfstepsInInterval ( int  interval,
int  chromaticAlteration 
)
inline

Calculates the number of 12-EDO chromatic halfsteps in the specified interval.

Parameters
intervalThe diatonic displacement (negative for downward intervals).
chromaticAlterationThe chromatic halfstep alteration that defines the chromatic interval.
Returns
The number of 12-EDO divisions (chromatic halfsteps) in the interval (negative means down)

◆ calcAlterationFrom12EdoHalfsteps()

int music_theory::calcAlterationFrom12EdoHalfsteps ( int  interval,
int  halfsteps 
)
inline

Calculates the alteration in chromatic halfsteps for the specified interval/halfsteps combination.

Parameters
intervalThe diatonic displacement (negative for downward transposition).
halfstepsThe number of 12-EDO chromatic halfsteps in the interval (negative means down).
Returns
The number of 12-EDO divisions (chromatic halfsteps) in the interval

◆ calcAlterationFromKeySigChange()

int music_theory::calcAlterationFromKeySigChange ( int  interval,
int  keySigChange 
)
inline

Determines the chromatic alteration needed for a diatonic interval to produce a desired key signature change.

Parameters
intervalThe diatonic interval (e.g. +3 for a perfect fourth up).
keySigChangeThe desired change in key signature (positive for sharps added, negative for flats).
Returns
The chromatic alteration in halfsteps required to produce that key signature change with the given diatonic interval.

◆ calcDisplacement()

int music_theory::calcDisplacement ( int  pitchClass,
int  octave 
)
inline

Calculates the displacement value for a given absolute pitch class and octave.

Parameters
pitchClass0..6 corresponding to C..B
octaveOctave 4 is the middle-C octave
Returns
A displacement value that can be used to create a Transposer instance.

◆ positiveModulus()

template<typename T >
constexpr T music_theory::positiveModulus ( n,
d,
T *  q = nullptr 
)
constexpr

Calculates a positive modulus in the range [0, d-1], even for negative dividends.

Template Parameters
TAn integer type.
Parameters
nThe dividend (may be negative).
dThe modulus base (must be positive).
qOptional pointer to receive the quotient such that n = d * q + result.
Returns
The result of n modulo d in the range [0, d-1].

◆ sign()

template<typename T >
constexpr T music_theory::sign ( n)
inlineconstexpr

Calculates the sign of an integer.

Parameters
nThe integer to process.
Returns
-1 for negative; 1 for non-negative.

◆ signedModulus()

template<typename T >
constexpr T music_theory::signedModulus ( n,
d 
)
constexpr

Calculates the modulus of positive and negative numbers in a predictable manner.

Template Parameters
Tan integer type
Parameters
nThe number (positive or negative) for which to calculate the modulus
dThe base of the modulus. This must be a positive number.
Returns
For non-negative, the result is the same as %. For negative numbers, the result is -1 * (abs(n) % d).

Variable Documentation

◆ DIATONIC_INTERVAL_ADJUSTMENTS

constexpr std::array<std::array<int, 2>, 7> music_theory::DIATONIC_INTERVAL_ADJUSTMENTS
constexpr
Initial value:
= { {
{ 0, 0 },
{ 2, -1 },
{ 4, -2 },
{-1, 1 },
{ 1, 0 },
{ 3, -1 },
{ 5, -2 }
}}

Array of diatonic intervals. Each member array contains.

  • the number of fifths to add, which is also the key signature adjustment for the interval
  • the number of octaves to subtract