From a1f6dc934c786646efbae4d30177665e3859d38b Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Mon, 22 Apr 2024 21:13:23 +0200 Subject: [PATCH] added setup option to select 22500 or 44100 Hz audio sample rate --- src/files.c | 5 +++++ src/libgame/sdl.c | 12 ++++++++++-- src/libgame/sdl.h | 1 + src/libgame/system.c | 2 ++ src/libgame/system.h | 3 +++ src/screens.c | 6 ++++++ src/tools.c | 21 +++++++++++++++++++++ src/tools.h | 1 + 8 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/files.c b/src/files.c index ce6928b7..5ae706b9 100644 --- a/src/files.c +++ b/src/files.c @@ -10867,6 +10867,10 @@ static struct TokenInfo global_setup_tokens[] = TYPE_INTEGER, &setup.volume_music, "volume_music" }, + { + TYPE_SWITCH, + &setup.audio_sample_rate_44100, "audio_sample_rate_44100" + }, { TYPE_SWITCH, &setup.network_mode, "network_mode" @@ -11662,6 +11666,7 @@ static void setSetupInfoToDefaults(struct SetupInfo *si) si->volume_simple = 100; // percent si->volume_loops = 100; // percent si->volume_music = 100; // percent + si->audio_sample_rate_44100 = FALSE; si->network_mode = FALSE; si->network_player_nr = 0; // first player diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index 76c70cc4..a19defa3 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -2512,8 +2512,10 @@ void SDLOpenAudio(void) return; } - if (Mix_OpenAudio(DEFAULT_AUDIO_SAMPLE_RATE, MIX_DEFAULT_FORMAT, - AUDIO_NUM_CHANNELS_STEREO, + // set audio sample rate for mixer + audio.sample_rate = (setup.audio_sample_rate_44100 ? 44100 : 22050); + + if (Mix_OpenAudio(audio.sample_rate, MIX_DEFAULT_FORMAT, AUDIO_NUM_CHANNELS_STEREO, setup.system.audio_fragment_size) < 0) { Warn("Mix_OpenAudio() failed: %s", SDL_GetError()); @@ -2543,6 +2545,12 @@ void SDLCloseAudio(void) SDL_QuitSubSystem(SDL_INIT_AUDIO); } +void SDLReopenAudio(void) +{ + SDLCloseAudio(); + SDLOpenAudio(); +} + // ============================================================================ // event functions diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index 2caf6af0..bbbaa767 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -445,6 +445,7 @@ void SDLSetMouseCursor(struct MouseCursorInfo *); void SDLOpenAudio(void); void SDLCloseAudio(void); +void SDLReopenAudio(void); void SDLWaitEvent(Event *); void SDLCorrectRawMousePosition(int *, int *); diff --git a/src/libgame/system.c b/src/libgame/system.c index 37fe3a77..a1f009f7 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -1668,6 +1668,8 @@ void OpenAudio(void) audio.device_name = NULL; audio.device_fd = -1; + audio.sample_rate = DEFAULT_AUDIO_SAMPLE_RATE; + audio.num_channels = 0; audio.music_channel = 0; audio.first_sound_channel = 0; diff --git a/src/libgame/system.h b/src/libgame/system.h index 6706f201..d8ca9a29 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -1114,6 +1114,8 @@ struct AudioSystemInfo char *device_name; int device_fd; + int sample_rate; + int num_channels; int music_channel; int first_sound_channel; @@ -1541,6 +1543,7 @@ struct SetupInfo int volume_simple; int volume_loops; int volume_music; + boolean audio_sample_rate_44100; boolean network_mode; int network_player_nr; diff --git a/src/screens.c b/src/screens.c index ea44e566..c7a69c30 100644 --- a/src/screens.c +++ b/src/screens.c @@ -8083,6 +8083,8 @@ static struct TokenInfo setup_info_sound[] = { TYPE_ENTER_LIST, execSetupChooseVolumeMusic, "Music Volume:" }, { TYPE_STRING, &volume_music_text, "" }, { TYPE_EMPTY, NULL, "" }, + { TYPE_SWITCH, &setup.audio_sample_rate_44100, "44100 Hz audio mixing:" }, + { TYPE_EMPTY, NULL, "" }, { TYPE_LEAVE_MENU, execSetupMain, "Back" }, { 0, NULL, NULL } @@ -8535,6 +8537,10 @@ static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx) if (si->value == &setup.fullscreen) ToggleFullscreenIfNeeded(); + // audio sample rate may have changed at this point + if (si->value == &setup.audio_sample_rate_44100) + ToggleAudioSampleRateIfNeeded(); + // network mode may have changed at this point if (si->value == &setup.network_mode) ToggleNetworkModeIfNeeded(); diff --git a/src/tools.c b/src/tools.c index 39976f9d..7c81b8f7 100644 --- a/src/tools.c +++ b/src/tools.c @@ -11431,6 +11431,27 @@ void PlaySoundSelecting(void) #endif } +void ToggleAudioSampleRateIfNeeded(void) +{ + int setup_audio_sample_rate = (setup.audio_sample_rate_44100 ? 44100 : 22050); + + // if setup and audio sample rate are already matching, nothing do do + if ((setup_audio_sample_rate == audio.sample_rate) || + !audio.sound_available) + return; + +#if 1 + // apparently changing the audio output sample rate does not work at runtime, + // so currently the program has to be restarted to apply the new sample rate + Request("Please restart the program to change audio sample rate!", REQ_CONFIRM); +#else + SDLReopenAudio(); + + // set setup value according to successfully changed audio sample rate + setup.audio_sample_rate_44100 = (audio.sample_rate == 44100); +#endif +} + void ToggleFullscreenIfNeeded(void) { // if setup and video fullscreen state are already matching, nothing do do diff --git a/src/tools.h b/src/tools.h index cc503c0a..67e0d8c5 100644 --- a/src/tools.h +++ b/src/tools.h @@ -304,6 +304,7 @@ void ResetFontStatus(void); void SetLevelSetInfo(char *, int); +void ToggleAudioSampleRateIfNeeded(void); void ToggleFullscreenIfNeeded(void); void ChangeWindowScalingIfNeeded(void); void ChangeVsyncModeIfNeeded(void); -- 2.34.1