Turn on and off microphone explicitly

On most devices these days, there is a mandatory microphone indicator
when an input device is being used. Moreover, on macOS and some Linux
distros, the user will be prompted to grant permission to the game for
microphone access. To ensure we're playing nicely with these
expectations, instead of just leaving the device on at all times on
first use, close and reopen the device as sound input is needed.
This commit is contained in:
Eidolon 2025-05-29 15:18:58 -05:00
parent fdd093ef92
commit 55a8a53077
3 changed files with 35 additions and 17 deletions

View file

@ -281,7 +281,7 @@ shouldsign_t ShouldSignChallenge(uint8_t *message)
if ((max(now, then) - min(now, then)) > 60*15)
return SIGN_BADTIME;
// ____ _____ ___ ____ _
// ____ _____ ___ ____ _
// / ___|_ _/ _ \| _ \| |
// \___ \ | || | | | |_) | |
// ___) || || |_| | __/|_|
@ -2436,6 +2436,11 @@ static void CL_ConnectToServer(void)
joinedIP[0] = '\0'; // And empty this for good measure regardless of whether or not we actually used it.
// Enable sound input/microphone in netgames, activating the microphone device.
if (netgame)
{
S_SoundInputSetEnabled(true);
}
}
static void Command_connect(void)
@ -2502,6 +2507,8 @@ static void Command_connect(void)
{
CONS_Alert(CONS_ERROR, M_GetText("There is no server identification with this network driver\n"));
D_CloseConnection();
S_SoundInputSetEnabled(false);
return;
}
}
@ -3659,6 +3666,7 @@ void D_QuitNetGame(void)
K_ClearClientPowerLevels();
G_ObliterateParties();
K_ResetMidVote();
S_SoundInputSetEnabled(false);
DEBFILE("===========================================================================\n"
" Log finish\n"
@ -7376,9 +7384,6 @@ void NetVoiceUpdate(void)
return;
}
// This necessarily runs every frame, not every tic
S_SoundInputSetEnabled(true);
UINT32 bytes_dequed = 0;
do
{

View file

@ -72,6 +72,24 @@ static boolean input_routine(INT32)
return false;
}
static void init_routine(void)
{
if (!netgame)
{
S_SoundInputSetEnabled(true);
}
}
static boolean quit_routine(void)
{
if (!netgame)
{
S_SoundInputSetEnabled(false);
}
return true;
}
menu_t OPTIONS_VoiceDef = {
sizeof (OPTIONS_Voice) / sizeof (menuitem_t),
&OPTIONS_MainDef,
@ -85,7 +103,7 @@ menu_t OPTIONS_VoiceDef = {
draw_routine,
M_DrawOptionsCogs,
tick_routine,
NULL,
NULL,
init_routine,
quit_routine,
input_routine,
};

View file

@ -198,7 +198,6 @@ static void (*music_fade_callback)();
static SDL_AudioDeviceID g_device_id;
static SDL_AudioDeviceID g_input_device_id;
static boolean g_input_device_paused;
void* I_GetSfx(sfxinfo_t* sfx)
{
@ -999,7 +998,7 @@ void I_UpdateAudioRecorder(void)
boolean I_SoundInputIsEnabled(void)
{
return g_input_device_id != 0 && !g_input_device_paused;
return g_input_device_id != 0;
}
boolean I_SoundInputSetEnabled(boolean enabled)
@ -1023,21 +1022,17 @@ boolean I_SoundInputSetEnabled(boolean enabled)
CONS_Alert(CONS_WARNING, "Failed to open input audio device: %s\n", SDL_GetError());
return false;
}
g_input_device_paused = true;
}
if (enabled && g_input_device_paused)
{
SDL_PauseAudioDevice(g_input_device_id, SDL_FALSE);
g_input_device_paused = false;
}
else if (!enabled && !g_input_device_paused)
else if (g_input_device_id != 0 && !enabled)
{
SDL_PauseAudioDevice(g_input_device_id, SDL_TRUE);
SDL_ClearQueuedAudio(g_input_device_id);
g_input_device_paused = true;
SDL_CloseAudioDevice(g_input_device_id);
g_input_device_id = 0;
}
return !g_input_device_paused;
return enabled;
}
UINT32 I_SoundInputDequeueSamples(void *data, UINT32 len)