sdl_audio: Implement SetVolume and add more error checking. ()

This commit is contained in:
squidbus 2024-12-27 14:07:16 -08:00 committed by GitHub
parent 3218c36b22
commit 49ffb7b120
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 8 deletions
src/core/libraries/audio

View file

@ -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"
@ -58,6 +58,8 @@ public:
}
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;
}
}
@ -74,6 +76,9 @@ public:
}
void Output(void* ptr, size_t size) override {
if (!stream) {
return;
}
auto* data = static_cast<u8*>(ptr);
std::unique_lock lock{buffer_mutex};

View file

@ -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"
@ -26,18 +24,28 @@ public:
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.
@ -46,7 +54,15 @@ public:
}
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: