LCOV - code coverage report
Current view: top level - src/engraving/dom - pitchspelling.h Coverage Total Hit
Test: filtered_coverage.info Lines: 100.0 % 5 5
Test Date: 2026-11-19 04:14:36

            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              : #include "key.h"
      28              : 
      29              : namespace mu::engraving {
      30              : class MidiNote;
      31              : class Note;
      32              : enum class Key : signed char;
      33              : 
      34              : static constexpr int INVALID_PITCH = -1;
      35              : static constexpr int MIN_PITCH     = 0;
      36              : static constexpr int MAX_PITCH     = 127;
      37              : 
      38              : // a list of tpc's, with legal ranges, not really an enum, so no way to convert into a class
      39              : enum Tpc : signed char {
      40              :     TPC_INVALID = -9,
      41              :     TPC_F_BBB, TPC_C_BBB, TPC_G_BBB, TPC_D_BBB, TPC_A_BBB, TPC_E_BBB, TPC_B_BBB,
      42              :     TPC_F_BB,  TPC_C_BB,  TPC_G_BB,  TPC_D_BB,  TPC_A_BB,  TPC_E_BB,  TPC_B_BB,
      43              :     TPC_F_B,   TPC_C_B,   TPC_G_B,   TPC_D_B,   TPC_A_B,   TPC_E_B,   TPC_B_B,
      44              :     TPC_F,     TPC_C,     TPC_G,     TPC_D,     TPC_A,     TPC_E,     TPC_B,
      45              :     TPC_F_S,   TPC_C_S,   TPC_G_S,   TPC_D_S,   TPC_A_S,   TPC_E_S,   TPC_B_S,
      46              :     TPC_F_SS,  TPC_C_SS,  TPC_G_SS,  TPC_D_SS,  TPC_A_SS,  TPC_E_SS,  TPC_B_SS,
      47              :     TPC_F_SSS, TPC_C_SSS, TPC_G_SSS, TPC_D_SSS, TPC_A_SSS, TPC_E_SSS, TPC_B_SSS,
      48              :     TPC_MIN = TPC_F_BBB,
      49              :     TPC_MAX = TPC_B_SSS
      50              : };
      51              : 
      52              : static constexpr int TPC_DELTA_SEMITONE   = 7;    // the delta in tpc value to go 1 semitone up or down
      53              : static constexpr int TPC_DELTA_ENHARMONIC = 12;   // the delta in tpc value to reach the next (or prev) enharmonic spelling
      54              : //static constexpr int TPC_FIRST_STEP       = 3;  // the step of the first valid tpc (= F = step 3)
      55              : static constexpr int PITCH_DELTA_OCTAVE   = 12;   // the delta in pitch value to go 1 octave up or down
      56              : static constexpr int STEP_DELTA_OCTAVE    = 7;    // the number of steps in an octave
      57              : //static constexpr int STEP_DELTA_TPC       = 4;  // the number of steps in a tpc step (= a fifth = 4 steps)
      58              : static constexpr int TPCS_PER_STEP        = (Tpc::TPC_MAX - Tpc::TPC_MIN + 1) / STEP_DELTA_OCTAVE;
      59              : static constexpr int MIN_STEP             = MIN_PITCH * STEP_DELTA_OCTAVE / PITCH_DELTA_OCTAVE;
      60              : static constexpr int MAX_STEP             = MAX_PITCH * STEP_DELTA_OCTAVE / PITCH_DELTA_OCTAVE;
      61              : static constexpr int KEY_TO_TPC_OFFSET = (int)Tpc::TPC_C - (int)Key::C;
      62              : 
      63              : //---------------------------------------------------------
      64              : //   pitch2tpc
      65              : //    muse::Returns a default tpc for a given midi pitch.
      66              : //    Midi pitch 60 is middle C.
      67              : //---------------------------------------------------------
      68              : 
      69              : // pitch2tpc(pitch) replaced by pitch2tpc(pitch, Key::C, Prefer::NEAREST)
      70              : 
      71              : enum class Prefer : char {
      72              :     FLATS=8, NEAREST=11, SHARPS=13
      73              : };
      74              : enum class NoteCaseType : signed char {
      75              :     AUTO = -1, CAPITAL = 0, LOWER, UPPER
      76              : };
      77              : 
      78              : extern int pitch2tpc(int pitch, Key, Prefer prefer);
      79              : 
      80              : extern String tpc2name(int tpc, NoteSpellingType spelling, NoteCaseType noteCase, bool explicitAccidental = false, bool full = false);
      81              : extern void tpc2name(int tpc, NoteSpellingType noteSpelling, NoteCaseType noteCase, String& s, String& acc, bool explicitAccidental = false,
      82              :                      bool full = false);
      83              : extern void tpc2name(int tpc, NoteSpellingType noteSpelling, NoteCaseType noteCase, String& s, AccidentalVal& acc);
      84              : extern String tpcUserName(int tpc, int pitch, bool explicitAccidental = false, bool full = false);
      85              : extern int step2tpc(const String& stepName, AccidentalVal alter);
      86              : extern int step2tpc(int step);
      87              : extern int step2tpc(int step, AccidentalVal alter);
      88              : extern int step2tpcByKey(int step, Key);
      89              : extern int tpc2pitch(int tpc);
      90              : extern int tpc2step(int tpc);
      91              : extern int tpc2stepByKey(int tpc, Key, int& alter);
      92              : extern int tpc2alterByKey(int tpc, Key);
      93              : extern int pitch2absStepByKey(int pitch, int tpc, Key, int& alter);
      94              : extern int pitch2JianpuOctave(int pitch, int tpc, Key key, KeyMode mode);
      95              : extern int pitch2JianpuOctave(int pitch, int tpc, int tonicTpc, KeyMode mode);
      96              : extern int absStep2pitchByKey(int step, Key);
      97              : extern int tpc2degree(int tpc, Key key);
      98              : extern int jianpuTonicTpc(Key relativeMajorKey, KeyMode mode);
      99              : extern void tpc2Jianpu(int tpc, int tonicTpc, KeyMode mode, String& accName, String& stepName);
     100              : extern void jianpuKeyMapping(const KeySigEvent& ks, KeyMode& mode, int& tonicTpc);
     101              : extern String jianpuKeyLabel(const KeySigEvent& ks);
     102              : extern String jianpuAccidentalMark(const String& accName);
     103              : extern int tpcInterval(int startTpc, int interval, int alter);
     104              : extern int step2pitchInterval(int step, int alter);
     105              : extern String tpc2Function(int tpc, Key key);
     106              : extern void tpc2Function(int tpc, Key key, String& accName, String& stepName);
     107              : extern int function2Tpc(const String& s, Key key);
     108              : extern int function2Tpc(const String& s, Key key, size_t& idx);
     109              : extern int convertNote(const String& s, NoteSpellingType noteSpelling, NoteCaseType& noteCase, size_t& idx);
     110              : extern int clampEnharmonic(int tpc, bool useDoubleSharpsFlats = true);
     111              : extern int clampPitch(int pitch);
     112              : extern int clampPitchOctaved(int pitch);
     113              : extern Key clampKey(Key key, PreferSharpFlat prefer = PreferSharpFlat::NONE);
     114              : extern int key2Tpc(Key key);
     115              : extern Key tpc2Key(int tpc);
     116              : 
     117              : //---------------------------------------------------------
     118              : //   tpc2alter
     119              : //---------------------------------------------------------
     120              : 
     121       141085 : inline static AccidentalVal tpc2alter(int tpc)
     122              : {
     123       141085 :     return AccidentalVal(((tpc - Tpc::TPC_MIN) / TPC_DELTA_SEMITONE) + int(AccidentalVal::MIN));
     124              : }
     125              : 
     126         1574 : inline static int playingOctave(int pitch, int tpc)
     127              : {
     128         1574 :     return ((pitch - static_cast<int>(tpc2alter(tpc))) / PITCH_DELTA_OCTAVE) - 1;
     129              : }
     130              : 
     131              : extern Char tpc2stepName(int tpc);
     132              : extern bool tpcIsValid(int val);
     133        54017 : inline bool pitchIsValid(int pitch) { return pitch >= MIN_PITCH && pitch <= MAX_PITCH; }
     134              : } // namespace mu::engraving
        

Generated by: LCOV version 2.0-1