From f37bbec78eb9041046b919064856bd35c55a3bc2 Mon Sep 17 00:00:00 2001 From: Sergey Degtyar Date: Thu, 26 Jun 2025 06:47:57 +0300 Subject: [PATCH] engine: sound: voice buffer isolation and correct MP3 volume handling --- engine/client/s_mix.c | 24 +++++++++++++++++++++--- engine/client/s_stream.c | 3 ++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/engine/client/s_mix.c b/engine/client/s_mix.c index 8379831e..fe28f6df 100644 --- a/engine/client/s_mix.c +++ b/engine/client/s_mix.c @@ -22,6 +22,7 @@ enum IPAINTBUFFER = 0, IROOMBUFFER, ISTREAMBUFFER, + IVOICEBUFFER, CPAINTBUFFERS, }; @@ -63,6 +64,7 @@ static portable_samplepair_t *g_curpaintbuffer; static portable_samplepair_t streambuffer[(PAINTBUFFER_SIZE+1)]; static portable_samplepair_t paintbuffer[(PAINTBUFFER_SIZE+1)]; static portable_samplepair_t roombuffer[(PAINTBUFFER_SIZE+1)]; +static portable_samplepair_t voicebuffer[(PAINTBUFFER_SIZE+1)]; static portable_samplepair_t temppaintbuffer[(PAINTBUFFER_SIZE+1)]; static paintbuffer_t paintbuffers[CPAINTBUFFERS]; @@ -216,6 +218,7 @@ void MIX_InitAllPaintbuffers( void ) paintbuffers[IPAINTBUFFER].pbuf = paintbuffer; paintbuffers[IROOMBUFFER].pbuf = roombuffer; paintbuffers[ISTREAMBUFFER].pbuf = streambuffer; + paintbuffers[IVOICEBUFFER].pbuf = voicebuffer; MIX_SetCurrentPaintbuffer( IPAINTBUFFER ); } @@ -921,11 +924,12 @@ static void S_MixUpsample( int sampleCount, int filtertype ) static void MIX_MixRawSamplesBuffer( int end ) { - portable_samplepair_t *pbuf, *roombuf, *streambuf; + portable_samplepair_t *pbuf, *roombuf, *streambuf, *voicebuf; uint i, j, stop; roombuf = MIX_GetPFrontFromIPaint( IROOMBUFFER ); streambuf = MIX_GetPFrontFromIPaint( ISTREAMBUFFER ); + voicebuf = MIX_GetPFrontFromIPaint( IVOICEBUFFER ); if( s_listener.paused ) return; @@ -935,6 +939,7 @@ static void MIX_MixRawSamplesBuffer( int end ) // copy from the streaming sound source rawchan_t *ch = raw_channels[i]; qboolean stream; + qboolean is_voice; if( !ch ) continue; @@ -943,8 +948,18 @@ static void MIX_MixRawSamplesBuffer( int end ) if( !ch->leftvol && !ch->rightvol ) continue; + is_voice = (ch->entnum > 0 && ch->entnum <= MAX_CLIENTS) || + (ch->entnum == VOICE_LOOPBACK_INDEX) || + (ch->entnum == VOICE_LOCALCLIENT_INDEX); + stream = ch->entnum == S_RAW_SOUND_BACKGROUNDTRACK || CL_IsPlayerIndex( ch->entnum ); - pbuf = stream ? streambuf : roombuf; + + if( is_voice ) + pbuf = voicebuf; + else if( stream ) + pbuf = streambuf; + else + pbuf = roombuf; stop = (end < ch->s_rawend) ? end : ch->s_rawend; @@ -1045,7 +1060,10 @@ void MIX_PaintChannels( int endtime ) MIX_MixPaintbuffers( IPAINTBUFFER, IROOMBUFFER, IPAINTBUFFER, count, S_GetMasterVolume() ); // add music or soundtrack from movie (no dsp) - MIX_MixPaintbuffers( IPAINTBUFFER, ISTREAMBUFFER, IPAINTBUFFER, count, S_GetMusicVolume() ); + MIX_MixPaintbuffers( IPAINTBUFFER, ISTREAMBUFFER, IPAINTBUFFER, count, 1.0f ); + + // voice chat + MIX_MixPaintbuffers( IPAINTBUFFER, IVOICEBUFFER, IPAINTBUFFER, count, 1.0f ); // clip all values > 16 bit down to 16 bit MIX_CompressPaintbuffer( IPAINTBUFFER, count ); diff --git a/engine/client/s_stream.c b/engine/client/s_stream.c index 36d9ae72..fbc24efa 100644 --- a/engine/client/s_stream.c +++ b/engine/client/s_stream.c @@ -242,7 +242,8 @@ void S_StreamBackgroundTrack( void ) if( r > 0 ) { // add to raw buffer - S_RawEntSamples( S_RAW_SOUND_BACKGROUNDTRACK, fileSamples, info->rate, info->width, info->channels, raw, 255 ); + int music_vol = (int)(255.0f * S_GetMusicVolume()); + S_RawEntSamples( S_RAW_SOUND_BACKGROUNDTRACK, fileSamples, info->rate, info->width, info->channels, raw, music_vol ); } else {