stream.cpp 3.9 KB

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