MuseScore/libmscore/sig.cpp

564 lines
18 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
2012-05-26 14:26:10 +02:00
//
// Copyright (C) 2002-2007 Werner Schweer
2012-05-26 14:26:10 +02:00
//
// 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
2012-05-26 14:26:10 +02:00
//=============================================================================
#include "sig.h"
#include "xml.h"
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// ticks_beat
//---------------------------------------------------------
2019-04-17 16:17:47 +02:00
int ticks_beat(int n)
2012-05-26 14:26:10 +02:00
{
int m = (MScore::division * 4) / n;
if ((MScore::division * 4) % n) {
qFatal("Mscore: ticks_beat(): bad divisor %d", n);
2012-05-26 14:26:10 +02:00
}
return m;
}
//---------------------------------------------------------
// ticks_measure
//---------------------------------------------------------
static int ticks_measure(const Fraction& f)
{
return (MScore::division * 4 * f.numerator()) / f.denominator();
}
//---------------------------------------------------------
// rtick2beatType
// caller must adjust rtick as appropriate if the measure's
// actual timeSig is different from the nominal timeSig.
//---------------------------------------------------------
BeatType TimeSigFrac::rtick2beatType(int rtick) const
{
if (rtick == 0)
return BeatType::DOWNBEAT; // note: only return DOWNBEAT for rtick = 0, not for rtick = measureTicks,
if (rtick % dUnitTicks() != 0)
return BeatType::SUBBEAT;
if (isCompound()) {
if (rtick % beatTicks() != 0)
return BeatType::COMPOUND_SUBBEAT;
}
const int beatNum = rtick / beatTicks();
int stressBeat = 0;
if (isTriple())
stressBeat = 3;
else if (isDuple())
stressBeat = 2;
else
stressBeat = (numerator() + 1) / 2; // Assumes 5/4 timeSig = (3+2)/4. (The same assumption is used for beaming)
if (stressBeat && beatNum % stressBeat == 0)
return isCompound() ? BeatType::COMPOUND_STRESSED : BeatType::SIMPLE_STRESSED;
return isCompound() ? BeatType::COMPOUND_UNSTRESSED : BeatType::SIMPLE_UNSTRESSED;
}
2016-08-19 22:58:41 +02:00
//---------------------------------------------------------
// strongestBeatInRange
// dUnitsCrossed - pointer to store number crossed
// subbeatTick - pointer to store tick of strongest beat
// saveLast - which tick to store if strongest type is
// crossed more than once
//
// caller must adjust rticks as appropriate if the measure's
// actual timeSig is different from the nominal timeSig.
//---------------------------------------------------------
BeatType TimeSigFrac::strongestBeatInRange(int rtick1, int rtick2, int* dUnitsCrossed, int* subbeatTick, bool saveLast) const
{
Q_ASSERT(rtick2 > rtick1);
BeatType strongest = BeatType::SUBBEAT;
for (int rtick = rtick1 + ticksToNextDUnit(rtick1); rtick < rtick2; rtick += dUnitTicks()) {
if (dUnitsCrossed)
(*dUnitsCrossed)++;
BeatType type = rtick2beatType(rtick);
if (static_cast<int>(type) < static_cast<int>(strongest) + saveLast) { // "<" behaves like "<=" if saveLast is true
strongest = type;
if (subbeatTick)
(*subbeatTick) = rtick;
}
}
return strongest;
}
//---------------------------------------------------------
// subbeatTicks
// divides dUnitTicks() by 2 once for each level.
//---------------------------------------------------------
int TimeSigFrac::subbeatTicks(int level) const
{
Q_ASSERT(level <= maxSubbeatLevel());
int subbeatTicks = dUnitTicks();
while (level > 0) {
subbeatTicks /= 2;
level--;
}
return subbeatTicks;
}
//---------------------------------------------------------
// maxSubbeatLevel
// subdivision beyond this level would result in rounding errors
//---------------------------------------------------------
int TimeSigFrac::maxSubbeatLevel() const
{
int level = 0;
int subbeatTicks = dUnitTicks();
while (subbeatTicks % 2 == 0) {
subbeatTicks /= 2;
level++;
}
return level;
}
//---------------------------------------------------------
// rtick2subbeatLevel
// returns 0 if rtick is on a beat or denominator unit.
// returns 1 if rtick lies halfway between dUnits
// returns 2 if rtick lies on a multiple of 1/4 of dUnit
// 3 1/8
// 4 1/16
// n 1/(2**n)
// returns -(n+1) if max n is reached and rtick still not found.
//
// Caller must adjust rtick as appropriate if the measure's
// actual timeSig is different from the nominal timeSig.
//---------------------------------------------------------
int TimeSigFrac::rtick2subbeatLevel(int rtick) const
{
int level = 0;
int subbeatTicks = dUnitTicks();
int remainder = rtick % subbeatTicks;
while (remainder != 0) {
level++;
if (subbeatTicks % 2 != 0)
return -level; // further sub-division would split measure into chunks of unequal length.
subbeatTicks /= 2;
remainder %= subbeatTicks;
}
return level;
}
//---------------------------------------------------------
// strongestSubbeatLevelInRange
// Return value is negative if none are found.
//
// Caller must adjust rtick as appropriate if the measure's
// actual timeSig is different from the nominal timeSig.
//---------------------------------------------------------
int TimeSigFrac::strongestSubbeatLevelInRange(int rtick1, int rtick2, int* subbeatTick) const
{
Q_ASSERT(rtick2 > rtick1);
for (int level = 0, subbeatTicks = dUnitTicks();;) {
int n = rtick1 / subbeatTicks;
int m = (rtick2 - 1) / subbeatTicks; // -1 to make the range exclusive
if (m > n) {
if (subbeatTick)
(*subbeatTick) = m * subbeatTicks;
return level;
}
level++;
if (subbeatTicks % 2 != 0)
return -level; // further sub-division would split measure into chunks of unequal length.
subbeatTicks /= 2;
}
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// operator==
//---------------------------------------------------------
bool SigEvent::operator==(const SigEvent& e) const
{
return (_timesig.identical(e._timesig));
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void TimeSigMap::add(int tick, const Fraction& f)
{
if (!f.isValid()) {
qDebug("illegal signature %d/%d", f.numerator(), f.denominator());
}
(*this)[tick] = SigEvent(f);
normalize();
}
void TimeSigMap::add(int tick, const SigEvent& ev)
{
(*this)[tick] = ev;
normalize();
}
//---------------------------------------------------------
// del
//---------------------------------------------------------
void TimeSigMap::del(int tick)
{
erase(tick);
normalize();
}
//---------------------------------------------------------
// clearRange
// Clears the given range, start tick included, end tick
// excluded.
//---------------------------------------------------------
void TimeSigMap::clearRange(int tick1, int tick2)
{
iterator first = lower_bound(tick1);
iterator last = lower_bound(tick2);
if (first == last)
return;
erase(first, last);
normalize();
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// TimeSigMap::normalize
//---------------------------------------------------------
void TimeSigMap::normalize()
{
int z = 4;
int n = 4;
int tick = 0;
TimeSigFrac bar;
int tm = ticks_measure(TimeSigFrac(z, n));
2012-05-26 14:26:10 +02:00
2013-03-13 18:15:36 +01:00
for (auto i = begin(); i != end(); ++i) {
2012-05-26 14:26:10 +02:00
SigEvent& e = i->second;
bar += TimeSigFrac(i->first - tick, tm).reduced();
e.setBar(bar.numerator() / bar.denominator());
2012-05-26 14:26:10 +02:00
tick = i->first;
tm = ticks_measure(e.timesig());
}
}
//---------------------------------------------------------
// timesig
//---------------------------------------------------------
const SigEvent& TimeSigMap::timesig(int tick) const
{
static const SigEvent ev(TimeSigFrac(4, 4));
2012-05-26 14:26:10 +02:00
if (empty())
return ev;
2013-03-13 18:15:36 +01:00
auto i = upper_bound(tick);
2012-05-26 14:26:10 +02:00
if (i != begin())
--i;
return i->second;
}
//---------------------------------------------------------
// tickValues
2013-05-14 16:43:21 +02:00
// t - some time moment on timeline (in ticks)
//
// Result - values computed for this time moment:
// bar - index of bar containing time moment t
// beat - index of beat in bar containing t
// tick - position of t in beat (in ticks)
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
void TimeSigMap::tickValues(int t, int* bar, int* beat, int* tick) const
{
if (empty()) {
*bar = 0;
*beat = 0;
*tick = 0;
return;
}
2013-03-13 18:15:36 +01:00
auto e = upper_bound(t);
2012-05-26 14:26:10 +02:00
if (empty() || e == begin()) {
qFatal("tickValue(0x%x) not found", t);
2012-05-26 14:26:10 +02:00
}
--e;
int delta = t - e->first;
2013-05-14 16:43:21 +02:00
int ticksB = ticks_beat(e->second.timesig().denominator()); // ticks in beat
int ticksM = ticksB * e->second.timesig().numerator(); // ticks in measure (bar)
2012-05-26 14:26:10 +02:00
if (ticksM == 0) {
qDebug("TimeSigMap::tickValues: at %d %s", t, qPrintable(e->second.timesig().print()));
*bar = 0;
*beat = 0;
*tick = 0;
return;
}
*bar = e->second.bar() + delta / ticksM;
int rest = delta % ticksM;
*beat = rest / ticksB;
*tick = rest % ticksB;
}
//---------------------------------------------------------
// pos
// Return string representation of tick position.
// This is not reentrant and only for debugging!
//---------------------------------------------------------
2014-08-07 10:18:50 +02:00
QString TimeSigMap::pos(int t) const
{
int bar, beat, tick;
tickValues(t, &bar, &beat, &tick);
2014-08-07 10:18:50 +02:00
return QString("%1:%2:%3").arg(bar+1).arg(beat).arg(tick);
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// bar2tick
2013-05-14 16:43:21 +02:00
// Returns the absolute start time (in ticks)
// of beat in bar
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
int TimeSigMap::bar2tick(int bar, int beat) const
{
2013-05-14 16:43:21 +02:00
// bar - index of current bar (terminology: bar == measure)
// beat - index of beat in current bar
2013-03-13 18:15:36 +01:00
auto e = begin();
2012-05-26 14:26:10 +02:00
2013-03-13 18:15:36 +01:00
for (; e != end(); ++e) {
2012-05-26 14:26:10 +02:00
if (bar < e->second.bar())
break;
}
if (empty() || e == begin()) {
qDebug("TimeSigMap::bar2tick(): not found(%d,%d) not found", bar, beat);
if (empty())
qDebug(" list is empty");
return 0;
}
2013-05-14 16:43:21 +02:00
--e; // current TimeSigMap value
int ticksB = ticks_beat(e->second.timesig().denominator()); // ticks per beat
int ticksM = ticksB * e->second.timesig().numerator(); // bar length in ticks
2012-05-26 14:26:10 +02:00
return e->first + (bar - e->second.bar()) * ticksM + ticksB * beat;
}
//---------------------------------------------------------
// TimeSigMap::write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void TimeSigMap::write(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
xml.stag("siglist");
2013-03-13 18:15:36 +01:00
for (auto i = begin(); i != end(); ++i)
2012-05-26 14:26:10 +02:00
i->second.write(xml, i->first);
xml.etag();
}
//---------------------------------------------------------
// TimeSigMap::read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void TimeSigMap::read(XmlReader& e, int fileDivision)
2012-05-26 14:26:10 +02:00
{
2013-01-11 18:10:18 +01:00
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
2012-05-26 14:26:10 +02:00
if (tag == "sig") {
SigEvent t;
int tick = t.read(e, fileDivision);
(*this)[tick] = t;
}
else
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
normalize();
}
//---------------------------------------------------------
// SigEvent
//---------------------------------------------------------
SigEvent::SigEvent(const SigEvent& e)
{
_timesig = e._timesig;
_nominal = e._nominal;
_bar = e._bar;
}
//---------------------------------------------------------
// SigEvent::write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void SigEvent::write(XmlWriter& xml, int tick) const
2012-05-26 14:26:10 +02:00
{
xml.stag(QString("sig tick=\"%1\"").arg(tick));
xml.tag("nom", _timesig.numerator());
xml.tag("denom", _timesig.denominator());
xml.etag();
}
//---------------------------------------------------------
// SigEvent::read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
int SigEvent::read(XmlReader& e, int fileDivision)
2012-05-26 14:26:10 +02:00
{
2013-01-11 18:10:18 +01:00
int tick = e.intAttribute("tick", 0);
2012-05-26 14:26:10 +02:00
tick = tick * MScore::division / fileDivision;
int numerator = 1;
int denominator = 1;
int denominator2 = -1;
int numerator2 = -1;
2013-01-11 18:10:18 +01:00
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
2012-05-26 14:26:10 +02:00
if (tag == "nom")
2013-01-11 18:10:18 +01:00
numerator = e.readInt();
2012-05-26 14:26:10 +02:00
else if (tag == "denom")
2013-01-11 18:10:18 +01:00
denominator = e.readInt();
2012-05-26 14:26:10 +02:00
else if (tag == "nom2")
2013-01-11 18:10:18 +01:00
numerator2 = e.readInt();
2012-05-26 14:26:10 +02:00
else if (tag == "denom2")
2013-01-11 18:10:18 +01:00
denominator2 = e.readInt();
2012-05-26 14:26:10 +02:00
else
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
if ((numerator2 == -1) || (denominator2 == -1)) {
numerator2 = numerator;
denominator2 = denominator;
}
_timesig = TimeSigFrac(numerator, denominator);
_nominal = TimeSigFrac(numerator2, denominator2);
2012-05-26 14:26:10 +02:00
return tick;
}
2013-05-14 16:43:21 +02:00
//---------------------------------------------------------
// ticksPerMeasure
//---------------------------------------------------------
int ticksPerMeasure(int numerator, int denominator)
{
return ticks_beat(denominator) * numerator;
}
//---------------------------------------------------------
// rasterEval
//---------------------------------------------------------
unsigned rasterEval(unsigned t, int raster, int startTick,
int numerator, int denominator, int addition)
{
int delta = t - startTick;
int ticksM = ticksPerMeasure(numerator, denominator);
if (raster == 0)
raster = ticksM;
int rest = delta % ticksM;
int bb = (delta / ticksM) * ticksM;
return startTick + bb + ((rest + addition) / raster) * raster;
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// raster
//---------------------------------------------------------
unsigned TimeSigMap::raster(unsigned t, int raster) const
{
if (raster == 1)
return t;
2013-03-13 18:15:36 +01:00
auto e = upper_bound(t);
2012-05-26 14:26:10 +02:00
if (e == end()) {
qDebug("TimeSigMap::raster(%x,)", t);
return t;
}
2013-05-14 16:43:21 +02:00
auto timesig = e->second.timesig();
return rasterEval(t, raster, e->first, timesig.numerator(),
timesig.denominator(), raster / 2);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// raster1
// round down
//---------------------------------------------------------
unsigned TimeSigMap::raster1(unsigned t, int raster) const
{
if (raster == 1)
return t;
2013-03-13 18:15:36 +01:00
auto e = upper_bound(t);
2013-05-14 16:43:21 +02:00
auto timesig = e->second.timesig();
return rasterEval(t, raster, e->first, timesig.numerator(),
timesig.denominator(), 0);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// raster2
// round up
//---------------------------------------------------------
unsigned TimeSigMap::raster2(unsigned t, int raster) const
{
if (raster == 1)
return t;
2013-03-13 18:15:36 +01:00
auto e = upper_bound(t);
2013-05-14 16:43:21 +02:00
auto timesig = e->second.timesig();
return rasterEval(t, raster, e->first, timesig.numerator(),
timesig.denominator(), raster - 1);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// rasterStep
//---------------------------------------------------------
int TimeSigMap::rasterStep(unsigned t, int raster) const
{
if (raster == 0) {
2013-05-14 16:43:21 +02:00
auto timesig = upper_bound(t)->second.timesig();
return ticksPerMeasure(timesig.denominator(), timesig.numerator());
2012-05-26 14:26:10 +02:00
}
return raster;
}
//---------------------------------------------------------
// TimeSigMap::dump
//---------------------------------------------------------
void TimeSigMap::dump() const
{
qDebug("TimeSigMap:");
2013-03-13 18:15:36 +01:00
for (auto i = begin(); i != end(); ++i)
2012-05-26 14:26:10 +02:00
qDebug("%6d timesig: %s measure: %d",
i->first, qPrintable(i->second.timesig().print()), i->second.bar());
}
2017-01-05 11:23:47 +01:00
//---------------------------------------------------------
// dUnitTicks
//---------------------------------------------------------
int TimeSigFrac::dUnitTicks() const
{
return (4 * MScore::division) / denominator();
}
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
}