Skip to content

Commit

Permalink
finally fix seeking
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Dec 31, 2024
1 parent afbe7c2 commit 2a4d536
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 69 deletions.
96 changes: 33 additions & 63 deletions src/engine/positionscratchcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ PositionScratchController::PositionScratchController(const QString& group)
m_callsToStop(1), // ?
m_p(1),
m_d(1),
m_f(0.4),
m_seekWaitCounter(-1) {
m_f(0.4) {
m_pMainSampleRate->connectValueChanged(this,
&PositionScratchController::slotUpdateFilterParameters);
}
Expand Down Expand Up @@ -162,56 +161,41 @@ void PositionScratchController::process(double currentSamplePos,
}

if (bufferSize != m_bufferSize) {
warn("!! buffer size update !!");
m_bufferSize = bufferSize;
slotUpdateFilterParameters(m_pMainSampleRate->get());
}

double queuedSeekPos = m_seekSamplePos;
bool adoptSeekPos = false;
if (!util_isnan(queuedSeekPos)) {
if (m_seekWaitCounter >= 0 &&
m_seekWaitCounter < kMaxSeekWaitCount) { //&&
// fabs(currentSamplePos - queuedSeekPos) < kSeekPosTolerance) {
double posDiff = queuedSeekPos - currentSamplePos;
const QString filler = QString(posDiff >= 0 ? " " : "");
loggg(QString("match queued seek pos after %1 calls at %2")
// .arg(QString::number(m_seekWaitCounter),
// QString::number(currentSamplePos, 'g', 18)));
.arg(m_seekWaitCounter),
static_cast<int>(currentSamplePos));
loggg(QString(" diff: %1").
arg(static_cast<int>(posDiff)));
resetSeekPos();
adoptSeekPos = true;
m_reportRate = true;

// ??
// fix high speed when seeking to wdifferent cues after scratching
// ad while mouse still (not quite??)
// m_mouseSampleTime = kDefaultSampleInterval + m_dt;
// m_mouseSampleTime = 0;

// TODO When seeking while mouse hasn't fully stopped yet,
// we may still get (undesired) position updates after seeking.
// Try to ignore those until mouse has stopped?
} else if (m_seekWaitCounter >= 0 && m_seekWaitCounter < kMaxSeekWaitCount) {
// loggg(QString("seek queued" << m_seekWaitCounter << "calls ago"));
m_seekWaitCounter++;
} else {
warn(QString("!! reset queued seek after %1 calls").arg(m_seekWaitCounter));
resetSeekPos();
}
if (!util_isnan(m_seekSamplePos)) {
// If we were notified about a sekk, adopt the new position immediately.
// fabs(currentSamplePos - queuedSeekPos) < kSeekPosTolerance) {
double posDiff = m_seekSamplePos - currentSamplePos;
const QString filler = QString(posDiff >= 0 ? " " : "");

Check failure on line 172 in src/engine/positionscratchcontroller.cpp

View workflow job for this annotation

GitHub Actions / clazy

unused QString [-Wclazy-unused-non-trivial-variable]
loggg(QString("adopt seek pos at %2").arg(static_cast<int>(currentSamplePos)));
loggg(QString(" diff: %1").arg(static_cast<int>(posDiff)));
m_seekSamplePos = std::numeric_limits<double>::quiet_NaN();
m_reportRate = true;

m_prevSamplePos = currentSamplePos;
m_rate = 0;
// Reset filters in a way that the system is settled.
// Set to the remaining error of a p controller // ??
m_samplePosDeltaSum = -(releaseRate / m_p) * m_callsPerDt;
m_pVelocityController->reset(-m_samplePosDeltaSum);
m_pRateIIFilter->reset(-m_samplePosDeltaSum);
m_scratchStartPos = m_pScratchPos->get();
m_scratchTargetDelta = 0;
m_moveDelay = 0;
return;
}

double scratchPosition = 0;
m_mouseSampleTime += m_dt;
if (m_mouseSampleTime >= kDefaultSampleInterval || !m_isScratching) {
loggg(QString(" <<<< m_mouseSampleTime >= kDefaultSampleInterval"), adoptSeekPos);
// loggg(QString(" <<<< m_mouseSampleTime >= kDefaultSampleInterval"), adoptSeekPos);
scratchPosition = m_pScratchPos->get();
m_mouseSampleTime = 0;
} else {
loggg(QString(" !! m_mouseSampleTime < kDefaultSampleInterval"), adoptSeekPos);
// loggg(QString(" !! m_mouseSampleTime < kDefaultSampleInterval"), adoptSeekPos);
}

if (m_isScratching) {
Expand Down Expand Up @@ -281,23 +265,14 @@ void PositionScratchController::process(double currentSamplePos,
(triggerPos - m_prevSamplePos) +
(currentSamplePos - targetPos);
} else {
// Seeks occur when jumping to cue/hotcue.
// Adopt the new position after seek.
if (adoptSeekPos) {
m_prevSamplePos = queuedSeekPos;
// loggg(QString(!----------------------adopt seek pos"));
}
sampleDelta = currentSamplePos - m_prevSamplePos;
loggg(QString(" sampleDelta: %1")
.arg(QString::number(sampleDelta, 'g', 18)), adoptSeekPos);
}

// Measure the total distance traveled since last call, add it to the
// running total and normalize to one buffer.
// This is required to scratch within loop boundaries.
m_samplePosDeltaSum += (sampleDelta) / (bufferSize * baseSampleRate);
loggg(QString(" m_samplePosDeltaSum: %1")
.arg(m_samplePosDeltaSum), adoptSeekPos);
// loggg(QString(" posDeltaSum: %1").arg(m_samplePosDeltaSum));

// If we may have a new position, calculate scratch parameters and
// eventually the new rate.
Expand All @@ -307,13 +282,12 @@ void PositionScratchController::process(double currentSamplePos,
// and normalize to one buffer
double scratchTargetDelta = (scratchPosition - m_scratchStartPos) /
(bufferSize * baseSampleRate);
loggg(QString(" scratchTargetDelta: %1")
.arg(scratchTargetDelta), adoptSeekPos);
// loggg(QString(" targetDelta: %1").arg(scratchTargetDelta));

bool calcRate = true;

if (m_scratchTargetDelta == scratchTargetDelta) {
loggg(QString("scratchTargetDelta == m_scratchTargetDelta"), adoptSeekPos);
// loggg(QString("scratchTargetDelta == m_scratchTargetDelta"));
// We get here if the next mouse position is delayed, the
// mouse is stopped or moves very slowly. Since we don't
// know the case we assume delayed mouse updates for 40 ms.
Expand All @@ -336,8 +310,8 @@ void PositionScratchController::process(double currentSamplePos,
}
}
} else {
loggg(QString("scratchTargetDelta != m_scratchTargetDelta"), adoptSeekPos);
loggg(QString("m_moveDelay = 0, adopt scratchTargetDelta"), adoptSeekPos);
// loggg(QString("scratchTargetDelta != m_scratchTargetDelta"));
// loggg(QString("m_moveDelay = 0, adopt scratchTargetDelta"));
m_moveDelay = 0;
m_scratchTargetDelta = scratchTargetDelta;
}
Expand Down Expand Up @@ -396,19 +370,16 @@ void PositionScratchController::notifySeek(mixxx::audio::FramePos position) {
// If so, use seek reason?
DEBUG_ASSERT(position.isValid());
double newPos = position.toEngineSamplePos();
if (m_prevSamplePos == newPos) {
loggg(QString("notifySeek: equal --> ignore"));
if (!isEnabled()) {
loggg(QString("notifySeek: not scratching --> ignore"));
return;
} else if (fabs(m_prevSamplePos - newPos) < 100) {
loggg(QString("notifySeek: very close --> ignore"));
return;
} else if (!isEnabled()) {
loggg(QString("notifySeek: not scratching --> ignore"));
} else if (m_prevSamplePos == newPos) {
loggg(QString("notifySeek: equal --> ignore"));
return;
}
if (m_seekWaitCounter == 0) {
warn("!! notifySeek while we still have an unprocessed seek");
}
// ?? Scratching continues after seek due to calculating the relative
// ?? distance traveled in m_samplePosDeltaSum
// Note: it can happen that RateControl is not yet aware of the seek position,
Expand All @@ -420,5 +391,4 @@ void PositionScratchController::notifySeek(mixxx::audio::FramePos position) {
loggg(QString(": ."));
loggg(QString("notifySeek: accept %1").arg(QString::number(newPos, 'g', 18)));
m_seekSamplePos = newPos;
m_seekWaitCounter = 0;
}
6 changes: 0 additions & 6 deletions src/engine/positionscratchcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ class PositionScratchController : public QObject {
void slotUpdateFilterParameters(double sampleRate);

private:
void resetSeekPos() {
m_seekSamplePos = std::numeric_limits<double>::quiet_NaN();
m_seekWaitCounter = -1;
}

void loggg(const QString text, bool condition = true) {

Check failure on line 44 in src/engine/positionscratchcontroller.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on large type (sizeof const class QString is 24 bytes) [-Wclazy-function-args-by-ref]

Check failure on line 44 in src/engine/positionscratchcontroller.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on large type (sizeof const class QString is 24 bytes) [-Wclazy-function-args-by-ref]
if (!condition) {
Expand Down Expand Up @@ -99,6 +95,4 @@ class PositionScratchController : public QObject {
double m_p;
double m_d;
double m_f;

int m_seekWaitCounter;
};

0 comments on commit 2a4d536

Please sign in to comment.