]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
snd_mutewhenidle: add only-when-minimised mode, fix cl_capturevideo compat, cleanup
authorbones_was_here <bones_was_here@xonotic.au>
Sun, 25 Aug 2024 02:43:30 +0000 (12:43 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Thu, 12 Sep 2024 13:51:11 +0000 (23:51 +1000)
Maybe snd_mutewhenidle 2 should be the default?

When inactive/minimised with snd_mutewhenidle enabled, the audio capture
was unreliable and sound was sometimes output when it should have been
muted.
Closes https://gitlab.com/xonotic/darkplaces/-/issues/92
(the video issue was already fixed).

Timedemos now share the same mute implementation as snd_mutewhenidle.

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
cl_screen.c
snd_main.c
snd_main.h
snd_mix.c
snd_null.c
snd_sdl.c
vid_sdl.c

index bf4c3d194566d8a87ea81d49ce4543b5811e1ab2..1ded3749ab7c332215a0699cb46e5da1d9de8292 100644 (file)
@@ -2280,7 +2280,8 @@ void CL_UpdateScreen(void)
        }
 
 #ifdef CONFIG_VIDEO_CAPTURE
-       if (vid_hidden && !cls.capturevideo.active && !cl_capturevideo.integer)
+       if (vid_hidden && !cls.capturevideo.active
+       && !cl_capturevideo.integer) // so we can start capturing while hidden
 #else
        if (vid_hidden)
 #endif
index 9ac20114b941d85b3a8d2b87b1c7df7e79fafe45..cee8ec583900fae6c2f80b64cdf7a6fdbf6db82b 100644 (file)
@@ -152,8 +152,8 @@ qbool simsound = false;
 static qbool recording_sound = false;
 #endif
 
-int snd_blocked = 0;
-static int current_swapstereo = false;
+bool snd_blocked = false;
+static bool current_swapstereo = false;
 static int current_channellayout = SND_CHANNELLAYOUT_AUTO;
 static int current_channellayout_used = SND_CHANNELLAYOUT_AUTO;
 
