1
0
Fork 0
forked from len0rd/rockbox

SDL: special-case pulseaudio backend with a smaller PCM buffer size

Pull in a small pile of cleanups as well

Change-Id: I087c553e098e9214ce528d2568c22d90e3e86f04
This commit is contained in:
Solomon Peachy 2024-12-12 18:07:38 -05:00
parent 789aa0a695
commit e9b0aa495b

View file

@ -43,6 +43,7 @@
#include "pcm.h" #include "pcm.h"
#include "pcm-internal.h" #include "pcm-internal.h"
#include "pcm_sampr.h" #include "pcm_sampr.h"
#include "pcm_mixer.h"
/*#define LOGF_ENABLE*/ /*#define LOGF_ENABLE*/
#include "logf.h" #include "logf.h"
@ -93,23 +94,26 @@ void pcm_play_unlock(void)
#define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0 #define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0
#endif #endif
static void sdl_audio_callback(struct pcm_udata *udata, Uint8 *stream, int len); static void sdl_audio_callback(void *handle, Uint8 *stream, int len);
static void pcm_dma_apply_settings_nolock(void) static void pcm_dma_apply_settings_nolock(void)
{ {
SDL_AudioSpec wanted_spec; SDL_AudioSpec wanted_spec;
wanted_spec.freq = pcm_sampr; wanted_spec.freq = pcm_sampr;
wanted_spec.format = AUDIO_S16SYS; wanted_spec.format = AUDIO_S16SYS;
wanted_spec.channels = 2; wanted_spec.channels = 2;
wanted_spec.samples = 2048; wanted_spec.samples = MIX_FRAME_SAMPLES * 2; /* Should be 2048, ie ~5ms @44KHz */
wanted_spec.callback = wanted_spec.callback = sdl_audio_callback;
(void (SDLCALL *)(void *userdata,
Uint8 *stream, int len))sdl_audio_callback;
wanted_spec.userdata = &udata; wanted_spec.userdata = &udata;
if (pcm_devid) if (pcm_devid)
SDL_CloseAudioDevice(pcm_devid); SDL_CloseAudioDevice(pcm_devid);
/* pulseaudio seems to be happier with smaller buffers */
if (!strcmp("pulseaudio", SDL_GetCurrentAudioDriver()))
wanted_spec.samples = MIX_FRAME_SAMPLES;
/* Open the audio device and start playing sound! */ /* Open the audio device and start playing sound! */
if((pcm_devid = SDL_OpenAudioDevice(audiodev, 0, &wanted_spec, &obtained, SDL_AUDIO_ALLOW_SAMPLES_CHANGE)) == 0) { if((pcm_devid = SDL_OpenAudioDevice(audiodev, 0, &wanted_spec, &obtained, SDL_AUDIO_ALLOW_SAMPLES_CHANGE)) == 0) {
panicf("Unable to open audio: %s\n", SDL_GetError()); panicf("Unable to open audio: %s", SDL_GetError());
return; return;
} }
switch (obtained.format) switch (obtained.format)
@ -131,7 +135,7 @@ static void pcm_dma_apply_settings_nolock(void)
pcm_channel_bytes = 4; pcm_channel_bytes = 4;
break; break;
default: default:
panicf("Unknown sample format obtained: %u\n", panicf("Unknown sample format obtained: %u",
(unsigned)obtained.format); (unsigned)obtained.format);
return; return;
} }
@ -154,7 +158,6 @@ void pcm_dma_apply_settings(void)
void pcm_play_dma_start(const void *addr, size_t size) void pcm_play_dma_start(const void *addr, size_t size)
{ {
pcm_data = addr; pcm_data = addr;
pcm_data_size = size; pcm_data_size = size;
@ -262,9 +265,11 @@ static void write_to_soundcard(struct pcm_udata *udata)
} }
} }
static void sdl_audio_callback(struct pcm_udata *udata, Uint8 *stream, int len) static void sdl_audio_callback(void *handle, Uint8 *stream, int len)
{ {
logf("sdl_audio_callback: len %d, pcm %d\n", len, pcm_data_size); struct pcm_udata *udata = handle;
logf("sdl_audio_callback: len %d, pcm %zd", len, pcm_data_size);
bool new_buffer = false; bool new_buffer = false;
udata->stream = stream; udata->stream = stream;
@ -284,6 +289,7 @@ static void sdl_audio_callback(struct pcm_udata *udata, Uint8 *stream, int len)
DEBUGF("sdl_audio_callback: No Data.\n"); DEBUGF("sdl_audio_callback: No Data.\n");
break; break;
} }
logf("audio_callback_cont: len %d, pcm %zd", len, pcm_data_size);
start: start:
udata->num_in = pcm_data_size / pcm_sample_bytes; udata->num_in = pcm_data_size / pcm_sample_bytes;
@ -374,7 +380,7 @@ void pcm_play_dma_init(void)
{ {
if (SDL_InitSubSystem(SDL_INIT_AUDIO)) if (SDL_InitSubSystem(SDL_INIT_AUDIO))
{ {
panicf("Could not initialize SDL audio subsystem!\n"); panicf("Could not initialize SDL audio subsystem!");
return; return;
} }
@ -395,7 +401,7 @@ void pcm_play_dma_init(void)
if (!audio_lock) if (!audio_lock)
{ {
panicf("Could not create audio_lock\n"); panicf("Could not create audio_lock");
return; return;
} }