stream.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. state = State::Stopped;
  44. ASSERT_MSG(false, "Unimplemented");
  45. }
  46. Stream::State Stream::GetState() const {
  47. return state;
  48. }
  49. s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const {
  50. const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()};
  51. return CoreTiming::usToCycles((static_cast<u64>(num_samples) * 1000000) / sample_rate);
  52. }
  53. static void VolumeAdjustSamples(std::vector<s16>& samples) {
  54. const float volume{std::clamp(Settings::values.volume, 0.0f, 1.0f)};
  55. if (volume == 1.0f) {
  56. return;
  57. }
  58. // Implementation of a volume slider with a dynamic range of 60 dB
  59. const float volume_scale_factor{std::exp(6.90775f * volume) * 0.001f};
  60. for (auto& sample : samples) {
  61. sample = static_cast<s16>(sample * volume_scale_factor);
  62. }
  63. }
  64. void Stream::PlayNextBuffer() {
  65. if (!IsPlaying()) {
  66. // Ensure we are in playing state before playing the next buffer
  67. sink_stream.Flush();
  68. return;
  69. }
  70. if (active_buffer) {
  71. // Do not queue a new buffer if we are already playing a buffer
  72. return;
  73. }
  74. if (queued_buffers.empty()) {
  75. // No queued buffers - we are effectively paused
  76. sink_stream.Flush();
  77. return;
  78. }
  79. active_buffer = queued_buffers.front();
  80. queued_buffers.pop();
  81. VolumeAdjustSamples(active_buffer->Samples());
  82. sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples());
  83. CoreTiming::ScheduleEventThreadsafe(GetBufferReleaseCycles(*active_buffer), release_event, {});
  84. }
  85. MICROPROFILE_DEFINE(AudioOutput, "Audio", "ReleaseActiveBuffer", MP_RGB(100, 100, 255));
  86. void Stream::ReleaseActiveBuffer() {
  87. MICROPROFILE_SCOPE(AudioOutput);
  88. ASSERT(active_buffer);
  89. released_buffers.push(std::move(active_buffer));
  90. release_callback();
  91. PlayNextBuffer();
  92. }
  93. bool Stream::QueueBuffer(BufferPtr&& buffer) {
  94. if (queued_buffers.size() < MaxAudioBufferCount) {
  95. queued_buffers.push(std::move(buffer));
  96. PlayNextBuffer();
  97. return true;
  98. }
  99. return false;
  100. }
  101. bool Stream::ContainsBuffer(Buffer::Tag tag) const {
  102. ASSERT_MSG(false, "Unimplemented");
  103. return {};
  104. }
  105. std::vector<Buffer::Tag> Stream::GetTagsAndReleaseBuffers(std::size_t max_count) {
  106. std::vector<Buffer::Tag> tags;
  107. for (std::size_t count = 0; count < max_count && !released_buffers.empty(); ++count) {
  108. tags.push_back(released_buffers.front()->GetTag());
  109. released_buffers.pop();
  110. }
  111. return tags;
  112. }
  113. } // namespace AudioCore