stream.cpp 3.7 KB

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