2012-05-26 14:26:10 +02:00
|
|
|
//=============================================================================
|
|
|
|
// MuseScore
|
|
|
|
// Music Composition & Notation
|
|
|
|
//
|
|
|
|
// Copyright (C) 2009-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
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
/**
|
|
|
|
\file
|
2019-10-28 10:33:45 +01:00
|
|
|
Implementation of class ChangeMap.
|
2012-05-26 14:26:10 +02:00
|
|
|
*/
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
#include "changeMap.h"
|
2012-05-26 14:26:10 +02:00
|
|
|
|
2013-05-13 18:49:17 +02:00
|
|
|
namespace Ms {
|
|
|
|
|
2019-08-10 20:33:05 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// interpolateVelocity
|
|
|
|
/// the maths looks complex, but is just a series of graph transformations.
|
|
|
|
/// You can see these graphically at: https://www.desmos.com/calculator/kk89ficmjk
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-12-28 19:15:23 +01:00
|
|
|
int ChangeMap::interpolate(Fraction& eventTick, ChangeEvent& event, Fraction& tick)
|
2019-08-10 20:33:05 +02:00
|
|
|
{
|
2019-10-28 10:33:45 +01:00
|
|
|
Q_ASSERT(event.type == ChangeEventType::RAMP);
|
2019-08-10 20:33:05 +02:00
|
|
|
|
|
|
|
// Prevent zero-division error
|
2019-12-29 12:07:40 +01:00
|
|
|
if (event.cachedStartVal == event.cachedEndVal || event.length.isZero()) {
|
|
|
|
return event.cachedStartVal;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ticks to change expression over
|
2019-12-28 19:15:23 +01:00
|
|
|
int exprTicks = event.length.ticks();
|
2019-12-29 12:07:40 +01:00
|
|
|
int exprDiff = event.cachedEndVal - event.cachedStartVal;
|
2019-08-10 20:33:05 +02:00
|
|
|
|
|
|
|
std::function<int(int)> valueFunction;
|
|
|
|
switch (event.method) {
|
2019-10-28 10:33:45 +01:00
|
|
|
case ChangeMethod::EXPONENTIAL:
|
2019-08-10 20:33:05 +02:00
|
|
|
// Due to the nth-root, exponential functions do not flip with negative values, and cause errors,
|
|
|
|
// so treat it as a piecewise function.
|
|
|
|
if (exprDiff > 0) {
|
|
|
|
valueFunction = [&](int ct) { return int(
|
|
|
|
pow(
|
|
|
|
pow((exprDiff + 1), 1.0 / double(exprTicks)), // the exprTicks root of d+1
|
|
|
|
double(ct) // to the power of the current tick (exponential)
|
|
|
|
) - 1
|
|
|
|
); };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
valueFunction = [&](int ct) { return -int(
|
|
|
|
pow(
|
|
|
|
pow((-exprDiff + 1), 1.0 / double(exprTicks)), // the exprTicks root of 1-d
|
|
|
|
double(ct) // again to the power of ct
|
|
|
|
) + 1
|
|
|
|
); };
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Uses sin x transformed, which _does_ flip with negative numbers
|
2019-10-28 10:33:45 +01:00
|
|
|
case ChangeMethod::EASE_IN_OUT:
|
2019-08-10 20:33:05 +02:00
|
|
|
valueFunction = [&](int ct) { return int(
|
|
|
|
(double(exprDiff) / 2.0) * (
|
|
|
|
sin(
|
|
|
|
double(ct) * (
|
|
|
|
double(M_PI / double(exprTicks))
|
|
|
|
) - double(M_PI / 2.0)
|
|
|
|
) + 1
|
|
|
|
)
|
|
|
|
); };
|
|
|
|
break;
|
2019-10-28 10:33:45 +01:00
|
|
|
case ChangeMethod::EASE_IN:
|
2019-08-10 20:33:05 +02:00
|
|
|
valueFunction = [&](int ct) { return int(
|
|
|
|
double(exprDiff) * (
|
|
|
|
sin(
|
|
|
|
double(ct - double(exprTicks)) * (
|
|
|
|
double(M_PI / double(2 * exprTicks))
|
|
|
|
)
|
|
|
|
) + 1
|
|
|
|
)
|
|
|
|
); };
|
|
|
|
break;
|
2019-10-28 10:33:45 +01:00
|
|
|
case ChangeMethod::EASE_OUT:
|
2019-08-10 20:33:05 +02:00
|
|
|
valueFunction = [&](int ct) { return int(
|
|
|
|
double(exprDiff) * sin(
|
|
|
|
double(ct) * (
|
|
|
|
double(M_PI / double(2 * exprTicks))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
); };
|
|
|
|
break;
|
2019-10-28 10:33:45 +01:00
|
|
|
case ChangeMethod::NORMAL:
|
2019-08-10 20:33:05 +02:00
|
|
|
default:
|
|
|
|
valueFunction = [&](int ct) { return int(
|
|
|
|
double(exprDiff) * (double(ct) / double(exprTicks))
|
|
|
|
); };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
return event.cachedStartVal + valueFunction(tick.ticks() - eventTick.ticks());
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 14:26:10 +02:00
|
|
|
//---------------------------------------------------------
|
2019-10-28 10:33:45 +01:00
|
|
|
// val
|
|
|
|
/// return value at tick position. Do not confuse with
|
|
|
|
/// `value`, which is a method of QMultiMap.
|
2012-05-26 14:26:10 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
int ChangeMap::val(Fraction tick)
|
2019-08-10 20:33:05 +02:00
|
|
|
{
|
|
|
|
if (!cleanedUp)
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
auto eventIter = upperBound(tick);
|
|
|
|
if (eventIter == begin()) {
|
2019-10-28 10:33:45 +01:00
|
|
|
return DEFAULT_VALUE;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
eventIter--;
|
|
|
|
bool foundRamp = false;
|
2019-10-28 10:33:45 +01:00
|
|
|
ChangeEvent& rampFound = eventIter.value(); // only used to init
|
2019-12-28 19:15:23 +01:00
|
|
|
Fraction rampFoundStartTick = eventIter.key();
|
|
|
|
for (auto& event : values(rampFoundStartTick)) {
|
2019-10-28 10:33:45 +01:00
|
|
|
if (event.type == ChangeEventType::RAMP) {
|
2019-08-10 20:33:05 +02:00
|
|
|
foundRamp = true;
|
|
|
|
rampFound = event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundRamp) {
|
|
|
|
// Last event must be a fix, since there are max two events at one tick
|
|
|
|
return eventIter.value().value;
|
|
|
|
}
|
|
|
|
|
2019-12-28 19:15:23 +01:00
|
|
|
if (tick >= (rampFoundStartTick + rampFound.length)) {
|
2019-12-29 12:07:40 +01:00
|
|
|
return rampFound.cachedEndVal;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Do some maths!
|
2019-12-28 19:15:23 +01:00
|
|
|
return interpolate(rampFoundStartTick, rampFound, tick); // NOTE:JT check should rampFound be eventIter.value()
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// addFixed
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
void ChangeMap::addFixed(Fraction tick, int value)
|
2019-08-10 20:33:05 +02:00
|
|
|
{
|
2019-12-28 19:15:23 +01:00
|
|
|
insert(tick, ChangeEvent(value));
|
2019-08-10 20:33:05 +02:00
|
|
|
cleanedUp = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// addRamp
|
2019-10-28 10:33:45 +01:00
|
|
|
/// A `change` of 0 means that the change in velocity should be calculated from the next fixed
|
2019-08-10 20:33:05 +02:00
|
|
|
/// velocity event.
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
void ChangeMap::addRamp(Fraction stick, Fraction etick, int change, ChangeMethod method, ChangeDirection direction)
|
2019-08-10 20:33:05 +02:00
|
|
|
{
|
2019-10-28 10:33:45 +01:00
|
|
|
change = abs(change);
|
|
|
|
change *= (direction == ChangeDirection::INCREASING) ? 1 : -1;
|
|
|
|
insert(stick, ChangeEvent(stick, etick, change, method, direction));
|
2019-08-10 20:33:05 +02:00
|
|
|
cleanedUp = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
2019-12-29 12:07:40 +01:00
|
|
|
// cleanupStage0
|
|
|
|
/// put the ramps in size order if they start at the same point
|
2019-08-10 20:33:05 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
void ChangeMap::cleanupStage0()
|
2012-05-26 14:26:10 +02:00
|
|
|
{
|
2019-08-10 20:33:05 +02:00
|
|
|
for (auto& tick : uniqueKeys()) {
|
2019-12-29 12:07:40 +01:00
|
|
|
// rampEvents will contain all the ramps at this tick
|
|
|
|
std::vector<ChangeEvent> rampEvents;
|
2019-08-10 20:33:05 +02:00
|
|
|
for (auto& event : values(tick)) {
|
2019-10-28 10:33:45 +01:00
|
|
|
if (event.type == ChangeEventType::FIX)
|
2019-08-10 20:33:05 +02:00
|
|
|
continue;
|
2019-12-29 12:07:40 +01:00
|
|
|
rampEvents.push_back(event);
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
if (int(rampEvents.size()) > 1) {
|
|
|
|
// Sort rampEvents so that the longest ramps come first -
|
|
|
|
// this is important for when we remove ramps/fixes enclosed wihtin other
|
|
|
|
// ramps during stage 1.
|
|
|
|
std::sort(rampEvents.begin(), rampEvents.end(), ChangeMap::compareRampEvents);
|
|
|
|
for (auto& event : rampEvents) {
|
2019-08-10 20:33:05 +02:00
|
|
|
insert(tick, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 12:07:40 +01:00
|
|
|
}
|
2019-08-10 20:33:05 +02:00
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// cleanupStage1
|
|
|
|
/// remove any ramps or fixes that are completely enclosed within other ramps
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void ChangeMap::cleanupStage1()
|
|
|
|
{
|
2019-08-10 20:33:05 +02:00
|
|
|
Fraction currentRampStart = Fraction(-1, 1); // start point of ramp we're in
|
|
|
|
Fraction currentRampEnd = Fraction(-1, 1); // end point of ramp we're in
|
|
|
|
Fraction lastFix = Fraction(-1, 1); // the position of the last fix event
|
|
|
|
bool inRamp = false; // whether we're in a ramp or not
|
|
|
|
|
|
|
|
// Keep a record of the endpoints
|
2019-12-29 12:07:40 +01:00
|
|
|
EndPointsVector endPoints;
|
2019-10-28 10:33:45 +01:00
|
|
|
std::vector<bool> startsInRamp;
|
2019-08-10 20:33:05 +02:00
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
auto i = begin();
|
|
|
|
while (i != end()) {
|
2019-12-28 19:15:23 +01:00
|
|
|
Fraction tick = i.key();
|
|
|
|
ChangeEvent& event = i.value();
|
|
|
|
Fraction etick = tick + event.length;
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
// Reset if we've left the ramp we were in
|
2019-12-28 19:15:23 +01:00
|
|
|
if (currentRampEnd < tick)
|
2019-08-10 20:33:05 +02:00
|
|
|
inRamp = false;
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
if (event.type == ChangeEventType::RAMP) {
|
2019-08-10 20:33:05 +02:00
|
|
|
if (inRamp) {
|
2019-12-28 19:15:23 +01:00
|
|
|
if (etick <= currentRampEnd) {
|
2019-08-10 20:33:05 +02:00
|
|
|
// delete, this event is enveloped
|
2019-12-29 12:07:40 +01:00
|
|
|
i = erase(i);
|
2019-08-10 20:33:05 +02:00
|
|
|
// don't add to the end points
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2019-12-28 19:15:23 +01:00
|
|
|
currentRampStart = tick;
|
|
|
|
currentRampEnd = etick;
|
2019-10-28 10:33:45 +01:00
|
|
|
startsInRamp.push_back(true);
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-12-28 19:15:23 +01:00
|
|
|
currentRampStart = tick;
|
|
|
|
currentRampEnd = etick;
|
2019-08-10 20:33:05 +02:00
|
|
|
inRamp = true;
|
2019-10-28 10:33:45 +01:00
|
|
|
startsInRamp.push_back(false);
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
2019-12-28 19:15:23 +01:00
|
|
|
endPoints.push_back(std::make_pair(tick, etick));
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
2019-10-28 10:33:45 +01:00
|
|
|
else if (event.type == ChangeEventType::FIX) {
|
2019-08-10 20:33:05 +02:00
|
|
|
if (inRamp) {
|
2019-12-28 19:15:23 +01:00
|
|
|
if (tick != currentRampStart && tick != currentRampEnd && lastFix != tick) {
|
2019-08-10 20:33:05 +02:00
|
|
|
// delete, this event is enveloped or at the same point as another fix
|
2019-12-29 12:07:40 +01:00
|
|
|
i = erase(i);
|
2019-08-10 20:33:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 19:15:23 +01:00
|
|
|
lastFix = tick;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
2019-12-29 12:07:40 +01:00
|
|
|
|
|
|
|
i++;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
cleanupStage2(startsInRamp, endPoints);
|
|
|
|
}
|
2019-12-28 19:15:23 +01:00
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// cleanupStage2
|
|
|
|
/// readjust lengths of any colliding ramps
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void ChangeMap::cleanupStage2(std::vector<bool>& startsInRamp, EndPointsVector& endPoints)
|
|
|
|
{
|
2019-12-28 19:15:23 +01:00
|
|
|
// moveTo stores the events that need to be moved to a Fraction position
|
|
|
|
std::map<Fraction, ChangeEvent> moveTo;
|
|
|
|
auto i = begin();
|
2019-08-10 20:33:05 +02:00
|
|
|
int j = -1;
|
2019-12-28 19:15:23 +01:00
|
|
|
while (i != end()) {
|
|
|
|
Fraction tick = i.key();
|
|
|
|
ChangeEvent& event = i.value();
|
|
|
|
if (event.type != ChangeEventType::RAMP) {
|
|
|
|
i++;
|
2019-08-10 20:33:05 +02:00
|
|
|
continue;
|
2019-12-28 19:15:23 +01:00
|
|
|
}
|
2019-08-10 20:33:05 +02:00
|
|
|
|
|
|
|
j++;
|
2019-12-28 19:15:23 +01:00
|
|
|
if (!startsInRamp[j]) {
|
|
|
|
i++;
|
2019-08-10 20:33:05 +02:00
|
|
|
continue;
|
2019-12-28 19:15:23 +01:00
|
|
|
}
|
2019-08-10 20:33:05 +02:00
|
|
|
|
2019-12-28 19:15:23 +01:00
|
|
|
// Take a copy of the event and remove it
|
|
|
|
Fraction newTick = endPoints[j-1].second;
|
2019-12-29 12:07:40 +01:00
|
|
|
event.length -= (newTick - tick);
|
2019-12-28 19:15:23 +01:00
|
|
|
moveTo[newTick] = event;
|
|
|
|
i = erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-insert the events that we need to move in their new positions
|
2020-02-11 13:29:14 +01:00
|
|
|
for (auto k = moveTo.begin(); k != moveTo.end(); k++) {
|
|
|
|
insert(k->first, k->second);
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
2019-12-29 12:07:40 +01:00
|
|
|
}
|
2019-08-10 20:33:05 +02:00
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// cleanupStage3
|
|
|
|
/// cache start and end values for each ramp
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void ChangeMap::cleanupStage3()
|
|
|
|
{
|
2019-08-10 20:33:05 +02:00
|
|
|
for (auto i = begin(); i != end(); i++) {
|
2019-12-28 19:15:23 +01:00
|
|
|
Fraction tick = i.key();
|
2019-08-10 20:33:05 +02:00
|
|
|
auto& event = i.value();
|
2019-10-28 10:33:45 +01:00
|
|
|
if (event.type != ChangeEventType::RAMP)
|
2019-08-10 20:33:05 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Phase 1: cache a start value for the ramp
|
|
|
|
// Try and get a fix at the tick of this ramp
|
|
|
|
bool foundFix = false;
|
2019-12-28 19:15:23 +01:00
|
|
|
for (auto& currentChangeEvent : values(tick)) {
|
2019-10-28 10:33:45 +01:00
|
|
|
if (currentChangeEvent.type == ChangeEventType::FIX) {
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedStartVal = currentChangeEvent.value;
|
2019-08-10 20:33:05 +02:00
|
|
|
foundFix = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there isn't a fix, use from the last event:
|
|
|
|
// - the cached end value if it's a ramp
|
|
|
|
// - the value if it's a fix
|
|
|
|
if (!foundFix) {
|
|
|
|
if (i != begin()) {
|
2019-10-28 10:33:45 +01:00
|
|
|
auto prevChangeEventIter = i;
|
|
|
|
prevChangeEventIter--;
|
2019-08-10 20:33:05 +02:00
|
|
|
|
|
|
|
// Look for a ramp first
|
|
|
|
bool foundRamp = false;
|
2019-10-28 10:33:45 +01:00
|
|
|
for (auto& prevChangeEvent : values(prevChangeEventIter.key())) {
|
|
|
|
if (prevChangeEvent.type == ChangeEventType::RAMP) {
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedStartVal = prevChangeEvent.cachedEndVal;
|
2019-08-10 20:33:05 +02:00
|
|
|
foundRamp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundRamp) {
|
2019-10-28 10:33:45 +01:00
|
|
|
// prevChangeEventIter must point to a fix in this case
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedStartVal = prevChangeEventIter.value().value;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedStartVal = DEFAULT_VALUE;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Phase 2: cache an end value for the ramp
|
|
|
|
// If there's no set velocity change:
|
|
|
|
if (event.value == 0) {
|
2019-12-29 12:07:40 +01:00
|
|
|
auto nextChangeEventIter = i;
|
|
|
|
nextChangeEventIter++;
|
|
|
|
// There's a chance that the next event is a fix at the same tick as the
|
|
|
|
// start of the current ramp. If so, get the next event, which is assured
|
|
|
|
// to be a different (larger) tick
|
|
|
|
if (nextChangeEventIter != end() && nextChangeEventIter.key() == tick)
|
|
|
|
nextChangeEventIter++;
|
2019-08-10 20:33:05 +02:00
|
|
|
|
|
|
|
// If this is the last event, there is no change
|
2019-10-28 10:33:45 +01:00
|
|
|
if (nextChangeEventIter == end()) {
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedEndVal = event.cachedStartVal;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Search for a fixed event at the next event point
|
2020-02-11 13:29:14 +01:00
|
|
|
bool foundFix2 = false;
|
2019-10-28 10:33:45 +01:00
|
|
|
for (auto& nextChangeEvent : values(nextChangeEventIter.key())) {
|
|
|
|
if (nextChangeEvent.type == ChangeEventType::FIX) {
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedEndVal = nextChangeEvent.value;
|
2020-02-11 13:29:14 +01:00
|
|
|
foundFix2 = true;
|
2019-08-10 20:33:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We haven't found a fix, so there must be a ramp. What does the user want?
|
|
|
|
// A good guess would to be to interpolate, but that might get complex, so just ignore
|
2019-10-28 10:33:45 +01:00
|
|
|
// this ramp and set the ending to be the same as the start.
|
2019-08-10 20:33:05 +02:00
|
|
|
// TODO: implementing some form of smart interpolation would be nice.
|
2020-02-11 13:29:14 +01:00
|
|
|
if (!foundFix2) {
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedEndVal = event.cachedStartVal;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-12-29 12:07:40 +01:00
|
|
|
event.cachedEndVal = event.cachedStartVal + event.value;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// And finally... if something's wrong, make it not wrong
|
2019-12-29 12:07:40 +01:00
|
|
|
if ((event.cachedStartVal > event.cachedEndVal && event.direction == ChangeDirection::INCREASING) ||
|
|
|
|
(event.cachedStartVal < event.cachedEndVal && event.direction == ChangeDirection::DECREASING)) {
|
|
|
|
event.cachedEndVal = event.cachedStartVal;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 12:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// cleanup
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void ChangeMap::cleanup()
|
|
|
|
{
|
|
|
|
if (cleanedUp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// qDebug() << "Before cleanup:";
|
|
|
|
// dump();
|
|
|
|
|
|
|
|
cleanupStage0();
|
|
|
|
cleanupStage1();
|
|
|
|
cleanupStage3();
|
2019-08-10 20:33:05 +02:00
|
|
|
cleanedUp = true;
|
2019-12-29 12:07:40 +01:00
|
|
|
|
|
|
|
// qDebug() << "After cleanup:";
|
|
|
|
// dump();
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// changesInRange
|
|
|
|
/// returns a list of changes in a range, and their start and end points
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
std::vector<std::pair<Fraction, Fraction>> ChangeMap::changesInRange(Fraction stick, Fraction etick)
|
2019-08-10 20:33:05 +02:00
|
|
|
{
|
|
|
|
if (!cleanedUp)
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
std::vector<std::pair<Fraction, Fraction>> tempChanges;
|
|
|
|
|
|
|
|
// Force a new event on every noteon, in case the velocity has changed
|
|
|
|
tempChanges.push_back(std::make_pair(stick, stick));
|
|
|
|
for (auto iter = lowerBound(stick); iter != end(); iter++) {
|
2019-12-28 19:15:23 +01:00
|
|
|
Fraction tick = iter.key();
|
|
|
|
if (tick > etick)
|
2019-08-10 20:33:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
auto& event = iter.value();
|
2019-10-28 10:33:45 +01:00
|
|
|
if (event.type == ChangeEventType::FIX)
|
2019-12-28 19:15:23 +01:00
|
|
|
tempChanges.push_back(std::make_pair(tick, tick));
|
2019-10-28 10:33:45 +01:00
|
|
|
else if (event.type == ChangeEventType::RAMP) {
|
2019-12-28 19:15:23 +01:00
|
|
|
Fraction eventEtick = tick + event.length;
|
|
|
|
Fraction useEtick = eventEtick > etick ? etick : eventEtick;
|
|
|
|
tempChanges.push_back(std::make_pair(tick, useEtick));
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
// And also go back one and try to find ramp coming into this range
|
2019-08-10 20:33:05 +02:00
|
|
|
auto iter = lowerBound(stick);
|
|
|
|
if (iter != begin()) {
|
|
|
|
iter--;
|
|
|
|
auto& event = iter.value();
|
2019-10-28 10:33:45 +01:00
|
|
|
if (event.type == ChangeEventType::RAMP) {
|
2019-12-28 19:15:23 +01:00
|
|
|
Fraction eventEtick = iter.key() + event.length;
|
|
|
|
if (eventEtick > stick) {
|
|
|
|
tempChanges.push_back(std::make_pair(stick, eventEtick));
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tempChanges;
|
2012-05-26 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// changeMethodTable
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
const std::vector<ChangeMap::ChangeMethodItem> ChangeMap::changeMethodTable {
|
|
|
|
{ ChangeMethod::NORMAL, "normal" },
|
|
|
|
{ ChangeMethod::EASE_IN, "ease-in" },
|
|
|
|
{ ChangeMethod::EASE_OUT, "ease-out" },
|
|
|
|
{ ChangeMethod::EASE_IN_OUT, "ease-in-out" },
|
|
|
|
{ ChangeMethod::EXPONENTIAL, "exponential" },
|
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// changeMethodToName
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
QString ChangeMap::changeMethodToName(ChangeMethod method)
|
|
|
|
{
|
|
|
|
for (auto i : ChangeMap::changeMethodTable) {
|
|
|
|
if (i.method == method)
|
|
|
|
return i.name;
|
|
|
|
}
|
|
|
|
qFatal("Unrecognised change method!");
|
|
|
|
return "none"; // silence a compiler warning
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// nameToChangeMethod
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
ChangeMethod ChangeMap::nameToChangeMethod(QString name)
|
|
|
|
{
|
|
|
|
for (auto i : ChangeMap::changeMethodTable) {
|
|
|
|
if (i.name == name)
|
|
|
|
return i.method;
|
|
|
|
}
|
|
|
|
return ChangeMethod::NORMAL; // default
|
|
|
|
}
|
|
|
|
|
2012-05-26 14:26:10 +02:00
|
|
|
//---------------------------------------------------------
|
2019-08-10 20:33:05 +02:00
|
|
|
// dump
|
2012-05-26 14:26:10 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
void ChangeMap::dump()
|
2012-05-26 14:26:10 +02:00
|
|
|
{
|
2019-10-28 10:33:45 +01:00
|
|
|
qDebug("\n\n=== ChangeMap: dump ===");
|
2019-12-28 19:15:23 +01:00
|
|
|
for (auto i = begin(); i != end(); i++) {
|
|
|
|
Fraction tick = i.key();
|
|
|
|
auto& event = i.value();
|
2019-10-28 10:33:45 +01:00
|
|
|
if (event.type == ChangeEventType::FIX) {
|
2019-12-28 19:15:23 +01:00
|
|
|
qDebug().nospace() << "===" << tick.ticks() << " : FIX " << event.value;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
2019-10-28 10:33:45 +01:00
|
|
|
else if (event.type == ChangeEventType::RAMP) {
|
2019-12-29 12:07:40 +01:00
|
|
|
qDebug().nospace() << "===" << tick.ticks() << " to " << (tick + event.length).ticks() << " : RAMP diff " << event.value << " " << ChangeMap::changeMethodToName(event.method) << " (" << event.cachedStartVal << ", " << event.cachedEndVal << ")";
|
|
|
|
}
|
|
|
|
else if (event.type == ChangeEventType::INVALID) {
|
|
|
|
qDebug().nospace() << "===" << tick.ticks() << " : INVALID value" << event.value;
|
2019-08-10 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-28 10:33:45 +01:00
|
|
|
qDebug("=== ChangeMap: dump end ===\n\n");
|
2012-05-26 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
2019-08-10 20:33:05 +02:00
|
|
|
// operator==
|
2012-05-26 14:26:10 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2019-10-28 10:33:45 +01:00
|
|
|
bool ChangeEvent::operator==(const ChangeEvent& event) const
|
2012-05-26 14:26:10 +02:00
|
|
|
{
|
2019-08-10 20:33:05 +02:00
|
|
|
return (
|
|
|
|
value == event.value &&
|
|
|
|
type == event.type &&
|
2019-12-28 19:15:23 +01:00
|
|
|
length == event.length &&
|
|
|
|
method == event.method &&
|
|
|
|
direction == event.direction
|
2019-08-10 20:33:05 +02:00
|
|
|
);
|
2012-05-26 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
2013-05-13 18:49:17 +02:00
|
|
|
}
|
|
|
|
|