2022-07-16 22:48:45 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
#include <span>
|
|
|
|
#include <vector>
|
2023-05-05 07:48:28 +00:00
|
|
|
#include <SDL.h>
|
2022-07-16 22:48:45 +00:00
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
#include "audio_core/common/common.h"
|
2022-07-16 22:48:45 +00:00
|
|
|
#include "audio_core/sink/sdl2_sink.h"
|
|
|
|
#include "audio_core/sink/sink_stream.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
|
|
|
|
namespace AudioCore::Sink {
|
|
|
|
/**
|
|
|
|
* SDL sink stream, responsible for sinking samples to hardware.
|
|
|
|
*/
|
|
|
|
class SDLSinkStream final : public SinkStream {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a new sink stream.
|
|
|
|
*
|
|
|
|
* @param device_channels_ - Number of channels supported by the hardware.
|
|
|
|
* @param system_channels_ - Number of channels the audio systems expect.
|
|
|
|
* @param output_device - Name of the output device to use for this stream.
|
|
|
|
* @param input_device - Name of the input device to use for this stream.
|
|
|
|
* @param type_ - Type of this stream.
|
|
|
|
* @param system_ - Core system.
|
|
|
|
* @param event - Event used only for audio renderer, signalled on buffer consume.
|
|
|
|
*/
|
2022-08-01 01:58:13 +00:00
|
|
|
SDLSinkStream(u32 device_channels_, u32 system_channels_, const std::string& output_device,
|
|
|
|
const std::string& input_device, StreamType type_, Core::System& system_)
|
|
|
|
: SinkStream{system_, type_} {
|
2022-07-16 22:48:45 +00:00
|
|
|
system_channels = system_channels_;
|
|
|
|
device_channels = device_channels_;
|
|
|
|
|
|
|
|
SDL_AudioSpec spec;
|
|
|
|
spec.freq = TargetSampleRate;
|
|
|
|
spec.channels = static_cast<u8>(device_channels);
|
|
|
|
spec.format = AUDIO_S16SYS;
|
2022-10-08 18:27:54 +00:00
|
|
|
spec.samples = TargetSampleCount * 2;
|
2022-07-16 22:48:45 +00:00
|
|
|
spec.callback = &SDLSinkStream::DataCallback;
|
|
|
|
spec.userdata = this;
|
|
|
|
|
|
|
|
std::string device_name{output_device};
|
|
|
|
bool capture{false};
|
|
|
|
if (type == StreamType::In) {
|
|
|
|
device_name = input_device;
|
|
|
|
capture = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_AudioSpec obtained;
|
|
|
|
if (device_name.empty()) {
|
|
|
|
device = SDL_OpenAudioDevice(nullptr, capture, &spec, &obtained, false);
|
|
|
|
} else {
|
|
|
|
device = SDL_OpenAudioDevice(device_name.c_str(), capture, &spec, &obtained, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device == 0) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Error opening SDL audio device: {}", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
LOG_INFO(Service_Audio,
|
|
|
|
"Opening SDL stream {} with: rate {} channels {} (system channels {}) "
|
|
|
|
" samples {}",
|
|
|
|
device, obtained.freq, obtained.channels, system_channels, obtained.samples);
|
2022-07-16 22:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the sink stream.
|
|
|
|
*/
|
|
|
|
~SDLSinkStream() override {
|
2022-08-01 01:58:13 +00:00
|
|
|
LOG_DEBUG(Service_Audio, "Destructing SDL stream {}", name);
|
|
|
|
Finalize();
|
2022-07-16 22:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalize the sink stream.
|
|
|
|
*/
|
|
|
|
void Finalize() override {
|
|
|
|
if (device == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
Stop();
|
2022-07-16 22:48:45 +00:00
|
|
|
SDL_CloseAudioDevice(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the sink stream.
|
|
|
|
*
|
|
|
|
* @param resume - Set to true if this is resuming the stream a previously-active stream.
|
|
|
|
* Default false.
|
|
|
|
*/
|
2022-08-01 01:58:13 +00:00
|
|
|
void Start(bool resume = false) override {
|
2022-09-10 20:14:03 +00:00
|
|
|
if (device == 0 || !paused) {
|
2022-07-16 22:48:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:14:03 +00:00
|
|
|
paused = false;
|
|
|
|
SDL_PauseAudioDevice(device, 0);
|
2022-07-16 22:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the sink stream.
|
|
|
|
*/
|
2022-08-01 01:58:13 +00:00
|
|
|
void Stop() override {
|
2022-09-10 20:14:03 +00:00
|
|
|
if (device == 0 || paused) {
|
2022-07-16 22:48:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
paused = true;
|
2022-09-10 20:14:03 +00:00
|
|
|
SDL_PauseAudioDevice(device, 1);
|
2022-07-16 22:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Main callback from SDL. Either expects samples from us (audio render/audio out), or will
|
|
|
|
* provide samples to be copied (audio in).
|
|
|
|
*
|
|
|
|
* @param userdata - Custom data pointer passed along, points to a SDLSinkStream.
|
|
|
|
* @param stream - Buffer of samples to be filled or read.
|
|
|
|
* @param len - Length of the stream in bytes.
|
|
|
|
*/
|
|
|
|
static void DataCallback(void* userdata, Uint8* stream, int len) {
|
|
|
|
auto* impl = static_cast<SDLSinkStream*>(userdata);
|
|
|
|
|
|
|
|
if (!impl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t num_channels = impl->GetDeviceChannels();
|
|
|
|
const std::size_t frame_size = num_channels;
|
|
|
|
const std::size_t num_frames{len / num_channels / sizeof(s16)};
|
|
|
|
|
|
|
|
if (impl->type == StreamType::In) {
|
2022-08-01 01:58:13 +00:00
|
|
|
std::span<const s16> input_buffer{reinterpret_cast<const s16*>(stream),
|
|
|
|
num_frames * frame_size};
|
|
|
|
impl->ProcessAudioIn(input_buffer, num_frames);
|
2022-07-16 22:48:45 +00:00
|
|
|
} else {
|
|
|
|
std::span<s16> output_buffer{reinterpret_cast<s16*>(stream), num_frames * frame_size};
|
2022-08-01 01:58:13 +00:00
|
|
|
impl->ProcessAudioOutAndRender(output_buffer, num_frames);
|
2022-07-16 22:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SDL device id of the opened input/output device
|
|
|
|
SDL_AudioDeviceID device{};
|
|
|
|
};
|
|
|
|
|
|
|
|
SDLSink::SDLSink(std::string_view target_device_name) {
|
|
|
|
if (!SDL_WasInit(SDL_INIT_AUDIO)) {
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem audio failed: {}", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target_device_name != auto_device_name && !target_device_name.empty()) {
|
|
|
|
output_device = target_device_name;
|
|
|
|
} else {
|
|
|
|
output_device.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
device_channels = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLSink::~SDLSink() = default;
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
SinkStream* SDLSink::AcquireSinkStream(Core::System& system, u32 system_channels,
|
|
|
|
const std::string&, StreamType type) {
|
2022-07-16 22:48:45 +00:00
|
|
|
SinkStreamPtr& stream = sink_streams.emplace_back(std::make_unique<SDLSinkStream>(
|
|
|
|
device_channels, system_channels, output_device, input_device, type, system));
|
|
|
|
return stream.get();
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
void SDLSink::CloseStream(SinkStream* stream) {
|
2022-07-16 22:48:45 +00:00
|
|
|
for (size_t i = 0; i < sink_streams.size(); i++) {
|
|
|
|
if (sink_streams[i].get() == stream) {
|
|
|
|
sink_streams[i].reset();
|
|
|
|
sink_streams.erase(sink_streams.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLSink::CloseStreams() {
|
|
|
|
sink_streams.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
f32 SDLSink::GetDeviceVolume() const {
|
|
|
|
if (sink_streams.empty()) {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sink_streams[0]->GetDeviceVolume();
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
void SDLSink::SetDeviceVolume(f32 volume) {
|
2022-07-16 22:48:45 +00:00
|
|
|
for (auto& stream : sink_streams) {
|
|
|
|
stream->SetDeviceVolume(volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
void SDLSink::SetSystemVolume(f32 volume) {
|
2022-07-16 22:48:45 +00:00
|
|
|
for (auto& stream : sink_streams) {
|
|
|
|
stream->SetSystemVolume(volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:58:13 +00:00
|
|
|
std::vector<std::string> ListSDLSinkDevices(bool capture) {
|
2022-07-16 22:48:45 +00:00
|
|
|
std::vector<std::string> device_list;
|
|
|
|
|
|
|
|
if (!SDL_WasInit(SDL_INIT_AUDIO)) {
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem audio failed: {}", SDL_GetError());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int device_count = SDL_GetNumAudioDevices(capture);
|
|
|
|
for (int i = 0; i < device_count; ++i) {
|
2022-10-17 00:37:11 +00:00
|
|
|
if (const char* name = SDL_GetAudioDeviceName(i, capture)) {
|
2022-10-16 07:14:52 +00:00
|
|
|
device_list.emplace_back(name);
|
|
|
|
}
|
2022-07-16 22:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return device_list;
|
|
|
|
}
|
|
|
|
|
2022-10-08 18:27:54 +00:00
|
|
|
u32 GetSDLLatency() {
|
|
|
|
return TargetSampleCount * 2;
|
|
|
|
}
|
|
|
|
|
2022-07-16 22:48:45 +00:00
|
|
|
} // namespace AudioCore::Sink
|