Skip to content

Commit

Permalink
sdl_audio: Implement SetVolume and add more error checking. (shadps4-…
Browse files Browse the repository at this point in the history
  • Loading branch information
squidbus authored and Xcedf committed Dec 29, 2024
1 parent f1e9497 commit ac816b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/core/libraries/audio/cubeb_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <mutex>
#include <cubeb/cubeb.h>

#include "common/assert.h"
#include "common/logging/log.h"
#include "common/ringbuffer.h"
#include "core/libraries/audio/audioout.h"
#include "core/libraries/audio/audioout_backend.h"
Expand Down Expand Up @@ -58,6 +58,8 @@ class CubebPortBackend : public PortBackend {
}
if (const auto ret = cubeb_stream_start(stream); ret != CUBEB_OK) {
LOG_ERROR(Lib_AudioOut, "Failed to start cubeb stream: {}", ret);
cubeb_stream_destroy(stream);
stream = nullptr;
return;
}
}
Expand All @@ -74,6 +76,9 @@ class CubebPortBackend : public PortBackend {
}

void Output(void* ptr, size_t size) override {
if (!stream) {
return;
}
auto* data = static_cast<u8*>(ptr);

std::unique_lock lock{buffer_mutex};
Expand Down
30 changes: 23 additions & 7 deletions src/core/libraries/audio/sdl_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include <thread>

#include <SDL3/SDL_audio.h>
#include <SDL3/SDL_init.h>

#include "common/logging/log.h"
#include "core/libraries/audio/audioout.h"
Expand All @@ -26,18 +24,28 @@ class SDLPortBackend : public PortBackend {
SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &fmt, nullptr, nullptr);
if (stream == nullptr) {
LOG_ERROR(Lib_AudioOut, "Failed to create SDL audio stream: {}", SDL_GetError());
return;
}
if (!SDL_ResumeAudioStreamDevice(stream)) {
LOG_ERROR(Lib_AudioOut, "Failed to resume SDL audio stream: {}", SDL_GetError());
SDL_DestroyAudioStream(stream);
stream = nullptr;
return;
}
SDL_ResumeAudioStreamDevice(stream);
}

~SDLPortBackend() override {
if (stream) {
SDL_DestroyAudioStream(stream);
stream = nullptr;
if (!stream) {
return;
}
SDL_DestroyAudioStream(stream);
stream = nullptr;
}

void Output(void* ptr, size_t size) override {
if (!stream) {
return;
}
SDL_PutAudioStreamData(stream, ptr, static_cast<int>(size));
while (SDL_GetAudioStreamAvailable(stream) > AUDIO_STREAM_BUFFER_THRESHOLD) {
// Yield to allow the stream to drain.
Expand All @@ -46,7 +54,15 @@ class SDLPortBackend : public PortBackend {
}

void SetVolume(const std::array<int, 8>& ch_volumes) override {
// TODO: Not yet implemented
if (!stream) {
return;
}
// SDL does not have per-channel volumes, for now just take the maximum of the channels.
const auto vol = *std::ranges::max_element(ch_volumes);
if (!SDL_SetAudioStreamGain(stream, static_cast<float>(vol) / SCE_AUDIO_OUT_VOLUME_0DB)) {
LOG_WARNING(Lib_AudioOut, "Failed to change SDL audio stream volume: {}",
SDL_GetError());
}
}

private:
Expand Down

0 comments on commit ac816b8

Please sign in to comment.