2016-04-27 04:57:29 -05:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <list>
|
2016-09-20 10:21:23 -05:00
|
|
|
#include <numeric>
|
2016-04-27 04:57:29 -05:00
|
|
|
#include <SDL.h>
|
2017-12-20 12:44:32 -06:00
|
|
|
#include "audio_core/audio_types.h"
|
2016-09-21 01:52:38 -05:00
|
|
|
#include "audio_core/sdl2_sink.h"
|
2016-04-27 04:57:29 -05: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;
|
|
|
|
|
|
|
|
std::list<std::vector<s16>> queue;
|
|
|
|
|
|
|
|
static void Callback(void* impl_, u8* buffer, int buffer_size_in_bytes);
|
|
|
|
};
|
|
|
|
|
2018-07-02 08:03:14 -05:00
|
|
|
SDL2Sink::SDL2Sink(std::string device_name) : impl(std::make_unique<Impl>()) {
|
2016-04-27 04:57:29 -05:00
|
|
|
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
2018-06-29 06:18:07 -05:00
|
|
|
LOG_CRITICAL(Audio_Sink, "SDL_Init(SDL_INIT_AUDIO) failed with: {}", SDL_GetError());
|
2016-04-27 04:57:29 -05: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 09:26:38 -05:00
|
|
|
desired_audiospec.samples = 512;
|
2016-04-27 04:57:29 -05:00
|
|
|
desired_audiospec.userdata = impl.get();
|
|
|
|
desired_audiospec.callback = &Impl::Callback;
|
|
|
|
|
|
|
|
SDL_AudioSpec obtained_audiospec;
|
|
|
|
SDL_zero(obtained_audiospec);
|
|
|
|
|
2017-01-25 21:33:26 -06:00
|
|
|
const char* device = nullptr;
|
2018-07-12 09:52:06 -05:00
|
|
|
if (device_name != auto_device_name && !device_name.empty()) {
|
2018-07-02 08:03:14 -05:00
|
|
|
device = device_name.c_str();
|
2017-01-25 21:33:26 -06:00
|
|
|
}
|
|
|
|
|
2017-10-23 20:19:36 -05:00
|
|
|
impl->audio_device_id = SDL_OpenAudioDevice(
|
|
|
|
device, false, &desired_audiospec, &obtained_audiospec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
|
2016-04-27 04:57:29 -05:00
|
|
|
if (impl->audio_device_id <= 0) {
|
2018-06-29 06:18:07 -05:00
|
|
|
LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed with code {} for device \"{}\"",
|
2018-07-02 08:03:14 -05:00
|
|
|
impl->audio_device_id, device_name);
|
2016-04-27 04:57:29 -05: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;
|
|
|
|
}
|
|
|
|
|
2016-08-31 10:55:10 -05:00
|
|
|
void SDL2Sink::EnqueueSamples(const s16* samples, size_t sample_count) {
|
2016-04-27 04:57:29 -05:00
|
|
|
if (impl->audio_device_id <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_LockAudioDevice(impl->audio_device_id);
|
2016-08-31 10:55:10 -05:00
|
|
|
impl->queue.emplace_back(samples, samples + sample_count * 2);
|
2016-04-27 04:57:29 -05:00
|
|
|
SDL_UnlockAudioDevice(impl->audio_device_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SDL2Sink::SamplesInQueue() const {
|
|
|
|
if (impl->audio_device_id <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
SDL_LockAudioDevice(impl->audio_device_id);
|
|
|
|
|
2016-09-17 19:38:01 -05:00
|
|
|
size_t total_size = std::accumulate(impl->queue.begin(), impl->queue.end(),
|
|
|
|
static_cast<size_t>(0), [](size_t sum, const auto& buffer) {
|
|
|
|
// Division by two because each stereo sample is made of
|
|
|
|
// two s16.
|
|
|
|
return sum + buffer.size() / 2;
|
|
|
|
});
|
2016-04-27 04:57:29 -05:00
|
|
|
|
|
|
|
SDL_UnlockAudioDevice(impl->audio_device_id);
|
|
|
|
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) {
|
|
|
|
Impl* impl = reinterpret_cast<Impl*>(impl_);
|
|
|
|
|
2016-09-17 19:38:01 -05:00
|
|
|
size_t remaining_size = static_cast<size_t>(buffer_size_in_bytes) /
|
|
|
|
sizeof(s16); // Keep track of size in 16-bit increments.
|
2016-04-27 04:57:29 -05:00
|
|
|
|
|
|
|
while (remaining_size > 0 && !impl->queue.empty()) {
|
|
|
|
if (impl->queue.front().size() <= remaining_size) {
|
|
|
|
memcpy(buffer, impl->queue.front().data(), impl->queue.front().size() * sizeof(s16));
|
|
|
|
buffer += impl->queue.front().size() * sizeof(s16);
|
|
|
|
remaining_size -= impl->queue.front().size();
|
|
|
|
impl->queue.pop_front();
|
|
|
|
} else {
|
|
|
|
memcpy(buffer, impl->queue.front().data(), remaining_size * sizeof(s16));
|
|
|
|
buffer += remaining_size * sizeof(s16);
|
2016-09-17 19:38:01 -05:00
|
|
|
impl->queue.front().erase(impl->queue.front().begin(),
|
|
|
|
impl->queue.front().begin() + remaining_size);
|
2016-04-27 04:57:29 -05:00
|
|
|
remaining_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remaining_size > 0) {
|
|
|
|
memset(buffer, 0, remaining_size * sizeof(s16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 08:03:14 -05: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 04:57:29 -05:00
|
|
|
} // namespace AudioCore
|