2016-04-27 10:57:29 +01:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-04-27 10:57:29 +01:00
|
|
|
#include <SDL.h>
|
2017-12-20 18:44:32 +00:00
|
|
|
#include "audio_core/audio_types.h"
|
2016-09-21 07:52:38 +01:00
|
|
|
#include "audio_core/sdl2_sink.h"
|
2016-04-27 10:57:29 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
|
|
|
struct SDL2Sink::Impl {
|
|
|
|
unsigned int sample_rate = 0;
|
|
|
|
|
|
|
|
SDL_AudioDeviceID audio_device_id = 0;
|
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
std::function<void(s16*, std::size_t)> cb;
|
2016-04-27 10:57:29 +01:00
|
|
|
|
|
|
|
static void Callback(void* impl_, u8* buffer, int buffer_size_in_bytes);
|
|
|
|
};
|
|
|
|
|
2018-07-02 14:03:14 +01:00
|
|
|
SDL2Sink::SDL2Sink(std::string device_name) : impl(std::make_unique<Impl>()) {
|
2016-04-27 10:57:29 +01:00
|
|
|
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_CRITICAL(Audio_Sink, "SDL_Init(SDL_INIT_AUDIO) failed with: {}", SDL_GetError());
|
2016-04-27 10:57:29 +01:00
|
|
|
impl->audio_device_id = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_AudioSpec desired_audiospec;
|
|
|
|
SDL_zero(desired_audiospec);
|
|
|
|
desired_audiospec.format = AUDIO_S16;
|
|
|
|
desired_audiospec.channels = 2;
|
|
|
|
desired_audiospec.freq = native_sample_rate;
|
2016-09-07 15:26:38 +01:00
|
|
|
desired_audiospec.samples = 512;
|
2016-04-27 10:57:29 +01:00
|
|
|
desired_audiospec.userdata = impl.get();
|
|
|
|
desired_audiospec.callback = &Impl::Callback;
|
|
|
|
|
|
|
|
SDL_AudioSpec obtained_audiospec;
|
|
|
|
SDL_zero(obtained_audiospec);
|
|
|
|
|
2017-01-26 03:33:26 +00:00
|
|
|
const char* device = nullptr;
|
2018-07-12 15:52:06 +01:00
|
|
|
if (device_name != auto_device_name && !device_name.empty()) {
|
2018-07-02 14:03:14 +01:00
|
|
|
device = device_name.c_str();
|
2017-01-26 03:33:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:24:52 +01:00
|
|
|
impl->audio_device_id =
|
|
|
|
SDL_OpenAudioDevice(device, false, &desired_audiospec, &obtained_audiospec, 0);
|
2016-04-27 10:57:29 +01:00
|
|
|
if (impl->audio_device_id <= 0) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed with code {} for device \"{}\"",
|
2018-07-02 14:03:14 +01:00
|
|
|
impl->audio_device_id, device_name);
|
2016-04-27 10:57:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl->sample_rate = obtained_audiospec.freq;
|
|
|
|
|
|
|
|
// SDL2 audio devices start out paused, unpause it:
|
|
|
|
SDL_PauseAudioDevice(impl->audio_device_id, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL2Sink::~SDL2Sink() {
|
|
|
|
if (impl->audio_device_id <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_CloseAudioDevice(impl->audio_device_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int SDL2Sink::GetNativeSampleRate() const {
|
|
|
|
if (impl->audio_device_id <= 0)
|
|
|
|
return native_sample_rate;
|
|
|
|
|
|
|
|
return impl->sample_rate;
|
|
|
|
}
|
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
void SDL2Sink::SetCallback(std::function<void(s16*, std::size_t)> cb) {
|
|
|
|
impl->cb = cb;
|
2016-04-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) {
|
|
|
|
Impl* impl = reinterpret_cast<Impl*>(impl_);
|
2018-09-08 21:07:28 +01:00
|
|
|
if (!impl || !impl->cb)
|
|
|
|
return;
|
2016-04-27 10:57:29 +01:00
|
|
|
|
2024-01-07 20:37:42 +00:00
|
|
|
const std::size_t num_frames = buffer_size_in_bytes / (2 * sizeof(s16));
|
2016-04-27 10:57:29 +01:00
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
impl->cb(reinterpret_cast<s16*>(buffer), num_frames);
|
2016-04-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
2018-07-02 14:03:14 +01:00
|
|
|
std::vector<std::string> ListSDL2SinkDevices() {
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem failed with: {}", SDL_GetError());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> device_list;
|
|
|
|
const int device_count = SDL_GetNumAudioDevices(0);
|
|
|
|
for (int i = 0; i < device_count; ++i) {
|
|
|
|
device_list.push_back(SDL_GetAudioDeviceName(i, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
|
|
|
|
return device_list;
|
|
|
|
}
|
|
|
|
|
2016-04-27 10:57:29 +01:00
|
|
|
} // namespace AudioCore
|