cubeb_sink.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <mutex>
  7. #include "audio_core/cubeb_sink.h"
  8. #include "audio_core/stream.h"
  9. #include "common/logging/log.h"
  10. namespace AudioCore {
  11. class SinkStreamImpl final : public SinkStream {
  12. public:
  13. SinkStreamImpl(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device,
  14. const std::string& name)
  15. : ctx{ctx}, num_channels{num_channels_} {
  16. if (num_channels == 6) {
  17. // 6-channel audio does not seem to work with cubeb + SDL, so we downsample this to 2
  18. // channel for now
  19. is_6_channel = true;
  20. num_channels = 2;
  21. }
  22. cubeb_stream_params params{};
  23. params.rate = sample_rate;
  24. params.channels = num_channels;
  25. params.format = CUBEB_SAMPLE_S16NE;
  26. params.layout = num_channels == 1 ? CUBEB_LAYOUT_MONO : CUBEB_LAYOUT_STEREO;
  27. u32 minimum_latency{};
  28. if (cubeb_get_min_latency(ctx, &params, &minimum_latency) != CUBEB_OK) {
  29. LOG_CRITICAL(Audio_Sink, "Error getting minimum latency");
  30. }
  31. if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device,
  32. &params, std::max(512u, minimum_latency),
  33. &SinkStreamImpl::DataCallback, &SinkStreamImpl::StateCallback,
  34. this) != CUBEB_OK) {
  35. LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream");
  36. return;
  37. }
  38. if (cubeb_stream_start(stream_backend) != CUBEB_OK) {
  39. LOG_CRITICAL(Audio_Sink, "Error starting cubeb stream");
  40. return;
  41. }
  42. }
  43. ~SinkStreamImpl() {
  44. if (!ctx) {
  45. return;
  46. }
  47. if (cubeb_stream_stop(stream_backend) != CUBEB_OK) {
  48. LOG_CRITICAL(Audio_Sink, "Error stopping cubeb stream");
  49. }
  50. cubeb_stream_destroy(stream_backend);
  51. }
  52. void EnqueueSamples(u32 num_channels, const std::vector<s16>& samples) override {
  53. if (!ctx) {
  54. return;
  55. }
  56. std::lock_guard lock{queue_mutex};
  57. queue.reserve(queue.size() + samples.size() * GetNumChannels());
  58. if (is_6_channel) {
  59. // Downsample 6 channels to 2
  60. const size_t sample_count_copy_size = samples.size() * 2;
  61. queue.reserve(sample_count_copy_size);
  62. for (size_t i = 0; i < samples.size(); i += num_channels) {
  63. queue.push_back(samples[i]);
  64. queue.push_back(samples[i + 1]);
  65. }
  66. } else {
  67. // Copy as-is
  68. std::copy(samples.begin(), samples.end(), std::back_inserter(queue));
  69. }
  70. }
  71. u32 GetNumChannels() const {
  72. return num_channels;
  73. }
  74. private:
  75. std::vector<std::string> device_list;
  76. cubeb* ctx{};
  77. cubeb_stream* stream_backend{};
  78. u32 num_channels{};
  79. bool is_6_channel{};
  80. std::mutex queue_mutex;
  81. std::vector<s16> queue;
  82. static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
  83. void* output_buffer, long num_frames);
  84. static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state);
  85. };
  86. CubebSink::CubebSink(std::string target_device_name) {
  87. if (cubeb_init(&ctx, "yuzu", nullptr) != CUBEB_OK) {
  88. LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
  89. return;
  90. }
  91. if (target_device_name != auto_device_name && !target_device_name.empty()) {
  92. cubeb_device_collection collection;
  93. if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) {
  94. LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported");
  95. } else {
  96. const auto collection_end{collection.device + collection.count};
  97. const auto device{std::find_if(collection.device, collection_end,
  98. [&](const cubeb_device_info& device) {
  99. return target_device_name == device.friendly_name;
  100. })};
  101. if (device != collection_end) {
  102. output_device = device->devid;
  103. }
  104. cubeb_device_collection_destroy(ctx, &collection);
  105. }
  106. }
  107. }
  108. CubebSink::~CubebSink() {
  109. if (!ctx) {
  110. return;
  111. }
  112. for (auto& sink_stream : sink_streams) {
  113. sink_stream.reset();
  114. }
  115. cubeb_destroy(ctx);
  116. }
  117. SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels,
  118. const std::string& name) {
  119. sink_streams.push_back(
  120. std::make_unique<SinkStreamImpl>(ctx, sample_rate, num_channels, output_device, name));
  121. return *sink_streams.back();
  122. }
  123. long SinkStreamImpl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
  124. void* output_buffer, long num_frames) {
  125. SinkStreamImpl* impl = static_cast<SinkStreamImpl*>(user_data);
  126. u8* buffer = reinterpret_cast<u8*>(output_buffer);
  127. if (!impl) {
  128. return {};
  129. }
  130. std::lock_guard lock{impl->queue_mutex};
  131. const size_t frames_to_write{
  132. std::min(impl->queue.size() / impl->GetNumChannels(), static_cast<size_t>(num_frames))};
  133. memcpy(buffer, impl->queue.data(), frames_to_write * sizeof(s16) * impl->GetNumChannels());
  134. impl->queue.erase(impl->queue.begin(),
  135. impl->queue.begin() + frames_to_write * impl->GetNumChannels());
  136. if (frames_to_write < num_frames) {
  137. // Fill the rest of the frames with silence
  138. memset(buffer + frames_to_write * sizeof(s16) * impl->GetNumChannels(), 0,
  139. (num_frames - frames_to_write) * sizeof(s16) * impl->GetNumChannels());
  140. }
  141. return num_frames;
  142. }
  143. void SinkStreamImpl::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {}
  144. std::vector<std::string> ListCubebSinkDevices() {
  145. std::vector<std::string> device_list;
  146. cubeb* ctx;
  147. if (cubeb_init(&ctx, "Citra Device Enumerator", nullptr) != CUBEB_OK) {
  148. LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
  149. return {};
  150. }
  151. cubeb_device_collection collection;
  152. if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) {
  153. LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported");
  154. } else {
  155. for (size_t i = 0; i < collection.count; i++) {
  156. const cubeb_device_info& device = collection.device[i];
  157. if (device.friendly_name) {
  158. device_list.emplace_back(device.friendly_name);
  159. }
  160. }
  161. cubeb_device_collection_destroy(ctx, &collection);
  162. }
  163. cubeb_destroy(ctx);
  164. return device_list;
  165. }
  166. } // namespace AudioCore