device_session.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "audio_core/audio_core.h"
  4. #include "audio_core/audio_manager.h"
  5. #include "audio_core/device/audio_buffer.h"
  6. #include "audio_core/device/device_session.h"
  7. #include "audio_core/sink/sink_stream.h"
  8. #include "core/core.h"
  9. #include "core/core_timing.h"
  10. #include "core/memory.h"
  11. namespace AudioCore {
  12. using namespace std::literals;
  13. constexpr auto INCREMENT_TIME{5ms};
  14. DeviceSession::DeviceSession(Core::System& system_)
  15. : system{system_}, thread_event{Core::Timing::CreateEvent(
  16. "AudioOutSampleTick",
  17. [this](std::uintptr_t, s64 time, std::chrono::nanoseconds) {
  18. return ThreadFunc();
  19. })} {}
  20. DeviceSession::~DeviceSession() {
  21. Finalize();
  22. }
  23. Result DeviceSession::Initialize(std::string_view name_, SampleFormat sample_format_,
  24. u16 channel_count_, size_t session_id_, u32 handle_,
  25. u64 applet_resource_user_id_, Sink::StreamType type_) {
  26. if (stream) {
  27. Finalize();
  28. }
  29. name = fmt::format("{}-{}", name_, session_id_);
  30. type = type_;
  31. sample_format = sample_format_;
  32. channel_count = channel_count_;
  33. session_id = session_id_;
  34. handle = handle_;
  35. applet_resource_user_id = applet_resource_user_id_;
  36. if (type == Sink::StreamType::In) {
  37. sink = &system.AudioCore().GetInputSink();
  38. } else {
  39. sink = &system.AudioCore().GetOutputSink();
  40. }
  41. stream = sink->AcquireSinkStream(system, channel_count, name, type);
  42. initialized = true;
  43. return ResultSuccess;
  44. }
  45. void DeviceSession::Finalize() {
  46. if (initialized) {
  47. Stop();
  48. sink->CloseStream(stream);
  49. stream = nullptr;
  50. }
  51. }
  52. void DeviceSession::Start() {
  53. if (stream) {
  54. stream->Start();
  55. system.CoreTiming().ScheduleLoopingEvent(std::chrono::nanoseconds::zero(), INCREMENT_TIME,
  56. thread_event);
  57. }
  58. }
  59. void DeviceSession::Stop() {
  60. if (stream) {
  61. stream->Stop();
  62. system.CoreTiming().UnscheduleEvent(thread_event, {});
  63. }
  64. }
  65. void DeviceSession::ClearBuffers() {
  66. if (stream) {
  67. stream->ClearQueue();
  68. }
  69. }
  70. void DeviceSession::AppendBuffers(std::span<const AudioBuffer> buffers) const {
  71. for (const auto& buffer : buffers) {
  72. Sink::SinkBuffer new_buffer{
  73. .frames = buffer.size / (channel_count * sizeof(s16)),
  74. .frames_played = 0,
  75. .tag = buffer.tag,
  76. .consumed = false,
  77. };
  78. if (type == Sink::StreamType::In) {
  79. std::vector<s16> samples{};
  80. stream->AppendBuffer(new_buffer, samples);
  81. } else {
  82. std::vector<s16> samples(buffer.size / sizeof(s16));
  83. system.ApplicationMemory().ReadBlockUnsafe(buffer.samples, samples.data(), buffer.size);
  84. stream->AppendBuffer(new_buffer, samples);
  85. }
  86. }
  87. }
  88. void DeviceSession::ReleaseBuffer(const AudioBuffer& buffer) const {
  89. if (type == Sink::StreamType::In) {
  90. auto samples{stream->ReleaseBuffer(buffer.size / sizeof(s16))};
  91. system.ApplicationMemory().WriteBlockUnsafe(buffer.samples, samples.data(), buffer.size);
  92. }
  93. }
  94. bool DeviceSession::IsBufferConsumed(const AudioBuffer& buffer) const {
  95. return played_sample_count >= buffer.end_timestamp;
  96. }
  97. void DeviceSession::SetVolume(f32 volume) const {
  98. if (stream) {
  99. stream->SetSystemVolume(volume);
  100. }
  101. }
  102. u64 DeviceSession::GetPlayedSampleCount() const {
  103. return played_sample_count;
  104. }
  105. std::optional<std::chrono::nanoseconds> DeviceSession::ThreadFunc() {
  106. played_sample_count = stream->GetExpectedPlayedSampleCount();
  107. if (type == Sink::StreamType::Out) {
  108. system.AudioCore().GetAudioManager().SetEvent(Event::Type::AudioOutManager, true);
  109. } else {
  110. system.AudioCore().GetAudioManager().SetEvent(Event::Type::AudioInManager, true);
  111. }
  112. return std::nullopt;
  113. }
  114. void DeviceSession::SetRingSize(u32 ring_size) {
  115. stream->SetRingSize(ring_size);
  116. }
  117. } // namespace AudioCore