Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #pragma once
24 :
25 : #include "types/string.h"
26 : #include "../types/types.h"
27 :
28 : namespace mu::engraving {
29 : class MidiNote;
30 : class Note;
31 : enum class Key : signed char;
32 :
33 : static constexpr int INVALID_PITCH = -1;
34 : static constexpr int MIN_PITCH = 0;
35 : static constexpr int MAX_PITCH = 127;
36 :
37 : // a list of tpc's, with legal ranges, not really an enum, so no way to convert into a class
38 : enum Tpc : signed char {
39 : TPC_INVALID = -9,
40 : TPC_F_BBB, TPC_C_BBB, TPC_G_BBB, TPC_D_BBB, TPC_A_BBB, TPC_E_BBB, TPC_B_BBB,
41 : TPC_F_BB, TPC_C_BB, TPC_G_BB, TPC_D_BB, TPC_A_BB, TPC_E_BB, TPC_B_BB,
42 : TPC_F_B, TPC_C_B, TPC_G_B, TPC_D_B, TPC_A_B, TPC_E_B, TPC_B_B,
43 : TPC_F, TPC_C, TPC_G, TPC_D, TPC_A, TPC_E, TPC_B,
44 : TPC_F_S, TPC_C_S, TPC_G_S, TPC_D_S, TPC_A_S, TPC_E_S, TPC_B_S,
45 : TPC_F_SS, TPC_C_SS, TPC_G_SS, TPC_D_SS, TPC_A_SS, TPC_E_SS, TPC_B_SS,
46 : TPC_F_SSS, TPC_C_SSS, TPC_G_SSS, TPC_D_SSS, TPC_A_SSS, TPC_E_SSS, TPC_B_SSS,
47 : TPC_MIN = TPC_F_BBB,
48 : TPC_MAX = TPC_B_SSS
49 : };
50 :
51 : static constexpr int TPC_DELTA_SEMITONE = 7; // the delta in tpc value to go 1 semitone up or down
52 : static constexpr int TPC_DELTA_ENHARMONIC = 12; // the delta in tpc value to reach the next (or prev) enharmonic spelling
53 : //static constexpr int TPC_FIRST_STEP = 3; // the step of the first valid tpc (= F = step 3)
54 : static constexpr int PITCH_DELTA_OCTAVE = 12; // the delta in pitch value to go 1 octave up or down
55 : static constexpr int STEP_DELTA_OCTAVE = 7; // the number of steps in an octave
56 : //static constexpr int STEP_DELTA_TPC = 4; // the number of steps in a tpc step (= a fifth = 4 steps)
57 : static constexpr int TPCS_PER_STEP = (Tpc::TPC_MAX - Tpc::TPC_MIN + 1) / STEP_DELTA_OCTAVE;
58 : static constexpr int MIN_STEP = MIN_PITCH * STEP_DELTA_OCTAVE / PITCH_DELTA_OCTAVE;
59 : static constexpr int MAX_STEP = MAX_PITCH * STEP_DELTA_OCTAVE / PITCH_DELTA_OCTAVE;
60 : static constexpr int KEY_TO_TPC_OFFSET = (int)Tpc::TPC_C - (int)Key::C;
61 :
62 : //---------------------------------------------------------
63 : // pitch2tpc
64 : // muse::Returns a default tpc for a given midi pitch.
65 : // Midi pitch 60 is middle C.
66 : //---------------------------------------------------------
67 :
68 : // pitch2tpc(pitch) replaced by pitch2tpc(pitch, Key::C, Prefer::NEAREST)
69 :
70 : enum class Prefer : char {
71 : FLATS=8, NEAREST=11, SHARPS=13
72 : };
73 : enum class NoteCaseType : signed char {
74 : AUTO = -1, CAPITAL = 0, LOWER, UPPER
75 : };
76 :
77 : extern int pitch2tpc(int pitch, Key, Prefer prefer);
78 :
79 : extern String tpc2name(int tpc, NoteSpellingType spelling, NoteCaseType noteCase, bool explicitAccidental = false, bool full = false);
80 : extern void tpc2name(int tpc, NoteSpellingType noteSpelling, NoteCaseType noteCase, String& s, String& acc, bool explicitAccidental = false,
81 : bool full = false);
82 : extern void tpc2name(int tpc, NoteSpellingType noteSpelling, NoteCaseType noteCase, String& s, AccidentalVal& acc);
83 : extern String tpcUserName(int tpc, int pitch, bool explicitAccidental = false, bool full = false);
84 : extern int step2tpc(const String& stepName, AccidentalVal alter);
85 : extern int step2tpc(int step);
86 : extern int step2tpc(int step, AccidentalVal alter);
87 : extern int step2tpcByKey(int step, Key);
88 : extern int tpc2pitch(int tpc);
89 : extern int tpc2step(int tpc);
90 : extern int tpc2stepByKey(int tpc, Key, int& alter);
91 : extern int tpc2alterByKey(int tpc, Key);
92 : extern int pitch2absStepByKey(int pitch, int tpc, Key, int& alter);
93 : extern int absStep2pitchByKey(int step, Key);
94 : extern int tpc2degree(int tpc, Key key);
95 : extern int tpcInterval(int startTpc, int interval, int alter);
96 : extern int step2pitchInterval(int step, int alter);
97 : extern String tpc2Function(int tpc, Key key);
98 : extern void tpc2Function(int tpc, Key key, String& accName, String& stepName);
99 : extern int function2Tpc(const String& s, Key key);
100 : extern int function2Tpc(const String& s, Key key, size_t& idx);
101 : extern int convertNote(const String& s, NoteSpellingType noteSpelling, NoteCaseType& noteCase, size_t& idx);
102 : extern int clampEnharmonic(int tpc, bool useDoubleSharpsFlats = true);
103 : extern int clampPitch(int pitch);
104 : extern int clampPitchOctaved(int pitch);
105 : extern Key clampKey(Key key, PreferSharpFlat prefer = PreferSharpFlat::NONE);
106 : extern int key2Tpc(Key key);
107 : extern Key tpc2Key(int tpc);
108 :
109 : //---------------------------------------------------------
110 : // tpc2alter
111 : //---------------------------------------------------------
112 :
113 125429 : inline static AccidentalVal tpc2alter(int tpc)
114 : {
115 125429 : return AccidentalVal(((tpc - Tpc::TPC_MIN) / TPC_DELTA_SEMITONE) + int(AccidentalVal::MIN));
116 : }
117 :
118 1574 : inline static int playingOctave(int pitch, int tpc)
119 : {
120 1574 : return ((pitch - static_cast<int>(tpc2alter(tpc))) / PITCH_DELTA_OCTAVE) - 1;
121 : }
122 :
123 : extern Char tpc2stepName(int tpc);
124 : extern bool tpcIsValid(int val);
125 51770 : inline bool pitchIsValid(int pitch) { return pitch >= MIN_PITCH && pitch <= MAX_PITCH; }
126 : } // namespace mu::engraving
|