sync: masked waveform correlation, stretch sync, manual start time

- Waveform sync no longer treats uncached waveform regions as silence:
  the envelope extraction now reports a per-window validity mask and the
  correlation skips invalid windows on either side, improving accuracy
  for partially cached clips
- Add stretch/speed sync: AudioWaveformSync::EstimateStretchAndOffset
  searches a playback-rate range plus offset, and a new timeline
  context action 'Synchronize by Waveform (Adjust Speed)' applies the
  estimated rate as a clip speed change (with undo) when plain offset
  alignment is inconclusive
- Footage properties dialog gains a Source Start Time field so the
  value used by source-time sync can be viewed and edited manually
  instead of relying solely on auto-detected metadata; applied via an
  undo command, with Footage::ClearSourceStartTime() for removal
- Regression tests for masked correlation, stretch estimation, the
  envelope validity mask, and source-start-time set/clear
This commit is contained in:
2026-07-16 23:09:12 +08:00
parent 547c2480e0
commit caafac4203
13 changed files with 544 additions and 26 deletions
+93
View File
@@ -87,3 +87,96 @@ TEST(AudioWaveformSync, RejectsSilence)
EXPECT_FALSE(result.valid);
}
TEST(AudioWaveformSync, MaskedEstimationIgnoresInvalidWindows)
{
const QVector<double> reference = { 0.5, 0.5, 0.5, 0.5, 0.9, 0.1,
0.7, 0.2, 0.8, 0.3, 0.6, 0.4 };
// Candidate is the reference delayed by 3 windows, but windows 3..7 were
// never cached (zeroed out) and are flagged invalid
QVector<double> candidate(12, 0.0);
QVector<bool> candidate_valid(12, true);
for (int i = 0; i + 3 < candidate.size(); i++) {
candidate[i + 3] = reference.at(i);
}
for (int i = 3; i <= 7; i++) {
candidate[i] = 0.0;
candidate_valid[i] = false;
}
const olive::AudioWaveformSync::OffsetResult unmasked =
olive::AudioWaveformSync::EstimateEnvelopeOffset(reference, candidate, 1,
8);
const olive::AudioWaveformSync::OffsetResult masked =
olive::AudioWaveformSync::EstimateEnvelopeOffset(
reference, candidate, QVector<bool>(), candidate_valid, 1, 8);
ASSERT_TRUE(masked.valid);
EXPECT_EQ(masked.offset_samples, 3);
EXPECT_GT(masked.confidence, 0.99);
// Ignoring the uncached placeholder windows must not make the estimate
// worse than treating them as silence
if (unmasked.valid) {
EXPECT_GE(masked.confidence, unmasked.confidence);
}
}
TEST(AudioWaveformSync, EstimatesStretchAndOffset)
{
// 20-window reference pattern
const QVector<double> reference = { 0.1, 0.9, 0.2, 0.8, 0.3,
0.7, 0.4, 0.6, 0.5, 1.0,
0.15, 0.85, 0.25, 0.75, 0.35,
0.65, 0.45, 0.55, 0.95, 0.05 };
// Candidate runs at half speed (each window duplicated) and is delayed by
// 6 candidate windows: candidate[j] = reference[(j-6)/2]
QVector<double> candidate(6 + 2 * reference.size(), 0.0);
for (int i = 0; i < reference.size(); i++) {
candidate[6 + 2 * i] = reference.at(i);
candidate[6 + 2 * i + 1] = reference.at(i);
}
const olive::AudioWaveformSync::StretchOffsetResult result =
olive::AudioWaveformSync::EstimateStretchAndOffset(
reference, candidate, QVector<bool>(), QVector<bool>(), 1, 12,
0.8, 2.5, 0.005);
ASSERT_TRUE(result.valid);
EXPECT_NEAR(result.rate, 2.0, 0.01);
// After resampling at 2x, the candidate lags the reference by 3 windows
EXPECT_EQ(result.offset_samples, 3);
EXPECT_GT(result.confidence, 0.95);
}
TEST(AudioWaveformSync, StretchEstimationRejectsSilence)
{
const QVector<double> silence(16, 0.0);
const olive::AudioWaveformSync::StretchOffsetResult result =
olive::AudioWaveformSync::EstimateStretchAndOffset(
silence, silence, QVector<bool>(), QVector<bool>(), 1, 8, 0.5,
2.0, 0.1);
EXPECT_FALSE(result.valid);
}
TEST(AudioWaveformSync, StretchEstimationRejectsInvalidParameters)
{
const QVector<double> envelope = { 0.5, 0.6, 0.7, 0.8 };
EXPECT_FALSE(olive::AudioWaveformSync::EstimateStretchAndOffset(
envelope, envelope, QVector<bool>(), QVector<bool>(), 1,
8, 0.0, 2.0, 0.1)
.valid);
EXPECT_FALSE(olive::AudioWaveformSync::EstimateStretchAndOffset(
envelope, envelope, QVector<bool>(), QVector<bool>(), 1,
8, 2.0, 0.5, 0.1)
.valid);
EXPECT_FALSE(olive::AudioWaveformSync::EstimateStretchAndOffset(
envelope, envelope, QVector<bool>(), QVector<bool>(), 1,
8, 0.5, 2.0, 0.0)
.valid);
}
+29
View File
@@ -129,3 +129,32 @@ TEST(TimecodeMetadata, FootagePersistsSourceStartTime)
EXPECT_EQ(footage.source_start_time(), olive::core::rational(3600));
EXPECT_EQ(footage.source_start_time_source(), QStringLiteral("timecode"));
}
TEST(TimecodeMetadata, FootageClearSourceStartTime)
{
olive::Footage footage;
footage.SetSourceStartTime(olive::core::rational(3600),
QStringLiteral("manual"));
ASSERT_TRUE(footage.HasSourceStartTime());
footage.ClearSourceStartTime();
EXPECT_FALSE(footage.HasSourceStartTime());
EXPECT_EQ(footage.source_start_time(), olive::core::rational());
EXPECT_TRUE(footage.source_start_time_source().isEmpty());
}
TEST(TimecodeMetadata, FootageSetSourceStartTimeOverridesPreviousValue)
{
olive::Footage footage;
footage.SetSourceStartTime(olive::core::rational(3600),
QStringLiteral("timecode"));
// A manual edit replaces both the value and the recorded source
footage.SetSourceStartTime(olive::core::rational(1800),
QStringLiteral("manual"));
EXPECT_TRUE(footage.HasSourceStartTime());
EXPECT_EQ(footage.source_start_time(), olive::core::rational(1800));
EXPECT_EQ(footage.source_start_time_source(), QStringLiteral("manual"));
}
@@ -95,6 +95,36 @@ TEST(TimelineWaveformSync, ExtractEnvelopeUsesOnlyValidatedRanges)
EXPECT_DOUBLE_EQ(envelope.at(59), 0.0);
}
TEST(TimelineWaveformSync, ExtractEnvelopeReportsValidityMask)
{
constexpr int kSampleRate = 48000;
constexpr size_t kWindowSamples = kSampleRate / 20; // 50 ms windows
AudioWaveformCache cache;
WritePartialWaveform(&cache, kSampleRate);
WaveformSyncClip clip;
clip.waveform = &cache;
clip.media_range = TimeRange(0, 3);
clip.sample_rate = kSampleRate;
QVector<bool> valid_mask;
const QVector<double> envelope =
TimelineWaveformSync::ExtractWaveformCacheEnvelope(
clip, kSampleRate, kWindowSamples, &valid_mask);
// One flag per envelope window
ASSERT_EQ(valid_mask.size(), envelope.size());
// Windows outside the validated second are flagged invalid, windows
// inside it are flagged valid
EXPECT_FALSE(valid_mask.at(0));
EXPECT_FALSE(valid_mask.at(59));
for (int i = 20; i < 40; ++i) {
EXPECT_TRUE(valid_mask.at(i));
}
}
TEST(TimelineWaveformSync, PartialCacheIsConsideredReady)
{
constexpr int kSampleRate = 48000;