Adapt MixSDL to the SDL2 audio API and allow format conversion.

Adapted from a patch by Ala-lala; assists with Windows sound output
This commit is contained in:
Michael Martin
2019-10-27 15:35:19 -07:00
parent c7bbbd717c
commit a3da3759b4
+36 -5
View File
@@ -24,6 +24,14 @@
#include <stdlib.h> #include <stdlib.h>
/* SDL2 wants to talk to a specific device. We'll let SDL1 use the same
* function names and just throw the device argument away. */
#if SDL_MAJOR_VERSION > 1
static SDL_AudioDeviceID dev;
#else
#define SDL_CloseAudioDevice(x) SDL_CloseAudio ()
#define SDL_PauseAudioDevice(x, y) SDL_PauseAudio (y)
#endif
static const audio_Driver mixSDL_Driver = static const audio_Driver mixSDL_Driver =
{ {
mixSDL_Uninit, mixSDL_Uninit,
@@ -145,7 +153,30 @@ mixSDL_Init (audio_Driver *driver, sint32 flags)
desired.callback = mixer_MixChannels; desired.callback = mixer_MixChannels;
log_add (log_Info, "Opening SDL audio device."); log_add (log_Info, "Opening SDL audio device.");
#if SDL_MAJOR_VERSION > 1
dev = SDL_OpenAudioDevice (NULL, 0, &desired, &obtained,
SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE
#ifdef SDL_AUDIO_ALLOW_SAMPLES_CHANGE
| SDL_AUDIO_ALLOW_SAMPLES_CHANGE
#endif
);
if (dev != 0 && obtained.channels != 1 && obtained.channels != 2)
{
/* Try again without SDL_AUDIO_ALLOW_CHANNELS_CHANGE
* in case the device only supports >2 channels for some
* reason */
SDL_CloseAudioDevice (dev);
dev = SDL_OpenAudioDevice (NULL, 0, &desired, &obtained,
SDL_AUDIO_ALLOW_FREQUENCY_CHANGE
#ifdef SDL_AUDIO_ALLOW_SAMPLES_CHANGE
| SDL_AUDIO_ALLOW_SAMPLES_CHANGE
#endif
);
}
if (dev == 0)
#else
if (SDL_OpenAudio (&desired, &obtained) < 0) if (SDL_OpenAudio (&desired, &obtained) < 0)
#endif
{ {
log_add (log_Error, "Unable to open audio device: %s", log_add (log_Error, "Unable to open audio device: %s",
SDL_GetError ()); SDL_GetError ());
@@ -181,7 +212,7 @@ mixSDL_Init (audio_Driver *driver, sint32 flags)
{ {
log_add (log_Error, "Mixer initialization failed: %x", log_add (log_Error, "Mixer initialization failed: %x",
mixer_GetError ()); mixer_GetError ());
SDL_CloseAudio (); SDL_CloseAudioDevice (dev);
SDL_QuitSubSystem (SDL_INIT_AUDIO); SDL_QuitSubSystem (SDL_INIT_AUDIO);
return -1; return -1;
} }
@@ -191,7 +222,7 @@ mixSDL_Init (audio_Driver *driver, sint32 flags)
if (SoundDecoder_Init (flags, &formats)) if (SoundDecoder_Init (flags, &formats))
{ {
log_add (log_Error, "Sound decoders initialization failed."); log_add (log_Error, "Sound decoders initialization failed.");
SDL_CloseAudio (); SDL_CloseAudioDevice (dev);
mixer_Uninit (); mixer_Uninit ();
SDL_QuitSubSystem (SDL_INIT_AUDIO); SDL_QuitSubSystem (SDL_INIT_AUDIO);
return -1; return -1;
@@ -209,7 +240,7 @@ mixSDL_Init (audio_Driver *driver, sint32 flags)
{ {
log_add (log_Error, "Stream decoder initialization failed."); log_add (log_Error, "Stream decoder initialization failed.");
// TODO: cleanup source mutexes [or is it "muti"? :) ] // TODO: cleanup source mutexes [or is it "muti"? :) ]
SDL_CloseAudio (); SDL_CloseAudioDevice (dev);
SoundDecoder_Uninit (); SoundDecoder_Uninit ();
mixer_Uninit (); mixer_Uninit ();
SDL_QuitSubSystem (SDL_INIT_AUDIO); SDL_QuitSubSystem (SDL_INIT_AUDIO);
@@ -222,7 +253,7 @@ mixSDL_Init (audio_Driver *driver, sint32 flags)
SetSpeechVolume (speechVolumeScale); SetSpeechVolume (speechVolumeScale);
SetMusicVolume ((COUNT)musicVolume); SetMusicVolume ((COUNT)musicVolume);
SDL_PauseAudio (0); SDL_PauseAudioDevice (dev, 0);
return 0; return 0;
} }
@@ -251,7 +282,7 @@ mixSDL_Uninit (void)
mixSDL_DeleteSources (1, &soundSource[i].handle); mixSDL_DeleteSources (1, &soundSource[i].handle);
} }
SDL_CloseAudio (); SDL_CloseAudioDevice (dev);
mixer_Uninit (); mixer_Uninit ();
SoundDecoder_Uninit (); SoundDecoder_Uninit ();
SDL_QuitSubSystem (SDL_INIT_AUDIO); SDL_QuitSubSystem (SDL_INIT_AUDIO);