sink_stream.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <atomic>
  5. #include <memory>
  6. #include <span>
  7. #include <vector>
  8. #include "audio_core/audio_core.h"
  9. #include "audio_core/common/common.h"
  10. #include "audio_core/sink/sink_stream.h"
  11. #include "common/common_types.h"
  12. #include "common/fixed_point.h"
  13. #include "common/settings.h"
  14. #include "core/core.h"
  15. namespace AudioCore::Sink {
  16. void SinkStream::AppendBuffer(SinkBuffer& buffer, std::vector<s16>& samples) {
  17. if (type == StreamType::In) {
  18. queue.enqueue(buffer);
  19. queued_buffers++;
  20. return;
  21. }
  22. constexpr s32 min{std::numeric_limits<s16>::min()};
  23. constexpr s32 max{std::numeric_limits<s16>::max()};
  24. auto yuzu_volume{Settings::Volume()};
  25. if (yuzu_volume > 1.0f) {
  26. yuzu_volume = 0.6f + 20 * std::log10(yuzu_volume);
  27. }
  28. auto volume{system_volume * device_volume * yuzu_volume};
  29. if (system_channels == 6 && device_channels == 2) {
  30. // We're given 6 channels, but our device only outputs 2, so downmix.
  31. constexpr std::array<f32, 4> down_mix_coeff{1.0f, 0.707f, 0.251f, 0.707f};
  32. for (u32 read_index = 0, write_index = 0; read_index < samples.size();
  33. read_index += system_channels, write_index += device_channels) {
  34. const auto left_sample{
  35. ((Common::FixedPoint<49, 15>(
  36. samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
  37. down_mix_coeff[0] +
  38. samples[read_index + static_cast<u32>(Channels::Center)] * down_mix_coeff[1] +
  39. samples[read_index + static_cast<u32>(Channels::LFE)] * down_mix_coeff[2] +
  40. samples[read_index + static_cast<u32>(Channels::BackLeft)] * down_mix_coeff[3]) *
  41. volume)
  42. .to_int()};
  43. const auto right_sample{
  44. ((Common::FixedPoint<49, 15>(
  45. samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
  46. down_mix_coeff[0] +
  47. samples[read_index + static_cast<u32>(Channels::Center)] * down_mix_coeff[1] +
  48. samples[read_index + static_cast<u32>(Channels::LFE)] * down_mix_coeff[2] +
  49. samples[read_index + static_cast<u32>(Channels::BackRight)] * down_mix_coeff[3]) *
  50. volume)
  51. .to_int()};
  52. samples[write_index + static_cast<u32>(Channels::FrontLeft)] =
  53. static_cast<s16>(std::clamp(left_sample, min, max));
  54. samples[write_index + static_cast<u32>(Channels::FrontRight)] =
  55. static_cast<s16>(std::clamp(right_sample, min, max));
  56. }
  57. samples.resize(samples.size() / system_channels * device_channels);
  58. } else if (system_channels == 2 && device_channels == 6) {
  59. // We need moar samples! Not all games will provide 6 channel audio.
  60. // TODO: Implement some upmixing here. Currently just passthrough, with other
  61. // channels left as silence.
  62. std::vector<s16> new_samples(samples.size() / system_channels * device_channels, 0);
  63. for (u32 read_index = 0, write_index = 0; read_index < samples.size();
  64. read_index += system_channels, write_index += device_channels) {
  65. const auto left_sample{static_cast<s16>(std::clamp(
  66. static_cast<s32>(
  67. static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
  68. volume),
  69. min, max))};
  70. new_samples[write_index + static_cast<u32>(Channels::FrontLeft)] = left_sample;
  71. const auto right_sample{static_cast<s16>(std::clamp(
  72. static_cast<s32>(
  73. static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
  74. volume),
  75. min, max))};
  76. new_samples[write_index + static_cast<u32>(Channels::FrontRight)] = right_sample;
  77. }
  78. samples = std::move(new_samples);
  79. } else if (volume != 1.0f) {
  80. for (u32 i = 0; i < samples.size(); i++) {
  81. samples[i] = static_cast<s16>(
  82. std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
  83. }
  84. }
  85. samples_buffer.Push(samples);
  86. queue.enqueue(buffer);
  87. queued_buffers++;
  88. }
  89. std::vector<s16> SinkStream::ReleaseBuffer(u64 num_samples) {
  90. constexpr s32 min = std::numeric_limits<s16>::min();
  91. constexpr s32 max = std::numeric_limits<s16>::max();
  92. auto samples{samples_buffer.Pop(num_samples)};
  93. // TODO: Up-mix to 6 channels if the game expects it.
  94. // For audio input this is unlikely to ever be the case though.
  95. // Incoming mic volume seems to always be very quiet, so multiply by an additional 8 here.
  96. // TODO: Play with this and find something that works better.
  97. auto volume{system_volume * device_volume * 8};
  98. for (u32 i = 0; i < samples.size(); i++) {
  99. samples[i] = static_cast<s16>(
  100. std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
  101. }
  102. if (samples.size() < num_samples) {
  103. samples.resize(num_samples, 0);
  104. }
  105. return samples;
  106. }
  107. void SinkStream::ClearQueue() {
  108. samples_buffer.Pop();
  109. while (queue.pop()) {
  110. }
  111. queued_buffers = 0;
  112. playing_buffer = {};
  113. playing_buffer.consumed = true;
  114. }
  115. void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t num_frames) {
  116. const std::size_t num_channels = GetDeviceChannels();
  117. const std::size_t frame_size = num_channels;
  118. const std::size_t frame_size_bytes = frame_size * sizeof(s16);
  119. size_t frames_written{0};
  120. // If we're paused or going to shut down, we don't want to consume buffers as coretiming is
  121. // paused and we'll desync, so just return.
  122. if (system.IsPaused() || system.IsShuttingDown()) {
  123. return;
  124. }
  125. if (queued_buffers > max_queue_size) {
  126. Stall();
  127. }
  128. while (frames_written < num_frames) {
  129. // If the playing buffer has been consumed or has no frames, we need a new one
  130. if (playing_buffer.consumed || playing_buffer.frames == 0) {
  131. if (!queue.try_dequeue(playing_buffer)) {
  132. // If no buffer was available we've underrun, just push the samples and
  133. // continue.
  134. samples_buffer.Push(&input_buffer[frames_written * frame_size],
  135. (num_frames - frames_written) * frame_size);
  136. frames_written = num_frames;
  137. continue;
  138. }
  139. // Successfully dequeued a new buffer.
  140. queued_buffers--;
  141. }
  142. // Get the minimum frames available between the currently playing buffer, and the
  143. // amount we have left to fill
  144. size_t frames_available{std::min<u64>(playing_buffer.frames - playing_buffer.frames_played,
  145. num_frames - frames_written)};
  146. samples_buffer.Push(&input_buffer[frames_written * frame_size],
  147. frames_available * frame_size);
  148. frames_written += frames_available;
  149. playing_buffer.frames_played += frames_available;
  150. // If that's all the frames in the current buffer, add its samples and mark it as
  151. // consumed
  152. if (playing_buffer.frames_played >= playing_buffer.frames) {
  153. playing_buffer.consumed = true;
  154. }
  155. }
  156. std::memcpy(&last_frame[0], &input_buffer[(frames_written - 1) * frame_size], frame_size_bytes);
  157. if (queued_buffers <= max_queue_size) {
  158. Unstall();
  159. }
  160. }
  161. void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::size_t num_frames) {
  162. const std::size_t num_channels = GetDeviceChannels();
  163. const std::size_t frame_size = num_channels;
  164. const std::size_t frame_size_bytes = frame_size * sizeof(s16);
  165. size_t frames_written{0};
  166. // If we're paused or going to shut down, we don't want to consume buffers as coretiming is
  167. // paused and we'll desync, so just play silence.
  168. if (system.IsPaused() || system.IsShuttingDown()) {
  169. constexpr std::array<s16, 6> silence{};
  170. for (size_t i = frames_written; i < num_frames; i++) {
  171. std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes);
  172. }
  173. return;
  174. }
  175. // Due to many frames being queued up with nvdec (5 frames or so?), a lot of buffers also get
  176. // queued up (30+) but not all at once, which causes constant stalling here, so just let the
  177. // video play out without attempting to stall.
  178. // Can hopefully remove this later with a more complete NVDEC implementation.
  179. const auto nvdec_active{system.AudioCore().IsNVDECActive()};
  180. // Core timing cannot be paused in single-core mode, so Stall ends up being called over and over
  181. // and never recovers to a normal state, so just skip attempting to sync things on single-core.
  182. if (system.IsMulticore() && !nvdec_active && queued_buffers > max_queue_size) {
  183. Stall();
  184. } else if (system.IsMulticore() && queued_buffers <= max_queue_size) {
  185. Unstall();
  186. }
  187. while (frames_written < num_frames) {
  188. // If the playing buffer has been consumed or has no frames, we need a new one
  189. if (playing_buffer.consumed || playing_buffer.frames == 0) {
  190. if (!queue.try_dequeue(playing_buffer)) {
  191. // If no buffer was available we've underrun, fill the remaining buffer with
  192. // the last written frame and continue.
  193. for (size_t i = frames_written; i < num_frames; i++) {
  194. std::memcpy(&output_buffer[i * frame_size], &last_frame[0], frame_size_bytes);
  195. }
  196. frames_written = num_frames;
  197. continue;
  198. }
  199. // Successfully dequeued a new buffer.
  200. queued_buffers--;
  201. }
  202. // Get the minimum frames available between the currently playing buffer, and the
  203. // amount we have left to fill
  204. size_t frames_available{std::min<u64>(playing_buffer.frames - playing_buffer.frames_played,
  205. num_frames - frames_written)};
  206. samples_buffer.Pop(&output_buffer[frames_written * frame_size],
  207. frames_available * frame_size);
  208. frames_written += frames_available;
  209. playing_buffer.frames_played += frames_available;
  210. // If that's all the frames in the current buffer, add its samples and mark it as
  211. // consumed
  212. if (playing_buffer.frames_played >= playing_buffer.frames) {
  213. playing_buffer.consumed = true;
  214. }
  215. }
  216. std::memcpy(&last_frame[0], &output_buffer[(frames_written - 1) * frame_size],
  217. frame_size_bytes);
  218. if (system.IsMulticore() && queued_buffers <= max_queue_size) {
  219. Unstall();
  220. }
  221. }
  222. void SinkStream::Stall() {
  223. if (stalled) {
  224. return;
  225. }
  226. stalled = true;
  227. system.StallProcesses();
  228. }
  229. void SinkStream::Unstall() {
  230. if (!stalled) {
  231. return;
  232. }
  233. system.UnstallProcesses();
  234. stalled = false;
  235. }
  236. } // namespace AudioCore::Sink