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
+4
View File
@@ -1384,6 +1384,10 @@ void ViewerWidget::finish_play_preprocess()
int64_t playback_start_time = get_timestamp();
// Restart the audio output clock for this playback run; the playback
// timer uses it as its master clock
AudioManager::instance()->reset_output_clock();
// Start audio waveform playback
if (!prequeued_audio_.isEmpty()) {
QString error;
+3 -1
View File
@@ -35,6 +35,7 @@
#include <QScreen>
#include <QTextEdit>
#include "audio/audiomanager.h"
#include "common/define.h"
#include "common/html.h"
#include "common/qtutils.h"
@@ -1632,7 +1633,8 @@ void ViewerDisplayWidget::play(const int64_t &start_timestamp,
playback_timebase_ = timebase;
playback_speed_ = playback_speed;
timer_.start(start_timestamp, playback_speed, timebase.to_double());
timer_.start(start_timestamp, playback_speed, timebase.to_double(),
AudioManager::instance());
if (start_updating) {
connect(this, &ViewerDisplayWidget::frame_swapped, this,
+21 -1
View File
@@ -28,16 +28,36 @@ namespace olive
void ViewerPlaybackTimer::start(const int64_t &start_timestamp,
const int &playback_speed,
const double &timebase)
const double &timebase,
const PlaybackAudioClock *audio_clock)
{
timer_.start();
start_timestamp_ = start_timestamp;
playback_speed_ = playback_speed;
timebase_ = timebase * 1000;
audio_clock_ = audio_clock;
}
int64_t ViewerPlaybackTimer::get_timestamp_now() const
{
// The audio output clock is the master clock when available: sound card
// consumption is what the viewer must stay in sync with, and unlike the
// wall clock it cannot drift away from what is actually heard
if (audio_clock_) {
const double audio_seconds = audio_clock_->seconds();
if (audio_seconds >= 0.0) {
// At speeds other than 1x the audio tempo is scaled, so one
// output second corresponds to |speed| timeline seconds. The
// result is already in timeline frames, so it is applied
// signed rather than multiplied by the speed again.
const double timeline_ms =
audio_seconds * 1000.0 * qAbs(playback_speed_);
const int64_t frames_since_start = qFloor(timeline_ms / timebase_);
return start_timestamp_ +
frames_since_start * (playback_speed_ < 0 ? -1 : 1);
}
}
int64_t real_time = timer_.elapsed();
int64_t frames_since_start =
+5 -1
View File
@@ -26,6 +26,7 @@
#include <QElapsedTimer>
#include "common/define.h"
#include "common/playbackaudioclock.h"
namespace olive
{
@@ -33,7 +34,8 @@ namespace olive
class ViewerPlaybackTimer {
public:
void start(const int64_t &start_timestamp, const int &playback_speed,
const double &timebase);
const double &timebase,
const PlaybackAudioClock *audio_clock = nullptr);
int64_t get_timestamp_now() const;
@@ -44,6 +46,8 @@ private:
int playback_speed_;
double timebase_;
const PlaybackAudioClock *audio_clock_ = nullptr;
};
}