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 @@

23Latency and Visona Sync

24Visona Sync with the app

-

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.

  1. In your DAW, add Visona Sync on an instrument track and send that track straight to a free output of your interface, such as 3/4, not to the master. Keep it off your speakers: it is a click on every bar.
  2. Connect that output to a free input of the interface Visona listens to, with a cable or the interface's loopback routing (TotalMix on an RME interface, for example).
  3. diff --git a/tests/AnalysisPipelineTests.cpp b/tests/AnalysisPipelineTests.cpp index b94ab7a..397ed57 100644 --- a/tests/AnalysisPipelineTests.cpp +++ b/tests/AnalysisPipelineTests.cpp @@ -851,7 +851,7 @@ TEST_CASE("Stop, SPP and Continue relocate the head without clearing", "[analysi TEST_CASE("Visona Sync on the sync input moves MIDI Clock onto the audio's bar lines", "[analysis][midi][sync]") { - const auto lag = GENERATE(1'234.0, -700.0); + const auto lag = GENERATE(1'234.0, -700.0, -36'000.0); CAPTURE(lag); // 120 BPM at 48 kHz: 1 000 frames per tick and 96 000 per bar. @@ -876,6 +876,7 @@ TEST_CASE("Visona Sync on the sync input moves MIDI Clock onto the audio's bar l session.run(static_cast(firstClock + 2.5 * barFrames), click, impulse); auto snapshot = session.snapshot(); REQUIRE(snapshot.syncState == visona::SidechainSyncState::locked); + CHECK(snapshot.transportState == TransportState::running); CHECK(std::abs(snapshot.syncOffsetFrames - lag) <= 1.0); CHECK(snapshot.syncImpulsePeak == 0.5f); @@ -1166,3 +1167,23 @@ TEST_CASE("AnalysisPipeline follows MIDI Clock across threads", "[analysis][midi CHECK(snapshots.readBuffer().musical); CHECK(snapshots.readBuffer().transportState == TransportState::running); } + +TEST_CASE("AnalysisPipeline checks MIDI clock loss before alignment offsets", "[analysis][midi]") +{ + const auto offset = GENERATE(-36'000.0, 36'000.0); + CAPTURE(offset); + MidiSession session(48'000.0, false, 0.0); + session.pipeline().setMidiOffset(offset); + constexpr double firstClock = 48'000.0; + session.midi(MidiClockEvent::Type::Start, firstClock); + const auto end = session.clocks(firstClock, 200, session.framesPerTick(120.0)); + for (auto frame = 60'000u; frame < static_cast(end); frame += 4'096) + { + session.run(frame); + REQUIRE(session.snapshot().transportState == TransportState::running); + } + session.run(static_cast(end + 48'000.0 * 0.45)); + CHECK(session.snapshot().transportState == TransportState::running); + session.run(static_cast(end + 48'000.0 * 0.55)); + CHECK(session.snapshot().transportState == TransportState::clockLost); +} diff --git a/tests/MidiClockTransportTests.cpp b/tests/MidiClockTransportTests.cpp index 4238129..2b95a43 100644 --- a/tests/MidiClockTransportTests.cpp +++ b/tests/MidiClockTransportTests.cpp @@ -687,3 +687,34 @@ TEST_CASE("MidiClockTransport does not allocate", "[transport][realtime]") CHECK(allocationCount == 0); } + +TEST_CASE("MIDI clock loss ignores musical alignment offsets", "[transport][midi]") +{ + const auto offset = GENERATE(-0.75 * rate, 0.75 * rate); + const auto startType = GENERATE(Type::Start, Type::Continue); + CAPTURE(offset, startType); + MidiClockTransport transport(rate); + const double start = 2.0 * rate; + transport.handle(startType, 0, start, offset); + transport.advanceTo(start + 0.49 * rate); + REQUIRE(transport.state() == TransportState::running); + transport.advanceTo(start + 0.51 * rate); + REQUIRE(transport.state() == TransportState::clockLost); + + // Fresh clocks recover and stay running with either sign of correction. + double now = start + rate; + for (int tick = 0; tick < 100; ++tick) + { + now += framesPerTick(120.0); + transport.handle(Type::Clock, 0, now, offset); + transport.advanceTo(now); + REQUIRE(transport.state() == TransportState::running); + } + CHECK(transport.span(transport.numSpans() - 1).start == now + offset); + transport.advanceTo(now + 0.49 * rate); + CHECK(transport.state() == TransportState::running); + // A Continue while running does not postpone genuine clock loss. + transport.handle(Type::Continue, 0, now + 0.49 * rate, offset); + transport.advanceTo(now + 0.51 * rate); + CHECK(transport.state() == TransportState::clockLost); +}