sdl2_sink.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <span>
  4. #include <vector>
  5. #include "audio_core/common/common.h"
  6. #include "audio_core/sink/sdl2_sink.h"
  7. #include "audio_core/sink/sink_stream.h"
  8. #include "common/logging/log.h"
  9. #include "core/core.h"
  10. // Ignore -Wimplicit-fallthrough due to https://github.com/libsdl-org/SDL/issues/4307
  11. #ifdef __clang__
  12. #pragma clang diagnostic push
  13. #pragma clang diagnostic ignored "-Wimplicit-fallthrough"
  14. #endif
  15. #include <SDL.h>
  16. #ifdef __clang__
  17. #pragma clang diagnostic pop
  18. #endif
  19. namespace AudioCore::Sink {
  20. /**
  21. * SDL sink stream, responsible for sinking samples to hardware.
  22. */
  23. class SDLSinkStream final : public SinkStream {
  24. public:
  25. /**
  26. * Create a new sink stream.
  27. *
  28. * @param device_channels_ - Number of channels supported by the hardware.
  29. * @param system_channels_ - Number of channels the audio systems expect.
  30. * @param output_device - Name of the output device to use for this stream.
  31. * @param input_device - Name of the input device to use for this stream.
  32. * @param type_ - Type of this stream.
  33. * @param system_ - Core system.
  34. * @param event - Event used only for audio renderer, signalled on buffer consume.
  35. */
  36. SDLSinkStream(u32 device_channels_, u32 system_channels_, const std::string& output_device,
  37. const std::string& input_device, StreamType type_, Core::System& system_)
  38. : SinkStream{system_, type_} {
  39. system_channels = system_channels_;
  40. device_channels = device_channels_;
  41. SDL_AudioSpec spec;
  42. spec.freq = TargetSampleRate;
  43. spec.channels = static_cast<u8>(device_channels);
  44. spec.format = AUDIO_S16SYS;
  45. spec.samples = TargetSampleCount * 2;
  46. spec.callback = &SDLSinkStream::DataCallback;
  47. spec.userdata = this;
  48. std::string device_name{output_device};
  49. bool capture{false};
  50. if (type == StreamType::In) {
  51. device_name = input_device;
  52. capture = true;
  53. }
  54. SDL_AudioSpec obtained;
  55. if (device_name.empty()) {
  56. device = SDL_OpenAudioDevice(nullptr, capture, &spec, &obtained, false);
  57. } else {
  58. device = SDL_OpenAudioDevice(device_name.c_str(), capture, &spec, &obtained, false);
  59. }
  60. if (device == 0) {
  61. LOG_CRITICAL(Audio_Sink, "Error opening SDL audio device: {}", SDL_GetError());
  62. return;
  63. }
  64. LOG_INFO(Service_Audio,
  65. "Opening SDL stream {} with: rate {} channels {} (system channels {}) "
  66. " samples {}",
  67. device, obtained.freq, obtained.channels, system_channels, obtained.samples);
  68. }
  69. /**
  70. * Destroy the sink stream.
  71. */
  72. ~SDLSinkStream() override {
  73. LOG_DEBUG(Service_Audio, "Destructing SDL stream {}", name);
  74. Finalize();
  75. }
  76. /**
  77. * Finalize the sink stream.
  78. */
  79. void Finalize() override {
  80. if (device == 0) {
  81. return;
  82. }
  83. Stop();
  84. SDL_CloseAudioDevice(device);
  85. }
  86. /**
  87. * Start the sink stream.
  88. *
  89. * @param resume - Set to true if this is resuming the stream a previously-active stream.
  90. * Default false.
  91. */
  92. void Start(bool resume = false) override {
  93. if (device == 0 || !paused) {
  94. return;
  95. }
  96. paused = false;
  97. SDL_PauseAudioDevice(device, 0);
  98. }
  99. /**
  100. * Stop the sink stream.
  101. */
  102. void Stop() override {
  103. if (device == 0 || paused) {
  104. return;
  105. }
  106. paused = true;
  107. SDL_PauseAudioDevice(device, 1);
  108. }
  109. private:
  110. /**
  111. * Main callback from SDL. Either expects samples from us (audio render/audio out), or will
  112. * provide samples to be copied (audio in).
  113. *
  114. * @param userdata - Custom data pointer passed along, points to a SDLSinkStream.
  115. * @param stream - Buffer of samples to be filled or read.
  116. * @param len - Length of the stream in bytes.
  117. */
  118. static void DataCallback(void* userdata, Uint8* stream, int len) {
  119. auto* impl = static_cast<SDLSinkStream*>(userdata);
  120. if (!impl) {
  121. return;
  122. }
  123. const std::size_t num_channels = impl->GetDeviceChannels();
  124. const std::size_t frame_size = num_channels;
  125. const std::size_t num_frames{len / num_channels / sizeof(s16)};
  126. if (impl->type == StreamType::In) {
  127. std::span<const s16> input_buffer{reinterpret_cast<const s16*>(stream),
  128. num_frames * frame_size};
  129. impl->ProcessAudioIn(input_buffer, num_frames);
  130. } else {
  131. std::span<s16> output_buffer{reinterpret_cast<s16*>(stream), num_frames * frame_size};
  132. impl->ProcessAudioOutAndRender(output_buffer, num_frames);
  133. }
  134. }
  135. /// SDL device id of the opened input/output device
  136. SDL_AudioDeviceID device{};
  137. };
  138. SDLSink::SDLSink(std::string_view target_device_name) {
  139. if (!SDL_WasInit(SDL_INIT_AUDIO)) {
  140. if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
  141. LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem audio failed: {}", SDL_GetError());
  142. return;
  143. }
  144. }
  145. if (target_device_name != auto_device_name && !target_device_name.empty()) {
  146. output_device = target_device_name;
  147. } else {
  148. output_device.clear();
  149. }
  150. device_channels = 2;
  151. }
  152. SDLSink::~SDLSink() = default;
  153. SinkStream* SDLSink::AcquireSinkStream(Core::System& system, u32 system_channels,
  154. const std::string&, StreamType type) {
  155. SinkStreamPtr& stream = sink_streams.emplace_back(std::make_unique<SDLSinkStream>(
  156. device_channels, system_channels, output_device, input_device, type, system));
  157. return stream.get();
  158. }
  159. void SDLSink::CloseStream(SinkStream* stream) {
  160. for (size_t i = 0; i < sink_streams.size(); i++) {
  161. if (sink_streams[i].get() == stream) {
  162. sink_streams[i].reset();
  163. sink_streams.erase(sink_streams.begin() + i);
  164. break;
  165. }
  166. }
  167. }
  168. void SDLSink::CloseStreams() {
  169. sink_streams.clear();
  170. }
  171. f32 SDLSink::GetDeviceVolume() const {
  172. if (sink_streams.empty()) {
  173. return 1.0f;
  174. }
  175. return sink_streams[0]->GetDeviceVolume();
  176. }
  177. void SDLSink::SetDeviceVolume(f32 volume) {
  178. for (auto& stream : sink_streams) {
  179. stream->SetDeviceVolume(volume);
  180. }
  181. }
  182. void SDLSink::SetSystemVolume(f32 volume) {
  183. for (auto& stream : sink_streams) {
  184. stream->SetSystemVolume(volume);
  185. }
  186. }
  187. std::vector<std::string> ListSDLSinkDevices(bool capture) {
  188. std::vector<std::string> device_list;
  189. if (!SDL_WasInit(SDL_INIT_AUDIO)) {
  190. if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
  191. LOG_CRITICAL(Audio_Sink, "SDL_InitSubSystem audio failed: {}", SDL_GetError());
  192. return {};
  193. }
  194. }
  195. const int device_count = SDL_GetNumAudioDevices(capture);
  196. for (int i = 0; i < device_count; ++i) {
  197. if (const char* name = SDL_GetAudioDeviceName(i, capture)) {
  198. device_list.emplace_back(name);
  199. }
  200. }
  201. return device_list;
  202. }
  203. u32 GetSDLLatency() {
  204. return TargetSampleCount * 2;
  205. }
  206. } // namespace AudioCore::Sink