Skip to content

Commit

Permalink
stronger typing for key
Browse files Browse the repository at this point in the history
  • Loading branch information
wschweer committed Jun 20, 2014
1 parent deca042 commit b4066a7
Show file tree
Hide file tree
Showing 47 changed files with 342 additions and 283 deletions.
8 changes: 4 additions & 4 deletions libmscore/ambitus.cpp
Expand Up @@ -245,7 +245,7 @@ void Ambitus::layout()
int bottomLine, topLine;
ClefType clf;
qreal headWdt = headWidth();
int key;
Key key;
qreal lineDist;
int numOfLines;
Segment* segm = segment();
Expand Down Expand Up @@ -275,7 +275,7 @@ void Ambitus::layout()
if (stf)
key = stf->key(segm->tick());
else
key = int(Key::C);
key = Key::C;

// top note head
if (_topPitch == INVALID_PITCH || _topTpc == Tpc::TPC_INVALID)
Expand All @@ -287,7 +287,7 @@ void Ambitus::layout()
// compute accidental
Accidental::AccidentalType accidType;
// if (13 <= (tpc - key) <= 19) there is no accidental)
if (_topTpc - key >= 13 && _topTpc - key <= 19)
if (_topTpc - int(key) >= 13 && _topTpc - int(key) <= 19)
accidType = Accidental::AccidentalType::NONE;
else {
AccidentalVal accidVal = AccidentalVal( (_topTpc - Tpc::TPC_MIN) / TPC_DELTA_SEMITONE - 2 );
Expand All @@ -312,7 +312,7 @@ void Ambitus::layout()
_bottomPos.setY(bottomLine * lineDist * 0.5);
// compute accidental
Accidental::AccidentalType accidType;
if (_bottomTpc - key >= 13 && _bottomTpc - key <= 19)
if (_bottomTpc - int(key) >= 13 && _bottomTpc - int(key) <= 19)
accidType = Accidental::AccidentalType::NONE;
else {
AccidentalVal accidVal = AccidentalVal( (_bottomTpc - Tpc::TPC_MIN) / TPC_DELTA_SEMITONE - 2 );
Expand Down
22 changes: 11 additions & 11 deletions libmscore/cmd.cpp
Expand Up @@ -278,7 +278,7 @@ void Score::cmdAddSpanner(Spanner* spanner, const QPointF& pos)
int n = e.numerator() / e.denominator();
QList<NoteEvent*> events;
int pitch = chord->upNote()->ppitch();
int key = chord->staff()->key(segment->tick());
Key key = chord->staff()->key(segment->tick());
int pitch2 = diatonicUpDown(key, pitch, 1);
int dpitch = pitch2 - pitch;
for (int i = 0; i < n; i += 2) {
Expand Down Expand Up @@ -461,7 +461,7 @@ void Score::cmdAddInterval(int val, const QList<Note*>& nl)
int tick = chord->tick();
Staff* estaff = staff(on->staffIdx() + chord->staffMove());
ClefType clef = estaff->clef(tick);
int key = estaff->key(tick);
Key key = estaff->key(tick);
npitch = line2pitch(line, clef, key);

int ntpc = pitch2tpc(npitch, key, Prefer::NEAREST);
Expand Down Expand Up @@ -1079,19 +1079,19 @@ qDebug(" ChangeCRLen:: %d += %d(actual=%d)", tick, f2.ticks(), f2.ticks() * tim
// upDownChromatic
//---------------------------------------------------------

static void upDownChromatic(bool up, int pitch, Note* n, int key, int tpc1, int tpc2, int& newPitch, int& newTpc1, int& newTpc2)
static void upDownChromatic(bool up, int pitch, Note* n, Key key, int tpc1, int tpc2, int& newPitch, int& newTpc1, int& newTpc2)
{
if (up && pitch < 127) {
newPitch = pitch + 1;
if (n->concertPitch()) {
if (tpc1 > Tpc::TPC_A + key)
if (tpc1 > Tpc::TPC_A + int(key))
newTpc1 = tpc1 - 5; // up semitone diatonic
else
newTpc1 = tpc1 + 7; // up semitone chromatic
newTpc2 = n->transposeTpc(newTpc1);
}
else {
if (tpc2 > Tpc::TPC_A + key)
if (tpc2 > Tpc::TPC_A + int(key))
newTpc2 = tpc2 - 5; // up semitone diatonic
else
newTpc2 = tpc2 + 7; // up semitone chromatic
Expand All @@ -1101,14 +1101,14 @@ static void upDownChromatic(bool up, int pitch, Note* n, int key, int tpc1, int
else if (!up && pitch > 0) {
newPitch = pitch - 1;
if (n->concertPitch()) {
if (tpc1 > Tpc::TPC_C + key)
if (tpc1 > Tpc::TPC_C + int(key))
newTpc1 = tpc1 - 7; // down semitone chromatic
else
newTpc1 = tpc1 + 5; // down semitone diatonic
newTpc2 = n->transposeTpc(newTpc1);
}
else {
if (tpc2 > Tpc::TPC_C + key)
if (tpc2 > Tpc::TPC_C + int(key))
newTpc2 = tpc2 - 7; // down semitone chromatic
else
newTpc2 = tpc2 + 5; // down semitone diatonic
Expand Down Expand Up @@ -1147,7 +1147,7 @@ void Score::upDown(bool up, UpDownMode mode)
foreach (Note* oNote, el) {
int tick = oNote->chord()->tick();
Part* part = oNote->staff()->part();
int key = oNote->staff()->key(tick);
Key key = oNote->staff()->key(tick);
int tpc1 = oNote->tpc1();
int tpc2 = oNote->tpc2();
int pitch = oNote->pitch();
Expand Down Expand Up @@ -1244,7 +1244,7 @@ void Score::upDown(bool up, UpDownMode mode)
{
int tpc = oNote->tpc();
if (up) {
if (tpc > Tpc::TPC_A + key) {
if (tpc > Tpc::TPC_A + int(key)) {
if (pitch < 127) {
newPitch = pitch + 1;
setTpc(oNote, tpc - 5, newTpc1, newTpc2);
Expand All @@ -1258,7 +1258,7 @@ void Score::upDown(bool up, UpDownMode mode)
}
}
else {
if (tpc > Tpc::TPC_C + key) {
if (tpc > Tpc::TPC_C + int(key)) {
if (pitch > 1) {
newPitch = pitch - 2;
setTpc(oNote, tpc - 2, newTpc1, newTpc2);
Expand Down Expand Up @@ -1432,7 +1432,7 @@ void Score::changeAccidental(Note* note, Accidental::AccidentalType accidental)
AccidentalVal acc2 = measure->findAccidental(note);
AccidentalVal acc = (accidental == Accidental::AccidentalType::NONE) ? acc2 : Accidental::subtype2value(accidental);

int pitch = line2pitch(note->line(), clef, 0) + int(acc);
int pitch = line2pitch(note->line(), clef, Key::C) + int(acc);
if (!note->concertPitch())
pitch += note->transposition();

Expand Down
98 changes: 65 additions & 33 deletions libmscore/key.cpp
Expand Up @@ -23,29 +23,29 @@ namespace Ms {
// KeySigEvent
//---------------------------------------------------------

KeySigEvent::KeySigEvent(int key)
KeySigEvent::KeySigEvent(Key k)
{
Q_ASSERT(key >= -7 && key <= 7);
_accidentalType = key;
Q_ASSERT(int(k) >= -7 && int(k) <= 7);
_key = k;
_invalid = false;
}

//---------------------------------------------------------
// enforceLimits - ensure _accidentalType
// enforceLimits - ensure _key
// is within acceptable limits (-7 .. +7).
// see KeySig::layout()
//---------------------------------------------------------

void KeySigEvent::enforceLimits()
{
const char* msg = 0;
if (_accidentalType < -7) {
_accidentalType = -7;
msg = "accidentalType < -7";
if (_key < Key::MIN) {
_key = Key::MIN;
msg = "key < -7";
}
else if (_accidentalType > 7) {
_accidentalType = 7;
msg = "accidentalType > 7";
else if (_key > Key::MAX) {
_key = Key::MAX;
msg = "key > 7";
}
if (msg)
qDebug("KeySigEvent: %s", msg);
Expand All @@ -57,10 +57,10 @@ void KeySigEvent::enforceLimits()

void KeySigEvent::setCustomType(int v)
{
_accidentalType = 0;
_customType = v;
_custom = true;
_invalid = false;
_key = Key::C;
_customType = v;
_custom = true;
_invalid = false;
}

//---------------------------------------------------------
Expand All @@ -76,19 +76,19 @@ void KeySigEvent::print() const
if (_custom)
qDebug("custom %d>", _customType);
else
qDebug("accidental %d>", _accidentalType);
qDebug("accidental %d>", int(_key));
}
}

//---------------------------------------------------------
// setAccidentalType
// setKey
//---------------------------------------------------------

void KeySigEvent::setAccidentalType(int v)
void KeySigEvent::setKey(Key v)
{
_accidentalType = v;
_custom = false;
_invalid = false;
_key = v;
_custom = false;
_invalid = false;
enforceLimits();
}

Expand All @@ -103,7 +103,7 @@ bool KeySigEvent::operator==(const KeySigEvent& e) const
if (_custom)
return e._customType == _customType;
else
return e._accidentalType == _accidentalType;
return e._key == _key;
}

//---------------------------------------------------------
Expand All @@ -117,27 +117,27 @@ bool KeySigEvent::operator!=(const KeySigEvent& e) const
if (_custom)
return e._customType != _customType;
else
return e._accidentalType != _accidentalType;
return e._key != _key;
}

//---------------------------------------------------------
// initLineList
// preset lines list with accidentals for given key
//---------------------------------------------------------

void AccidentalState::init(int key)
void AccidentalState::init(Key key)
{
memset(state, 2, 74);
for (int octave = 0; octave < 11; ++octave) {
if (key > 0) {
for (int i = 0; i < key; ++i) {
for (int i = 0; i < int(key); ++i) {
int idx = tpc2step(20 + i) + octave * 7;
if (idx < 74)
state[idx] = 1 + 2;
}
}
else {
for (int i = 0; i > key; --i) {
for (int i = 0; i > int(key); --i) {
int idx = tpc2step(12 + i) + octave * 7;
if (idx < 74)
state[idx] = -1 + 2;
Expand All @@ -150,14 +150,16 @@ void AccidentalState::init(int key)
// transposeKey
//---------------------------------------------------------

int transposeKey(int key, const Interval& interval)
Key transposeKey(Key key, const Interval& interval)
{
int tpc = key+14;
tpc = transposeTpc(tpc, interval, false);
int tpc = int(key) + 14;
tpc = transposeTpc(tpc, interval, false);
// check for valid key sigs
if (tpc > 21) tpc-=12; // no more than 7 sharps in keysig
if (tpc < 7) tpc+=12; // no more than 7 flats in keysig
return (tpc-14);
if (tpc > 21)
tpc -= 12; // no more than 7 sharps in keysig
if (tpc < 7)
tpc += 12; // no more than 7 flats in keysig
return Key(tpc - 14);
}

//---------------------------------------------------------
Expand All @@ -170,7 +172,7 @@ void KeySigEvent::initFromSubtype(int st)
union U {
int subtype;
struct {
int _accidentalType:4;
int _key:4;
int _naturalType:4;
unsigned _customType:16;
bool _custom : 1;
Expand All @@ -179,12 +181,42 @@ void KeySigEvent::initFromSubtype(int st)
};
U a;
a.subtype = st;
_accidentalType = a._accidentalType;
_key = Key(a._key);
_customType = a._customType;
_custom = a._custom;
_invalid = a._invalid;
enforceLimits();
}

//---------------------------------------------------------
// accidentalVal
//---------------------------------------------------------

AccidentalVal AccidentalState::accidentalVal(int line) const
{
Q_ASSERT(line >= 0 && line < 75);
return AccidentalVal((state[line] & 0x0f) - 2);
}

//---------------------------------------------------------
// tieContext
//---------------------------------------------------------

bool AccidentalState::tieContext(int line) const
{
Q_ASSERT(line >= 0 && line < 75);
return state[line] & TIE_CONTEXT;
}

//---------------------------------------------------------
// setAccidentalVal
//---------------------------------------------------------

void AccidentalState::setAccidentalVal(int line, AccidentalVal val, bool tieContext)
{
Q_ASSERT(line >= 0 && line < 75);
Q_ASSERT(val >= AccidentalVal::FLAT2 && val <= AccidentalVal::SHARP2);
state[line] = (int(val) + 2) | (tieContext ? TIE_CONTEXT : 0);
}
}

0 comments on commit b4066a7

Please sign in to comment.