@@ -168,7 +168,6 @@ cvar_t snd_initialized = {CF_CLIENT | CF_READONLY, "snd_initialized", "0", "indi
 
 // Cvars declared in snd_main.h (shared with other snd_*.c files)
 cvar_t snd_channellayout = {CF_CLIENT, "snd_channellayout", "0", "channel layout. Can be 0 (auto - snd_restart needed), 1 (standard layout), or 2 (ALSA layout)"};
-cvar_t snd_mutewhenidle = {CF_CLIENT | CF_ARCHIVE, "snd_mutewhenidle", "1", "whether to disable sound output when game window is inactive"};
 cvar_t snd_streaming = {CF_CLIENT | CF_ARCHIVE, "snd_streaming", "1", "enables keeping compressed ogg sound files compressed, decompressing them only as needed, otherwise they will be decompressed completely at load (may use a lot of memory); when set to 2, streaming is performed even if this would waste memory"};
 cvar_t snd_streaming_length = {CF_CLIENT | CF_ARCHIVE, "snd_streaming_length", "1", "decompress sounds completely if they are less than this play time when snd_streaming is 1"};
 cvar_t snd_waterfx = {CF_CLIENT | CF_ARCHIVE, "snd_waterfx", "1", "underwater sound filter strength"};
@@ -191,6 +190,7 @@ cvar_t snd_spatialization_occlusion = {CF_CLIENT | CF_ARCHIVE, "snd_spatializati
 cvar_t _snd_mixahead = {CF_CLIENT | CF_ARCHIVE, "_snd_mixahead", "0.15", "how much sound to mix ahead of time"};
 cvar_t snd_swapstereo = {CF_CLIENT | CF_ARCHIVE, "snd_swapstereo", "0", "swaps left/right speakers for old ISA soundblaster cards"};
 cvar_t snd_maxchannelvolume = {CF_CLIENT | CF_ARCHIVE, "snd_maxchannelvolume", "10", "maximum volume of a single sound"};
+cvar_t snd_mutewhenidle = {CF_CLIENT | CF_ARCHIVE, "snd_mutewhenidle", "1", "1 disables sound output when game window is inactive, 2 disables it only when the window is minimised"};
 cvar_t snd_softclip = {CF_CLIENT | CF_ARCHIVE, "snd_softclip", "0", "Use soft-clipping. Soft-clipping can make the sound more smooth if very high volume levels are used. Enable this option if the dynamic range of the loudspeakers is very low. WARNING: This feature creates distortion and should be considered a last resort."};
 //cvar_t snd_softclip = {CF_CLIENT | CF_ARCHIVE, "snd_softclip", "0", "Use soft-clipping (when set to 2, use it even if output is floating point). Soft-clipping can make the sound more smooth if very high volume levels are used. Enable this option if the dynamic range of the loudspeakers is very low. WARNING: This feature creates distortion and should be considered a last resort."};
 cvar_t snd_entchannel0volume = {CF_CLIENT | CF_ARCHIVE, "snd_entchannel0volume", "1", "volume multiplier of the auto-allocate entity channel of regular entities (DEPRECATED)"};
@@ -1107,27 +1107,6 @@ qbool S_IsSoundPrecached (const sfx_t *sfx)
        return (sfx != NULL && sfx->fetcher != NULL) || (sfx == &changevolume_sfx);
 }
 
-/*
-==================
-S_BlockSound
-==================
-*/
-void S_BlockSound (void)
-{
-       snd_blocked++;
-}
-
-
-/*
-==================
-S_UnblockSound
-==================
-*/
-void S_UnblockSound (void)
-{
-       snd_blocked--;
-}
-
 
 /*
 =================
@@ -1974,7 +1953,9 @@ static void S_PaintAndSubmit (void)
        }
        soundtimehack = usesoundtimehack;
 
-       if (!soundtimehack && snd_blocked > 0)
+       // mixing is always required here when capturing, even if output is muted
+       // (capture doesn't use threaded/callback mode)
+       if (!soundtimehack && snd_blocked && !cls.capturevideo.active)
                return;
 
        if (snd_usethreadedmixing)
@@ -2086,6 +2067,11 @@ void S_Update(const matrix4x4_t *listenermatrix)
        if (snd_renderbuffer == NULL || nosound.integer)
                return;
 
+       // enable/disable sound on focus gain/loss
+       snd_blocked = (snd_mutewhenidle.integer == 1 && !vid_activewindow)
+                  || (snd_mutewhenidle.integer == 2 && vid_hidden)
+                  || cls.timedemo;
+
        {
                double mindist_trans, maxdist_trans;
 
index 681fc32f45083adccad75c8aa7ee7d1e19f222d8..f276f8d5ad1196357c4b7b49ad7331e10912e785 100644 (file)
@@ -127,7 +127,6 @@ extern snd_ringbuffer_t *snd_renderbuffer;
 extern qbool snd_threaded; ///< enables use of snd_usethreadedmixing, provided that no sound hacks are in effect (like timedemo)
 extern qbool snd_usethreadedmixing; ///< if true, the main thread does not mix sound, soundtime does not advance, and neither does snd_renderbuffer->endframe, instead the audio thread will call S_MixToBuffer as needed
 
-extern cvar_t snd_mutewhenidle;
 extern cvar_t snd_waterfx;
 extern cvar_t snd_streaming;
 extern cvar_t snd_streaming_length;
@@ -137,7 +136,7 @@ extern cvar_t snd_streaming_length;
 #define SND_CHANNELLAYOUT_ALSA     2
 extern cvar_t snd_channellayout;
 
-extern int snd_blocked;                // counter. When > 0, we stop submitting sound to the audio device
+extern bool snd_blocked; ///< When true, we submit silence to the audio device
 
 extern struct mempool_s *snd_mempool;
 
index d8ea1dbe3764a297ead5aef44712325d5d901ee4..41358feeb6f987292bdd75e9ef3adaef90d01422 100644 (file)
--- a/snd_mix.c
+++ b/snd_mix.c
@@ -184,10 +184,6 @@ static void S_ConvertPaintBuffer(portable_sampleframe_t *painted_ptr, void *rb_p
                                *snd_out++ = painted_ptr->sample[0];
                        }
                }
-
-               // noise is really really annoying
-               if (cls.timedemo)
-                       memset(rb_ptr, 0, nbframes * nchannels * width);
        }
        else if (width == 2)  // 16bit
        {
@@ -243,10 +239,6 @@ static void S_ConvertPaintBuffer(portable_sampleframe_t *painted_ptr, void *rb_p
                                val = (int)((painted_ptr->sample[0] + painted_ptr->sample[1]) * 16384.0f);*snd_out++ = bound(-32768, val, 32767);
                        }
                }
-
-               // noise is really really annoying
-               if (cls.timedemo)
-                       memset(rb_ptr, 0, nbframes * nchannels * width);
        }
        else  // 8bit
        {
@@ -302,10 +294,6 @@ static void S_ConvertPaintBuffer(portable_sampleframe_t *painted_ptr, void *rb_p
                                val = (int)((painted_ptr->sample[0] + painted_ptr->sample[1]) * 64.0f) + 128; *snd_out++ = bound(0, val, 255);
                        }
                }
-
-               // noise is really really annoying
-               if (cls.timedemo)
-                       memset(rb_ptr, 128, nbframes * nchannels);
        }
 }
 
index 28309c39016ec3a0d6e25b5d6cdff4f146660caa..5d15bff0f194143804f474d11c5ab10ff8669c9d 100755 (executable)
@@ -127,14 +127,6 @@ qbool S_LocalSoundEx (const char *s, int chan, float fvol)
        return false;
 }
 
-void S_BlockSound (void)
-{
-}
-
-void S_UnblockSound (void)
-{
-}
-
 int S_GetSoundRate(void)
 {
        return 0;
index e34a46e8506fb00db4ebde4c7c603d7edb237bd8..706fa30479dc8883b189edfce872c0bfd04b3a09 100644 (file)
--- a/snd_sdl.c
+++ b/snd_sdl.c
@@ -58,7 +58,9 @@ static void Buffer_Callback (void *userdata, Uint8 *stream, int len)
                FrameCount = MaxFrames;
        StartOffset = snd_renderbuffer->startframe % snd_renderbuffer->maxframes;
        EndOffset = (snd_renderbuffer->startframe + FrameCount) % snd_renderbuffer->maxframes;
-       if (StartOffset > EndOffset)  // if the buffer wraps
+       if (snd_blocked)
+               memset(stream, snd_renderbuffer->format.width == 1 ? 0x80 : 0, len);
+       else if (StartOffset > EndOffset)  // if the buffer wraps
        {
                unsigned int PartialLength1, PartialLength2;
 
index 631b0da85de7451d9f2581b40ffff9d36f97bb81..ea6fe26e0f900d626b686ac226442fd69d7d4385 100644 (file)
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -1042,7 +1042,6 @@ static keynum_t buttonremap[] =
 //#define DEBUGSDLEVENTS
 void Sys_SDL_HandleEvents(void)
 {
-       static qbool sound_active = true;
        int keycode;
        int i;
        const char *chp;
@@ -1330,24 +1329,6 @@ void Sys_SDL_HandleEvents(void)
 
        vid_activewindow = !vid_hidden && vid_hasfocus;
 
-       // enable/disable sound on focus gain/loss
-       if (vid_activewindow || !snd_mutewhenidle.integer)
-       {
-               if (!sound_active)
-               {
-                       S_UnblockSound ();
-                       sound_active = true;
-               }
-       }
-       else
-       {
-               if (sound_active)
-               {
-                       S_BlockSound ();
-                       sound_active = false;
-               }
-       }
-
        if (!vid_activewindow || key_consoleactive || scr_loading)
                VID_SetMouse(false, false);
        else if (key_dest == key_menu || key_dest == key_menu_grabbed)