Merge pull request #2439 from birarda/master

check for an audio cutoff change only every TRAILING_AVERAGE_FRAMES
This commit is contained in:
Stephen Birarda 2014-03-21 16:21:26 -07:00
commit 949aefac62
2 changed files with 41 additions and 28 deletions

View file

@ -364,6 +364,9 @@ void AudioMixer::run() {
int usecToSleep = BUFFER_SEND_INTERVAL_USECS;
float audabilityCutoffRatio = 0;
const int TRAILING_AVERAGE_FRAMES = 100;
int framesSinceCutoffEvent = TRAILING_AVERAGE_FRAMES;
while (!_isFinished) {
@ -374,10 +377,9 @@ void AudioMixer::run() {
}
const float STRUGGLE_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.10f;
const float BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.30f;
const float BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.20f;
const float CUTOFF_EPSILON = 0.0001f;
const int TRAILING_AVERAGE_FRAMES = 100;
const float CURRENT_FRAME_RATIO = 1.0f / TRAILING_AVERAGE_FRAMES;
const float PREVIOUS_FRAMES_RATIO = 1.0f - CURRENT_FRAME_RATIO;
@ -391,30 +393,36 @@ void AudioMixer::run() {
float lastCutoffRatio = audabilityCutoffRatio;
bool hasRatioChanged = false;
if (_trailingSleepRatio <= STRUGGLE_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD) {
// we're struggling - change our min required loudness to reduce some load
audabilityCutoffRatio += (1.0f - audabilityCutoffRatio) / 2.0f;
qDebug() << "Mixer is struggling, sleeping" << _trailingSleepRatio * 100 << "% of frame time. Old cutoff was"
<< lastCutoffRatio << "and is now" << audabilityCutoffRatio;
hasRatioChanged = true;
} else if (_trailingSleepRatio >= BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD && audabilityCutoffRatio != 0) {
// we've recovered and can back off the required loudness
audabilityCutoffRatio -= audabilityCutoffRatio / 2.0f;
if (audabilityCutoffRatio < CUTOFF_EPSILON) {
audabilityCutoffRatio = 0.0f;
if (framesSinceCutoffEvent >= TRAILING_AVERAGE_FRAMES) {
if (_trailingSleepRatio <= STRUGGLE_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD) {
// we're struggling - change our min required loudness to reduce some load
audabilityCutoffRatio += (1.0f - audabilityCutoffRatio) / 2.0f;
qDebug() << "Mixer is struggling, sleeping" << _trailingSleepRatio * 100 << "% of frame time. Old cutoff was"
<< lastCutoffRatio << "and is now" << audabilityCutoffRatio;
hasRatioChanged = true;
} else if (_trailingSleepRatio >= BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD && audabilityCutoffRatio != 0) {
// we've recovered and can back off the required loudness
audabilityCutoffRatio -= audabilityCutoffRatio / 2.0f;
if (audabilityCutoffRatio < CUTOFF_EPSILON) {
audabilityCutoffRatio = 0.0f;
}
qDebug() << "Mixer is recovering, sleeping" << _trailingSleepRatio * 100 << "% of frame time. Old cutoff was"
<< lastCutoffRatio << "and is now" << audabilityCutoffRatio;
hasRatioChanged = true;
}
qDebug() << "Mixer is recovering, sleeping" << _trailingSleepRatio * 100 << "% of frame time. Old cutoff was"
<< lastCutoffRatio << "and is now" << audabilityCutoffRatio;
hasRatioChanged = true;
}
if (hasRatioChanged) {
// set out min required loudness from the new ratio
_minAudibilityThreshold = LOUDNESS_TO_DISTANCE_RATIO / (2.0f * (1.0f - audabilityCutoffRatio));
qDebug() << "Minimum loudness required to be mixed is now" << _minAudibilityThreshold;
if (hasRatioChanged) {
// set out min audability threshold from the new ratio
_minAudibilityThreshold = LOUDNESS_TO_DISTANCE_RATIO / (2.0f * (1.0f - audabilityCutoffRatio));
qDebug() << "Minimum audability required to be mixed is now" << _minAudibilityThreshold;
framesSinceCutoffEvent = 0;
}
} else {
framesSinceCutoffEvent++;
}
foreach (const SharedNodePointer& node, nodeList->getNodeHash()) {
@ -446,10 +454,7 @@ void AudioMixer::run() {
if (usecToSleep > 0) {
usleep(usecToSleep);
} else {
qDebug() << "AudioMixer loop took" << -usecToSleep << "of extra time. Not sleeping.";
}
}
delete[] clientMixBuffer;

View file

@ -67,7 +67,15 @@ void AudioRingBuffer::updateAverageLoudnessForBoundarySamples(int numSamples) {
nextLoudness /= numSamples;
nextLoudness /= MAX_SAMPLE_VALUE;
_averageLoudness = nextLoudness;
const int TRAILING_AVERAGE_FRAMES = 100;
const float CURRENT_FRAME_RATIO = 1 / TRAILING_AVERAGE_FRAMES;
const float PREVIOUS_FRAMES_RATIO = 1 - CURRENT_FRAME_RATIO;
if (nextLoudness >= _averageLoudness) {
_averageLoudness = nextLoudness;
} else {
_averageLoudness = (_averageLoudness * PREVIOUS_FRAMES_RATIO) + (CURRENT_FRAME_RATIO * nextLoudness);
}
}
qint64 AudioRingBuffer::readSamples(int16_t* destination, qint64 maxSamples) {