MuseScore/libmscore/key.cpp

193 lines
5.5 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2011 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include "key.h"
#include "xml.h"
#include "utils.h"
#include "score.h"
#include "pitchspelling.h"
2014-06-03 15:28:10 +02:00
#include "keylist.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// KeySigEvent
//---------------------------------------------------------
2014-06-03 15:28:10 +02:00
KeySigEvent::KeySigEvent(int at)
2012-05-26 14:26:10 +02:00
{
2014-06-03 15:28:10 +02:00
_accidentalType = at;
2012-05-26 14:26:10 +02:00
_invalid = false;
enforceLimits();
}
//---------------------------------------------------------
2014-06-03 15:28:10 +02:00
// enforceLimits - ensure _accidentalType
// is within acceptable limits (-7 .. +7).
2012-05-26 14:26:10 +02:00
// see KeySig::layout()
//---------------------------------------------------------
void KeySigEvent::enforceLimits()
{
const char* msg = 0;
if (_accidentalType < -7) {
_accidentalType = -7;
msg = "accidentalType < -7";
}
else if (_accidentalType > 7) {
_accidentalType = 7;
msg = "accidentalType > 7";
}
if (msg)
qDebug("KeySigEvent: %s", msg);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// setCustomType
//---------------------------------------------------------
void KeySigEvent::setCustomType(int v)
{
_accidentalType = 0;
_customType = v;
_custom = true;
_invalid = false;
}
//---------------------------------------------------------
// print
//---------------------------------------------------------
void KeySigEvent::print() const
{
qDebug("<KeySigEvent: ");
if (_invalid)
qDebug("invalid>");
else {
if (_custom)
2014-06-03 15:28:10 +02:00
qDebug("custom %d>", _customType);
2012-05-26 14:26:10 +02:00
else
2014-06-03 15:28:10 +02:00
qDebug("accidental %d>", _accidentalType);
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// setAccidentalType
//---------------------------------------------------------
void KeySigEvent::setAccidentalType(int v)
{
_accidentalType = v;
_custom = false;
_invalid = false;
enforceLimits();
}
//---------------------------------------------------------
// KeySigEvent::operator==
//---------------------------------------------------------
bool KeySigEvent::operator==(const KeySigEvent& e) const
{
if ((e._invalid != _invalid) || (e._custom != _custom))
return false;
if (_custom)
return e._customType == _customType;
else
return e._accidentalType == _accidentalType;
}
//---------------------------------------------------------
// KeySigEvent::operator!=
//---------------------------------------------------------
bool KeySigEvent::operator!=(const KeySigEvent& e) const
{
if ((e._invalid != _invalid) || (e._custom != _custom))
return true;
if (_custom)
return e._customType != _customType;
else
return e._accidentalType != _accidentalType;
}
//---------------------------------------------------------
// initLineList
// preset lines list with accidentals for given key
//---------------------------------------------------------
void AccidentalState::init(const KeySigEvent& ks)
{
int type = ks.accidentalType();
memset(state, 2, 74);
for (int octave = 0; octave < 11; ++octave) {
if (type > 0) {
for (int i = 0; i < type; ++i) {
int idx = tpc2step(20 + i) + octave * 7;
if (idx < 74)
state[idx] = 1 + 2;
}
}
else {
for (int i = 0; i > type; --i) {
int idx = tpc2step(12 + i) + octave * 7;
if (idx < 74)
state[idx] = -1 + 2;
}
}
}
}
//---------------------------------------------------------
// transposeKey
//---------------------------------------------------------
int transposeKey(int key, const Interval& interval)
{
int tpc = key+14;
2012-05-26 14:26:10 +02:00
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);
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// initFromSubtype
// for backward compatibility
//---------------------------------------------------------
void KeySigEvent::initFromSubtype(int st)
{
union U {
int subtype;
struct {
int _accidentalType:4;
int _naturalType:4;
unsigned _customType:16;
bool _custom : 1;
bool _invalid : 1;
};
};
U a;
a.subtype = st;
_accidentalType = a._accidentalType;
_customType = a._customType;
_custom = a._custom;
_invalid = a._invalid;
enforceLimits();
}
2013-05-13 18:49:17 +02:00
}