From aea9775e4dba802a6ac766b9757020ea43a5f94e Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 5 Sep 2018 16:55:46 -0700 Subject: [PATCH] get back to ideal frame time when slow --- assignment-client/src/audio/AudioMixer.cpp | 29 +++++++++++----------- assignment-client/src/audio/AudioMixer.h | 5 +++- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index bba3398cbd..b5318a72d9 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -386,15 +386,18 @@ void AudioMixer::start() { // mix state unsigned int frame = 1; - auto frameTimestamp = p_high_resolution_clock::now(); while (!_isFinished) { auto ticTimer = _ticTiming.timer(); - { - auto timer = _checkTimeTiming.timer(); - auto frameDuration = timeFrame(frameTimestamp); - throttle(frameDuration, frame); + if (_startFrameTimestamp.time_since_epoch().count() == 0) { + _startFrameTimestamp = _idealFrameTimestamp = p_high_resolution_clock::now(); + } else { + { + auto timer = _checkTimeTiming.timer(); + auto frameDuration = timeFrame(); + throttle(frameDuration, frame); + } } auto frameTimer = _frameTiming.timer(); @@ -449,26 +452,22 @@ void AudioMixer::start() { } } -chrono::microseconds AudioMixer::timeFrame(p_high_resolution_clock::time_point& timestamp) { +chrono::microseconds AudioMixer::timeFrame() { // advance the next frame - auto nextTimestamp = timestamp + chrono::microseconds(AudioConstants::NETWORK_FRAME_USECS); auto now = p_high_resolution_clock::now(); // compute how long the last frame took - auto duration = chrono::duration_cast(now - timestamp); + auto duration = chrono::duration_cast(now - _startFrameTimestamp); - // set the new frame timestamp - timestamp = max(now, nextTimestamp); + _idealFrameTimestamp += chrono::microseconds(AudioConstants::NETWORK_FRAME_USECS); { auto timer = _sleepTiming.timer(); - - // sleep until the next frame should start - // WIN32 sleep_until is broken until VS2015 Update 2 - // instead, max (above) guarantees that timestamp >= now, so we can sleep_for - this_thread::sleep_for(timestamp - now); + this_thread::sleep_until(_idealFrameTimestamp); } + _startFrameTimestamp = p_high_resolution_clock::now(); + return duration; } diff --git a/assignment-client/src/audio/AudioMixer.h b/assignment-client/src/audio/AudioMixer.h index 99b01683bf..b8ea0d5c58 100644 --- a/assignment-client/src/audio/AudioMixer.h +++ b/assignment-client/src/audio/AudioMixer.h @@ -84,7 +84,7 @@ private slots: private: // mixing helpers - std::chrono::microseconds timeFrame(p_high_resolution_clock::time_point& timestamp); + std::chrono::microseconds timeFrame(); void throttle(std::chrono::microseconds frameDuration, int frame); AudioMixerClientData* getOrCreateClientData(Node* node); @@ -94,6 +94,9 @@ private: void parseSettingsObject(const QJsonObject& settingsObject); void clearDomainSettings(); + p_high_resolution_clock::time_point _idealFrameTimestamp; + p_high_resolution_clock::time_point _startFrameTimestamp; + float _trailingMixRatio { 0.0f }; float _throttlingRatio { 0.0f };