Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions core/include/visona/MidiClockTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions core/src/AnalysisPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 10 additions & 6 deletions core/src/MidiClockTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion site/static/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ <h2><span class="num">23</span>Latency and Visona Sync</h2>

<section id="sync-app">
<h2><span class="num">24</span>Visona Sync with the app</h2>
<p>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.</p>
<p>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.</p>
<ol>
<li>In your DAW, add <strong>Visona Sync</strong> 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.</li>
<li>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).</li>
Expand Down
23 changes: 22 additions & 1 deletion tests/AnalysisPipelineTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<std::uint64_t>(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);

Expand Down Expand Up @@ -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<unsigned>(end); frame += 4'096)
{
session.run(frame);
REQUIRE(session.snapshot().transportState == TransportState::running);
}
session.run(static_cast<std::uint64_t>(end + 48'000.0 * 0.45));
CHECK(session.snapshot().transportState == TransportState::running);
session.run(static_cast<std::uint64_t>(end + 48'000.0 * 0.55));
CHECK(session.snapshot().transportState == TransportState::clockLost);
}
31 changes: 31 additions & 0 deletions tests/MidiClockTransportTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading