MuseScore  3.4
Music composition and notation
key.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2002-2014 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __KEY__H__
14 #define __KEY__H__
15 
16 namespace Ms {
17 
18 class XmlWriter;
19 class Score;
20 class XmlReader;
21 enum class AccidentalVal : signed char;
22 enum class ClefType : signed char;
23 
24 //---------------------------------------------------------
25 // Key
26 //---------------------------------------------------------
27 
28 enum class Key {
29  C_B = -7,
30  G_B,
31  D_B,
32  A_B,
33  E_B,
34  B_B,
35  F,
36  C, // == 0
37  G,
38  D,
39  A,
40  E,
41  B,
42  F_S,
43  C_S,
44  MIN = Key::C_B,
45  MAX = Key::C_S,
46  INVALID = Key::MIN - 1,
47  NUM_OF = Key::MAX - Key::MIN + 1,
48  DELTA_ENHARMONIC = 12
49  };
50 
51 //---------------------------------------------------------
52 // KeyMode
53 //---------------------------------------------------------
54 
55 enum class KeyMode {
56  UNKNOWN = -1,
57  NONE,
58  MAJOR,
59  MINOR,
60  DORIAN,
61  PHRYGIAN,
62  LYDIAN,
63  MIXOLYDIAN,
64  AEOLIAN,
65  IONIAN,
66  LOCRIAN
67  };
68 
69 static inline bool operator< (Key a, Key b) { return static_cast<int>(a) < static_cast<int>(b); }
70 static inline bool operator> (Key a, Key b) { return static_cast<int>(a) > static_cast<int>(b); }
71 static inline bool operator> (Key a, int b) { return static_cast<int>(a) > b; }
72 static inline bool operator< (Key a, int b) { return static_cast<int>(a) < b; }
73 static inline bool operator== (const Key a, const Key b) { return int(a) == int(b); }
74 static inline bool operator!= (const Key a, const Key b) { return static_cast<int>(a) != static_cast<int>(b); }
75 static inline Key operator+= (Key& a, const Key& b) { return a = Key(static_cast<int>(a) + static_cast<int>(b)); }
76 static inline Key operator-= (Key& a, const Key& b) { return a = Key(static_cast<int>(a) - static_cast<int>(b)); }
77 
78 enum class SymId;
79 
80 //---------------------------------------------------------
81 // KeySym
82 // position of one symbol in KeySig
83 //---------------------------------------------------------
84 
85 struct KeySym {
87  QPointF spos; // position in spatium units
88  QPointF pos; // actual pixel position on screen (set by layout)
89  };
90 
91 //---------------------------------------------------------
92 // KeySigEvent
93 //---------------------------------------------------------
94 
95 class KeySigEvent {
96  Key _key { Key::INVALID }; // -7 -> +7
98  bool _custom { false };
99  QList<KeySym> _keySymbols;
100 
101  void enforceLimits();
102 
103  public:
105  KeySigEvent(const KeySigEvent&);
106 
107  bool operator==(const KeySigEvent& e) const;
108  bool operator!=(const KeySigEvent& e) const { return !(*this == e); }
109 
110  void setKey(Key v);
111  void print() const;
112 
113  Key key() const { return _key; }
114  KeyMode mode() const { return _mode; }
115  void setMode(KeyMode m) { _mode = m; }
116  bool custom() const { return _custom; }
117  void setCustom(bool val) { _custom = val; _key = Key::C; }
118  bool isValid() const { return _key != Key::INVALID; }
119  bool isAtonal() const { return _mode == KeyMode::NONE; }
120  void initFromSubtype(int); // for backward compatibility
121  QList<KeySym>& keySymbols() { return _keySymbols; }
122  const QList<KeySym>& keySymbols() const { return _keySymbols; }
123  };
124 
125 //---------------------------------------------------------
126 // AccidentalState
128 //---------------------------------------------------------
129 
130 static const int TIE_CONTEXT = 0x10;
131 static const int MIN_ACC_STATE = 0;
132 static const int MAX_ACC_STATE = 75;
133 
135  uchar state[MAX_ACC_STATE]; // (0 -- 4) | TIE_CONTEXT
136 
137  public:
139  void init(Key key);
140  void init(const KeySigEvent&, ClefType);
141  AccidentalVal accidentalVal(int line, bool &error) const;
142  AccidentalVal accidentalVal(int line) const;
143  bool tieContext(int line) const;
144  void setAccidentalVal(int line, AccidentalVal val, bool tieContext = false);
145  };
146 
147 struct Interval;
148 extern Key transposeKey(Key oldKey, const Interval&);
149 
150 
151 } // namespace Ms
152 #endif
153 
AccidentalVal
Definition: mscore.h:147
Key
Definition: key.h:28
KeyMode
Definition: key.h:55
QList< KeySym > _keySymbols
Definition: key.h:99
SymId
Definition: sym.h:30
void setMode(KeyMode m)
Definition: key.h:115
AccidentalState()
Definition: key.h:138
const QList< KeySym > & keySymbols() const
Definition: key.h:122
Definition: key.h:134
bool operator!=(const KeySigEvent &e) const
Definition: key.h:108
QPointF pos
Definition: key.h:88
Definition: key.h:85
KeyMode mode() const
Definition: key.h:114
Definition: key.h:95
Definition: interval.h:22
bool isValid() const
Definition: key.h:118
Definition: aeolus.cpp:26
Key key() const
Definition: key.h:113
Definition: xmlwriter.h:26
KeySigEvent()
Definition: key.h:104
Key transposeKey(Key key, const Interval &interval)
Definition: key.cpp:108
Definition: xmlreader.h:28
bool custom() const
Definition: key.h:116
bool isAtonal() const
Definition: key.h:119
ClefType
Definition: clef.h:37
SymId sym
Definition: key.h:86
QPointF spos
Definition: key.h:87
void setCustom(bool val)
Definition: key.h:117
QList< KeySym > & keySymbols()
Definition: key.h:121