LCOV - code coverage report
Current view: top level - src/engraving/dom - pitchspelling.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 71.7 % 474 340
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              : //  This file contains the implementation of an pitch spelling
      24              : //  algorithmus from Emilios Cambouropoulos as published in:
      25              : //  "Automatic Pitch Spelling: From Numbers to Sharps and Flats"
      26              : 
      27              : #include "pitchspelling.h"
      28              : 
      29              : #include "translation.h"
      30              : #include "types/typesconv.h"
      31              : 
      32              : #include "key.h"
      33              : 
      34              : #include "log.h"
      35              : 
      36              : namespace mu::engraving {
      37              : //---------------------------------------------------------
      38              : //   tpcIsValid
      39              : //---------------------------------------------------------
      40              : 
      41       344457 : bool tpcIsValid(int val)
      42              : {
      43       344457 :     return val >= Tpc::TPC_MIN && val <= Tpc::TPC_MAX;
      44              : }
      45              : 
      46              : //---------------------------------------------------------
      47              : //   step2tpc
      48              : //---------------------------------------------------------
      49              : 
      50        14766 : int step2tpc(int step, AccidentalVal alter)
      51              : {
      52              :     //    TPC - tonal pitch classes
      53              :     //    "line of fifth's" LOF
      54              : 
      55              :     static const int spellings[] = {
      56              :         // bbb  bb  b   -   #  ##  ###
      57              :         -7,  0,  7, 14, 21, 28, 35,  // C
      58              :         -5,  2,  9, 16, 23, 30, 37,  // D
      59              :         -3,  4, 11, 18, 25, 32, 39,  // E
      60              :         -8, -1,  6, 13, 20, 27, 34,  // F
      61              :         -6,  1,  8, 15, 22, 29, 36,  // G
      62              :         -4,  3, 10, 17, 24, 31, 38,  // A
      63              :         -2,  5, 12, 19, 26, 33, 40,  // B
      64              :     };
      65              : 
      66        14766 :     int i = (step * TPCS_PER_STEP) + (int(alter) - int(AccidentalVal::MIN));
      67        14766 :     assert(i >= 0 && (i < int(sizeof(spellings) / sizeof(*spellings))));
      68        14766 :     return spellings[i];
      69              : }
      70              : 
      71              : static const int tpcByStepAndKey[int(Key::NUM_OF)][STEP_DELTA_OCTAVE] = {
      72              : // step          C             D             E             F             G             A             B        Key
      73              :     { Tpc::TPC_C_B, Tpc::TPC_D_B, Tpc::TPC_E_B, Tpc::TPC_F_B, Tpc::TPC_G_B, Tpc::TPC_A_B, Tpc::TPC_B_B },  // Cb
      74              :     { Tpc::TPC_C_B, Tpc::TPC_D_B, Tpc::TPC_E_B, Tpc::TPC_F,   Tpc::TPC_G_B, Tpc::TPC_A_B, Tpc::TPC_B_B },  // Gb
      75              :     { Tpc::TPC_C,   Tpc::TPC_D_B, Tpc::TPC_E_B, Tpc::TPC_F,   TPC_G_B,      Tpc::TPC_A_B, Tpc::TPC_B_B },  // Db
      76              :     { Tpc::TPC_C,   Tpc::TPC_D_B, Tpc::TPC_E_B, Tpc::TPC_F,   Tpc::TPC_G,   Tpc::TPC_A_B, Tpc::TPC_B_B },  // Ab
      77              :     { Tpc::TPC_C,   Tpc::TPC_D,   Tpc::TPC_E_B, Tpc::TPC_F,   Tpc::TPC_G,   Tpc::TPC_A_B, Tpc::TPC_B_B },  // Eb
      78              :     { Tpc::TPC_C,   Tpc::TPC_D,   Tpc::TPC_E_B, Tpc::TPC_F,   Tpc::TPC_G,   Tpc::TPC_A,   Tpc::TPC_B_B },  // B
      79              :     { Tpc::TPC_C,   Tpc::TPC_D,   Tpc::TPC_E,   Tpc::TPC_F,   Tpc::TPC_G,   Tpc::TPC_A,   Tpc::TPC_B_B },  // F
      80              :     { Tpc::TPC_C,   Tpc::TPC_D,   Tpc::TPC_E,   Tpc::TPC_F,   Tpc::TPC_G,   Tpc::TPC_A,   Tpc::TPC_B },    // C
      81              :     { Tpc::TPC_C,   Tpc::TPC_D,   Tpc::TPC_E,   Tpc::TPC_F_S, Tpc::TPC_G,   Tpc::TPC_A,   Tpc::TPC_B },    // G
      82              :     { Tpc::TPC_C_S, Tpc::TPC_D,   Tpc::TPC_E,   Tpc::TPC_F_S, Tpc::TPC_G,   Tpc::TPC_A,   Tpc::TPC_B },    // D
      83              :     { Tpc::TPC_C_S, Tpc::TPC_D,   Tpc::TPC_E,   Tpc::TPC_F_S, Tpc::TPC_G_S, Tpc::TPC_A,   Tpc::TPC_B },    // A
      84              :     { Tpc::TPC_C_S, Tpc::TPC_D_S, Tpc::TPC_E,   Tpc::TPC_F_S, Tpc::TPC_G_S, Tpc::TPC_A,   Tpc::TPC_B },    // E
      85              :     { Tpc::TPC_C_S, Tpc::TPC_D_S, Tpc::TPC_E,   Tpc::TPC_F_S, Tpc::TPC_G_S, Tpc::TPC_A_S, Tpc::TPC_B },    // H
      86              :     { Tpc::TPC_C_S, Tpc::TPC_D_S, Tpc::TPC_E_S, Tpc::TPC_F_S, Tpc::TPC_G_S, Tpc::TPC_A_S, Tpc::TPC_B },    // F#
      87              :     { Tpc::TPC_C_S, Tpc::TPC_D_S, Tpc::TPC_E_S, Tpc::TPC_F_S, Tpc::TPC_G_S, Tpc::TPC_A_S, Tpc::TPC_B_S },  // C#
      88              : };
      89              : 
      90          662 : int step2tpcByKey(int step, Key key)
      91              : {
      92          662 :     while (step < 0) {
      93            0 :         step += STEP_DELTA_OCTAVE;
      94              :     }
      95              : 
      96          662 :     return tpcByStepAndKey[int(clampKey(key)) - int(Key::MIN)][step % STEP_DELTA_OCTAVE];
      97              : }
      98              : 
      99              : //---------------------------------------------------------
     100              : //   tpc2step
     101              : //---------------------------------------------------------
     102              : 
     103       232526 : int tpc2step(int tpc)
     104              : {
     105              :     // 14 - C
     106              :     // 15 % 7 = 1
     107              :     //                                            f  c  g  d  a  e  b
     108              :     static const int steps[STEP_DELTA_OCTAVE] = { 3, 0, 4, 1, 5, 2, 6 };
     109              :     // TODO: optimize -TCP_MIN
     110       232526 :     return steps[(tpc - Tpc::TPC_MIN) % STEP_DELTA_OCTAVE];
     111              : // without a table, could also be rendered as:
     112              : //      return ((tpc-Tpc::TPC_MIN) * STEP_DELTA_TPC) / STEP_DELTA_OCTAVE + TPC_FIRST_STEP;
     113              : }
     114              : 
     115              : //---------------------------------------------------------
     116              : //   tpc2stepByKey
     117              : //---------------------------------------------------------
     118              : 
     119            0 : int tpc2stepByKey(int tpc, Key key, int& alter)
     120              : {
     121            0 :     alter = tpc2alterByKey(tpc, key);
     122            0 :     return tpc2step(tpc);
     123              : }
     124              : 
     125              : //---------------------------------------------------------
     126              : //   step2tpc
     127              : //---------------------------------------------------------
     128              : 
     129           71 : int step2tpc(const String& stepName, AccidentalVal alter)
     130              : {
     131           71 :     if (stepName.isEmpty()) {
     132            0 :         return Tpc::TPC_INVALID;
     133              :     }
     134              :     int r;
     135           71 :     switch (stepName.at(0).toLower().toAscii()) {
     136           42 :     case 'c': r = 0;
     137           42 :         break;
     138            1 :     case 'd': r = 1;
     139            1 :         break;
     140            3 :     case 'e': r = 2;
     141            3 :         break;
     142            6 :     case 'f': r = 3;
     143            6 :         break;
     144            6 :     case 'g': r = 4;
     145            6 :         break;
     146            8 :     case 'a': r = 5;
     147            8 :         break;
     148            5 :     case 'b': r = 6;
     149            5 :         break;
     150            0 :     default:
     151            0 :         return Tpc::TPC_INVALID;
     152              :     }
     153           71 :     return step2tpc(r, alter);
     154              : }
     155              : 
     156              : //---------------------------------------------------------
     157              : //   step2deltaPitchByKey
     158              : //
     159              : // returns the delta pitch above natural C for the given step in the given key
     160              : // step: 0 - 6
     161              : // key: -7 - +7
     162              : //---------------------------------------------------------
     163              : 
     164              : static const int pitchByStepAndKey[int(Key::NUM_OF)][STEP_DELTA_OCTAVE] = {
     165              : // step  C   D   E   F   G   A   B           Key
     166              :     { -1,  1,  3,  4,  6,  8, 10 },       // Cb
     167              :     { -1,  1,  3,  5,  6,  8, 10 },       // Gb
     168              :     { 0,  1,  3,  5,  6,  8, 10 },        // Db
     169              :     { 0,  1,  3,  5,  7,  8, 10 },        // Ab
     170              :     { 0,  2,  3,  5,  7,  8, 10 },        // Eb
     171              :     { 0,  2,  3,  5,  7,  9, 10 },        // B
     172              :     { 0,  2,  4,  5,  7,  9, 10 },        // F
     173              :     { 0,  2,  4,  5,  7,  9, 11 },        // C
     174              :     { 0,  2,  4,  6,  7,  9, 11 },        // G
     175              :     { 1,  2,  4,  6,  7,  9, 11 },        // D
     176              :     { 1,  2,  4,  6,  8,  9, 11 },        // A
     177              :     { 1,  3,  4,  6,  8,  9, 11 },        // E
     178              :     { 1,  3,  4,  6,  8, 10, 11 },        // H
     179              :     { 1,  3,  5,  6,  8, 10, 11 },        // F#
     180              :     { 1,  3,  5,  6,  8, 10, 12 },        // C#
     181              : };
     182              : 
     183          662 : int step2deltaPitchByKey(int step, Key key)
     184              : {
     185          662 :     while (step < MIN_STEP) {
     186            0 :         step += STEP_DELTA_OCTAVE;
     187              :     }
     188          662 :     return pitchByStepAndKey[int(clampKey(key)) - int(Key::MIN)][step % STEP_DELTA_OCTAVE];
     189              : }
     190              : 
     191              : //---------------------------------------------------------
     192              : //   tpc2pitch
     193              : //---------------------------------------------------------
     194              : 
     195       205395 : int tpc2pitch(int tpc)
     196              : {
     197       205395 :     assert(tpcIsValid(tpc));
     198              : 
     199              :     static int pitches[] = {
     200              : //step: F   C   G   D   A   E   B
     201              :         2, -3,  4, -1,  6,  1,  8,     // bbb
     202              :         3, -2,  5,  0,  7,  2,  9,     // bb
     203              :         4, -1,  6,  1,  8,  3, 10,     // b
     204              :         5,  0,  7,  2,  9,  4, 11,     // -
     205              :         6,  1,  8,  3, 10,  5, 12,     // #
     206              :         7,  2,  9,  4, 11,  6, 13,     // ##
     207              :         8,  3, 10,  5, 12,  7, 14      // ###
     208              :     };
     209       205395 :     return pitches[tpc - Tpc::TPC_MIN];
     210              : }
     211              : 
     212              : //---------------------------------------------------------
     213              : //   tpc2alterByKey
     214              : //
     215              : // returns the alteration (-3 to 3) of a given tpc in the given key
     216              : // to understand the formula:
     217              : //    in the highest key (C#Maj), each of the first 7 tpcs (Fbb to Bbb; tpc-Tpc::TPC_MIN: 0 to 7)
     218              : //          is 3 semitones below its key degree (alter = -3)
     219              : //    the second 7 tpcs (Fb to Bb; tpc-Tpc::TPC_MIN: 8 to 13) are 2 semitones below (alter = -2) and so on up to 1
     220              : //    thus, for C#Maj:
     221              : // (1)      (tpc-Tpc::TPC_MIN) - 0         =  0 to 34 (for tcp-TCP_MIN from 0 to 34)
     222              : // (2)      (tpc-Tpc::TPC_MIN) - 0) / 7    =  0 to 4  (for each settuple of tcp's) and finally
     223              : // (3)      (tcp-Tpc::TPC_MIN) - 0) / 7 -3 = -3 to 1  (for each settuple of tcp's)
     224              : //          where 0 = Key::C_S - Key::MAX
     225              : //    for each previous key, the result of (1) increases by 1 and the classes of alter are shifted 1 TPC 'up':
     226              : //          F#Maj: Fbb-Ebb => -3, Bbb to Eb => -2 and so on
     227              : //          BMaj:  Fbb-Abb => -3, Ebb to Ab => -2 and so on
     228              : //          and so on
     229              : //    thus, for any 'key', the formula is:
     230              : //          ((tcp-Tpc::TPC_MIN) - (key-Key::MAX)) / TCP_DELTA_SEMITONE - 3
     231              : //---------------------------------------------------------
     232              : 
     233         7526 : int tpc2alterByKey(int tpc, Key key)
     234              : {
     235         7526 :     return (tpc - int(key) - int(Tpc::TPC_MIN) + int(Key::MAX)) / TPC_DELTA_SEMITONE - (int(AccidentalVal::MAX) + 1);
     236              : }
     237              : 
     238              : //---------------------------------------------------------
     239              : //   tpc2name
     240              : //    return note name
     241              : //---------------------------------------------------------
     242              : 
     243         1175 : String tpc2name(int tpc, NoteSpellingType noteSpelling, NoteCaseType noteCase, bool explicitAccidental, bool full)
     244              : {
     245         1175 :     String s;
     246         1175 :     String acc;
     247         1175 :     tpc2name(tpc, noteSpelling, noteCase, s, acc, explicitAccidental, full);
     248         2350 :     return s + (explicitAccidental ? u" " : u"") + acc;
     249         1175 : }
     250              : 
     251              : //---------------------------------------------------------
     252              : //   tpc2name
     253              : //---------------------------------------------------------
     254              : 
     255         7513 : void tpc2name(int tpc, NoteSpellingType noteSpelling, NoteCaseType noteCase, String& s, String& acc, bool explicitAccidental, bool full)
     256              : {
     257              :     AccidentalVal accVal;
     258         7513 :     tpc2name(tpc, noteSpelling, noteCase, s, accVal);
     259         7513 :     switch (accVal) {
     260            0 :     case AccidentalVal::FLAT3:
     261            0 :         if (explicitAccidental) {
     262            0 :             acc = muse::mtrc("engraving", TConv::userName(AccidentalVal::FLAT3, full));
     263            0 :         } else if (noteSpelling == NoteSpellingType::GERMAN_PURE) {
     264            0 :             switch (tpc) {
     265            0 :             case TPC_A_BBB: acc = u"sasas";
     266            0 :                 break;
     267            0 :             case TPC_E_BBB: acc = u"seses";
     268            0 :                 break;
     269            0 :             default: acc = u"eseses";
     270              :             }
     271              :         } else {
     272            0 :             acc = u"bbb";
     273              :         }
     274            0 :         break;
     275            5 :     case AccidentalVal::FLAT2:
     276            5 :         if (explicitAccidental) {
     277            0 :             acc = muse::mtrc("engraving", TConv::userName(AccidentalVal::FLAT2, full));
     278            5 :         } else if (noteSpelling == NoteSpellingType::GERMAN_PURE) {
     279            0 :             switch (tpc) {
     280            0 :             case TPC_A_BB: acc = u"sas";
     281            0 :                 break;
     282            0 :             case TPC_E_BB: acc = u"ses";
     283            0 :                 break;
     284            0 :             default: acc = u"eses";
     285              :             }
     286              :         } else {
     287            5 :             acc = u"bb";
     288              :         }
     289            5 :         break;
     290          452 :     case AccidentalVal::FLAT:
     291          452 :         if (explicitAccidental) {
     292            0 :             acc = muse::mtrc("engraving", TConv::userName(AccidentalVal::FLAT, full));
     293          452 :         } else if (noteSpelling == NoteSpellingType::GERMAN_PURE) {
     294            0 :             acc = (tpc == TPC_A_B || tpc == TPC_E_B) ? u"s" : u"es";
     295              :         } else {
     296          452 :             acc = u"b";
     297              :         }
     298          452 :         break;
     299         5961 :     case  AccidentalVal::NATURAL: acc = u"";
     300         5961 :         break;
     301         1091 :     case  AccidentalVal::SHARP:
     302         1091 :         if (explicitAccidental) {
     303            0 :             acc = muse::mtrc("engraving", TConv::userName(AccidentalVal::SHARP, full));
     304              :         } else {
     305         1091 :             acc = (noteSpelling == NoteSpellingType::GERMAN_PURE) ? u"is" : u"#";
     306              :         }
     307         1091 :         break;
     308            4 :     case  AccidentalVal::SHARP2:
     309            4 :         if (explicitAccidental) {
     310            0 :             acc = muse::mtrc("engraving", TConv::userName(AccidentalVal::SHARP2, full));
     311              :         } else {
     312            4 :             acc = (noteSpelling == NoteSpellingType::GERMAN_PURE) ? u"isis" : u"##";
     313              :         }
     314            4 :         break;
     315            0 :     case AccidentalVal::SHARP3:
     316            0 :         if (explicitAccidental) {
     317            0 :             acc = muse::mtrc("engraving", TConv::userName(AccidentalVal::SHARP3, full));
     318              :         } else {
     319            0 :             acc = (noteSpelling == NoteSpellingType::GERMAN_PURE) ? u"isisis" : u"###";
     320              :         }
     321            0 :         break;
     322            0 :     default:
     323            0 :         LOGD("tpc2name(%d): acc %d", tpc, static_cast<int>(accVal));
     324            0 :         acc = u"";
     325            0 :         break;
     326              :     }
     327         7513 : }
     328              : 
     329              : //---------------------------------------------------------
     330              : //   tpc2name
     331              : //---------------------------------------------------------
     332              : 
     333        10300 : void tpc2name(int tpc, NoteSpellingType noteSpelling, NoteCaseType noteCase, String& s, AccidentalVal& acc)
     334              : {
     335        10300 :     const char names[]  = "FCGDAEB";
     336        10300 :     const char gnames[] = "FCGDAEH";
     337        92700 :     const String snames[] = { u"Fa", u"Do", u"Sol", u"Re", u"La", u"Mi", u"Si" };
     338              : 
     339        10300 :     acc = tpc2alter(tpc);
     340        10300 :     int idx = (tpc - Tpc::TPC_MIN) % TPC_DELTA_SEMITONE;
     341        10300 :     switch (noteSpelling) {
     342            0 :     case NoteSpellingType::GERMAN:
     343              :     case NoteSpellingType::GERMAN_PURE:
     344            0 :         s = String(Char::fromAscii(gnames[idx]));
     345            0 :         if (s == "H" && acc == AccidentalVal::FLAT) {
     346            0 :             s = u"B";
     347            0 :             if (noteSpelling == NoteSpellingType::GERMAN_PURE) {
     348            0 :                 acc = AccidentalVal::NATURAL;
     349              :             }
     350              :         }
     351            0 :         break;
     352           30 :     case NoteSpellingType::SOLFEGGIO:
     353           30 :         s = snames[idx];
     354           30 :         break;
     355            0 :     case NoteSpellingType::FRENCH:
     356            0 :         s = snames[idx];
     357            0 :         if (s == u"Re") {
     358            0 :             s = u"Ré";
     359              :         }
     360            0 :         break;
     361        10270 :     default:
     362        10270 :         s = String(Char::fromAscii(names[idx]));
     363        10270 :         break;
     364              :     }
     365        10300 :     switch (noteCase) {
     366           51 :     case NoteCaseType::LOWER: s = s.toLower();
     367           51 :         break;
     368         3551 :     case NoteCaseType::UPPER: s = s.toUpper();
     369         3551 :         break;
     370         6698 :     case NoteCaseType::CAPITAL:
     371              :     case NoteCaseType::AUTO:
     372              :     default:
     373         6698 :         break;
     374              :     }
     375        92700 : }
     376              : 
     377              : //---------------------------------------------------------
     378              : //   tpc2stepName
     379              : //---------------------------------------------------------
     380              : 
     381         4610 : Char tpc2stepName(int tpc)
     382              : {
     383         4610 :     static const String names = u"FCGDAEB";
     384         4610 :     return names.at((tpc - Tpc::TPC_MIN) % 7);
     385              : }
     386              : 
     387            0 : String tpcUserName(int tpc, int pitch, bool explicitAccidental, bool full)
     388              : {
     389            0 :     String pitchStr = tpc2name(tpc, NoteSpellingType::STANDARD, NoteCaseType::AUTO, explicitAccidental, full);
     390            0 :     if (!explicitAccidental) {
     391            0 :         pitchStr.replace(u"b", u"♭");
     392            0 :         pitchStr.replace(u"#", u"♯");
     393              :     }
     394              : 
     395            0 :     const String octaveStr = String::number(((pitch - static_cast<int>(tpc2alter(tpc))) / PITCH_DELTA_OCTAVE) - 1);
     396              : 
     397            0 :     return pitchStr + octaveStr;
     398            0 : }
     399              : 
     400              : //---------------------------------------------------------
     401              : //   pitch2tpc2
     402              : //---------------------------------------------------------
     403              : 
     404              : // pitch2tpc2(pitch, false) replaced by pitch2tpc(pitch, Key::C, Prefer::FLATS)
     405              : // pitch2tpc2(pitch, true) replaced by pitch2tpc(pitch, Key::C, Prefer::SHARPS)
     406              : 
     407              : //---------------------------------------------------------
     408              : //   pitch2tpc
     409              : //    preferred pitch spelling depending on key
     410              : //    key -7 to +7
     411              : //
     412              : // The value of prefer sets the preferred mix of flats and sharps
     413              : // for pitches that are non-diatonic in the key specified, by
     414              : // positioning the window along the tpc sequence.
     415              : //
     416              : // Scale tones are the range shown in [ ].
     417              : // A value of 8 (Prefer::FLATS) specifies 5b 2b 6b 3b 7b [4 1 5 2 6 3 7]
     418              : // A value of 11 (Prefer::NEAREST) specifies 3b 7b [4 1 5 2 6 3 7] 4# 1# 5#
     419              : // A value of 13 (Prefer::SHARPS) specifies [4 1 5 2 6 3 7] 4# 1# 5# 2# 6#
     420              : //
     421              : // Examples for Prefer::NEAREST (n indicates explicit natural):
     422              : // C major will use Eb Bb [F C G D A E B] F# C# G#.
     423              : // E major will use Gn Dn [A E B F# C# G# D#] A# E# B#.
     424              : // F# major will use An En [B F# C# G# D# A# E#] B# Fx Cx.
     425              : // Eb major will use Gb Db [Ab Eb Bb F C G D] An En Bn.
     426              : // Gb major will use Bbb Fb [Cb Gb Db Ab Eb Bb F] Cn Gn Dn.
     427              : //---------------------------------------------------------
     428              : 
     429         6965 : int pitch2tpc(int pitch, Key key, Prefer prefer)
     430              : {
     431         6965 :     return (pitch * 7 + 26 - (int(prefer) + int(key))) % 12 + (int(prefer) + int(key));
     432              : }
     433              : 
     434              : //---------------------------------------------------------
     435              : //   pitch2absStepByKey
     436              : //    absolute step (C0 = 0, D0 = 1, ... C1 = 7, D2 = 8, ...) for a pitch/tpc according to key
     437              : //    if pAlter not null, returns in it the alteration with respect to the corresponding key degree (-3 to 3)
     438              : //    (for instance, an F in GMaj yields alteration -1 i.e. 1 semitone below corresp. deg. of GMaj which is F#)
     439              : //    key: between Key::MIN and Key::MAX
     440              : //---------------------------------------------------------
     441              : 
     442          662 : int pitch2absStepByKey(int pitch, int tpc, Key key, int& alter)
     443              : {
     444              :     // sanitize input data
     445          662 :     pitch = clampPitchOctaved(pitch);
     446          662 :     tpc = clampEnharmonic(tpc);
     447          662 :     key = clampKey(key);
     448              : 
     449          662 :     int octave = (pitch - int(tpc2alter(tpc))) / PITCH_DELTA_OCTAVE;
     450          662 :     int step = tpc2step(tpc);
     451          662 :     alter = tpc2alterByKey(tpc, key);
     452          662 :     return octave * STEP_DELTA_OCTAVE + step;
     453              : }
     454              : 
     455         7186 : static int jianpuModeDegreeOffset(KeyMode mode)
     456              : {
     457         7186 :     switch (mode) {
     458           11 :     case KeyMode::DORIAN: return 1;
     459          237 :     case KeyMode::PHRYGIAN: return 2;
     460            1 :     case KeyMode::LYDIAN: return 3;
     461            1 :     case KeyMode::MIXOLYDIAN: return 4;
     462            1 :     case KeyMode::AEOLIAN: return 5;
     463           11 :     case KeyMode::MINOR: return 5;
     464            1 :     case KeyMode::LOCRIAN: return 6;
     465         6923 :     case KeyMode::UNKNOWN:
     466              :     case KeyMode::NONE:
     467              :     case KeyMode::MAJOR:
     468              :     case KeyMode::IONIAN:
     469         6923 :         return 0;
     470              :     }
     471            0 :     return 0;
     472              : }
     473              : 
     474         1771 : static const int* jianpuModeIntervals(KeyMode mode)
     475              : {
     476              :     static const int ionian[] = { 0, 2, 4, 5, 7, 9, 11 };
     477              :     static const int dorian[] = { 0, 2, 3, 5, 7, 9, 10 };
     478              :     static const int phrygian[] = { 0, 1, 3, 5, 7, 8, 10 };
     479              :     static const int lydian[] = { 0, 2, 4, 6, 7, 9, 11 };
     480              :     static const int mixolydian[] = { 0, 2, 4, 5, 7, 9, 10 };
     481              :     static const int aeolian[] = { 0, 2, 3, 5, 7, 8, 10 };
     482              :     static const int locrian[] = { 0, 1, 3, 5, 6, 8, 10 };
     483              : 
     484         1771 :     switch (mode) {
     485            3 :     case KeyMode::DORIAN: return dorian;
     486          111 :     case KeyMode::PHRYGIAN: return phrygian;
     487            0 :     case KeyMode::LYDIAN: return lydian;
     488            0 :     case KeyMode::MIXOLYDIAN: return mixolydian;
     489            0 :     case KeyMode::AEOLIAN: return aeolian;
     490            2 :     case KeyMode::MINOR: return aeolian;
     491            0 :     case KeyMode::LOCRIAN: return locrian;
     492         1655 :     case KeyMode::UNKNOWN:
     493              :     case KeyMode::NONE:
     494              :     case KeyMode::MAJOR:
     495              :     case KeyMode::IONIAN:
     496         1655 :         return ionian;
     497              :     }
     498            0 :     return ionian;
     499              : }
     500              : 
     501         5202 : static int normalizeJianpuAlteration(int alteration)
     502              : {
     503         5232 :     while (alteration > 6) {
     504           30 :         alteration -= 12;
     505              :     }
     506         5202 :     while (alteration < -6) {
     507            0 :         alteration += 12;
     508              :     }
     509         5202 :     return alteration;
     510              : }
     511              : 
     512         3431 : int jianpuTonicTpc(Key relativeMajorKey, KeyMode mode)
     513              : {
     514              :     static const int majorScaleSemitones[] = { 0, 2, 4, 5, 7, 9, 11 };
     515              : 
     516         3431 :     const int degreeOffset = jianpuModeDegreeOffset(mode);
     517         3431 :     const int relativeMajorTpc = key2Tpc(relativeMajorKey);
     518         3431 :     const int relativeMajorStep = tpc2step(relativeMajorTpc);
     519         3431 :     const int tonicStep = (relativeMajorStep + degreeOffset) % STEP_DELTA_OCTAVE;
     520         3431 :     const int relativeMajorPitch = (majorScaleSemitones[relativeMajorStep] + int(tpc2alter(relativeMajorTpc)) + 12) % 12;
     521         3431 :     const int tonicPitch = (relativeMajorPitch + majorScaleSemitones[degreeOffset]) % 12;
     522         3431 :     const int tonicAlteration = normalizeJianpuAlteration(tonicPitch - majorScaleSemitones[tonicStep]);
     523              : 
     524         3431 :     return step2tpc(tonicStep, AccidentalVal(tonicAlteration));
     525              : }
     526              : 
     527         1771 : void tpc2Jianpu(int tpc, int tonicTpc, KeyMode mode, String& accName, String& stepName)
     528              : {
     529              :     static const int majorScaleSemitones[] = { 0, 2, 4, 5, 7, 9, 11 };
     530         1771 :     static const String stepNames = u"1234567";
     531              : 
     532         1771 :     tpc = clampEnharmonic(tpc);
     533         1771 :     tonicTpc = clampEnharmonic(tonicTpc);
     534         1771 :     const int tonicStep = tpc2step(tonicTpc);
     535         1771 :     const int noteStep = tpc2step(tpc);
     536         1771 :     const int scaleDegree = (noteStep - tonicStep + STEP_DELTA_OCTAVE) % STEP_DELTA_OCTAVE;
     537         1771 :     const int jianpuDegree = (jianpuModeDegreeOffset(mode) + scaleDegree) % STEP_DELTA_OCTAVE;
     538         1771 :     stepName = stepNames.at(jianpuDegree);
     539              : 
     540         1771 :     const int* modeIntervals = jianpuModeIntervals(mode);
     541         1771 :     const int tonicPitch = (majorScaleSemitones[tonicStep] + int(tpc2alter(tonicTpc)) + 12) % 12;
     542         1771 :     const int expectedPitch = (tonicPitch + modeIntervals[scaleDegree]) % 12;
     543         1771 :     const int expectedAlteration = normalizeJianpuAlteration(expectedPitch - majorScaleSemitones[noteStep]);
     544         1771 :     const int alteration = int(tpc2alter(tpc)) - expectedAlteration;
     545              : 
     546         1771 :     accName = String();
     547         1771 :     const Char accidental = alteration < 0 ? Char(u'b') : Char(u'#');
     548         1898 :     for (int i = 0; i < std::abs(alteration); ++i) {
     549          127 :         accName.append(accidental);
     550              :     }
     551         1771 : }
     552              : 
     553         1782 : int pitch2JianpuOctave(int pitch, int tpc, int tonicTpc, KeyMode mode)
     554              : {
     555         1782 :     tonicTpc = clampEnharmonic(tonicTpc);
     556         1782 :     tpc = clampEnharmonic(tpc);
     557         1782 :     const int noteOctave = (clampPitchOctaved(pitch) - int(tpc2alter(tpc))) / PITCH_DELTA_OCTAVE;
     558         1782 :     const int absStep = noteOctave * STEP_DELTA_OCTAVE + tpc2step(tpc);
     559         1782 :     const int tonicAbsStep = 4 * STEP_DELTA_OCTAVE + tpc2step(tonicTpc);
     560         1782 :     const int degreeOneOffset = (STEP_DELTA_OCTAVE - jianpuModeDegreeOffset(mode)) % STEP_DELTA_OCTAVE;
     561         1782 :     const int relativeStep = absStep - tonicAbsStep - degreeOneOffset;
     562         1782 :     return relativeStep >= 0 ? relativeStep / STEP_DELTA_OCTAVE
     563         1782 :            : (relativeStep - STEP_DELTA_OCTAVE + 1) / STEP_DELTA_OCTAVE;
     564              : }
     565              : 
     566           11 : int pitch2JianpuOctave(int pitch, int tpc, Key key, KeyMode mode)
     567              : {
     568           11 :     return pitch2JianpuOctave(pitch, tpc, key2Tpc(key), mode);
     569              : }
     570              : 
     571         3726 : void jianpuKeyMapping(const KeySigEvent& ks, KeyMode& mode, int& tonicTpc)
     572              : {
     573         3726 :     if (ks.jianpuNumbering() == JianpuNumbering::CUSTOM) {
     574          316 :         mode = ks.jianpuTonicMode();
     575          316 :         tonicTpc = key2Tpc(ks.jianpuTonicKey());
     576              :     } else {
     577         3410 :         mode = ks.mode();
     578         3410 :         tonicTpc = jianpuTonicTpc(ks.key(), mode);
     579              :     }
     580         3726 : }
     581              : 
     582              : //---------------------------------------------------------
     583              : //   jianpuAccidentalMark
     584              : //    Converts the ASCII accidental spelling returned by tpc2Jianpu (e.g.
     585              : //    "#", "bb") into the display form used next to a Jianpu digit.
     586              : //---------------------------------------------------------
     587              : 
     588         1966 : String jianpuAccidentalMark(const String& accName)
     589              : {
     590         1966 :     String mark = accName;
     591         1966 :     mark.replace(u"b", u"♭");
     592         1966 :     mark.replace(u"#", u"♯");
     593         1966 :     return mark;
     594            0 : }
     595              : 
     596              : //---------------------------------------------------------
     597              : //   jianpuKeyLabel
     598              : //    Computes the Jianpu key-signature label (e.g. "1=C", "6=A"). An unset
     599              : //    (UNKNOWN) or atonal (NONE) mode in Follow-score-key numbering falls
     600              : //    back to the same major mapping already used for note digits, since the
     601              : //    key signature alone can't distinguish a major key from its relative minor.
     602              : //---------------------------------------------------------
     603              : 
     604          202 : String jianpuKeyLabel(const KeySigEvent& ks)
     605              : {
     606              :     KeyMode mode;
     607              :     int tonicTpc;
     608          202 :     jianpuKeyMapping(ks, mode, tonicTpc);
     609              : 
     610          202 :     const int degree = jianpuModeDegreeOffset(mode) + 1;
     611          202 :     String tonicName = tpc2name(tonicTpc, NoteSpellingType::STANDARD, NoteCaseType::AUTO);
     612          202 :     tonicName = jianpuAccidentalMark(tonicName);
     613              : 
     614          404 :     return String(u"%1=%2").arg(String::number(degree), tonicName);
     615          202 : }
     616              : 
     617              : //---------------------------------------------------------
     618              : //   absStep2pitchByKey
     619              : //    the default pitch for the given absolute step in the given key
     620              : //---------------------------------------------------------
     621              : 
     622          662 : int absStep2pitchByKey(int step, Key key)
     623              : {
     624              :     // sanitize input data
     625          662 :     if (step < MIN_STEP) {
     626            0 :         step += STEP_DELTA_OCTAVE;
     627              :     }
     628          662 :     if (step > MAX_STEP) {
     629            0 :         step -= STEP_DELTA_OCTAVE;
     630              :     }
     631          662 :     key = clampKey(key);
     632              : 
     633          662 :     int octave = step / STEP_DELTA_OCTAVE;
     634          662 :     int deltaPitch = step2deltaPitchByKey(step % STEP_DELTA_OCTAVE, key);
     635          662 :     return octave * PITCH_DELTA_OCTAVE + deltaPitch;
     636              : }
     637              : 
     638              : //---------------------------------------------------------
     639              : //   tpc2degree
     640              : //    the scale degree of a TPC for a given Key
     641              : //---------------------------------------------------------
     642              : 
     643          620 : int tpc2degree(int tpc, Key key)
     644              : {
     645          620 :     static const String names(u"CDEFGAB");
     646          620 :     static const String scales(u"CGDAEBFCGDAEBFC");
     647          620 :     Char scale = scales.at(int(key) + 7);
     648          620 :     Char stepName = tpc2stepName(tpc);
     649         1240 :     return (names.indexOf(stepName) - names.indexOf(scale) + 28) % 7;
     650              : }
     651              : 
     652              : //---------------------------------------------------------
     653              : //   tpcInterval
     654              : ///   Finds tpc of a note based on an altered interval
     655              : ///   from a starting note
     656              : //---------------------------------------------------------
     657              : 
     658         1859 : int tpcInterval(int startTpc, int interval, int alter)
     659              : {
     660         1859 :     assert(interval > 0);
     661              :     static const int intervals[7] = {
     662              : //          1  2  3   4  5  6  7
     663              :         0, 2, 4, -1, 1, 3, 5
     664              :     };
     665              : 
     666              :     //ensure that we don't have anything more than double sharp or double flat
     667              :     //(I know, breaking some convention, but it's the best we can do for now)
     668         1859 :     return clampEnharmonic(startTpc + intervals[(interval - 1) % 7] + alter * TPC_DELTA_SEMITONE);
     669              : }
     670              : 
     671              : //---------------------------------------------------------
     672              : //   step2pitchInterval
     673              : ///   Finds pitch between notes a specified altered interval away
     674              : ///
     675              : ///   For example:
     676              : ///         step = 3, alter = 0 means major 3rd
     677              : ///         step = 5, alter = -1 means diminished 5
     678              : ///         step = 6, alter = 2 means augmented sixth
     679              : //---------------------------------------------------------
     680              : 
     681         1166 : int step2pitchInterval(int step, int alter)
     682              : {
     683         1166 :     assert(step > 0);
     684              :     static const int intervals[7] = {
     685              : //          1  2  3  4  5  6  7
     686              :         0, 2, 4, 5, 7, 9, 11
     687              :     };
     688              : 
     689         1166 :     return intervals[(step - 1) % 7] + alter;
     690              : }
     691              : 
     692          232 : void tpc2Function(int tpc, Key key, String& accName, String& stepName)
     693              : {
     694          232 :     if (key == Key::INVALID) {
     695            0 :         LOGD() << "Invalid key";
     696            0 :         return;
     697              :     }
     698          232 :     int step = tpc2degree(tpc, key);
     699          232 :     static const String stepNames = u"1234567";
     700          232 :     assert(step < (int)stepNames.size());
     701          232 :     stepName = stepNames.at(step);
     702              : 
     703          232 :     int alter = tpc2alterByKey(tpc, key);
     704          232 :     int accidentalNo = std::abs(alter);
     705          232 :     accName = String();
     706          232 :     bool flat = alter < 0;
     707          342 :     for (int i = 0; i < accidentalNo; i++) {
     708          110 :         accName.append(flat ? u"b" : u"#");
     709              :     }
     710              : }
     711              : 
     712           44 : String tpc2Function(int tpc, Key key)
     713              : {
     714           44 :     String accStr;
     715           44 :     String stepStr;
     716           44 :     tpc2Function(tpc, key, accStr, stepStr);
     717           88 :     return accStr + stepStr;
     718           44 : }
     719              : 
     720              : //----------------------------------------------
     721              : //   function2Tpc
     722              : ///   might be temporary, just used to parse nashville notation now
     723              : ///
     724              : //----------------------------------------------
     725              : 
     726           20 : int function2Tpc(const String& s, Key key)
     727              : {
     728           20 :     size_t idx = 0;
     729           40 :     return function2Tpc(s, key, idx);
     730              : }
     731              : 
     732           20 : int function2Tpc(const String& s, Key key, size_t& idx)
     733              : {
     734              :     // TODO - PHV: allow for alternate spellings
     735           20 :     int alter = 0;
     736              :     int step;
     737              : 
     738           20 :     if (!s.isEmpty() && s.at(0).isDigit()) {
     739           18 :         step = s.at(0).digitValue();
     740           18 :         idx = 1;
     741            2 :     } else if (s.size() > 1) {
     742            2 :         constexpr int NUM_LEN = 1;
     743            2 :         size_t accIdx = s.size() - NUM_LEN;
     744            2 :         String acc = s.left(accIdx);
     745            2 :         String num = s.right(NUM_LEN);
     746            2 :         if (num.size() > NUM_LEN || num.empty() || !num.at(0).isDigit()) {
     747            0 :             return Tpc::TPC_INVALID;
     748              :         }
     749              : 
     750            2 :         step = num.at(0).digitValue();
     751            2 :         idx = NUM_LEN;
     752            2 :         if (acc.startsWith(u"bb")) {
     753            0 :             alter = -2;
     754            0 :             idx += 2;
     755            2 :         } else if (acc.startsWith(u"b")) {
     756            2 :             alter = -1;
     757            2 :             idx += 1;
     758            0 :         } else if (acc.startsWith(u"#")) {
     759            0 :             alter = 1;
     760            0 :             idx += 1;
     761            0 :         } else if (acc.startsWith(u"##")) {
     762            0 :             alter = 2;
     763            0 :             idx += 2;
     764              :         }
     765            2 :     } else {
     766            0 :         return Tpc::TPC_INVALID;
     767              :     }
     768              : 
     769           20 :     int keyTpc = int(key) + 14;   //tpc of key (ex. F# major would be Tpc::F_S)
     770           20 :     int tpc = tpcInterval(keyTpc, step, alter);
     771           20 :     return tpc;
     772              : }
     773              : 
     774              : //---------------------------------------------------------
     775              : //   convertNote
     776              : //    convert something like "C#" into tpc 21
     777              : //---------------------------------------------------------
     778         9989 : int convertNote(const String& s, NoteSpellingType noteSpelling, NoteCaseType& noteCase, size_t& idx)
     779              : {
     780         9989 :     bool useGerman = false;
     781         9989 :     bool useSolfeggio = false;
     782              :     static const int spellings[] = {
     783              :         // bb  b   -   #  ##
     784              :         0,  7, 14, 21, 28,      // C
     785              :         2,  9, 16, 23, 30,      // D
     786              :         4, 11, 18, 25, 32,      // E
     787              :         -1,  6, 13, 20, 27,     // F
     788              :         1,  8, 15, 22, 29,      // G
     789              :         3, 10, 17, 24, 31,      // A
     790              :         5, 12, 19, 26, 33,      // B
     791              :     };
     792         9989 :     if (s.empty()) {
     793            0 :         return Tpc::TPC_INVALID;
     794              :     }
     795         9989 :     noteCase = s.at(0).isLower() ? NoteCaseType::LOWER : NoteCaseType::CAPITAL;
     796              :     int acci;
     797         9989 :     switch (noteSpelling) {
     798            7 :     case NoteSpellingType::SOLFEGGIO:
     799              :     case NoteSpellingType::FRENCH:
     800            7 :         useSolfeggio = true;
     801            7 :         if (s.startsWith(u"sol", muse::CaseInsensitive)) {
     802            0 :             acci = 3;
     803              :         } else {
     804            7 :             acci = 2;
     805              :         }
     806            7 :         break;
     807            0 :     case NoteSpellingType::GERMAN:
     808              :     case NoteSpellingType::GERMAN_PURE:
     809            0 :         useGerman = true;
     810              :     // fall through
     811         9982 :     default:
     812         9982 :         acci = 1;
     813              :     }
     814         9989 :     idx = acci;
     815         9989 :     int alter = 0;
     816         9989 :     size_t n = s.size();
     817        19978 :     String acc = s.right(n - acci);
     818         9989 :     if (!acc.empty()) {
     819         7793 :         if (acc.startsWith(u"bb")) {
     820            1 :             alter = -2;
     821            1 :             idx += 2;
     822         7792 :         } else if (acc.startsWith(u"b")) {
     823         2675 :             alter = -1;
     824         2675 :             idx += 1;
     825         5117 :         } else if (useGerman && acc.startsWith(u"eses")) {
     826            0 :             alter = -2;
     827            0 :             idx += 4;
     828         5117 :         } else if (useGerman && (acc.startsWith(u"ses") || acc.startsWith(u"sas"))) {
     829            0 :             alter = -2;
     830            0 :             idx += 3;
     831         5117 :         } else if (useGerman && acc.startsWith(u"es")) {
     832            0 :             alter = -1;
     833            0 :             idx += 2;
     834         5117 :         } else if (useGerman && acc.startsWith(u"s") && !acc.startsWith(u"su")) {
     835            0 :             alter = -1;
     836            0 :             idx += 1;
     837         5117 :         } else if (acc.startsWith(u"##")) {
     838            0 :             alter = 2;
     839            0 :             idx += 2;
     840         5117 :         } else if (acc.startsWith(u"x")) {
     841            0 :             alter = 2;
     842            0 :             idx += 1;
     843         5117 :         } else if (acc.startsWith(u"#")) {
     844         1512 :             alter = 1;
     845         1512 :             idx += 1;
     846         3605 :         } else if (useGerman && acc.startsWith(u"isis")) {
     847            0 :             alter = 2;
     848            0 :             idx += 4;
     849         3605 :         } else if (useGerman && acc.startsWith(u"is")) {
     850            0 :             alter = 1;
     851            0 :             idx += 2;
     852              :         }
     853              :     }
     854              :     int r;
     855         9989 :     if (useGerman) {
     856            0 :         switch (s.at(0).toLower().toAscii()) {
     857            0 :         case 'c':   r = 0;
     858            0 :             break;
     859            0 :         case 'd':   r = 1;
     860            0 :             break;
     861            0 :         case 'e':   r = 2;
     862            0 :             break;
     863            0 :         case 'f':   r = 3;
     864            0 :             break;
     865            0 :         case 'g':   r = 4;
     866            0 :             break;
     867            0 :         case 'a':   r = 5;
     868            0 :             break;
     869            0 :         case 'h':   r = 6;
     870            0 :             break;
     871            0 :         case 'b':
     872            0 :             if (alter && alter != -1) {
     873            0 :                 return Tpc::TPC_INVALID;
     874              :             }
     875            0 :             r = 6;
     876            0 :             alter = -1;
     877            0 :             break;
     878            0 :         default:
     879            0 :             return Tpc::TPC_INVALID;
     880              :         }
     881         9989 :     } else if (useSolfeggio) {
     882            7 :         if (s.size() < 2) {
     883            1 :             return Tpc::TPC_INVALID;
     884              :         }
     885            7 :         if (s.at(1).isUpper()) {
     886            1 :             noteCase = NoteCaseType::UPPER;
     887              :         }
     888            7 :         String ss = s.toLower().left(2);
     889            7 :         if (ss == "do") {
     890            0 :             r = 0;
     891            7 :         } else if (ss == "re" || ss == "ré") {
     892            0 :             r = 1;
     893            7 :         } else if (ss == "mi") {
     894            0 :             r = 2;
     895            7 :         } else if (ss == "fa") {
     896            0 :             r = 3;
     897            7 :         } else if (ss == "so") {    // sol, but only check first 2 characters
     898            0 :             r = 4;
     899            7 :         } else if (ss == "la") {
     900            6 :             r = 5;
     901            1 :         } else if (ss == "si") {
     902            0 :             r = 6;
     903              :         } else {
     904            1 :             return Tpc::TPC_INVALID;
     905              :         }
     906            7 :     } else {
     907         9982 :         switch (s.at(0).toLower().toAscii()) {
     908         1343 :         case 'c':   r = 0;
     909         1343 :             break;
     910         1431 :         case 'd':   r = 1;
     911         1431 :             break;
     912         1423 :         case 'e':   r = 2;
     913         1423 :             break;
     914         1395 :         case 'f':   r = 3;
     915         1395 :             break;
     916         1504 :         case 'g':   r = 4;
     917         1504 :             break;
     918         1478 :         case 'a':   r = 5;
     919         1478 :             break;
     920         1386 :         case 'b':   r = 6;
     921         1386 :             break;
     922           22 :         default:    return Tpc::TPC_INVALID;
     923              :         }
     924              :     }
     925         9966 :     r = spellings[r * 5 + alter + 2];
     926         9966 :     return r;
     927              : }
     928              : 
     929        12342 : int clampEnharmonic(int tpc, bool useDoubleSharpsFlats)
     930              : {
     931        12342 :     while (tpc > (useDoubleSharpsFlats ? Tpc::TPC_MAX : Tpc::TPC_B_S)) {
     932            0 :         tpc -= TPC_DELTA_ENHARMONIC;
     933              :     }
     934        12360 :     while (tpc < (useDoubleSharpsFlats ? Tpc::TPC_MIN : Tpc::TPC_F_B)) {
     935           18 :         tpc += TPC_DELTA_ENHARMONIC;
     936              :     }
     937        12342 :     return tpc;
     938              : }
     939              : 
     940        22419 : int clampPitch(int pitch)
     941              : {
     942        22419 :     return std::clamp(pitch, MIN_PITCH, MAX_PITCH);
     943              : }
     944              : 
     945         3166 : int clampPitchOctaved(int pitch)
     946              : {
     947         3166 :     while (pitch > MAX_PITCH) {
     948            0 :         pitch -= PITCH_DELTA_OCTAVE;
     949              :     }
     950         3166 :     while (pitch < MIN_PITCH) {
     951            0 :         pitch += PITCH_DELTA_OCTAVE;
     952              :     }
     953         3166 :     return pitch;
     954              : }
     955              : 
     956         6928 : Key clampKey(Key key, PreferSharpFlat prefer)
     957              : {
     958         6928 :     Key smallest = Key::G_B;
     959         6928 :     Key largest = Key::F_S;
     960              : 
     961         6928 :     if (prefer != PreferSharpFlat::AUTO) {
     962         6116 :         smallest = (prefer == PreferSharpFlat::SHARPS) ? Key::A_B : Key::MIN;
     963         6116 :         largest  = (prefer == PreferSharpFlat::FLATS) ? Key::E : Key::MAX;
     964              :     }
     965              : 
     966         6946 :     while (key < smallest) {
     967           18 :         key += Key::DELTA_ENHARMONIC;
     968              :     }
     969         6990 :     while (key > largest) {
     970           62 :         key -= Key::DELTA_ENHARMONIC;
     971              :     }
     972              : 
     973         6928 :     return key;
     974              : }
     975              : 
     976         8040 : int key2Tpc(Key key)
     977              : {
     978         8040 :     return (int)key + KEY_TO_TPC_OFFSET;
     979              : }
     980              : 
     981         4280 : Key tpc2Key(int tpc)
     982              : {
     983         4280 :     return Key(tpc - KEY_TO_TPC_OFFSET);
     984              : }
     985              : }
        

Generated by: LCOV version 2.0-1