LCOV - code coverage report
Current view: top level - src/engraving/dom - key.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 84.6 % 143 121
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              : #include "key.h"
      24              : 
      25              : #include "accidental.h"
      26              : #include "part.h"
      27              : #include "pitchspelling.h"
      28              : #include "score.h"
      29              : #include "utils.h"
      30              : 
      31              : #include "log.h"
      32              : 
      33              : using namespace mu;
      34              : 
      35              : namespace mu::engraving {
      36              : // transposition table for microtonal accidentals
      37              : const SymId accTable[] = {
      38              :     SymId::noSym,
      39              :     // standard accidentals
      40              :     SymId::accidentalTripleFlat,
      41              :     SymId::accidentalDoubleFlat,
      42              :     SymId::accidentalFlat,
      43              :     SymId::accidentalNatural,
      44              :     SymId::accidentalSharp,
      45              :     SymId::accidentalDoubleSharp,
      46              :     SymId::accidentalTripleSharp,
      47              :     SymId::noSym,
      48              :     //  natural sharp
      49              :     SymId::accidentalNatural,
      50              :     SymId::accidentalNaturalSharp,
      51              :     SymId::accidentalSharp,
      52              :     SymId::noSym,
      53              :     //  natural flat
      54              :     SymId::accidentalDoubleFlat,
      55              :     SymId::accidentalNaturalFlat,
      56              :     SymId::accidentalNatural,
      57              :     SymId::noSym,
      58              :     //  natural sharp sharp
      59              :     SymId::accidentalSharp,
      60              :     SymId::accidentalSharpSharp,
      61              :     SymId::accidentalTripleSharp,
      62              :     SymId::noSym,
      63              :     // Gould quarter tone
      64              :     //  arrow down
      65              :     SymId::accidentalFiveQuarterTonesFlatArrowDown,
      66              :     SymId::accidentalThreeQuarterTonesFlatArrowDown,
      67              :     SymId::accidentalQuarterToneFlatNaturalArrowDown,
      68              :     SymId::accidentalQuarterToneSharpArrowDown,
      69              :     SymId::accidentalThreeQuarterTonesSharpArrowDown,
      70              :     SymId::accidentalFiveQuarterTonesFlatArrowDown,
      71              :     SymId::noSym,
      72              :     //  arrow up
      73              :     SymId::accidentalThreeQuarterTonesFlatArrowUp,
      74              :     SymId::accidentalQuarterToneFlatArrowUp,
      75              :     SymId::accidentalQuarterToneSharpNaturalArrowUp,
      76              :     SymId::accidentalThreeQuarterTonesSharpArrowUp,
      77              :     SymId::accidentalFiveQuarterTonesSharpArrowUp,
      78              :     SymId::noSym,
      79              :     // Stein-Zimmermann
      80              :     //  basic
      81              :     SymId::accidentalThreeQuarterTonesFlatZimmermann,
      82              :     SymId::accidentalQuarterToneFlatStein,
      83              :     SymId::accidentalQuarterToneSharpStein,
      84              :     SymId::accidentalThreeQuarterTonesSharpStein,
      85              :     SymId::noSym,
      86              :     //  narrow (as variant of above)
      87              :     SymId::accidentalNarrowReversedFlatAndFlat,
      88              :     SymId::accidentalNarrowReversedFlat,
      89              :     SymId::accidentalQuarterToneSharpStein,
      90              :     // Extended Helmholtz-Ellis (just intonation)
      91              :     //  one syntonic comma down arrow
      92              :     SymId::accidentalDoubleFlatOneArrowDown,
      93              :     SymId::accidentalFlatOneArrowDown,
      94              :     SymId::accidentalNaturalOneArrowDown,
      95              :     SymId::accidentalSharpOneArrowDown,
      96              :     SymId::accidentalDoubleSharpOneArrowDown,
      97              :     SymId::noSym,
      98              :     //   one syntonic comma up arrow
      99              :     SymId::accidentalDoubleFlatOneArrowUp,
     100              :     SymId::accidentalFlatOneArrowUp,
     101              :     SymId::accidentalNaturalOneArrowUp,
     102              :     SymId::accidentalSharpOneArrowUp,
     103              :     SymId::accidentalDoubleSharpOneArrowUp,
     104              :     SymId::noSym,
     105              :     //   two syntonic commas down
     106              :     SymId::accidentalDoubleFlatTwoArrowsDown,
     107              :     SymId::accidentalFlatTwoArrowsDown,
     108              :     SymId::accidentalNaturalTwoArrowsDown,
     109              :     SymId::accidentalSharpTwoArrowsDown,
     110              :     SymId::accidentalDoubleSharpTwoArrowsDown,
     111              :     SymId::noSym,
     112              :     //   two syntonic commas up
     113              :     SymId::accidentalDoubleFlatTwoArrowsUp,
     114              :     SymId::accidentalFlatTwoArrowsUp,
     115              :     SymId::accidentalNaturalTwoArrowsUp,
     116              :     SymId::accidentalSharpTwoArrowsUp,
     117              :     SymId::accidentalDoubleSharpTwoArrowsUp,
     118              :     SymId::noSym,
     119              :     //   three syntonic commas down
     120              :     SymId::accidentalDoubleFlatThreeArrowsDown,
     121              :     SymId::accidentalFlatThreeArrowsDown,
     122              :     SymId::accidentalNaturalThreeArrowsDown,
     123              :     SymId::accidentalSharpThreeArrowsDown,
     124              :     SymId::accidentalDoubleSharpThreeArrowsDown,
     125              :     SymId::noSym,
     126              :     //   three syntonic commas up
     127              :     SymId::accidentalDoubleFlatThreeArrowsUp,
     128              :     SymId::accidentalFlatThreeArrowsUp,
     129              :     SymId::accidentalNaturalThreeArrowsUp,
     130              :     SymId::accidentalSharpThreeArrowsUp,
     131              :     SymId::accidentalDoubleSharpThreeArrowsUp,
     132              :     SymId::noSym,
     133              :     //   equal tempered semitone
     134              :     SymId::accidentalDoubleFlatEqualTempered,
     135              :     SymId::accidentalFlatEqualTempered,
     136              :     SymId::accidentalNaturalEqualTempered,
     137              :     SymId::accidentalSharpEqualTempered,
     138              :     SymId::accidentalDoubleSharpEqualTempered,
     139              :     SymId::noSym
     140              : };
     141              : 
     142              : //---------------------------------------------------------
     143              : //   enforceLimits - ensure _key
     144              : //   is within acceptable limits (-7 .. +7).
     145              : //   see KeySig::layout()
     146              : //---------------------------------------------------------
     147              : 
     148      1950625 : void KeySigEvent::enforceLimits(bool transposing)
     149              : {
     150      1950625 :     if (m_key < Key::MIN) {
     151            0 :         m_key = Key::MIN;
     152            0 :         LOGD("key < -7");
     153      1950625 :     } else if (m_key > Key::MAX) {
     154            4 :         m_key = Key::MAX;
     155            4 :         LOGD("key > 7");
     156              :     }
     157              : 
     158      1950625 :     if (transposing) {
     159         1669 :         return;
     160              :     }
     161              : 
     162      1948956 :     if (m_concertKey < Key::MIN) {
     163            0 :         m_concertKey = Key::MIN;
     164            0 :         LOGD("key < -7");
     165      1948956 :     } else if (m_concertKey > Key::MAX) {
     166            2 :         m_concertKey = Key::MAX;
     167            2 :         LOGD("key > 7");
     168              :     }
     169              : }
     170              : 
     171              : //---------------------------------------------------------
     172              : //   print
     173              : //---------------------------------------------------------
     174              : 
     175            0 : void KeySigEvent::print() const
     176              : {
     177            0 :     LOGD("<KeySigEvent: ");
     178            0 :     if (!isValid()) {
     179            0 :         LOGD("invalid>");
     180              :     } else {
     181            0 :         if (isAtonal()) {
     182            0 :             LOGD("atonal>");
     183            0 :         } else if (custom()) {
     184            0 :             LOGD("custom>");
     185              :         } else {
     186            0 :             LOGD("accidental %d>", int(m_key));
     187              :         }
     188              :     }
     189            0 : }
     190              : 
     191              : //---------------------------------------------------------
     192              : //   setKey
     193              : //---------------------------------------------------------
     194              : 
     195         1669 : void KeySigEvent::setKey(Key v)
     196              : {
     197         1669 :     m_key = v;
     198         1669 :     enforceLimits(true);
     199         1669 : }
     200              : 
     201              : //---------------------------------------------------------
     202              : //   setConcertKey
     203              : //---------------------------------------------------------
     204              : 
     205      1948952 : void KeySigEvent::setConcertKey(Key v)
     206              : {
     207      1948952 :     m_key = v;
     208      1948952 :     m_concertKey = v;
     209      1948952 :     enforceLimits();
     210      1948952 : }
     211              : 
     212              : //---------------------------------------------------------
     213              : //   setCustom
     214              : //---------------------------------------------------------
     215              : 
     216           31 : void KeySigEvent::setCustom(bool val)
     217              : {
     218           31 :     m_custom = val;
     219           31 :     if (m_key == Key::INVALID) {
     220           21 :         m_concertKey = Key::C;
     221           21 :         m_key = Key::C;
     222              :     }
     223           31 : }
     224              : 
     225              : //---------------------------------------------------------
     226              : //   KeySigEvent::operator==
     227              : //---------------------------------------------------------
     228              : 
     229        89458 : bool KeySigEvent::operator==(const KeySigEvent& e) const
     230              : {
     231        89458 :     if (e.m_custom != m_custom || e.m_mode != m_mode || e.m_forInstrumentChange != m_forInstrumentChange) {
     232           62 :         return false;
     233              :     }
     234        89394 :     if (e.m_jianpuNumbering != m_jianpuNumbering || e.m_jianpuTonicKey != m_jianpuTonicKey
     235       178790 :         || e.m_jianpuTonicMode != m_jianpuTonicMode) {
     236            4 :         return false;
     237              :     }
     238        89392 :     if (m_custom && !isAtonal()) {
     239           60 :         if (e.m_customKeyDefs.size() != m_customKeyDefs.size()) {
     240            0 :             return false;
     241              :         }
     242           68 :         for (size_t i = 0; i < m_customKeyDefs.size(); ++i) {
     243              :             // check note and sym, don't care xAlt and octAlt
     244           60 :             if (e.m_customKeyDefs[i].degree != m_customKeyDefs[i].degree || e.m_customKeyDefs[i].sym != m_customKeyDefs[i].sym) {
     245           52 :                 return false;
     246              :             }
     247              :             // TODO: position matters // does it?
     248              :         }
     249            8 :         return true;
     250              :     }
     251        89332 :     return e.m_concertKey == m_concertKey && e.m_key == m_key;
     252              : }
     253              : 
     254              : //---------------------------------------------------------
     255              : //   calculateInterval
     256              : //    Calculates the interval to move from one key to another
     257              : //---------------------------------------------------------
     258              : 
     259           25 : Interval calculateInterval(Key key1, Key key2)
     260              : {
     261           25 :     int chromatic = 7 * ((int)key2 - (int)key1);
     262           25 :     chromatic = chromatic % 12;
     263           25 :     if (chromatic < 0) {
     264            1 :         chromatic += 12;
     265              :     }
     266           25 :     return Interval(chromatic);
     267              : }
     268              : 
     269              : //---------------------------------------------------------
     270              : //   initFromSubtype
     271              : //    for backward compatibility
     272              : //---------------------------------------------------------
     273              : 
     274            4 : void KeySigEvent::initFromSubtype(int st)
     275              : {
     276              :     //anatoly-os: legacy code. I don't understand why it is so overcomplicated.
     277              :     //Did refactoring to avoid exception on MSVC, but left the same logic.
     278              :     struct {
     279              :         int _key : 4;
     280              :         int _naturalType : 4;
     281              :         unsigned _customType : 16;
     282              :         bool _custom : 1;
     283              :         bool _invalid : 1;
     284              :     } a;
     285              : 
     286            4 :     a._key         = (st & 0xf);
     287            4 :     a._naturalType = (st >> 4) & 0xf;
     288            4 :     a._customType  = (st >> 8) & 0xffff;
     289            4 :     a._custom      = (st >> 24) & 0x1;
     290            4 :     a._invalid     = (st >> 25) & 0x1;
     291              :     //end of legacy code
     292              : 
     293            4 :     m_key            = Key(a._key);
     294            4 :     m_concertKey     = m_key;
     295              : //      _customType     = a._customType;
     296            4 :     m_custom         = a._custom;
     297            4 :     if (a._invalid) {
     298            0 :         m_key = Key::INVALID;
     299              :     }
     300            4 :     enforceLimits();
     301            4 : }
     302              : 
     303              : //---------------------------------------------------------
     304              : //   accidentalVal
     305              : //---------------------------------------------------------
     306              : 
     307        59948 : AccidentalVal AccidentalState::accidentalVal(int line, bool& error) const
     308              : {
     309        59948 :     if (line < MIN_ACC_STATE || line >= MAX_ACC_STATE) {
     310           17 :         error = true;
     311           17 :         return AccidentalVal::NATURAL;
     312              :     }
     313        59931 :     return AccidentalVal((m_state[line] & 0x0f) + int(AccidentalVal::MIN));
     314              : }
     315              : 
     316              : //---------------------------------------------------------
     317              : //   init
     318              : //    preset lines list with accidentals for given key
     319              : //---------------------------------------------------------
     320              : 
     321              : static const int ACC_STATE_NATURAL = int(AccidentalVal::NATURAL) - int(AccidentalVal::MIN);
     322              : static const int ACC_STATE_FLAT = int(AccidentalVal::FLAT) - int(AccidentalVal::MIN);
     323              : static const int ACC_STATE_SHARP = int(AccidentalVal::SHARP) - int(AccidentalVal::MIN);
     324              : 
     325        60046 : void AccidentalState::init(Key key)
     326              : {
     327        60046 :     memset(m_state, ACC_STATE_NATURAL, MAX_ACC_STATE);
     328              :     // The numerical value of key tells us the number of sharps (or flats, if negative) in the key signature
     329        60046 :     if (key > 0 && key <= Key::MAX) {
     330        12857 :         for (int i = 0; i < int(key); ++i) {
     331              :             // First F#, then C#, then G#, etc.
     332         9186 :             int idx = tpc2step(Tpc::TPC_F_S + i);
     333       109844 :             for (int octave = 0; octave < (11 * 7); octave += 7) {
     334       101046 :                 int j = idx + octave;
     335       101046 :                 if (j >= MAX_ACC_STATE) {
     336          388 :                     break;
     337              :                 }
     338       100658 :                 m_state[j] = ACC_STATE_SHARP;
     339              :             }
     340              :         }
     341        56375 :     } else if (key < 0 && key >= Key::MIN) {
     342         6766 :         for (int i = 0; i > int(key); --i) {
     343              :             // First Bb, then Eb, then Ab, etc.
     344         4891 :             int idx = tpc2step(Tpc::TPC_B_B + i);
     345        56033 :             for (int octave = 0; octave < (11 * 7); octave += 7) {
     346        53801 :                 int j = idx + octave;
     347        53801 :                 if (j >= MAX_ACC_STATE) {
     348         2659 :                     break;
     349              :                 }
     350        51142 :                 m_state[j] = ACC_STATE_FLAT;
     351              :             }
     352              :         }
     353              :     }
     354        60046 : }
     355              : 
     356              : //---------------------------------------------------------
     357              : //   init
     358              : //---------------------------------------------------------
     359              : 
     360        57304 : void AccidentalState::init(const KeySigEvent& keySig)
     361              : {
     362        57304 :     init(keySig.key());
     363        57304 :     if (keySig.custom()) {
     364          230 :         for (const CustDef& d : keySig.customKeyDefs()) {
     365          100 :             SymId sym = keySig.symInKey(d.sym, d.degree);
     366          100 :             int degree = keySig.degInKey(d.degree);
     367          100 :             AccidentalVal a = sym2accidentalVal(sym);
     368         1176 :             for (int octave = 0; octave < (11 * 7); octave += 7) {
     369         1100 :                 int i = degree + octave;
     370         1100 :                 if (i >= MAX_ACC_STATE) {
     371           24 :                     break;
     372              :                 }
     373         1076 :                 m_state[i] = static_cast<uint8_t>(int(a) - int(AccidentalVal::MIN));
     374              :             }
     375              :         }
     376              :     }
     377        57304 :     m_forceRestateAccidental.fill(false);
     378        57304 : }
     379              : 
     380              : //---------------------------------------------------------
     381              : //   accidentalVal
     382              : //---------------------------------------------------------
     383              : 
     384          693 : AccidentalVal AccidentalState::accidentalVal(int line) const
     385              : {
     386          693 :     assert(line >= MIN_ACC_STATE && line < MAX_ACC_STATE);
     387          693 :     return AccidentalVal((m_state[line] & 0x0f) + int(AccidentalVal::MIN));
     388              : }
     389              : 
     390        55859 : bool AccidentalState::forceRestateAccidental(int line) const
     391              : {
     392        55859 :     assert(line >= MIN_ACC_STATE && line < MAX_ACC_STATE);
     393        55859 :     return m_forceRestateAccidental[line];
     394              : }
     395              : 
     396              : //---------------------------------------------------------
     397              : //   tieContext
     398              : //---------------------------------------------------------
     399              : 
     400        55873 : bool AccidentalState::tieContext(int line) const
     401              : {
     402        55873 :     assert(line >= MIN_ACC_STATE && line < MAX_ACC_STATE);
     403        55873 :     return m_state[line] & TIE_CONTEXT;
     404              : }
     405              : 
     406              : //---------------------------------------------------------
     407              : //   setAccidentalVal
     408              : //---------------------------------------------------------
     409              : 
     410         4453 : void AccidentalState::setAccidentalVal(int line, AccidentalVal val, bool tieContext)
     411              : {
     412         4453 :     assert(line >= MIN_ACC_STATE && line < MAX_ACC_STATE);
     413              :     // casts needed to work around a bug in Xcode 4.2 on Mac, see #25910
     414         4453 :     assert(int(val) >= int(AccidentalVal::MIN) && int(val) <= int(AccidentalVal::MAX));
     415         4453 :     m_state[line] = (int(val) - int(AccidentalVal::MIN)) | (tieContext ? TIE_CONTEXT : 0);
     416         4453 : }
     417              : 
     418        59765 : void AccidentalState::setForceRestateAccidental(int line, bool forceRestate)
     419              : {
     420        59765 :     assert(line >= MIN_ACC_STATE && line < MAX_ACC_STATE);
     421        59765 :     m_forceRestateAccidental[line] = forceRestate;
     422        59765 : }
     423              : 
     424              : //---------------------------------------------------------
     425              : //   degInKey
     426              : //    degree to "absolute degree"
     427              : //    first degree in F major is F: 0 -> 3, ...
     428              : //---------------------------------------------------------
     429              : 
     430          396 : int KeySigEvent::degInKey(int degree) const
     431              : {
     432          396 :     return (degree + ((static_cast<int>(key()) + 7) * 4)) % 7;
     433              : }
     434              : 
     435              : //---------------------------------------------------------
     436              : //   symInKey
     437              : //---------------------------------------------------------
     438              : 
     439          198 : SymId KeySigEvent::symInKey(SymId sym, int degree) const
     440              : {
     441          198 :     degree = degInKey(degree);
     442          198 :     int keyval = static_cast<int>(key());
     443          594 :     int accIndex = std::distance(std::begin(accTable), std::find(std::begin(accTable), std::end(accTable), sym));
     444              : 
     445              :     // non transposed key
     446          198 :     if (keyval == 0 || std::abs(keyval) > 7) {
     447          198 :         return sym;
     448              :     }
     449              : 
     450              :     // sym is not transposable (it is not in table)
     451            0 :     if (!(accIndex < std::end(accTable) - std::begin(accTable))) {
     452            0 :         return SymId::noSym;
     453              :     }
     454              : 
     455            0 :     for (int i = 1; i <= std::abs(keyval); ++i) {
     456            0 :         if ((degree * 2 + 2) % 7 == (keyval < 0 ? 8 - i : i) % 7) {
     457            0 :             accIndex += keyval < 0 ? -1 : 1;
     458              :         }
     459              :     }
     460            0 :     return accTable[accIndex];
     461              : }
     462              : }
        

Generated by: LCOV version 2.0-1