cubeb_sink.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_view 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{
  101. std::find_if(collection.device, collection_end, [&](const cubeb_device_info& info) {
  102. return info.friendly_name != nullptr &&
  103. target_device_name == info.friendly_name;
  104. })};
  105. if (device != collection_end) {
  106. output_device = device->devid;
  107. }
  108. cubeb_device_collection_destroy(ctx, &collection);
  109. }
  110. }
  111. }
  112. CubebSink::~CubebSink() {
  113. if (!ctx) {
  114. return;
  115. }
  116. for (auto& sink_stream : sink_streams) {
  117. sink_stream.reset();
  118. }
  119. cubeb_destroy(ctx);
  120. }
  121. SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels,
  122. const std::string& name) {
  123. sink_streams.push_back(
  124. std::make_unique<CubebSinkStream>(ctx, sample_rate, num_channels, output_device, name));
  125. return *sink_streams.back();
  126. }
  127. long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
  128. void* output_buffer, long num_frames) {
  129. CubebSinkStream* impl = static_cast<CubebSinkStream*>(user_data);
  130. u8* buffer = reinterpret_cast<u8*>(output_buffer);
  131. if (!impl) {
  132. return {};
  133. }
  134. const std::size_t num_channels = impl->GetNumChannels();
  135. const std::size_t samples_to_write = num_channels * num_frames;
  136. std::size_t samples_written;
  137. if (Settings::values.enable_audio_stretching) {
  138. const std::vector<s16> in{impl->queue.Pop()};
  139. const std::size_t num_in{in.size() / num_channels};
  140. s16* const out{reinterpret_cast<s16*>(buffer)};
  141. const std::size_t out_frames =
  142. impl->time_stretch.Process(in.data(), num_in, out, num_frames);
  143. samples_written = out_frames * num_channels;
  144. if (impl->should_flush) {
  145. impl->time_stretch.Flush();
  146. impl->should_flush = false;
  147. }
  148. } else {
  149. samples_written = impl->queue.Pop(buffer, samples_to_write);
  150. }
  151. if (samples_written >= num_channels) {
  152. std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16),
  153. num_channels * sizeof(s16));
  154. }
  155. // Fill the rest of the frames with last_frame
  156. for (std::size_t i = samples_written; i < samples_to_write; i += num_channels) {
  157. std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16));
  158. }
  159. return num_frames;
  160. }
  161. void CubebSinkStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {}
  162. std::vector<std::string> ListCubebSinkDevices() {
  163. std::vector<std::string> device_list;
  164. cubeb* ctx;
  165. if (cubeb_init(&ctx, "yuzu Device Enumerator", nullptr) != CUBEB_OK) {
  166. LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
  167. return {};
  168. }
  169. cubeb_device_collection collection;
  170. if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) {
  171. LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported");
  172. } else {
  173. for (std::size_t i = 0; i < collection.count; i++) {
  174. const cubeb_device_info& device = collection.device[i];
  175. if (device.friendly_name) {
  176. device_list.emplace_back(device.friendly_name);
  177. }
  178. }
  179. cubeb_device_collection_destroy(ctx, &collection);
  180. }
  181. cubeb_destroy(ctx);
  182. return device_list;
  183. }
  184. } // namespace AudioCore