DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::harmony Namespace Reference

Namespaces

namespace  detail
 

Classes

struct  Chord
 A chord "recipe" defining the required intervals relative to its root. More...
 
struct  DiatonicChord
 Contains the structure and generated symbol name of a diatonic chord. More...
 
struct  Scale
 Descriptor holding the name, pitch mask and chord tags for a musical scale. More...
 

Typedefs

using NoteSet = std::uint16_t
 A 12-bit bitmask representing the 12 pitch-classes of the chromatic scale.
 
using Degree = int
 Integer index used to select a degree inside standard diatonic representations (0-based).
 

Enumerations

enum class  ChordTag : std::uint16_t {
  MajorTriad = 1u << 0 , MinorTriad = 1u << 1 , DiminishedTriad = 1u << 2 , AugmentedTriad = 1u << 3 ,
  Major7 = 1u << 4 , Dominant7 = 1u << 5 , Minor7 = 1u << 6 , HalfDim7 = 1u << 7 ,
  Dim7 = 1u << 8 , Sus2Triad = 1u << 9 , Sus4Triad = 1u << 10 , Major9 = 1u << 11 ,
  Dominant9 = 1u << 12 , Minor9 = 1u << 13 , Major11 = 1u << 14 , Dominant11 = 1u << 15 ,
  All = 0xFFFFu
}
 Bitmask flags describing chord "families" characteristic of a scale. More...
 
enum class  ChordLevel : std::uint8_t {
  TriadsOnly = 0 , Triads7 = 1 , Triads79 = 2 , Triads7911 = 3 ,
  Triads791113 = 4
}
 Complexity level describing which extensions to generate for diatonic chords. More...
 

Functions

constexpr ChordTag operator| (ChordTag lhs, ChordTag rhs) noexcept
 Bitwise OR operator for ChordTag flags.
 
constexpr ChordTag operator& (ChordTag lhs, ChordTag rhs) noexcept
 Bitwise AND operator for ChordTag flags (intersection).
 
constexpr bool hasTags (ChordTag tags, ChordTag wanted) noexcept
 Tests whether tags contains ALL flags in wanted.
 
