diff options
Diffstat (limited to 'src/audio_core/sdl2_sink.cpp')
| -rw-r--r-- | src/audio_core/sdl2_sink.cpp | 167 |
1 files changed, 167 insertions, 0 deletions
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 | ||