audio: master-clock playback timing, output clock compensation, buffer config, interpolated speed

- playback timer uses the audio output device as its master clock: the
  PortAudio callback counts consumed frames (including underrun
  zero-fill) so video cannot drift away from what is heard; wall clock
  remains as fallback when no clocked output is running
- output clock compensates for device output latency; new Preferences >
  Audio buffer size setting (0 = auto)
- SampleBuffer::speed() now uses linear interpolation instead of
  nearest-neighbor sampling
- regression tests: audio-clock driven timer (fwd/rev/speed), wall
  clock fallback, interpolation correctness
This commit is contained in:
2026-07-19 21:44:34 +08:00
parent 0c02ff0d77
commit a7ddc0f114
16 changed files with 266 additions and 8 deletions
+23
View File
@@ -251,6 +251,29 @@ TEST(CoreSampleBuffer, Speed)
EXPECT_FLOAT_EQ(b.data(0)[1], 0.3f);
}
TEST(CoreSampleBuffer, SpeedInterpolatesBetweenSamples)
{
AudioParams params = make_params();
SampleBuffer b(params, 4);
b.data(0)[0] = 0.0f;
b.data(0)[1] = 1.0f;
b.data(0)[2] = 0.0f;
b.data(0)[3] = -1.0f;
// 0.5x doubles the length; odd output samples sit exactly between two
// input samples and must be interpolated, not nearest-neighbor picked
b.speed(0.5);
ASSERT_EQ(b.sample_count(), 8u);
EXPECT_FLOAT_EQ(b.data(0)[0], 0.0f);
EXPECT_FLOAT_EQ(b.data(0)[1], 0.5f);
EXPECT_FLOAT_EQ(b.data(0)[2], 1.0f);
EXPECT_FLOAT_EQ(b.data(0)[3], 0.5f);
EXPECT_FLOAT_EQ(b.data(0)[4], 0.0f);
EXPECT_FLOAT_EQ(b.data(0)[5], -0.5f);
EXPECT_FLOAT_EQ(b.data(0)[6], -1.0f);
EXPECT_FLOAT_EQ(b.data(0)[7], -1.0f); // clamped at the last sample
}
TEST(CoreSampleBuffer, UnallocatedOperationsNoCrash)
{
SampleBuffer b;
+55
View File
@@ -145,6 +145,61 @@ TEST(ViewerSmokeTimer, ZeroSpeed)
EXPECT_EQ(ts1, ts2);
}
class FakeAudioClock : public PlaybackAudioClock {
public:
virtual double seconds() const override
{
return seconds_;
}
double seconds_ = -1.0;
};
TEST(ViewerSmokeTimer, AudioClockDrivesTimestamp)
{
FakeAudioClock clock;
ViewerPlaybackTimer timer;
// Start at timestamp 100, 1x speed, 32fps (exact in floating point)
timer.start(100, 1, 1.0 / 32.0, &clock);
// One second of consumed audio = 32 frames, regardless of wall time
clock.seconds_ = 1.0;
EXPECT_EQ(timer.get_timestamp_now(), 132);
// The audio clock fully overrides the wall clock (no sleep involved)
clock.seconds_ = 0.5;
EXPECT_EQ(timer.get_timestamp_now(), 116);
}
TEST(ViewerSmokeTimer, AudioClockScalesWithPlaybackSpeed)
{
FakeAudioClock clock;
ViewerPlaybackTimer timer;
// At 2x speed one output second is two timeline seconds
timer.start(0, 2, 1.0 / 32.0, &clock);
clock.seconds_ = 0.5;
EXPECT_EQ(timer.get_timestamp_now(), 32);
// In reverse the playhead moves backward at |speed|
timer.start(100, -2, 1.0 / 32.0, &clock);
clock.seconds_ = 1.0;
EXPECT_EQ(timer.get_timestamp_now(), 36);
}
TEST(ViewerSmokeTimer, InvalidAudioClockFallsBackToWallClock)
{
FakeAudioClock clock; // seconds() returns -1: no clocked output running
ViewerPlaybackTimer timer;
timer.start(0, 1, 1.0 / 24.0, &clock);
EXPECT_GE(timer.get_timestamp_now(), 0);
QThread::msleep(50); // 50ms > one 24fps frame period
EXPECT_GT(timer.get_timestamp_now(), 0);
}
// ============================================================================
// Smoke Test: ViewerQueue
// ============================================================================