calculate a cutoff loudness for mixer recovery

This commit is contained in:
Stephen Birarda 2014-03-20 12:52:17 -07:00
parent fb73b6e1ce
commit a8ef64e0ce
4 changed files with 46 additions and 4 deletions

View file

@ -64,7 +64,9 @@ void attachNewBufferToNode(Node *newNode) {
AudioMixer::AudioMixer(const QByteArray& packet) :
ThreadedAssignment(packet),
_minSourceLoudnessInFrame(1.0f),
_maxSourceLoudnessInFrame(0.0f)
_maxSourceLoudnessInFrame(0.0f),
_loudnessCutoffRatio(0.0f),
_minRequiredLoudness(0.0f)
{
}
@ -352,6 +354,9 @@ void AudioMixer::run() {
char* clientMixBuffer = new char[NETWORK_BUFFER_LENGTH_BYTES_STEREO
+ numBytesForPacketHeaderGivenPacketType(PacketTypeMixedAudio)];
int usecToSleep = 0;
bool isFirstRun = true;
while (!_isFinished) {
@ -365,6 +370,39 @@ void AudioMixer::run() {
_maxSourceLoudnessInFrame);
}
}
if (!isFirstRun) {
const float STRUGGLE_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.10;
const float BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD = 0.30;
const float CUTOFF_EPSILON = 0.0001;
float percentageSleep = (usecToSleep / (float) BUFFER_SEND_INTERVAL_USECS);
float lastCutoffRatio = _loudnessCutoffRatio;
if (percentageSleep <= STRUGGLE_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD || usecToSleep < 0) {
// we're struggling - change our min required loudness to reduce some load
_loudnessCutoffRatio += (1 - _loudnessCutoffRatio) / 2;
qDebug() << "Mixer is struggling, sleeping" << percentageSleep * 100 << "% of frame time. Old cutoff was"
<< lastCutoffRatio << "and is now" << _loudnessCutoffRatio;
} else if (percentageSleep >= BACK_OFF_TRIGGER_SLEEP_PERCENTAGE_THRESHOLD && _loudnessCutoffRatio != 0) {
// we've recovered and can back off the required loudness
_loudnessCutoffRatio -= _loudnessCutoffRatio / 2;
if (_loudnessCutoffRatio < CUTOFF_EPSILON) {
_loudnessCutoffRatio = 0;
}
qDebug() << "Mixer is recovering, sleeping" << percentageSleep * 100 << "% of frame time. Old cutoff was"
<< lastCutoffRatio << "and is now" << _loudnessCutoffRatio;
}
// set out min required loudness from the new ratio
_minRequiredLoudness = _loudnessCutoffRatio * (_maxSourceLoudnessInFrame - _minSourceLoudnessInFrame);
} else {
isFirstRun = false;
}
foreach (const SharedNodePointer& node, nodeList->getNodeHash()) {
if (node->getType() == NodeType::Agent && node->getActiveSocket() && node->getLinkedData()
@ -391,7 +429,7 @@ void AudioMixer::run() {
break;
}
int usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
if (usecToSleep > 0) {
usleep(usecToSleep);

View file

@ -37,10 +37,13 @@ private:
void prepareMixForListeningNode(Node* node);
// client samples capacity is larger than what will be sent to optimize mixing
int16_t _clientSamples[NETWORK_BUFFER_LENGTH_SAMPLES_STEREO + SAMPLE_PHASE_DELAY_AT_90];
// we are MMX adding 4 samples at a time so we need client samples to have an extra 4
int16_t _clientSamples[NETWORK_BUFFER_LENGTH_SAMPLES_STEREO + (SAMPLE_PHASE_DELAY_AT_90 * 2)];
float _minSourceLoudnessInFrame;
float _maxSourceLoudnessInFrame;
float _loudnessCutoffRatio;
float _minRequiredLoudness;
};
#endif /* defined(__hifi__AudioMixer__) */

View file

@ -97,7 +97,7 @@ void AudioMixerClientData::checkBuffersBeforeFrameSend(int jitterBufferLengthSam
// that would be mixed in
_nextOutputLoudness = _ringBuffers[i]->averageLoudnessForBoundarySamples(NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL);
if (_nextOutputLoudness < currentMinLoudness) {
if (_nextOutputLoudness != 0 && _nextOutputLoudness < currentMinLoudness) {
currentMinLoudness = _nextOutputLoudness;
}

View file

@ -50,6 +50,7 @@ int PositionalAudioRingBuffer::parseData(const QByteArray& packet) {
int16_t numSilentSamples;
memcpy(&numSilentSamples, packet.data() + readBytes, sizeof(int16_t));
readBytes += sizeof(int16_t);
addSilentFrame(numSilentSamples);