summaryrefslogtreecommitdiffstats
path: root/src/audio_core
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core')
-rw-r--r--src/audio_core/CMakeLists.txt5
-rw-r--r--src/audio_core/common.h2
-rw-r--r--src/audio_core/sdl2_sink.cpp163
-rw-r--r--src/audio_core/sdl2_sink.h29
-rw-r--r--src/audio_core/sink_context.h2
-rw-r--r--src/audio_core/sink_details.cpp10
-rw-r--r--src/audio_core/stream.cpp7
-rw-r--r--src/audio_core/stream.h6
8 files changed, 221 insertions, 3 deletions
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt
index a0ae07752..d25a1a645 100644
--- a/src/audio_core/CMakeLists.txt
+++ b/src/audio_core/CMakeLists.txt
@@ -42,6 +42,7 @@ add_library(audio_core STATIC
voice_context.h
$<$<BOOL:${ENABLE_CUBEB}>:cubeb_sink.cpp cubeb_sink.h>
+ $<$<BOOL:${ENABLE_SDL2}>:sdl2_sink.cpp sdl2_sink.h>
)
create_target_directory_groups(audio_core)
@@ -71,3 +72,7 @@ if(ENABLE_CUBEB)
target_link_libraries(audio_core PRIVATE cubeb)
target_compile_definitions(audio_core PRIVATE -DHAVE_CUBEB=1)
endif()
+if(ENABLE_SDL2)
+ target_link_libraries(audio_core PRIVATE SDL2)
+ target_compile_definitions(audio_core PRIVATE HAVE_SDL2)
+endif()
diff --git a/src/audio_core/common.h b/src/audio_core/common.h
index fe546c55d..1ab537588 100644
--- a/src/audio_core/common.h
+++ b/src/audio_core/common.h
@@ -15,7 +15,7 @@ constexpr ResultCode ERR_INVALID_PARAMETERS{ErrorModule::Audio, 41};
constexpr ResultCode ERR_SPLITTER_SORT_FAILED{ErrorModule::Audio, 43};
} // namespace Audren
-constexpr u32_le CURRENT_PROCESS_REVISION = Common::MakeMagic('R', 'E', 'V', '8');
+constexpr u32_le CURRENT_PROCESS_REVISION = Common::MakeMagic('R', 'E', 'V', '9');
constexpr std::size_t MAX_MIX_BUFFERS = 24;
constexpr std::size_t MAX_BIQUAD_FILTERS = 2;
constexpr std::size_t MAX_CHANNEL_COUNT = 6;
diff --git a/src/audio_core/sdl2_sink.cpp b/src/audio_core/sdl2_sink.cpp
new file mode 100644
index 000000000..62d3716a6
--- /dev/null
+++ b/src/audio_core/sdl2_sink.cpp
@@ -0,0 +1,163 @@
+// Copyright 2018 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <algorithm>
+#include <atomic>
+#include <cstring>
+#include "audio_core/sdl2_sink.h"
+#include "audio_core/stream.h"
+#include "audio_core/time_stretch.h"
+#include "common/assert.h"
+#include "common/logging/log.h"
+//#include "common/settings.h"
+
+// Ignore -Wimplicit-fallthrough due to https://github.com/libsdl-org/SDL/issues/4307
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
+#endif
+#include <SDL.h>
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+
+namespace AudioCore {
+
+class SDLSinkStream final : public SinkStream {
+public:
+ SDLSinkStream(u32 sample_rate, u32 num_channels_, const std::string& output_device)
+ : num_channels{std::min(num_channels_, 6u)}, time_stretch{sample_rate, num_channels} {
+
+ SDL_AudioSpec spec;
+ spec.freq = sample_rate;
+ spec.channels = static_cast<u8>(num_channels);
+ spec.format = AUDIO_S16SYS;
+ spec.samples = 4096;
+ spec.callback = nullptr;
+
+ SDL_AudioSpec obtained;
+ if (output_device.empty()) {
+ dev = SDL_OpenAudioDevice(nullptr, 0, &spec, &obtained, 0);
+ } else {
+ dev = SDL_OpenAudioDevice(output_device.c_str(), 0, &spec, &obtained, 0);
+ }
+
+ if (dev == 0) {
+ LOG_CRITICAL(Audio_Sink, "Error opening sdl audio device: {}", SDL_GetError());
+ return;
+ }
+
+ SDL_PauseAudioDevice(dev, 0);
+ }
+
+ ~SDLSinkStream() override {
+ if (dev == 0) {
+ return;
+ }
+
+ SDL_CloseAudioDevice(dev);
+ }
+
+ void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override {
+ if (source_num_channels > num_channels) {
+ // Downsample 6 channels to 2
+ ASSERT_MSG(source_num_channels == 6, "Channel count must be 6");
+
+ std::vector<s16> buf;
+ buf.reserve(samples.size() * num_channels / source_num_channels);
+ for (std::size_t i = 0; i < samples.size(); i += source_num_channels) {
+ // Downmixing implementation taken from the ATSC standard
+ const s16 left{samples[i + 0]};
+ const s16 right{samples[i + 1]};
+ const s16 center{samples[i + 2]};
+ const s16 surround_left{samples[i + 4]};
+ const s16 surround_right{samples[i + 5]};
+ // Not used in the ATSC reference implementation
+ [[maybe_unused]] const s16 low_frequency_effects{samples[i + 3]};
+
+ constexpr s32 clev{707}; // center mixing level coefficient
+ constexpr s32 slev{707}; // surround mixing level coefficient
+
+ buf.push_back(static_cast<s16>(left + (clev * center / 1000) +
+ (slev * surround_left / 1000)));
+ buf.push_back(static_cast<s16>(right + (clev * center / 1000) +
+ (slev * surround_right / 1000)));
+ }
+ int ret = SDL_QueueAudio(dev, static_cast<const void*>(buf.data()),
+ static_cast<u32>(buf.size() * sizeof(s16)));
+ if (ret < 0)
+ LOG_WARNING(Audio_Sink, "Could not queue audio buffer: {}", SDL_GetError());
+ return;
+ }
+
+ int ret = SDL_QueueAudio(dev, static_cast<const void*>(samples.data()),
+ static_cast<u32>(samples.size() * sizeof(s16)));
+ if (ret < 0)
+ LOG_WARNING(Audio_Sink, "Could not queue audio buffer: {}", SDL_GetError());
+ }
+
+ std::size_t SamplesInQueue(u32 channel_count) const override {
+ if (dev == 0)
+ return 0;
+
+ return SDL_GetQueuedAudioSize(dev) / (channel_count * sizeof(s16));
+ }
+
+ void Flush() override {
+ should_flush = true;
+ }
+
+ u32 GetNumChannels() const {
+ return num_channels;
+ }
+
+private:
+ SDL_AudioDeviceID dev = 0;
+ u32 num_channels{};
+ std::atomic<bool> should_flush{};
+ TimeStretcher time_stretch;
+};
+
+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();
+ }
+}
+
+SDLSink::~SDLSink() = default;
+
+SinkStream& SDLSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, const std::string&) {
+ sink_streams.push_back(
+ std::make_unique<SDLSinkStream>(sample_rate, num_channels, output_device));
+ return *sink_streams.back();
+}
+
+std::vector<std::string> ListSDLSinkDevices() {
+ 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(0);
+ for (int i = 0; i < device_count; ++i) {
+ device_list.emplace_back(SDL_GetAudioDeviceName(i, 0));
+ }
+
+ return device_list;
+}
+
+} // namespace AudioCore
diff --git a/src/audio_core/sdl2_sink.h b/src/audio_core/sdl2_sink.h
new file mode 100644
index 000000000..8ec1526d8
--- /dev/null
+++ b/src/audio_core/sdl2_sink.h
@@ -0,0 +1,29 @@
+// Copyright 2018 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "audio_core/sink.h"
+
+namespace AudioCore {
+
+class SDLSink final : public Sink {
+public:
+ explicit SDLSink(std::string_view device_id);
+ ~SDLSink() override;
+
+ SinkStream& AcquireSinkStream(u32 sample_rate, u32 num_channels,
+ const std::string& name) override;
+
+private:
+ std::string output_device;
+ std::vector<SinkStreamPtr> sink_streams;
+};
+
+std::vector<std::string> ListSDLSinkDevices();
+
+} // namespace AudioCore
diff --git a/src/audio_core/sink_context.h b/src/audio_core/sink_context.h
index 66ee4e8a0..9e2b69785 100644
--- a/src/audio_core/sink_context.h
+++ b/src/audio_core/sink_context.h
@@ -4,6 +4,8 @@
#pragma once
+#include <array>
+#include <vector>
#include "audio_core/common.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
diff --git a/src/audio_core/sink_details.cpp b/src/audio_core/sink_details.cpp
index a848eb1c9..de10aecd2 100644
--- a/src/audio_core/sink_details.cpp
+++ b/src/audio_core/sink_details.cpp
@@ -11,6 +11,9 @@
#ifdef HAVE_CUBEB
#include "audio_core/cubeb_sink.h"
#endif
+#ifdef HAVE_SDL2
+#include "audio_core/sdl2_sink.h"
+#endif
#include "common/logging/log.h"
namespace AudioCore {
@@ -36,6 +39,13 @@ constexpr SinkDetails sink_details[] = {
},
&ListCubebSinkDevices},
#endif
+#ifdef HAVE_SDL2
+ SinkDetails{"sdl2",
+ [](std::string_view device_id) -> std::unique_ptr<Sink> {
+ return std::make_unique<SDLSink>(device_id);
+ },
+ &ListSDLSinkDevices},
+#endif
SinkDetails{"null",
[](std::string_view device_id) -> std::unique_ptr<Sink> {
return std::make_unique<NullSink>(device_id);
diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp
index ad6c587c2..5a30f55a7 100644
--- a/src/audio_core/stream.cpp
+++ b/src/audio_core/stream.cpp
@@ -107,9 +107,12 @@ void Stream::PlayNextBuffer(std::chrono::nanoseconds ns_late) {
active_buffer = queued_buffers.front();
queued_buffers.pop();
- VolumeAdjustSamples(active_buffer->GetSamples(), game_volume);
+ auto& samples = active_buffer->GetSamples();
- sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples());
+ VolumeAdjustSamples(samples, game_volume);
+
+ sink_stream.EnqueueSamples(GetNumChannels(), samples);
+ played_samples += samples.size();
const auto buffer_release_ns = GetBufferReleaseNS(*active_buffer);
diff --git a/src/audio_core/stream.h b/src/audio_core/stream.h
index 559844b9b..dbd97ec9c 100644
--- a/src/audio_core/stream.h
+++ b/src/audio_core/stream.h
@@ -89,6 +89,11 @@ public:
return sample_rate;
}
+ /// Gets the number of samples played so far
+ [[nodiscard]] u64 GetPlayedSampleCount() const {
+ return played_samples;
+ }
+
/// Gets the number of channels
[[nodiscard]] u32 GetNumChannels() const;
@@ -106,6 +111,7 @@ private:
[[nodiscard]] std::chrono::nanoseconds GetBufferReleaseNS(const Buffer& buffer) const;
u32 sample_rate; ///< Sample rate of the stream
+ u64 played_samples{}; ///< The current played sample count
Format format; ///< Format of the stream
float game_volume = 1.0f; ///< The volume the game currently has set
ReleaseCallback release_callback; ///< Buffer release callback for the stream