consteval NoteSet makeMask (bool b0, bool b1, bool b2, bool b3, bool b4, bool b5, bool b6, bool b7, bool b8, bool b9, bool b10, bool b11) noexcept
 Build a NoteSet from 12 boolean flags (b0 = C, b1 = C#/Db, ... b11 = B).
 
consteval NoteSet makeMask (std::initializer_list< int > degrees) noexcept
 Build a NoteSet from a list of semitone degrees.
 
constexpr std::string_view noteName (int midi, int root=0) noexcept
 Returns a human-readable note name for the given MIDI note (0..127).
 
constexpr std::optional< int > parseNote (std::string_view s) noexcept
 Parse a simple note name (no octave) into a pitch-class (0..11).
 
constexpr NoteSet scaleAtRoot (NoteSet base, int root) noexcept
 Circularly rotate a NoteSet so it becomes rooted at a specific key.
 
constexpr std::array< int, 7 > chordAtRootMidi (const Chord &c, int rootMidi, int inversion=0) noexcept
 Build MIDI note numbers for a chord recipe located at a specific root.
 
constexpr std::array< const Scale *, 16 > scalesForChordMask (NoteSet chordMask) noexcept
 Reverse lookup: find up to 16 scales that fully contain chordMask.
 
constexpr std::array< const Scale *, 16 > scalesForChord (const Chord &chord) noexcept
 Reverse lookup wrapper: find scales that can contain a specific Chord recipe.
 
constexpr DiatonicChord diatonicChord (const Scale &sc, Degree degree, ChordLevel level) noexcept
 Generates the diatonic chord built upon a specific degree of a scale.
 
constexpr std::array< int, 7 > diatonicChordToMidi (const DiatonicChord &c, int rootMidi) noexcept
 Convert a DiatonicChord's interval recipe into absolute MIDI notes.
 
constexpr std::optional< int > parseNoteWithOctave (std::string_view note) noexcept
 Parse a note string containing an optional octave (e.g. "C#4", "C-1").
 
constexpr int getOctaveFromNote (std::string_view note) noexcept
 Extract octave number from a note string. Supports negative octaves.
 
constexpr int transposeByOctaves (int midi, int octaveDelta) noexcept
 Transpose a MIDI note by a discrete number of full octaves.
 

Variables

constexpr std::array< Scale, 61 > allScales
 
constexpr std::array< std::string_view, 12 > sharpNames
 
constexpr std::array< std::string_view, 12 > flatNames
 
constexpr std::array< bool, 12 > useSharpsForRoot
 
constexpr std::array< Chord, 15 > allChords
 

Typedef Documentation

◆ Degree

Integer index used to select a degree inside standard diatonic representations (0-based).

Definition at line 51 of file HarmonyConstants.h.

◆ NoteSet

A 12-bit bitmask representing the 12 pitch-classes of the chromatic scale.

Bits 0..11 correspond to semitones above the root (0=C, 1=C#/Db, ..., 11=B). Fits perfectly in CPU registers for extremely fast subset/superset evaluations.

Definition at line 45 of file HarmonyConstants.h.

Enumeration Type Documentation

◆ ChordLevel

enum class dspark::harmony::ChordLevel : std::uint8_t
strong

Complexity level describing which extensions to generate for diatonic chords.

Enumerator
TriadsOnly 

Generate base triads only (R-3-5).

Triads7 

Generate up to 7th chords.

Triads79 

Up to 9ths.

Triads7911 

Up to 11ths.

Triads791113 

Up to 13ths.

Definition at line 493 of file HarmonyConstants.h.

◆ ChordTag

enum class dspark::harmony::ChordTag : std::uint16_t
strong

Bitmask flags describing chord "families" characteristic of a scale.

Used to filter scales by chord type without computing intervals dynamically.

Note
The tags stored in allScales are REPRESENTATIVE of each scale's sonority (typically the root-degree triad and seventh), not an exhaustive list of every chord family constructible inside the scale. For exhaustive containment queries use scalesForChordMask(), which derives the answer from the pitch masks themselves.
Enumerator
MajorTriad 

Contains a major triad (e.g., C-E-G).

MinorTriad 

Contains a minor triad (e.g., C-Eb-G).

DiminishedTriad 

Contains a diminished triad (e.g., C-Eb-Gb).

AugmentedTriad 

Contains an augmented triad (e.g., C-E-G#).

Major7 

Contains a major 7th chord.

Dominant7 

Contains a dominant 7th chord.

Minor7 

Contains a minor 7th chord.

HalfDim7 

Contains a half-diminished 7th chord.

Dim7 

Contains a fully-diminished 7th chord.

Sus2Triad 

Contains a suspended-2 triad.

Sus4Triad 

Contains a suspended-4 triad.

Major9 

Contains a major 9th chord.

Dominant9 

Contains a dominant 9th chord.

Minor9 

Contains a minor 9th chord.

Major11 

Contains a major 11th chord.

Dominant11 

Contains a dominant 11th chord.

All 

Convenience: all flags set.

Definition at line 70 of file HarmonyConstants.h.

Function Documentation

◆ chordAtRootMidi()

constexpr std::array< int, 7 > dspark::harmony::chordAtRootMidi ( const Chord c,
int  rootMidi,
int  inversion = 0 
)
constexprnoexcept

Build MIDI note numbers for a chord recipe located at a specific root.

Parameters
cChord definition to use.
rootMidiMIDI note for chord root (0..127 typical).
inversionWhich chord tone to place in bass (0=root position, 1=first inversion...).
Returns
Array of 7 ints: valid MIDI numbers for present tones, unused slots = -1.

Definition at line 429 of file HarmonyConstants.h.

◆ diatonicChord()

constexpr DiatonicChord dspark::harmony::diatonicChord ( const Scale sc,
Degree  degree,
ChordLevel  level 
)
constexprnoexcept

Generates the diatonic chord built upon a specific degree of a scale.

Parameters
scScale definition (assumes root=C internally).
degreeThe 0-based degree index inside the scale (0..6).
levelWhich chord extensions to include.
Returns
A DiatonicChord object with the dynamically evaluated intervals and symbol.

Definition at line 532 of file HarmonyConstants.h.

◆ diatonicChordToMidi()

constexpr std::array< int, 7 > dspark::harmony::diatonicChordToMidi ( const DiatonicChord c,
int  rootMidi 
)
constexprnoexcept

Convert a DiatonicChord's interval recipe into absolute MIDI notes.

Parameters
cThe DiatonicChord object.
rootMidiThe MIDI note assigned as the root of the generated chord.
Returns
Array of 7 ints with valid MIDI notes, -1 for absent positions.

Definition at line 601 of file HarmonyConstants.h.

◆ getOctaveFromNote()

constexpr int dspark::harmony::getOctaveFromNote ( std::string_view  note)
constexprnoexcept

Extract octave number from a note string. Supports negative octaves.

Parameters
noteString representation of the note (e.g. "C#5", "C-1").
Returns
Valid octave integer. Defaults to 4 if not present.

Definition at line 634 of file HarmonyConstants.h.

◆ hasTags()

constexpr bool dspark::harmony::hasTags ( ChordTag  tags,
ChordTag  wanted 
)
constexprnoexcept

Tests whether tags contains ALL flags in wanted.

Definition at line 114 of file HarmonyConstants.h.

◆ makeMask() [1/2]

consteval NoteSet dspark::harmony::makeMask ( bool  b0,
bool  b1,
bool  b2,
bool  b3,
bool  b4,
bool  b5,
bool  b6,
bool  b7,
bool  b8,
bool  b9,
bool  b10,
bool  b11 
)
noexcept

Build a NoteSet from 12 boolean flags (b0 = C, b1 = C#/Db, ... b11 = B).

Definition at line 142 of file HarmonyConstants.h.

◆ makeMask() [2/2]

consteval NoteSet dspark::harmony::makeMask ( std::initializer_list< int >  degrees)
noexcept

Build a NoteSet from a list of semitone degrees.

Degree values are taken modulo 12. Negative degrees are ignored.

Definition at line 159 of file HarmonyConstants.h.

◆ noteName()

constexpr std::string_view dspark::harmony::noteName ( int  midi,
int  root = 0 
)
constexprnoexcept

Returns a human-readable note name for the given MIDI note (0..127).

Parameters
midiThe MIDI note number.
rootThe pitch-class of the key root (0..11) to decide enharmonic spelling.
Returns
std::string_view pointing to a static note name (e.g., "C#", "Db").

Definition at line 329 of file HarmonyConstants.h.

◆ operator&()

constexpr ChordTag dspark::harmony::operator& ( ChordTag  lhs,
ChordTag  rhs 
)
constexprnoexcept

Bitwise AND operator for ChordTag flags (intersection).

Returns
Flags present in both operands.

Definition at line 105 of file HarmonyConstants.h.

◆ operator|()

constexpr ChordTag dspark::harmony::operator| ( ChordTag  lhs,
ChordTag  rhs 
)
constexprnoexcept

Bitwise OR operator for ChordTag flags.

Returns
Combination of both flags.

Definition at line 95 of file HarmonyConstants.h.

◆ parseNote()

constexpr std::optional< int > dspark::harmony::parseNote ( std::string_view  s)
constexprnoexcept

Parse a simple note name (no octave) into a pitch-class (0..11).

Parameters
sString containing note name (e.g., "C#", "Bb", "Fb").
Returns
Pitch-class 0..11 on success, std::nullopt on failure.

Definition at line 341 of file HarmonyConstants.h.

◆ parseNoteWithOctave()

constexpr std::optional< int > dspark::harmony::parseNoteWithOctave ( std::string_view  note)
constexprnoexcept

Parse a note string containing an optional octave (e.g. "C#4", "C-1").

Parameters
noteString representation of the note.
Returns
Pitch-class (0..11) on success, or std::nullopt if format is invalid.

Definition at line 619 of file HarmonyConstants.h.

◆ scaleAtRoot()

constexpr NoteSet dspark::harmony::scaleAtRoot ( NoteSet  base,
int  root 
)
constexprnoexcept

Circularly rotate a NoteSet so it becomes rooted at a specific key.

Parameters
baseNoteSet defined with root = C.
rootDesired root as semitone offset from C (0..11).
Returns
New bitmask representing the transposed scale.

Definition at line 377 of file HarmonyConstants.h.

◆ scalesForChord()

constexpr std::array< const Scale *, 16 > dspark::harmony::scalesForChord ( const Chord chord)
constexprnoexcept

Reverse lookup wrapper: find scales that can contain a specific Chord recipe.

Parameters
chordThe Chord recipe object.
Returns
Array of pointers to compatible scales.

Definition at line 476 of file HarmonyConstants.h.

◆ scalesForChordMask()

constexpr std::array< const Scale *, 16 > dspark::harmony::scalesForChordMask ( NoteSet  chordMask)
constexprnoexcept

Reverse lookup: find up to 16 scales that fully contain chordMask.

Parameters
chordMaskA NoteSet representing the notes of the chord.
Returns
Array of pointers to compatible scales. Remaining entries are nullptr.

Definition at line 458 of file HarmonyConstants.h.

◆ transposeByOctaves()

constexpr int dspark::harmony::transposeByOctaves ( int  midi,
int  octaveDelta 
)
constexprnoexcept

Transpose a MIDI note by a discrete number of full octaves.

Parameters
midiOriginal MIDI note number.
octaveDeltaNumber of octaves to transpose (can be negative).
Returns
New MIDI note number.

Definition at line 665 of file HarmonyConstants.h.

Variable Documentation

◆ allChords

constexpr std::array<Chord, 15> dspark::harmony::allChords
inlineconstexpr
Initial value:
{{
{"Major", {0, 4, 7, -1, -1, -1, -1}},
{"Minor", {0, 3, 7, -1, -1, -1, -1}},
{"Diminished", {0, 3, 6, -1, -1, -1, -1}},
{"Augmented", {0, 4, 8, -1, -1, -1, -1}},
{"Major7", {0, 4, 7, 11, -1, -1, -1}},
{"Dominant7", {0, 4, 7, 10, -1, -1, -1}},
{"Minor7", {0, 3, 7, 10, -1, -1, -1}},
{"HalfDim7", {0, 3, 6, 10, -1, -1, -1}},
{"Dim7", {0, 3, 6, 9, -1, -1, -1}},
{"Sus2", {0, 2, 7, -1, -1, -1, -1}},
{"Sus4", {0, 5, 7, -1, -1, -1, -1}},
{"Major9", {0, 4, 7, 11, 14, -1, -1}},
{"Dominant9", {0, 4, 7, 10, 14, -1, -1}},
{"Minor9", {0, 3, 7, 10, 14, -1, -1}},
{"Major13", {0, 4, 7, 11, 14, 17, 21}}
}}

Definition at line 399 of file HarmonyConstants.h.

◆ allScales

constexpr std::array<Scale, 61> dspark::harmony::allScales
inlineconstexpr

Definition at line 222 of file HarmonyConstants.h.

◆ flatNames

constexpr std::array<std::string_view, 12> dspark::harmony::flatNames
inlineconstexpr
Initial value:
{
"C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"
}

Definition at line 309 of file HarmonyConstants.h.

◆ sharpNames

constexpr std::array<std::string_view, 12> dspark::harmony::sharpNames
inlineconstexpr
Initial value:
{
"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"
}

Definition at line 306 of file HarmonyConstants.h.

◆ useSharpsForRoot

constexpr std::array<bool,12> dspark::harmony::useSharpsForRoot
inlineconstexpr
Initial value:
{
1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1
}

Definition at line 315 of file HarmonyConstants.h.