From 4661553acb865d1d887e5256cac870d5298320e2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 21 Mar 2014 15:40:08 -0700 Subject: [PATCH 1/5] check for cutoff change only every TRAILING_AVERAGE_FRAMES --- assignment-client/src/audio/AudioMixer.cpp | 54 +++++++++++++--------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index 15e1b864eb..cab7af5fd0 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -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" + 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; + 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 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; + + framesSinceCutoffEvent = 0; + } + } else { + framesSinceCutoffEvent++; } foreach (const SharedNodePointer& node, nodeList->getNodeHash()) { From 75bc64010434f4b8b61dc18ebf9f7884fa30eae4 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 21 Mar 2014 15:42:58 -0700 Subject: [PATCH 2/5] fix some indentation is audio mixer cutoff code --- assignment-client/src/audio/AudioMixer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index cab7af5fd0..fd7e5e360d 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -399,7 +399,7 @@ void AudioMixer::run() { audabilityCutoffRatio += (1.0f - audabilityCutoffRatio) / 2.0f; qDebug() << "Mixer is struggling, sleeping" << _trailingSleepRatio * 100 << "% of frame time. Old cutoff was" - << lastCutoffRatio << "and is now" << audabilityCutoffRatio; + << 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 @@ -410,7 +410,7 @@ void AudioMixer::run() { } qDebug() << "Mixer is recovering, sleeping" << _trailingSleepRatio * 100 << "% of frame time. Old cutoff was" - << lastCutoffRatio << "and is now" << audabilityCutoffRatio; + << lastCutoffRatio << "and is now" << audabilityCutoffRatio; hasRatioChanged = true; } From 50007d7f4b8918839bfa2a410b53d9c3281ef775 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 21 Mar 2014 15:43:39 -0700 Subject: [PATCH 3/5] fix a debug message for audability threshold --- assignment-client/src/audio/AudioMixer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index fd7e5e360d..dc066f8857 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -415,9 +415,9 @@ void AudioMixer::run() { } if (hasRatioChanged) { - // set out min required loudness from the new ratio + // set out min audability threshold from the new ratio _minAudibilityThreshold = LOUDNESS_TO_DISTANCE_RATIO / (2.0f * (1.0f - audabilityCutoffRatio)); - qDebug() << "Minimum loudness required to be mixed is now" << _minAudibilityThreshold; + qDebug() << "Minimum audability required to be mixed is now" << _minAudibilityThreshold; framesSinceCutoffEvent = 0; } From dc2d050187568c8606a01c6aedc9b2af23d36da0 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 21 Mar 2014 15:47:21 -0700 Subject: [PATCH 4/5] remove sleep time debug now that the audio-mixer reports struggle --- assignment-client/src/audio/AudioMixer.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index dc066f8857..6754b1eee7 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -454,10 +454,7 @@ void AudioMixer::run() { if (usecToSleep > 0) { usleep(usecToSleep); - } else { - qDebug() << "AudioMixer loop took" << -usecToSleep << "of extra time. Not sleeping."; } - } delete[] clientMixBuffer; From bad209bf09f1b315ede15fe758bdd324ef0fa4a9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 21 Mar 2014 15:53:00 -0700 Subject: [PATCH 5/5] use a trailing average for a quieter loudness --- libraries/audio/src/AudioRingBuffer.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libraries/audio/src/AudioRingBuffer.cpp b/libraries/audio/src/AudioRingBuffer.cpp index 800f4f64ee..efe62de515 100644 --- a/libraries/audio/src/AudioRingBuffer.cpp +++ b/libraries/audio/src/AudioRingBuffer.cpp @@ -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) {