mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-05-14 08:12:16 +00:00
sdl_audio: Implement SetVolume and add more error checking. (#1935)
This commit is contained in:
parent
3218c36b22
commit
49ffb7b120
2 changed files with 29 additions and 8 deletions
|
@ -5,7 +5,7 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <cubeb/cubeb.h>
|
#include <cubeb/cubeb.h>
|
||||||
|
|
||||||
#include "common/assert.h"
|
#include "common/logging/log.h"
|
||||||
#include "common/ringbuffer.h"
|
#include "common/ringbuffer.h"
|
||||||
#include "core/libraries/audio/audioout.h"
|
#include "core/libraries/audio/audioout.h"
|
||||||
#include "core/libraries/audio/audioout_backend.h"
|
#include "core/libraries/audio/audioout_backend.h"
|
||||||
|
@ -58,6 +58,8 @@ public:
|
||||||
}
|
}
|
||||||
if (const auto ret = cubeb_stream_start(stream); ret != CUBEB_OK) {
|
if (const auto ret = cubeb_stream_start(stream); ret != CUBEB_OK) {
|
||||||
LOG_ERROR(Lib_AudioOut, "Failed to start cubeb stream: {}", ret);
|
LOG_ERROR(Lib_AudioOut, "Failed to start cubeb stream: {}", ret);
|
||||||
|
cubeb_stream_destroy(stream);
|
||||||
|
stream = nullptr;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,6 +76,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void Output(void* ptr, size_t size) override {
|
void Output(void* ptr, size_t size) override {
|
||||||
|
if (!stream) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
auto* data = static_cast<u8*>(ptr);
|
auto* data = static_cast<u8*>(ptr);
|
||||||
|
|
||||||
std::unique_lock lock{buffer_mutex};
|
std::unique_lock lock{buffer_mutex};
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include <SDL3/SDL_audio.h>
|
#include <SDL3/SDL_audio.h>
|
||||||
#include <SDL3/SDL_init.h>
|
|
||||||
|
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "core/libraries/audio/audioout.h"
|
#include "core/libraries/audio/audioout.h"
|
||||||
|
@ -26,18 +24,28 @@ public:
|
||||||
SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &fmt, nullptr, nullptr);
|
SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &fmt, nullptr, nullptr);
|
||||||
if (stream == nullptr) {
|
if (stream == nullptr) {
|
||||||
LOG_ERROR(Lib_AudioOut, "Failed to create SDL audio stream: {}", SDL_GetError());
|
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 {
|
~SDLPortBackend() override {
|
||||||
if (stream) {
|
if (!stream) {
|
||||||
SDL_DestroyAudioStream(stream);
|
return;
|
||||||
stream = nullptr;
|
|
||||||
}
|
}
|
||||||
|
SDL_DestroyAudioStream(stream);
|
||||||
|
stream = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Output(void* ptr, size_t size) override {
|
void Output(void* ptr, size_t size) override {
|
||||||
|
if (!stream) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
SDL_PutAudioStreamData(stream, ptr, static_cast<int>(size));
|
SDL_PutAudioStreamData(stream, ptr, static_cast<int>(size));
|
||||||
while (SDL_GetAudioStreamAvailable(stream) > AUDIO_STREAM_BUFFER_THRESHOLD) {
|
while (SDL_GetAudioStreamAvailable(stream) > AUDIO_STREAM_BUFFER_THRESHOLD) {
|
||||||
// Yield to allow the stream to drain.
|
// Yield to allow the stream to drain.
|
||||||
|
@ -46,7 +54,15 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetVolume(const std::array<int, 8>& ch_volumes) override {
|
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:
|
private:
|
||||||
|
|
Loading…
Add table
Reference in a new issue