Skip to content

Commit

Permalink
pipewire: Report correct device default formats instead of hardcoding…
Browse files Browse the repository at this point in the history
… Float32.

The comment in the source wasn't true; PipeWire doesn't _have_ to work in
float format. It presumably does if it has to mix, but if a game is the only
thing making noise on the system--a common scenario--then it might be able to
pass, say, Sint16 data straight through to the hardware without conversion.

Fixes libsdl-org#12129.
  • Loading branch information
icculus committed Jan 30, 2025
1 parent 943579a commit 943c4ab
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/audio/pipewire/SDL_pipewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,25 @@ static bool get_int_param(const struct spa_pod *param, Uint32 key, int *val)
return false;
}

static SDL_AudioFormat SPAFormatToSDL(enum spa_audio_format spafmt)
{
switch (spafmt) {
#define CHECKFMT(spa,sdl) case SPA_AUDIO_FORMAT_##spa: return SDL_AUDIO_##sdl
CHECKFMT(U8, U8);
CHECKFMT(S8, S8);
CHECKFMT(S16_LE, S16LE);
CHECKFMT(S16_BE, S16BE);
CHECKFMT(S32_LE, S32LE);
CHECKFMT(S32_BE, S32BE);
CHECKFMT(F32_LE, F32LE);
CHECKFMT(F32_BE, F32BE);
#undef CHECKFMT
default: break;
}

return SDL_AUDIO_UNKNOWN;
}

// Interface node callbacks
static void node_event_info(void *object, const struct pw_node_info *info)
{
Expand Down Expand Up @@ -541,6 +560,15 @@ static void node_event_param(void *object, int seq, uint32_t id, uint32_t index,
struct node_object *node = object;
struct io_node *io = node->userdata;

if ((id == SPA_PARAM_Format) && (io->spec.format == SDL_AUDIO_UNKNOWN)) {
struct spa_audio_info_raw info;
SDL_zero(info);
if (spa_format_audio_raw_parse(param, &info) == 0) {
//SDL_Log("Sink Format: %d, Rate: %d Hz, Channels: %d", info.format, info.rate, info.channels);
io->spec.format = SPAFormatToSDL(info.format);
}
}

// Get the default frequency
if (io->spec.freq == 0) {
get_range_param(param, SPA_FORMAT_AUDIO_rate, &io->spec.freq, NULL, NULL);
Expand Down Expand Up @@ -672,7 +700,9 @@ static void registry_event_global_callback(void *object, uint32_t id, uint32_t p
// Begin setting the node properties
io->id = id;
io->recording = recording;
io->spec.format = SDL_AUDIO_F32; // Pipewire uses floats internally, other formats require conversion.
if (io->spec.format == SDL_AUDIO_UNKNOWN) {
io->spec.format = SDL_AUDIO_S16; // we'll go conservative here if for some reason the format isn't known.
}
io->name = io->buf;
io->path = io->buf + desc_buffer_len;
SDL_strlcpy(io->buf, node_desc, desc_buffer_len);
Expand Down

0 comments on commit 943c4ab

Please sign in to comment.