x11: Use new SDL2 Audio APIs. Fix no sound with many audio devices on Ubuntu Desktop 22.04. Index: x11/soundmng.c =================================================================== --- x11/soundmng.c +++ x11/soundmng.c @@ -918,9 +918,10 @@ static BRESULT sdlaudio_init(UINT rate, UINT samples) { - static SDL_AudioSpec fmt; + SDL_AudioSpec fmt; int rv; + memset(&fmt, 0, sizeof(fmt)); fmt.freq = rate; fmt.format = AUDIO_S16SYS; fmt.channels = 2; @@ -935,7 +936,11 @@ return FAILURE; } +#if SDL_VERSION_ATLEAST(2, 0, 0) + audio_fd = SDL_OpenAudioDevice(NULL, 0, &fmt, NULL, 0); +#else audio_fd = SDL_OpenAudio(&fmt, NULL); +#endif if (audio_fd < 0) { g_printerr("sdlaudio_init: SDL_OpenAudio(): %s\n", SDL_GetError()); @@ -953,8 +958,13 @@ sdlaudio_term(void) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_PauseAudioDevice(audio_fd, 1); + SDL_CloseAudioDevice(audio_fd); +#else SDL_PauseAudio(1); SDL_CloseAudio(); +#endif return SUCCESS; } @@ -963,7 +973,11 @@ sdlaudio_lock(void) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_LockAudioDevice(audio_fd); +#else SDL_LockAudio(); +#endif } static void @@ -970,7 +984,11 @@ sdlaudio_unlock(void) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_UnlockAudioDevice(audio_fd); +#else SDL_UnlockAudio(); +#endif } static void @@ -977,7 +995,11 @@ sdlaudio_play(void) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_PauseAudioDevice(audio_fd, 0); +#else SDL_PauseAudio(0); +#endif } static void @@ -984,7 +1006,11 @@ sdlaudio_stop(void) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_PauseAudioDevice(audio_fd, 1); +#else SDL_PauseAudio(1); +#endif } static BRESULT @@ -1028,7 +1054,15 @@ goto failure; } +#if defined(SDL_MIXER_VERSION_ATLEAST) +#if SDL_MIXER_VERSION_ATLEAST(2, 0, 2) + rv = Mix_OpenAudioDevice(rate, AUDIO_S16SYS, 2, samples, NULL, 1); +#else rv = Mix_OpenAudio(rate, AUDIO_S16SYS, 2, samples); +#endif +#else + rv = Mix_OpenAudio(rate, AUDIO_S16SYS, 2, samples); +#endif if (rv < 0) { g_printerr("sdlmixer_init: Mix_OpenAudio(): %s\n", Mix_GetError()); @@ -1179,9 +1213,15 @@ SNDBUF_FILLED_QUEUE_REMOVE_HEAD(); sndbuf_unlock(); +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_MixAudioFormat(stream, + sndbuf->buf + (sndbuf->size - sndbuf->remain), + AUDIO_S16SYS, sndbuf->remain, SDL_MIX_MAXVOLUME); +#else SDL_MixAudio(stream, sndbuf->buf + (sndbuf->size - sndbuf->remain), sndbuf->remain, SDL_MIX_MAXVOLUME); +#endif stream += sndbuf->remain; len -= sndbuf->remain; sndbuf->remain = 0; @@ -1198,8 +1238,14 @@ sndbuf_unlock(); } +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_MixAudioFormat(stream, + sndbuf->buf + (sndbuf->size - sndbuf->remain), AUDIO_S16SYS, + len, SDL_MIX_MAXVOLUME); +#else SDL_MixAudio(stream, sndbuf->buf + (sndbuf->size - sndbuf->remain), len, SDL_MIX_MAXVOLUME); +#endif sndbuf->remain -= len; if (sndbuf->remain == 0) {