diff options
| author | 2021-06-05 15:35:57 +0200 | |
|---|---|---|
| committer | 2021-06-06 11:28:38 +0200 | |
| commit | c7c99905f4ba5f3d7b824ec2bc6f46d755d46d80 (patch) | |
| tree | d1e170f7a1a414f03aed418579ec205ca72c5d5a | |
| parent | Merge pull request #6402 from Kelebek1/UI (diff) | |
| download | yuzu-c7c99905f4ba5f3d7b824ec2bc6f46d755d46d80.tar.gz yuzu-c7c99905f4ba5f3d7b824ec2bc6f46d755d46d80.tar.xz yuzu-c7c99905f4ba5f3d7b824ec2bc6f46d755d46d80.zip | |
Add SDL2 audio backend
| -rw-r--r-- | externals/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/audio_core/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | src/audio_core/sdl2_sink.cpp | 167 | ||||
| -rw-r--r-- | src/audio_core/sdl2_sink.h | 29 | ||||
| -rw-r--r-- | src/audio_core/sink_details.cpp | 10 |
5 files changed, 213 insertions, 2 deletions
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index aae0baa0b..ec3c0432b 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt | |||
| @@ -48,10 +48,10 @@ target_include_directories(unicorn-headers INTERFACE ./unicorn/include) | |||
| 48 | # SDL2 | 48 | # SDL2 |
| 49 | if (NOT SDL2_FOUND AND ENABLE_SDL2) | 49 | if (NOT SDL2_FOUND AND ENABLE_SDL2) |
| 50 | if (NOT WIN32) | 50 | if (NOT WIN32) |
| 51 | # Yuzu itself needs: Events Joystick Haptic Sensor Timers | 51 | # Yuzu itself needs: Events Joystick Haptic Sensor Timers Audio |
| 52 | # Yuzu-cmd also needs: Video (depends on Loadso/Dlopen) | 52 | # Yuzu-cmd also needs: Video (depends on Loadso/Dlopen) |
| 53 | set(SDL_UNUSED_SUBSYSTEMS | 53 | set(SDL_UNUSED_SUBSYSTEMS |
| 54 | Atomic Audio Render Power Threads | 54 | Atomic Render Power Threads |
| 55 | File CPUinfo Filesystem Locale) | 55 | File CPUinfo Filesystem Locale) |
| 56 | foreach(_SUB ${SDL_UNUSED_SUBSYSTEMS}) | 56 | foreach(_SUB ${SDL_UNUSED_SUBSYSTEMS}) |
| 57 | string(TOUPPER ${_SUB} _OPT) | 57 | string(TOUPPER ${_SUB} _OPT) |
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 | |||
| 42 | voice_context.h | 42 | voice_context.h |
| 43 | 43 | ||
| 44 | $<$<BOOL:${ENABLE_CUBEB}>:cubeb_sink.cpp cubeb_sink.h> | 44 | $<$<BOOL:${ENABLE_CUBEB}>:cubeb_sink.cpp cubeb_sink.h> |
| 45 | $<$<BOOL:${ENABLE_SDL2}>:sdl2_sink.cpp sdl2_sink.h> | ||
| 45 | ) | 46 | ) |
| 46 | 47 | ||
| 47 | create_target_directory_groups(audio_core) | 48 | create_target_directory_groups(audio_core) |
| @@ -71,3 +72,7 @@ if(ENABLE_CUBEB) | |||
| 71 | target_link_libraries(audio_core PRIVATE cubeb) | 72 | target_link_libraries(audio_core PRIVATE cubeb) |
| 72 | target_compile_definitions(audio_core PRIVATE -DHAVE_CUBEB=1) | 73 | target_compile_definitions(audio_core PRIVATE -DHAVE_CUBEB=1) |
| 73 | endif() | 74 | endif() |
| 75 | if(ENABLE_SDL2) | ||
| 76 | target_link_libraries(audio_core PRIVATE SDL2) | ||
| 77 | target_compile_definitions(audio_core PRIVATE HAVE_SDL2) | ||
| 78 | endif() | ||
diff --git a/src/audio_core/sdl2_sink.cpp b/src/audio_core/sdl2_sink.cpp new file mode 100644 index 000000000..75c6202ef --- /dev/null +++ b/src/audio_core/sdl2_sink.cpp | |||
| @@ -0,0 +1,167 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <atomic> | ||
| 7 | #include <cstring> | ||
| 8 | #include "audio_core/sdl2_sink.h" | ||
| 9 | #include "audio_core/stream.h" | ||
| 10 | #include "audio_core/time_stretch.h" | ||
| 11 | #include "common/assert.h" | ||
| 12 | #include "common/logging/log.h" | ||
| 13 | //#include "common/settings.h" | ||
| 14 | |||
| 15 | // Ignore -Wimplicit-fallthrough due to https://github.com/libsdl-org/SDL/issues/4307 | ||
| 16 | #ifdef __clang__ | ||
| 17 | #pragma clang diagnostic push | ||
| 18 | #pragma clang diagnostic ignored "-Wimplicit-fallthrough" | ||
| 19 | #endif | ||
| 20 | #include <SDL.h> | ||
| 21 | #ifdef __clang__ | ||
| 22 | #pragma clang diagnostic pop | ||
| 23 | #endif | ||
| 24 | |||
| 25 | namespace AudioCore { | ||
| 26 | |||
| 27 | class SDLSinkStream final : public SinkStream { | ||
| 28 | public: | ||
| 29 | SDLSinkStream(u32 sample_rate, u32 num_channels_, const std::string& output_device) | ||
| 30 | : num_channels{std::min(num_channels_, 6u)}, time_stretch{sample_rate, num_channels} { | ||
| 31 | |||
| 32 | SDL_AudioSpec spec; | ||
| 33 | spec.freq = sample_rate; | ||
| 34 | spec.channels = static_cast<u8>(num_channels); | ||
| 35 | spec.format = AUDIO_S16SYS; | ||
| 36 | spec.samples = 4096; | ||
| 37 | spec.callback = nullptr; | ||
| 38 | |||
| 39 | SDL_AudioSpec obtained; | ||
| 40 | if (output_device.empty()) | ||
| 41 | dev = SDL_OpenAudioDevice(nullptr, 0, &spec, &obtained, 0); | ||
| 42 | else | ||
| 43 | dev = SDL_OpenAudioDevice(output_device.c_str(), 0, &spec, &obtained, 0); | ||
| 44 | |||
| 45 | if (dev == 0) { | ||
| 46 | LOG_CRITICAL(Audio_Sink, "Error opening sdl audio device: {}", SDL_GetError()); | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | SDL_PauseAudioDevice(dev, 0); | ||
| 51 | } | ||
| 52 | |||
| 53 | ~SDLSinkStream() override { | ||
| 54 | if (dev == 0) { | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | SDL_PauseAudioDevice(dev, 1); | ||
| 59 | SDL_CloseAudioDevice(dev); | ||
| 60 | } | ||
| 61 | |||
| 62 | void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override { | ||
| 63 | if (source_num_channels > num_channels) { | ||
| 64 | // Downsample 6 channels to 2 | ||
| 65 | ASSERT_MSG(source_num_channels == 6, "Channel count must be 6"); | ||
| 66 | |||
| 67 | std::vector<s16> buf; | ||
| 68 | buf.reserve(samples.size() * num_channels / source_num_channels); | ||
| 69 | for (std::size_t i = 0; i < samples.size(); i += source_num_channels) { | ||
| 70 | // Downmixing implementation taken from the ATSC standard | ||
| 71 | const s16 left{samples[i + 0]}; | ||
| 72 | const s16 right{samples[i + 1]}; | ||
| 73 | const s16 center{samples[i + 2]}; | ||
| 74 | const s16 surround_left{samples[i + 4]}; | ||
| 75 | const s16 surround_right{samples[i + 5]}; | ||
| 76 | // Not used in the ATSC reference implementation | ||
| 77 | [[maybe_unused]] const s16 low_frequency_effects{samples[i + 3]}; | ||
| 78 | |||
| 79 | constexpr s32 clev{707}; // center mixing level coefficient | ||
| 80 | constexpr s32 slev{707}; // surround mixing level coefficient | ||
| 81 | |||
| 82 | buf.push_back(static_cast<s16>(left + (clev * center / 1000) + | ||
| 83 | (slev * surround_left / 1000))); | ||
| 84 | buf.push_back(static_cast<s16>(right + (clev * center / 1000) + | ||
| 85 | (slev * surround_right / 1000))); | ||
| 86 | } | ||
| 87 | int ret = SDL_QueueAudio(dev, static_cast<const void*>(buf.data()), | ||
| 88 | static_cast<u32>(buf.size() * sizeof(s16))); | ||
| 89 | if (ret < 0) | ||
| 90 | LOG_WARNING(Audio_Sink, "Could not queue audio buffer: {}", SDL_GetError()); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | int ret = SDL_QueueAudio(dev, static_cast<const void*>(samples.data()), | ||
| 95 | static_cast<u32>(samples.size() * sizeof(s16))); | ||
| 96 | if (ret < 0) | ||
| 97 | LOG_WARNING(Audio_Sink, "Could not queue audio buffer: {}", SDL_GetError()); | ||
| 98 | } | ||
| 99 | |||
| 100 | std::size_t SamplesInQueue(u32 channel_count) const override { | ||
| 101 | if (dev == 0) | ||
| 102 | return 0; | ||
| 103 | |||
| 104 | return SDL_GetQueuedAudioSize(dev) / (channel_count * sizeof(s16)); | ||
| 105 | } | ||
| 106 | |||
| 107 | void Flush() override { | ||
| 108 | should_flush = true; | ||
| 109 | } | ||
| 110 | |||
| 111 | u32 GetNumChannels() const { | ||
| 112 | return num_channels; | ||
| 113 | } | ||
| 114 | |||
| 115 | private: | ||
| 116 | SDL_AudioDeviceID dev = 0; | ||
| 117 | u32 num_channels{}; | ||
| 118 | std::atomic<bool> should_flush{}; | ||
| 119 | TimeStretcher time_stretch; | ||
| 120 | }; | ||
| 121 | |||
| 122 | SDLSink::SDLSink(std::string_view target_device_name) { | ||
| 123 | if (!SDL_WasInit(SDL_INIT_AUDIO)) { | ||
| 124 | if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { | ||
| 125 | LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem audio failed: {}", SDL_GetError()); | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | if (target_device_name != auto_device_name && !target_device_name.empty()) { | ||
| 131 | output_device = target_device_name; | ||
| 132 | } else { | ||
| 133 | output_device.clear(); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | SDLSink::~SDLSink() { | ||
| 138 | for (auto& sink_stream : sink_streams) { | ||
| 139 | sink_stream.reset(); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | SinkStream& SDLSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, const std::string&) { | ||
| 144 | sink_streams.push_back( | ||
| 145 | std::make_unique<SDLSinkStream>(sample_rate, num_channels, output_device)); | ||
| 146 | return *sink_streams.back(); | ||
| 147 | } | ||
| 148 | |||
| 149 | std::vector<std::string> ListSDLSinkDevices() { | ||
| 150 | std::vector<std::string> device_list; | ||
| 151 | |||
| 152 | if (!SDL_WasInit(SDL_INIT_AUDIO)) { | ||
| 153 | if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { | ||
| 154 | LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem audio failed: {}", SDL_GetError()); | ||
| 155 | return std::vector<std::string>(); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | int device_count = SDL_GetNumAudioDevices(0); | ||
| 160 | for (int i = 0; i < device_count; ++i) { | ||
| 161 | device_list.emplace_back(SDL_GetAudioDeviceName(i, 0)); | ||
| 162 | } | ||
| 163 | |||
| 164 | return device_list; | ||
| 165 | } | ||
| 166 | |||
| 167 | } // 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 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "audio_core/sink.h" | ||
| 11 | |||
| 12 | namespace AudioCore { | ||
| 13 | |||
| 14 | class SDLSink final : public Sink { | ||
| 15 | public: | ||
| 16 | explicit SDLSink(std::string_view device_id); | ||
| 17 | ~SDLSink() override; | ||
| 18 | |||
| 19 | SinkStream& AcquireSinkStream(u32 sample_rate, u32 num_channels, | ||
| 20 | const std::string& name) override; | ||
| 21 | |||
| 22 | private: | ||
| 23 | std::string output_device; | ||
| 24 | std::vector<SinkStreamPtr> sink_streams; | ||
| 25 | }; | ||
| 26 | |||
| 27 | std::vector<std::string> ListSDLSinkDevices(); | ||
| 28 | |||
| 29 | } // namespace AudioCore | ||
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 @@ | |||
| 11 | #ifdef HAVE_CUBEB | 11 | #ifdef HAVE_CUBEB |
| 12 | #include "audio_core/cubeb_sink.h" | 12 | #include "audio_core/cubeb_sink.h" |
| 13 | #endif | 13 | #endif |
| 14 | #ifdef HAVE_SDL2 | ||
| 15 | #include "audio_core/sdl2_sink.h" | ||
| 16 | #endif | ||
| 14 | #include "common/logging/log.h" | 17 | #include "common/logging/log.h" |
| 15 | 18 | ||
| 16 | namespace AudioCore { | 19 | namespace AudioCore { |
| @@ -36,6 +39,13 @@ constexpr SinkDetails sink_details[] = { | |||
| 36 | }, | 39 | }, |
| 37 | &ListCubebSinkDevices}, | 40 | &ListCubebSinkDevices}, |
| 38 | #endif | 41 | #endif |
| 42 | #ifdef HAVE_SDL2 | ||
| 43 | SinkDetails{"sdl2", | ||
| 44 | [](std::string_view device_id) -> std::unique_ptr<Sink> { | ||
| 45 | return std::make_unique<SDLSink>(device_id); | ||
| 46 | }, | ||
| 47 | &ListSDLSinkDevices}, | ||
| 48 | #endif | ||
| 39 | SinkDetails{"null", | 49 | SinkDetails{"null", |
| 40 | [](std::string_view device_id) -> std::unique_ptr<Sink> { | 50 | [](std::string_view device_id) -> std::unique_ptr<Sink> { |
| 41 | return std::make_unique<NullSink>(device_id); | 51 | return std::make_unique<NullSink>(device_id); |