Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Audio on SDL3: playback from the AudioQueue, the microphone

Rafael Abud Ferreira377c70d63a78
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d2f5de..4e8b05b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -195,6 +195,8 @@ if(NOT XCLOUD_PS5 AND (NOT DEFINED XCLOUD_DESKTOP_APP OR XCLOUD_DESKTOP_APP))
src/input/sdl_input.cpp
src/input/controller_sdl.cpp
src/input/kbm_sdl.cpp
+ src/media/audio_out_sdl.cpp
+ src/media/audio_in_sdl.cpp
)
target_link_libraries(xcloud_backends_sdl_io PUBLIC xcloud_core xcloud_media SDL3::SDL3)
if(NOT MSVC)
diff --git a/src/media/audio_in_sdl.cpp b/src/media/audio_in_sdl.cpp
new file mode 100644
index 0000000..f69f9e9
--- /dev/null
+++ b/src/media/audio_in_sdl.cpp
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (C) 2026 RafaelNGP
+// The microphone on the desktop (audio_in.h): SDL3's default recording
+// device, 48 kHz mono S16, handed to audio_in_common.h's gain, mute, meter
+// and 20 ms frames in the PS5's 256-sample grains, on SDL's device thread.
+// No system mute is read here (isHardwareMuted() stays false).
+#include "media/audio_in.h"
+#include "media/audio_in_common.h"
+#include "util/log.h"
+
+#include <SDL3/SDL.h>
+
+#include <vector>
+
+namespace xc::media {
+
+struct AudioIn::Impl {
+ SDL_AudioStream* stream = nullptr;
+ bool initedAudio = false; // SDL's audio subsystem started here (not by the shell)
+ MicPipeline mic;
+ std::vector<int16_t> grain = std::vector<int16_t>(MicPipeline::kGrain);
+ uint64_t grains = 0;
+};
+
+AudioIn::AudioIn() : impl_(std::make_unique<Impl>()) {}
+
+AudioIn::~AudioIn() {
+ close();
+}
+
+bool AudioIn::open(AudioCallback cb) {
+ if (running_) return true;
+ callback_ = std::move(cb);
+
+ if (!SDL_WasInit(SDL_INIT_AUDIO)) {
+ if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) {
+ XC_LOGE("audio in: SDL audio: %s", SDL_GetError());
+ return false;
+ }
+ impl_->initedAudio = true;
+ }
+ impl_->mic.reset();
+ impl_->grains = 0;
+ // SDL's device thread, as samples come in: whole grains through the
+ // pipeline, the rest left in the stream for next time.
+ auto put = [](void* self, SDL_AudioStream* stream, int, int) {
+ AudioIn& in = *static_cast<AudioIn*>(self);
+ Impl& impl = *in.impl_;
+ const int bytes = static_cast<int>(MicPipeline::kGrain * sizeof(int16_t));
+ while (in.running_ && SDL_GetAudioStreamAvailable(stream) >= bytes) {
+ if (SDL_GetAudioStreamData(stream, impl.grain.data(), bytes) != bytes) break;
+ if (++impl.grains == 1) XC_LOGI("audio in: first input buffer received (%zu samples)", MicPipeline::kGrain);
+ impl.mic.process(impl.grain.data(), impl.grain.size(), in.muted_.load(std::memory_order_relaxed),
+ in.gain_.load(std::memory_order_relaxed), in.level_, in.callback_);
+ }
+ };
+ const SDL_AudioSpec spec{SDL_AUDIO_S16, kChannels, kSampleRate};
+ running_ = true;
+ impl_->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec, put, this);
+ if (!impl_->stream) {
+ running_ = false;
+ XC_LOGE("audio in: SDL_OpenAudioDeviceStream (recording): %s", SDL_GetError());
+ if (impl_->initedAudio) SDL_QuitSubSystem(SDL_INIT_AUDIO);
+ impl_->initedAudio = false;
+ return false;
+ }
+ SDL_AudioSpec device{};
+ int frames = 0;
+ SDL_GetAudioDeviceFormat(SDL_GetAudioStreamDevice(impl_->stream), &device, &frames);
+ SDL_ResumeAudioStreamDevice(impl_->stream);
+ XC_LOGI("audio in: SDL microphone opened (48 kHz mono S16; device %d Hz, %d channels, %d-frame buffer)", device.freq,
+ device.channels, frames);
+ return true;
+}
+
+void AudioIn::close() {
+ if (!running_) return;
+ running_ = false;
+ level_.store(0.0f, std::memory_order_relaxed);
+ hardwareMuted_.store(false, std::memory_order_relaxed);
+
+ if (impl_->stream) {
+ SDL_DestroyAudioStream(impl_->stream); // no more callbacks once it returns
+ impl_->stream = nullptr;
+ }
+ if (impl_->initedAudio) SDL_QuitSubSystem(SDL_INIT_AUDIO);
+ impl_->initedAudio = false;
+ callback_ = nullptr;
+ XC_LOGI("audio in: closed");
+}
+
+} // namespace xc::media
diff --git a/src/media/audio_out_sdl.cpp b/src/media/audio_out_sdl.cpp
new file mode 100644
index 0000000..ffe4712
--- /dev/null
+++ b/src/media/audio_out_sdl.cpp
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (C) 2026 RafaelNGP
+// Sound out on the desktop (audio_out.h): SDL3's default playback device,
+// 48 kHz float stereo, pulling from the shared queue (audio_queue.h) as the
+// device asks for more (silence on underrun). It asks for 256-frame device
+// buffers, as the PS5's grain; the system may give others (logged).
+// Both routes play on the default device (the system picks the speakers or
+// a headset).
+#include "media/audio_out.h"
+
+#include "media/audio_queue.h"
+#include "util/log.h"
+
+#include <SDL3/SDL.h>
+
+#include <mutex>
+#include <vector>
+
+namespace xc::media {
+
+namespace {
+
+std::mutex g_mutex; // start / stop
+SDL_AudioStream* g_stream = nullptr;
+bool g_initedAudio = false; // SDL's audio subsystem started here (not by the shell)
+std::vector<float> g_buffer; // the device thread's
+
+// SDL's device thread: `needed` more bytes wanted.
+void SDLCALL feed(void*, SDL_AudioStream* stream, int needed, int) {
+ if (needed <= 0) return;
+ size_t frames = (static_cast<size_t>(needed) + 2 * sizeof(float) - 1) / (2 * sizeof(float));
+ if (g_buffer.size() < frames * 2) g_buffer.resize(frames * 2);
+ audioQueue().pull(g_buffer.data(), frames);
+ SDL_PutAudioStreamData(stream, g_buffer.data(), static_cast<int>(frames * 2 * sizeof(float)));
+}
+
+} // namespace
+
+bool audioStart() {
+ std::lock_guard<std::mutex> lock(g_mutex);
+ if (g_stream) return true;
+ if (!SDL_WasInit(SDL_INIT_AUDIO)) {
+ if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) {
+ XC_LOGE("SDL audio: %s", SDL_GetError());
+ return false;
+ }
+ g_initedAudio = true;
+ }
+ SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, "256");
+ const SDL_AudioSpec spec{SDL_AUDIO_F32, 2, AudioQueue::kRate};
+ g_stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, feed, nullptr);
+ if (!g_stream) {
+ XC_LOGE("SDL_OpenAudioDeviceStream (playback): %s", SDL_GetError());
+ if (g_initedAudio) SDL_QuitSubSystem(SDL_INIT_AUDIO);
+ g_initedAudio = false;
+ return false;
+ }
+ SDL_AudioSpec device{};
+ int frames = 0;
+ SDL_GetAudioDeviceFormat(SDL_GetAudioStreamDevice(g_stream), &device, &frames);
+ audioQueue().open();
+ SDL_ResumeAudioStreamDevice(g_stream);
+ const char* driver = SDL_GetCurrentAudioDriver();
+ XC_LOGI("audio output open (48 kHz float stereo; SDL %s device: %d Hz, %d channels, %d-frame buffer, %.1f ms)",
+ driver ? driver : "?", device.freq, device.channels, frames,
+ device.freq > 0 ? frames * 1000.0 / device.freq : 0.0);
+ return true;
+}
+
+void audioStop() {
+ std::lock_guard<std::mutex> lock(g_mutex);
+ if (!g_stream) return;
+ audioQueue().close();
+ SDL_DestroyAudioStream(g_stream); // the device with it; no more feeding once it returns
+ g_stream = nullptr;
+ audioQueue().clear();
+ if (g_initedAudio) SDL_QuitSubSystem(SDL_INIT_AUDIO);
+ g_initedAudio = false;
+ XC_LOGI("audio output closed");
+}
+
+} // namespace xc::media