cubeb_sink.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <atomic>
  6. #include <cstring>
  7. #include "audio_core/cubeb_sink.h"
  8. #include "audio_core/stream.h"
  9. #include "audio_core/time_stretch.h"
  10. #include "common/logging/log.h"
  11. #include "common/ring_buffer.h"
  12. #include "core/settings.h"
  13. #ifdef _MSC_VER
  14. #include <objbase.h>
  15. #endif
  16. namespace AudioCore {
  17. class CubebSinkStream final : public SinkStream {
  18. public:
  19. CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device,
  20. const std::string& name)
  21. : ctx{ctx}, num_channels{std::min(num_channels_, 2u)}, time_stretch{sample_rate,
  22. num_channels} {
  23. cubeb_stream_params params{};
  24. params.rate = sample_rate;
  25. params.channels = num_channels;
  26. params.format = CUBEB_SAMPLE_S16NE;
  27. params.layout = num_channels == 1 ? CUBEB_LAYOUT_MONO : CUBEB_LAYOUT_STEREO;
  28. u32 minimum_latency{};
  29. if (cubeb_get_min_latency(ctx, &params, &minimum_latency) != CUBEB_OK) {
  30. LOG_CRITICAL(Audio_Sink, "Error getting minimum latency");
  31. }
  32. if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device,
  33. &params, std::max(512u, minimum_latency),
  34. &CubebSinkStream::DataCallback, &CubebSinkStream::StateCallback,
  35. this) != CUBEB_OK) {
  36. LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream");
  37. return;
  38. }
  39. if (cubeb_stream_start(stream_backend) != CUBEB_OK) {
  40. LOG_CRITICAL(Audio_Sink, "Error starting cubeb stream");
  41. return;
  42. }
  43. }
  44. ~CubebSinkStream() override {
  45. if (!ctx) {
  46. return;
  47. }
  48. if (cubeb_stream_stop(stream_backend) != CUBEB_OK) {
  49. LOG_CRITICAL(Audio_Sink, "Error stopping cubeb stream");
  50. }
  51. cubeb_stream_destroy(stream_backend);
  52. }
  53. void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override {
  54. if (source_num_channels > num_channels) {
  55. // Downsample 6 channels to 2
  56. std::vector<s16> buf;
  57. buf.reserve(samples.size() * num_channels / source_num_channels);
  58. for (std::size_t i = 0; i < samples.size(); i += source_num_channels) {
  59. for (std::size_t ch = 0; ch < num_channels; ch++) {
  60. buf.push_back(samples[i + ch]);
  61. }
  62. }
  63. queue.Push(buf);
  64. return;
  65. }
  66. queue.Push(samples);
  67. }
  68. std::size_t SamplesInQueue(u32 channel_count) const override {
  69. if (!ctx)
  70. return 0;
  71. return queue.Size() / channel_count;
  72. }
  73. void Flush() override {
  74. should_flush = true;
  75. }
  76. u32 GetNumChannels() const {
  77. return num_channels;
  78. }
  79. private:
  80. std::vector<std::string> device_list;
  81. cubeb* ctx{};
  82. cubeb_stream* stream_backend{};
  83. u32 num_channels{};
  84. Common::RingBuffer<s16, 0x10000> queue;
  85. std::array<s16, 2> last_frame{};
  86. std::atomic<bool> should_flush{};
  87. TimeStretcher time_stretch;
  88. static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
  89. void* output_buffer, long num_frames);
  90. static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state);
  91. };
  92. CubebSink::CubebSink(std::string_view target_device_name) {
  93. // Cubeb requires COM to be initialized on the thread calling cubeb_init on Windows
  94. #ifdef _MSC_VER
  95. com_init_result = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
  96. #endif
  97. if (cubeb_init(&ctx, "yuzu", nullptr) != CUBEB_OK) {
  98. LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
  99. return;
  100. }
  101. if (target_device_name != auto_device_name && !target_device_name.empty()) {
  102. cubeb_device_collection collection;
  103. if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) {
  104. LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported");
  105. } else {
  106. const auto collection_end{collection.device + collection.count};
  107. const auto device{
  108. std::find_if(collection.device, collection_end, [&](const cubeb_device_info& info) {
  109. return info.friendly_name != nullptr &&
  110. target_device_name == info.friendly_name;
  111. })};
  112. if (device != collection_end) {
  113. output_device = device->devid;
  114. }
  115. cubeb_device_collection_destroy(ctx, &collection);
  116. }
  117. }
  118. }
  119. CubebSink::~CubebSink() {
  120. if (!ctx) {
  121. return;
  122. }
  123. for (auto& sink_stream : sink_streams) {
  124. sink_stream.reset();
  125. }
  126. cubeb_destroy(ctx);
  127. #ifdef _MSC_VER
  128. if (SUCCEEDED(com_init_result)) {
  129. CoUninitialize();
  130. }
  131. #endif
  132. }
  133. SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels,
  134. const std::string& name) {
  135. sink_streams.push_back(
  136. std::make_unique<CubebSinkStream>(ctx, sample_rate, num_channels, output_device, name));
  137. return *sink_streams.back();
  138. }
  139. long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
  140. void* output_buffer, long num_frames) {
  141. CubebSinkStream* impl = static_cast<CubebSinkStream*>(user_data);
  142. u8* buffer = reinterpret_cast<u8*>(output_buffer);
  143. if (!impl) {
  144. return {};
  145. }
  146. const std::size_t num_channels = impl->GetNumChannels();
  147. const std::size_t samples_to_write = num_channels * num_frames;
  148. std::size_t samples_written;
  149. if (Settings::values.enable_audio_stretching) {
  150. const std::vector<s16> in{impl->queue.Pop()};
  151. const std::size_t num_in{in.size() / num_channels};
  152. s16* const out{reinterpret_cast<s16*>(buffer)};
  153. const std::size_t out_frames =
  154. impl->time_stretch.Process(in.data(), num_in, out, num_frames);
  155. samples_written = out_frames * num_channels;
  156. if (impl->should_flush) {
  157. impl->time_stretch.Flush();
  158. impl->should_flush = false;
  159. }
  160. } else {
  161. samples_written = impl->queue.Pop(buffer, samples_to_write);
  162. }
  163. if (samples_written >= num_channels) {
  164. std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16),
  165. num_channels * sizeof(s16));
  166. }
  167. // Fill the rest of the frames with last_frame
  168. for (std::size_t i = samples_written; i < samples_to_write; i += num_channels) {
  169. std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16));
  170. }
  171. return num_frames;
  172. }
  173. void CubebSinkStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {}
  174. std::vector<std::string> ListCubebSinkDevices() {
  175. std::vector<std::string> device_list;
  176. cubeb* ctx;
  177. if (cubeb_init(&ctx, "yuzu Device Enumerator", nullptr) != CUBEB_OK) {
  178. LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
  179. return {};
  180. }
  181. cubeb_device_collection collection;
  182. if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) {
  183. LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported");
  184. } else {
  185. for (std::size_t i = 0; i < collection.count; i++) {
  186. const cubeb_device_info& device = collection.device[i];
  187. if (device.friendly_name) {
  188. device_list.emplace_back(device.friendly_name);
  189. }
  190. }
  191. cubeb_device_collection_destroy(ctx, &collection);
  192. }
  193. cubeb_destroy(ctx);
  194. return device_list;
  195. }
  196. } // namespace AudioCore