1616#include < utility>
1717
1818#ifdef USE_SDL3
19- #include < SDL3/SDL_audio.h>
2019#include < SDL3/SDL_error.h>
2120#include < SDL3/SDL_timer.h>
21+ #include < SDL3_mixer/SDL_mixer.h>
2222#else
2323#include < Aulib/Stream.h>
2424#include < SDL.h>
@@ -42,7 +42,7 @@ namespace devilution {
4242bool gbSndInited;
4343
4444#ifdef USE_SDL3
45- SDL_AudioDeviceID CurrentAudioDeviceId ;
45+ MIX_Mixer *CurrentMixer ;
4646#endif
4747
4848/* * The active background music track id. */
@@ -119,9 +119,6 @@ std::optional<SdlMutex> duplicateSoundsMutex;
119119
120120SoundSample *DuplicateSound (const SoundSample &sound)
121121{
122- #ifdef USE_SDL3
123- return nullptr ;
124- #else
125122 auto duplicate = std::make_unique<SoundSample>();
126123 if (duplicate->DuplicateFrom (sound) != 0 )
127124 return nullptr ;
@@ -133,12 +130,13 @@ SoundSample *DuplicateSound(const SoundSample &sound)
133130 it = duplicateSounds.end ();
134131 --it;
135132 }
133+ #ifndef USE_SDL3
136134 result->SetFinishCallback ([it]([[maybe_unused]] Aulib::Stream &stream) {
137135 const std::lock_guard<SdlMutex> lock (*duplicateSoundsMutex);
138136 duplicateSounds.erase (it);
139137 });
140- return result;
141138#endif
139+ return result;
142140}
143141
144142/* * Maps from track ID to track name in spawn. */
@@ -263,18 +261,22 @@ void snd_init()
263261 // 22kHz, the audio format to 16-bit signed, use 2 output channels
264262 // (stereo), and a 2KiB output buffer.
265263#ifdef USE_SDL3
264+ if (!MIX_Init ()) {
265+ LogError (LogCategory::Audio, " Failed to initialize SDL_mixer: {}" , SDL_GetError ());
266+ return ;
267+ }
266268 const AudioOptions &audioOptions = GetOptions ().Audio ;
267269 SDL_AudioSpec specHint = {};
268270 specHint.format = SDL_AUDIO_S16LE;
269271 specHint.channels = *audioOptions.channels ;
270272 specHint.freq = static_cast <int >(*audioOptions.sampleRate );
271- const SDL_AudioDeviceID resolvedId = SDL_OpenAudioDevice (audioOptions.device .id (), &specHint);
272- if (resolvedId == 0 ) {
273- LogError (LogCategory::Audio, " Failed to open audio device: {}" , SDL_GetError ());
273+ CurrentMixer = MIX_CreateMixerDevice (audioOptions.device .id (), &specHint);
274+ if (CurrentMixer == nullptr ) {
275+ LogError (LogCategory::Audio, " Failed to create mixer device: {}" , SDL_GetError ());
274276 SDL_ClearError ();
277+ MIX_Quit ();
275278 return ;
276279 }
277- CurrentAudioDeviceId = resolvedId;
278280#else
279281 if (!Aulib::init (*GetOptions ().Audio .sampleRate , AUDIO_S16, *GetOptions ().Audio .channels , *GetOptions ().Audio .bufferSize , *GetOptions ().Audio .device )) {
280282 LogError (LogCategory::Audio, " Failed to initialize audio (Aulib::init): {}" , SDL_GetError ());
@@ -291,9 +293,11 @@ void snd_init()
291293void snd_deinit ()
292294{
293295 if (gbSndInited) {
296+ ClearDuplicateSounds ();
294297#ifdef USE_SDL3
295- const AudioOptions &audioOptions = GetOptions ().Audio ;
296- SDL_CloseAudioDevice (audioOptions.device .id ());
298+ MIX_DestroyMixer (CurrentMixer);
299+ CurrentMixer = nullptr ;
300+ MIX_Quit ();
297301#else
298302 Aulib::quit ();
299303#endif
0 commit comments