cubeb_sink.cpp 7.5 KB

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