mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-06-21 08:49:13 +02:00
calculate a cutoff loudness for mixer recovery
This commit is contained in:
parent
fb73b6e1ce
commit
a8ef64e0ce
4 changed files with 46 additions and 4 deletions
|
@ -64,7 +64,9 @@ void attachNewBufferToNode(Node *newNode) {
|
||||||
AudioMixer::AudioMixer(const QByteArray& packet) :
|
AudioMixer::AudioMixer(const QByteArray& packet) :
|
||||||
ThreadedAssignment(packet),
|
ThreadedAssignment(packet),
|
||||||
_minSourceLoudnessInFrame(1.0f),
|
_minSourceLoudnessInFrame(1.0f),
|
||||||
_maxSourceLoudnessInFrame(0.0f)
|
_maxSourceLoudnessInFrame(0.0f),
|
||||||
|
_loudnessCutoffRatio(0.0f),
|
||||||
|
_minRequiredLoudness(0.0f)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -353,6 +355,9 @@ void AudioMixer::run() {
|
||||||
char* clientMixBuffer = new char[NETWORK_BUFFER_LENGTH_BYTES_STEREO
|
char* clientMixBuffer = new char[NETWORK_BUFFER_LENGTH_BYTES_STEREO
|
||||||
+ numBytesForPacketHeaderGivenPacketType(PacketTypeMixedAudio)];
|
+ numBytesForPacketHeaderGivenPacketType(PacketTypeMixedAudio)];
|
||||||
|
|
||||||
|
int usecToSleep = 0;
|
||||||
|
bool isFirstRun = true;
|
||||||
|
|
||||||
while (!_isFinished) {
|
while (!_isFinished) {
|
||||||
|
|
||||||
_minSourceLoudnessInFrame = 1.0f;
|
_minSourceLoudnessInFrame = 1.0f;
|
||||||
|
@ -366,6 +371,39 @@ void AudioMixer::run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()) {
|
foreach (const SharedNodePointer& node, nodeList->getNodeHash()) {
|
||||||
if (node->getType() == NodeType::Agent && node->getActiveSocket() && node->getLinkedData()
|
if (node->getType() == NodeType::Agent && node->getActiveSocket() && node->getLinkedData()
|
||||||
&& ((AudioMixerClientData*) node->getLinkedData())->getAvatarAudioRingBuffer()) {
|
&& ((AudioMixerClientData*) node->getLinkedData())->getAvatarAudioRingBuffer()) {
|
||||||
|
@ -391,7 +429,7 @@ void AudioMixer::run() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
||||||
|
|
||||||
if (usecToSleep > 0) {
|
if (usecToSleep > 0) {
|
||||||
usleep(usecToSleep);
|
usleep(usecToSleep);
|
||||||
|
|
|
@ -37,10 +37,13 @@ private:
|
||||||
void prepareMixForListeningNode(Node* node);
|
void prepareMixForListeningNode(Node* node);
|
||||||
|
|
||||||
// client samples capacity is larger than what will be sent to optimize mixing
|
// 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 _minSourceLoudnessInFrame;
|
||||||
float _maxSourceLoudnessInFrame;
|
float _maxSourceLoudnessInFrame;
|
||||||
|
float _loudnessCutoffRatio;
|
||||||
|
float _minRequiredLoudness;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* defined(__hifi__AudioMixer__) */
|
#endif /* defined(__hifi__AudioMixer__) */
|
||||||
|
|
|
@ -97,7 +97,7 @@ void AudioMixerClientData::checkBuffersBeforeFrameSend(int jitterBufferLengthSam
|
||||||
// that would be mixed in
|
// that would be mixed in
|
||||||
_nextOutputLoudness = _ringBuffers[i]->averageLoudnessForBoundarySamples(NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL);
|
_nextOutputLoudness = _ringBuffers[i]->averageLoudnessForBoundarySamples(NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL);
|
||||||
|
|
||||||
if (_nextOutputLoudness < currentMinLoudness) {
|
if (_nextOutputLoudness != 0 && _nextOutputLoudness < currentMinLoudness) {
|
||||||
currentMinLoudness = _nextOutputLoudness;
|
currentMinLoudness = _nextOutputLoudness;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ int PositionalAudioRingBuffer::parseData(const QByteArray& packet) {
|
||||||
int16_t numSilentSamples;
|
int16_t numSilentSamples;
|
||||||
|
|
||||||
memcpy(&numSilentSamples, packet.data() + readBytes, sizeof(int16_t));
|
memcpy(&numSilentSamples, packet.data() + readBytes, sizeof(int16_t));
|
||||||
|
|
||||||
readBytes += sizeof(int16_t);
|
readBytes += sizeof(int16_t);
|
||||||
|
|
||||||
addSilentFrame(numSilentSamples);
|
addSilentFrame(numSilentSamples);
|
||||||
|
|
Loading…
Reference in a new issue