added setup option to select 22500 or 44100 Hz audio sample rate
authorHolger Schemel <info@artsoft.org>
Mon, 22 Apr 2024 19:13:23 +0000 (21:13 +0200)
committerHolger Schemel <info@artsoft.org>
Mon, 22 Apr 2024 19:16:20 +0000 (21:16 +0200)
src/files.c
src/libgame/sdl.c
src/libgame/sdl.h
src/libgame/system.c
src/libgame/system.h
src/screens.c
src/tools.c
src/tools.h

index ce6928b7c43514c9bdb8a191107703652a630a6b..5ae706b91353694ea68aede8dd31128c2cd38974 100644 (file)
@@ -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
index 76c70cc4a66e6b2d3d8ee27c9b1261b5330197da..a19defa37acc5a7f7cc8cc07eeb958db0eafe973 100644 (file)
@@ -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
index 2caf6af0277cf4bd224da5d484b8761c76e62d71..bbbaa7675919640c264161d7f86a9a467b35669c 100644 (file)
@@ -445,6 +445,7 @@ void SDLSetMouseCursor(struct MouseCursorInfo *);
 
 void SDLOpenAudio(void);
 void SDLCloseAudio(void);
+void SDLReopenAudio(void);
 
 void SDLWaitEvent(Event *);
 void SDLCorrectRawMousePosition(int *, int *);
index 37fe3a77cab0821c1bd30d4c8a5f07548aec3125..a1f009f77c8470f5d0b7e0fcbe85eb97db53ecd0 100644 (file)
@@ -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;
index 6706f2014241f2b935cbbb5fedb6e74ffbda1d32..d8ca9a29a1e7f4191b32e22d5c4b9a5e213f6ef3 100644 (file)
@@ -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;
index ea44e566826723cb041f72e60b1a8f1cbb72d7e0..c7a69c3005eeec67c497204bd6d06647e73de02a 100644 (file)
@@ -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();
index 39976f9d9522949cc221b0234f69950c968b4e08..7c81b8f7748ba8f67339e8d784d6898371729a8d 100644 (file)
@@ -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
index cc503c0ae922c6fa332693885efdaa4df2ca4192..67e0d8c54ee4c2d15a9ef9fe36b90bd76eb57e64 100644 (file)
@@ -304,6 +304,7 @@ void ResetFontStatus(void);
 
 void SetLevelSetInfo(char *, int);
 
+void ToggleAudioSampleRateIfNeeded(void);
 void ToggleFullscreenIfNeeded(void);
 void ChangeWindowScalingIfNeeded(void);
 void ChangeVsyncModeIfNeeded(void);