stream.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <cmath>
  6. #include "audio_core/sink.h"
  7. #include "audio_core/sink_details.h"
  8. #include "audio_core/sink_stream.h"
  9. #include "audio_core/stream.h"
  10. #include "common/assert.h"
  11. #include "common/logging/log.h"
  12. #include "common/microprofile.h"
  13. #include "core/core_timing.h"
  14. #include "core/core_timing_util.h"
  15. #include "core/settings.h"
  16. namespace AudioCore {
  17. constexpr std::size_t MaxAudioBufferCount{32};
  18. u32 Stream::GetNumChannels() const {
  19. switch (format) {
  20. case Format::Mono16:
  21. return 1;
  22. case Format::Stereo16:
  23. return 2;
  24. case Format::Multi51Channel16:
  25. return 6;
  26. }
  27. LOG_CRITICAL(Audio, "Unimplemented format={}", static_cast<u32>(format));
  28. UNREACHABLE();
  29. return {};
  30. }
  31. Stream::Stream(u32 sample_rate, Format format, ReleaseCallback&& release_callback,
  32. SinkStream& sink_stream, std::string&& name_)
  33. : sample_rate{sample_rate}, format{format}, release_callback{std::move(release_callback)},
  34. sink_stream{sink_stream}, name{std::move(name_)} {
  35. release_event = CoreTiming::RegisterEvent(
  36. name, [this](u64 userdata, int cycles_late) { ReleaseActiveBuffer(); });
  37. }
  38. void Stream::Play() {
  39. state = State::Playing;
  40. PlayNextBuffer();
  41. }
  42. void Stream::Stop() {
  43. ASSERT_MSG(false, "Unimplemented");
  44. }
  45. s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const {
  46. const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()};
  47. return CoreTiming::usToCycles((static_cast<u64>(num_samples) * 1000000) / sample_rate);
  48. }
  49. static void VolumeAdjustSamples(std::vector<s16>& samples) {
  50. const float volume{std::clamp(Settings::values.volume, 0.0f, 1.0f)};
  51. if (volume == 1.0f) {
  52. return;
  53. }
  54. // Implementation of a volume slider with a dynamic range of 60 dB
  55. const float volume_scale_factor{std::exp(6.90775f * volume) * 0.001f};
  56. for (auto& sample : samples) {
  57. sample = static_cast<s16>(sample * volume_scale_factor);
  58. }
  59. }
  60. void Stream::PlayNextBuffer() {
  61. if (!IsPlaying()) {
  62. // Ensure we are in playing state before playing the next buffer
  63. sink_stream.Flush();
  64. return;
  65. }
  66. if (active_buffer) {
  67. // Do not queue a new buffer if we are already playing a buffer
  68. return;
  69. }
  70. if (queued_buffers.empty()) {
  71. // No queued buffers - we are effectively paused
  72. sink_stream.Flush();
  73. return;
  74. }
  75. active_buffer = queued_buffers.front();
  76. queued_buffers.pop();
  77. VolumeAdjustSamples(active_buffer->Samples());
  78. sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples());
  79. CoreTiming::ScheduleEventThreadsafe(GetBufferReleaseCycles(*active_buffer), release_event, {});
  80. }
  81. MICROPROFILE_DEFINE(AudioOutput, "Audio", "ReleaseActiveBuffer", MP_RGB(100, 100, 255));
  82. void Stream::ReleaseActiveBuffer() {
  83. MICROPROFILE_SCOPE(AudioOutput);
  84. ASSERT(active_buffer);
  85. released_buffers.push(std::move(active_buffer));
  86. release_callback();
  87. PlayNextBuffer();
  88. }
  89. bool Stream::QueueBuffer(BufferPtr&& buffer) {
  90. if (queued_buffers.size() < MaxAudioBufferCount) {
  91. queued_buffers.push(std::move(buffer));
  92. PlayNextBuffer();
  93. return true;
  94. }
  95. return false;
  96. }
  97. bool Stream::ContainsBuffer(Buffer::Tag tag) const {
  98. ASSERT_MSG(false, "Unimplemented");
  99. return {};
  100. }
  101. std::vector<Buffer::Tag> Stream::GetTagsAndReleaseBuffers(std::size_t max_count) {
  102. std::vector<Buffer::Tag> tags;
  103. for (std::size_t count = 0; count < max_count && !released_buffers.empty(); ++count) {
  104. tags.push_back(released_buffers.front()->GetTag());
  105. released_buffers.pop();
  106. }
  107. return tags;
  108. }
  109. } // namespace AudioCore