diff --git a/core/include/visona/MidiClockTransport.h b/core/include/visona/MidiClockTransport.h index ba4dee9..12f3209 100644 --- a/core/include/visona/MidiClockTransport.h +++ b/core/include/visona/MidiClockTransport.h @@ -107,8 +107,10 @@ class MidiClockTransport void reset(double sampleRate) noexcept; /** Handles one message at stream position `sampleTime`. `sppValue` is for Song Position - Pointer only. */ - void handle(MidiClockEvent::Type type, std::uint16_t sppValue, double sampleTime) noexcept; + Pointer only. `timelineOffset` aligns the musical spans with audio, but does not + change the clock-loss deadline. */ + void handle(MidiClockEvent::Type type, std::uint16_t sppValue, double sampleTime, + double timelineOffset = 0.0) noexcept; /** Leaves Stopped or clock loss for the free-running sweep from `sampleTime` on, as before the @@ -194,9 +196,9 @@ class MidiClockTransport std::uint64_t startCount_ = 0; std::uint64_t ignoredSpp_ = 0; - // While running: either waiting for the first clock since runStart_, or following ticks. + // The clock-loss deadline uses unshifted event times, independently of musical alignment. bool awaitingTick_ = false; - double runStart_ = 0.0; + double lastClockActivity_ = 0.0; double lastTickTime_ = 0.0; double lastInterval_ = 0.0; diff --git a/core/src/AnalysisPipeline.cpp b/core/src/AnalysisPipeline.cpp index 20a16a4..f4e1967 100644 --- a/core/src/AnalysisPipeline.cpp +++ b/core/src/AnalysisPipeline.cpp @@ -251,8 +251,8 @@ bool AnalysisPipeline::handleMidi() noexcept ++midiEvents_; changed_ = true; if (ring_ != nullptr) - transport_.handle(event->type, event->sppValue, - mapper_.sampleTimeOf(event->hostTimeNs) + offset); + transport_.handle(event->type, event->sppValue, mapper_.sampleTimeOf(event->hostTimeNs), + offset); } return true; } diff --git a/core/src/MidiClockTransport.cpp b/core/src/MidiClockTransport.cpp index 3dd9815..e70eb95 100644 --- a/core/src/MidiClockTransport.cpp +++ b/core/src/MidiClockTransport.cpp @@ -43,6 +43,7 @@ void MidiClockTransport::reset(double sampleRate) noexcept ignoredSpp_ = 0; awaitingTick_ = false; lastInterval_ = 0.0; + lastClockActivity_ = 0.0; tempoCount_ = 0; framesPerClock_ = 0.0; tempoFollowClocks_ = 0; @@ -52,9 +53,15 @@ void MidiClockTransport::reset(double sampleRate) noexcept } void MidiClockTransport::handle(MidiClockEvent::Type type, std::uint16_t sppValue, - double sampleTime) noexcept + double sampleTime, double timelineOffset) noexcept { - sampleTime = clampToTimeline(sampleTime); + // A sync correction can exceed the timeout in either direction. Only the original + // event time tells us whether MIDI Clock is still arriving. An ignored Continue must + // not keep an already running transport alive. + if (type == MidiClockEvent::Type::Clock || type == MidiClockEvent::Type::Start || + (type == MidiClockEvent::Type::Continue && state_ != TransportState::running)) + lastClockActivity_ = sampleTime; + sampleTime = clampToTimeline(sampleTime + timelineOffset); switch (type) { case MidiClockEvent::Type::Clock: @@ -97,8 +104,7 @@ void MidiClockTransport::advanceTo(double sampleTime) noexcept if (state_ != TransportState::running) return; const auto timeout = clockLossSeconds * sampleRate_; - const auto lastSeen = awaitingTick_ ? runStart_ : lastTickTime_; - if (sampleTime - lastSeen <= timeout) + if (sampleTime - lastClockActivity_ <= timeout) return; state_ = TransportState::clockLost; @@ -288,7 +294,6 @@ void MidiClockTransport::onStart(double sampleTime) noexcept tempoFollowClocks_ = 0; state_ = TransportState::running; awaitingTick_ = true; - runStart_ = sampleTime; closeOpenAt(sampleTime); pushOpen(TransportSpan::Kind::frozen, sampleTime, 0.0); } @@ -303,7 +308,6 @@ void MidiClockTransport::onContinue(double sampleTime) noexcept ++startCount_; state_ = TransportState::running; awaitingTick_ = true; - runStart_ = sampleTime; closeOpenAt(sampleTime); pushOpen(TransportSpan::Kind::frozen, sampleTime, 0.0); } diff --git a/site/static/manual/index.html b/site/static/manual/index.html index 6be3251..e3dd2d4 100644 --- a/site/static/manual/index.html +++ b/site/static/manual/index.html @@ -369,7 +369,7 @@
The app allows for its own input latency, but not for what lies between your DAW's MIDI Clock and the audio reaching the interface: the DAW's output latency, clock delay settings and the interfaces on the way. Visona Sync measures that too, if its impulse comes in on an input of its own.
+The app allows for its own input latency, but not for what lies between your DAW's MIDI Clock and the audio reaching the interface: the DAW's output latency, clock delay settings and the interfaces on the way. Visona Sync measures that too, if its impulse comes in on an input of its own. This alignment correction does not affect MIDI Clock loss detection: the warning appears only when clock pulses stop arriving for more than half a second.