MNX Document Model
Loading...
Searching...
No Matches
MusicTheoryConversions.h
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#pragma once
23
24#include <stdexcept>
25
26#include "Sequence.h"
27#include "music_theory/music_theory.hpp"
28
29namespace mnx::util {
30
34inline music_theory::NoteName toMusicTheoryNoteName(NoteStep step)
35{
36 switch (step) {
37 case NoteStep::C: return music_theory::NoteName::C;
38 case NoteStep::D: return music_theory::NoteName::D;
39 case NoteStep::E: return music_theory::NoteName::E;
40 case NoteStep::F: return music_theory::NoteName::F;
41 case NoteStep::G: return music_theory::NoteName::G;
42 case NoteStep::A: return music_theory::NoteName::A;
43 case NoteStep::B: return music_theory::NoteName::B;
44 }
45 throw std::invalid_argument("Unknown NoteStep value.");
46}
47
51inline NoteStep toMnxNoteStep(music_theory::NoteName noteName)
52{
53 switch (noteName) {
54 case music_theory::NoteName::C: return NoteStep::C;
55 case music_theory::NoteName::D: return NoteStep::D;
56 case music_theory::NoteName::E: return NoteStep::E;
57 case music_theory::NoteName::F: return NoteStep::F;
58 case music_theory::NoteName::G: return NoteStep::G;
59 case music_theory::NoteName::A: return NoteStep::A;
60 case music_theory::NoteName::B: return NoteStep::B;
61 }
62 throw std::invalid_argument("Unknown music_theory::NoteName value.");
63}
64
68inline music_theory::Pitch toMusicTheoryPitch(const sequence::Pitch::Required& pitch)
69{
70 return {
71 toMusicTheoryNoteName(pitch.step),
72 pitch.octave,
73 pitch.alter
74 };
75}
76
80inline music_theory::Pitch toMusicTheoryPitch(const sequence::Pitch& pitch)
81{
82 return toMusicTheoryPitch(static_cast<sequence::Pitch::Required>(pitch));
83}
84
85} // namespace mnx::util
NoteStep
The diatonic note step values.
Definition Enumerations.h:292
NoteName
The available note names in array order.
Definition music_theory.hpp:66
A spelled pitch, expressed relative to C4.
Definition music_theory.hpp:83