mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 05:48:26 +02:00
use a steadier clock for AudioMixer block timing
This commit is contained in:
parent
3e03f85f07
commit
0fd32b36ad
1 changed files with 15 additions and 12 deletions
|
@ -19,6 +19,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
@ -577,15 +578,14 @@ void AudioMixer::domainSettingsRequestComplete() {
|
||||||
void AudioMixer::broadcastMixes() {
|
void AudioMixer::broadcastMixes() {
|
||||||
auto nodeList = DependencyManager::get<NodeList>();
|
auto nodeList = DependencyManager::get<NodeList>();
|
||||||
|
|
||||||
int64_t nextFrame = 0;
|
auto nextFrameTimestamp = p_high_resolution_clock::now();
|
||||||
QElapsedTimer timer;
|
auto timeToSleep = std::chrono::microseconds(0);
|
||||||
timer.start();
|
|
||||||
|
|
||||||
int64_t usecToSleep = AudioConstants::NETWORK_FRAME_USECS;
|
|
||||||
|
|
||||||
const int TRAILING_AVERAGE_FRAMES = 100;
|
const int TRAILING_AVERAGE_FRAMES = 100;
|
||||||
int framesSinceCutoffEvent = TRAILING_AVERAGE_FRAMES;
|
int framesSinceCutoffEvent = TRAILING_AVERAGE_FRAMES;
|
||||||
|
|
||||||
|
int currentFrame { 1 };
|
||||||
|
|
||||||
while (!_isFinished) {
|
while (!_isFinished) {
|
||||||
const float STRUGGLE_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.10f;
|
const float STRUGGLE_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.10f;
|
||||||
const float BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.20f;
|
const float BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.20f;
|
||||||
|
@ -595,12 +595,12 @@ void AudioMixer::broadcastMixes() {
|
||||||
const float CURRENT_FRAME_RATIO = 1.0f / TRAILING_AVERAGE_FRAMES;
|
const float CURRENT_FRAME_RATIO = 1.0f / TRAILING_AVERAGE_FRAMES;
|
||||||
const float PREVIOUS_FRAMES_RATIO = 1.0f - CURRENT_FRAME_RATIO;
|
const float PREVIOUS_FRAMES_RATIO = 1.0f - CURRENT_FRAME_RATIO;
|
||||||
|
|
||||||
if (usecToSleep < 0) {
|
if (timeToSleep.count() < 0) {
|
||||||
usecToSleep = 0;
|
timeToSleep = std::chrono::microseconds(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
_trailingSleepRatio = (PREVIOUS_FRAMES_RATIO * _trailingSleepRatio)
|
_trailingSleepRatio = (PREVIOUS_FRAMES_RATIO * _trailingSleepRatio)
|
||||||
+ (usecToSleep * CURRENT_FRAME_RATIO / (float) AudioConstants::NETWORK_FRAME_USECS);
|
+ (timeToSleep.count() * CURRENT_FRAME_RATIO / (float) AudioConstants::NETWORK_FRAME_USECS);
|
||||||
|
|
||||||
float lastCutoffRatio = _performanceThrottlingRatio;
|
float lastCutoffRatio = _performanceThrottlingRatio;
|
||||||
bool hasRatioChanged = false;
|
bool hasRatioChanged = false;
|
||||||
|
@ -718,11 +718,14 @@ void AudioMixer::broadcastMixes() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
usecToSleep = (++nextFrame * AudioConstants::NETWORK_FRAME_USECS) - (timer.nsecsElapsed() / 1000);
|
// push the next frame timestamp to when we should send the next
|
||||||
|
nextFrameTimestamp += std::chrono::microseconds(AudioConstants::NETWORK_FRAME_USECS);
|
||||||
|
|
||||||
if (usecToSleep > 0) {
|
// sleep as long as we need until next frame, if we can
|
||||||
usleep(usecToSleep);
|
auto now = p_high_resolution_clock::now();
|
||||||
}
|
timeToSleep = std::chrono::duration_cast<std::chrono::microseconds>(nextFrameTimestamp - now);
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(timeToSleep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue