Skip to content

Commit 7b65d70

Browse files
committed
Implement audio via SDL3_mixer
1 parent cf2fa34 commit 7b65d70

7 files changed

Lines changed: 283 additions & 49 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ comparer-config.toml
1717

1818
#ignore cmake cache
1919
/build-*/
20+
/build_*/
2021
.vscode/tasks.json
2122

2223
# Extra files in the source distribution (see make_src_dist.py)

3rdParty/SDL3_mixer/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ set(SDLMIXER_VORBIS_TREMOR OFF)
2727
set(SDLMIXER_WAVPACK OFF)
2828

2929
FetchContent_Declare_ExcludeFromAll(SDL_mixer
30-
URL https://github.com/libsdl-org/SDL_mixer/archive/7d37755016f0952c32c9483c556d8608da7ee82f.tar.gz
31-
URL_HASH SHA256=2fa63f1eb623e3acd0012a461771eb93332e2026205f9487da3a3a75bc790111
30+
URL https://github.com/libsdl-org/SDL_mixer/archive/refs/tags/release-3.2.0.tar.gz
31+
URL_HASH SHA256=1077fef4a6e9513027da0a08e6f1b8645f8c88c8bef001c17befeb52b129b5dd
3232
)
3333
FetchContent_MakeAvailable_ExcludeFromAll(SDL_mixer)

Source/engine/sound.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
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 {
4242
bool 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

120120
SoundSample *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()
291293
void 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

Source/engine/sound.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#ifndef NOSOUND
1919
#ifdef USE_SDL3
20-
#include <SDL3/SDL_audio.h>
20+
struct MIX_Mixer;
2121
#endif
2222

2323
#include "utils/soundsample.h"
@@ -59,7 +59,7 @@ struct TSnd {
5959

6060
extern bool gbSndInited;
6161
#ifdef USE_SDL3
62-
extern SDL_AudioDeviceID CurrentAudioDeviceId;
62+
extern MIX_Mixer *CurrentMixer;
6363
#endif
6464

6565
extern _music_id sgnMusicTrack;

Source/storm/storm_svid.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#ifndef NOSOUND
1717
#include <SDL3/SDL_audio.h>
18+
#include <SDL3_mixer/SDL_mixer.h>
1819
#endif
1920
#else
2021
#include <SDL.h>
@@ -30,6 +31,7 @@
3031
#include "engine/assets.hpp"
3132
#include "engine/dx.h"
3233
#include "engine/palette.h"
34+
#include "engine/sound.h"
3335
#include "options.h"
3436
#include "utils/display.h"
3537
#include "utils/log.hpp"
@@ -313,7 +315,9 @@ void SVidInitAudioStream(const SmackerAudioInfo &audioInfo)
313315
SVidAudioStream = nullptr;
314316
return;
315317
}
316-
if (!SDL_BindAudioStream(CurrentAudioDeviceId, SVidAudioStream)) {
318+
const SDL_AudioDeviceID deviceId = static_cast<SDL_AudioDeviceID>(
319+
SDL_GetNumberProperty(MIX_GetMixerProperties(CurrentMixer), MIX_PROP_MIXER_DEVICE_NUMBER, 0));
320+
if (!SDL_BindAudioStream(deviceId, SVidAudioStream)) {
317321
LogError(LogCategory::Audio, "SDL_BindAudioStream (from SVidPlayBegin): {}", SDL_GetError());
318322
SDL_ClearError();
319323
SDL_DestroyAudioStream(SVidAudioStream);

0 commit comments

Comments
 (0)