|
|
da847a |
|
|
|
da847a |
#include <tools modifierjitter.h="" modifiers=""></tools>
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
//*****************************************************************************************
|
|
|
da847a |
// static functions
|
|
|
da847a |
//*****************************************************************************************
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
namespace {
|
|
|
da847a |
|
|
|
da847a |
class Jitter {
|
|
|
da847a |
public:
|
|
|
da847a |
static inline double randomNext(unsigned int &seed) {
|
|
|
da847a |
static const unsigned int max = 32767;
|
|
|
da847a |
static const double k = 1.0/max;
|
|
|
da847a |
seed = ((1103515245*seed + 12345) >> 16) & max;
|
|
|
da847a |
return seed*k;
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
static TPointD getPoint(unsigned int seed, int i, double *prevX = nullptr) {
|
|
|
da847a |
unsigned int pseed = seed^i;
|
|
|
da847a |
double dx = randomNext(pseed);
|
|
|
da847a |
double dy = randomNext(pseed)*2 - 1;
|
|
|
da847a |
if (dx < 0.5) {
|
|
|
da847a |
double px = prevX ? *prevX : getPoint(seed, i-1).x;
|
|
|
da847a |
px += 0.5 - i;
|
|
|
da847a |
if (dx < px)
|
|
|
da847a |
dx = randomNext(pseed)*(1 - px) + px;
|
|
|
da847a |
}
|
|
|
da847a |
return TPointD(dx + i, dy);
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
static inline double spline(double x, TPointD *points) {
|
|
|
da847a |
double x0 = points[1].x;
|
|
|
da847a |
double y0 = points[1].y;
|
|
|
da847a |
double x1 = points[2].x;
|
|
|
da847a |
double y1 = points[2].y;
|
|
|
da847a |
double t0 = (y1 - points[0].y)/(x1 - points[0].x)*(x1 - x0);
|
|
|
da847a |
double t1 = (points[3].y - y0)/(points[3].x - x0)*(x1 - x0);
|
|
|
da847a |
|
|
|
da847a |
double l = (x - x0)/(x1 - x0);
|
|
|
da847a |
double ll = l*l;
|
|
|
da847a |
double lll = ll*l;
|
|
|
da847a |
|
|
|
da847a |
return y0*( 2*lll - 3*ll + 1)
|
|
|
da847a |
+ y1*(-2*lll + 3*ll )
|
|
|
da847a |
+ t0*( lll - 2*ll + l)
|
|
|
da847a |
+ t1*( lll - ll );
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
static inline double func(unsigned int seed, double x) {
|
|
|
da847a |
int i = (int)floor(x);
|
|
|
da847a |
TPointD points[5];
|
|
|
da847a |
points[0] = getPoint(seed, i-2);
|
|
|
da847a |
for(int j = 1; j < 5; ++j)
|
|
|
da847a |
points[j] = getPoint(seed, i-2+j, &points[j-1].x);
|
|
|
da847a |
return spline(x, &points[ x < points[2].x ? 0 : 1 ]);
|
|
|
da847a |
}
|
|
|
da847a |
};
|
|
|
da847a |
|
|
|
da847a |
static inline unsigned int trackSeedX(const TTrack &track) {
|
|
|
da847a |
unsigned int seed = track.id;
|
|
|
da847a |
Jitter::randomNext(seed);
|
|
|
da847a |
return seed;
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
static inline unsigned int trackSeedY(const TTrack &track) {
|
|
|
da847a |
unsigned int seed = track.id^32143;
|
|
|
da847a |
Jitter::randomNext(seed);
|
|
|
da847a |
return seed;
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
} // namespace
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
//*****************************************************************************************
|
|
|
da847a |
// TModifierJitter::Interpolator implementation
|
|
|
da847a |
//*****************************************************************************************
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
TModifierJitter::Interpolator::Interpolator(TTrack &track, double period, double amplitude):
|
|
|
da847a |
TTrackInterpolator(track),
|
|
|
da847a |
seedX(trackSeedX(track)),
|
|
|
da847a |
seedY(trackSeedY(track)),
|
|
|
da847a |
frequency(fabs(period) > TConsts::epsilon ? 1/period : 0),
|
|
|
da847a |
amplitude(fabs(period) > TConsts::epsilon ? amplitude : 0)
|
|
|
da847a |
{ }
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
TTrackPoint TModifierJitter::Interpolator::interpolateFromOriginal(double originalIndex) {
|
|
|
da847a |
TTrackPoint p = track.calcPointFromOriginal(originalIndex);
|
|
|
da847a |
double l = p.length*frequency;
|
|
|
da847a |
p.position.x += Jitter::func(seedX, l)*amplitude;
|
|
|
da847a |
p.position.y += Jitter::func(seedY, l)*amplitude;
|
|
|
da847a |
return p;
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
TTrackPoint TModifierJitter::Interpolator::interpolate(double index)
|
|
|
da847a |
{ return interpolateFromOriginal(track.originalIndexByIndex(index)); }
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
//*****************************************************************************************
|
|
|
da847a |
// TModifierJitter implementation
|
|
|
da847a |
//*****************************************************************************************
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
TModifierJitter::TModifierJitter(double period, double amplitude, int skipFirst):
|
|
|
da847a |
period(period), amplitude(amplitude), skipFirst(skipFirst) { }
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
void TModifierJitter::modifyTrack(const TTrack &track,
|
|
|
da847a |
TTrackList &outTracks)
|
|
|
da847a |
{
|
|
|
da847a |
if (!track.handler && fabs(period) > TConsts::epsilon) {
|
|
|
da847a |
Handler *handler = new Handler();
|
|
|
da847a |
track.handler = handler;
|
|
|
da847a |
handler->track = new TTrack(track);
|
|
|
da847a |
new Interpolator(*handler->track, period, amplitude);
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
Handler *handler = dynamic_cast<handler*>(track.handler.getPointer());</handler*>
|
|
|
da847a |
if (!handler)
|
|
|
da847a |
return TInputModifier::modifyTrack(track, outTracks);
|
|
|
da847a |
|
|
|
da847a |
outTracks.push_back(handler->track);
|
|
|
da847a |
TTrack &subTrack = *handler->track;
|
|
|
da847a |
|
|
|
da847a |
if (!track.changed())
|
|
|
da847a |
return;
|
|
|
da847a |
|
|
|
da847a |
Interpolator *intr = dynamic_cast<interpolator*>(subTrack.getInterpolator().getPointer());</interpolator*>
|
|
|
da847a |
if (!intr)
|
|
|
da847a |
return;
|
|
|
da847a |
|
|
|
da847a |
int start = track.size() - track.pointsAdded;
|
|
|
da847a |
if (start < 0) start = 0;
|
|
|
da847a |
|
|
|
da847a |
// process sub-track
|
|
|
da847a |
subTrack.truncate(start);
|
|
|
da847a |
for (int i = start; i < track.size(); ++i)
|
|
|
da847a |
subTrack.push_back(intr->interpolateFromOriginal(i), false);
|
|
|
da847a |
|
|
|
da847a |
// fit points
|
|
|
da847a |
subTrack.fix_to(track.fixedSize());
|
|
|
da847a |
|
|
|
da847a |
track.resetChanges();
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
void
|
|
|
da847a |
TModifierJitter::modifyTracks(
|
|
|
da847a |
const TTrackList &tracks,
|
|
|
da847a |
TTrackList &outTracks )
|
|
|
da847a |
{
|
|
|
da847a |
int cnt = std::min( std::max(0, skipFirst), (int)tracks.size() );
|
|
|
da847a |
TTrackList::const_iterator split = tracks.begin() + cnt;
|
|
|
da847a |
for(TTrackList::const_iterator i = tracks.begin(); i != split; ++i)
|
|
|
da847a |
TInputModifier::modifyTrack(**i, outTracks);
|
|
|
da847a |
for(TTrackList::const_iterator i = split; i != tracks.end(); ++i)
|
|
|
da847a |
modifyTrack(**i, outTracks);
|
|
|
da847a |
}
|
|
|
da847a |
|
|
|
da847a |
|
|
|
da847a |
double TModifierJitter::func(unsigned int seed, double x)
|
|
|
da847a |
{ return Jitter::func(seed, x); }
|
|
|
da847a |
|
|
|
da847a |
|