Skip to content

Commit 100f2cc

Browse files
mcfnordclaude
andcommitted
Server: read the fade-in gain once per channel per frame
DecodeReceiveData() called CChannel::GetFadeInGain() twice per channel pair, so every frame performed 2N^2-N int-to-float conversions and divisions on N values that do not change within the frame: the fade-in counters are only written under CServer::Mutex, which OnTimer() holds for the whole decode phase. Read each connected channel's fade-in gain into vecfFadeInGains in the loop that builds vecChanIDsCurConChan, in the same order and under the same lock, and multiply from that vector instead. The two factors are applied in the same order as before. Closes #3945 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent c64c00b commit 100f2cc

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎src/server.cpp‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ CServer::CServer ( const int iNewMaxNumChan,
172172

173173
// allocate worst case memory for the temporary vectors
174174
vecChanIDsCurConChan.Init ( iMaxNumChannels );
175+
vecfFadeInGains.Init ( iMaxNumChannels );
175176
vecvecfGains.Init ( iMaxNumChannels );
176177
vecvecfPannings.Init ( iMaxNumChannels );
177178
vecvecsData.Init ( iMaxNumChannels );
@@ -678,6 +679,12 @@ void CServer::OnTimer()
678679
// connected clients is less, only a subset of elements of this
679680
// vector are actually used and the others are dummy elements)
680681
vecChanIDsCurConChan[iNumClients] = i;
682+
683+
// read the fade-in gain of the channel once per frame for use in
684+
// DecodeReceiveData() (the fade-in counters are only written under
685+
// the server mutex, which is held here and for the whole decode phase)
686+
vecfFadeInGains[iNumClients] = vecChannels[i].GetFadeInGain();
687+
681688
iNumClients++;
682689
}
683690
}
@@ -896,24 +903,24 @@ void CServer::DecodeReceiveData ( const int iChanCnt, const int iNumClients )
896903
CurOpusDecoder = nullptr;
897904
}
898905

899-
// get gains and pannings of all connected channels, compacted to the
900-
// order of "vecChanIDsCurConChan".
901-
// The second index of "vecvecfGains" does not represent
902-
// the channel ID! Therefore we have to use
903-
// "vecChanIDsCurConChan" to query the IDs of the currently
904-
// connected channels
906+
// get gains and pannings of all connected channels, compacted to the order of
907+
// "vecChanIDsCurConChan" (the second index of "vecvecfGains" does not represent the
908+
// channel ID but the position in "vecChanIDsCurConChan")
905909
vecChannels[iCurChanID].GetGainsAndPannings ( vecChanIDsCurConChan, iNumClients, vecvecfGains[iChanCnt], vecvecfPannings[iChanCnt] );
906910

911+
// apply the audio fade-in: each connected channel fades in with its own fade-in gain,
912+
// and the fade-in gain of the current channel is applied to all other channels as well
913+
// to avoid the client volumes being at 100% when joining a server (#628); the fade-in
914+
// gains were read once per frame into "vecfFadeInGains" in the same order
915+
const float fCurChanFadeInGain = vecfFadeInGains[iChanCnt];
916+
907917
for ( int j = 0; j < iNumClients; j++ )
908918
{
909-
// consider audio fade-in
910-
vecvecfGains[iChanCnt][j] *= vecChannels[vecChanIDsCurConChan[j]].GetFadeInGain();
919+
vecvecfGains[iChanCnt][j] *= vecfFadeInGains[j];
911920

912-
// use the fade in of the current channel for all other connected clients
913-
// as well to avoid the client volumes are at 100% when joining a server (#628)
914921
if ( j != iChanCnt )
915922
{
916-
vecvecfGains[iChanCnt][j] *= vecChannels[iCurChanID].GetFadeInGain();
923+
vecvecfGains[iChanCnt][j] *= fCurChanFadeInGain;
917924
}
918925
}
919926

‎src/server.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
276276

277277
CVector<QString> vstrChatColors;
278278
CVector<int> vecChanIDsCurConChan;
279+
CVector<float> vecfFadeInGains;
279280

280281
CVector<CVector<float>> vecvecfGains;
281282
CVector<CVector<float>> vecvecfPannings;

0 commit comments

Comments
 (0)