summaryrefslogtreecommitdiffstats
path: root/src/audio_core/renderer/upsampler
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core/renderer/upsampler')
-rw-r--r--src/audio_core/renderer/upsampler/upsampler_info.cpp6
-rw-r--r--src/audio_core/renderer/upsampler/upsampler_info.h35
-rw-r--r--src/audio_core/renderer/upsampler/upsampler_manager.cpp44
-rw-r--r--src/audio_core/renderer/upsampler/upsampler_manager.h45
-rw-r--r--src/audio_core/renderer/upsampler/upsampler_state.h40
5 files changed, 170 insertions, 0 deletions
diff --git a/src/audio_core/renderer/upsampler/upsampler_info.cpp b/src/audio_core/renderer/upsampler/upsampler_info.cpp
new file mode 100644
index 000000000..e3d2f7db0
--- /dev/null
+++ b/src/audio_core/renderer/upsampler/upsampler_info.cpp
@@ -0,0 +1,6 @@
+// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "audio_core/renderer/upsampler/upsampler_info.h"
+
+namespace AudioCore::AudioRenderer {} // namespace AudioCore::AudioRenderer
diff --git a/src/audio_core/renderer/upsampler/upsampler_info.h b/src/audio_core/renderer/upsampler/upsampler_info.h
new file mode 100644
index 000000000..a43c15af3
--- /dev/null
+++ b/src/audio_core/renderer/upsampler/upsampler_info.h
@@ -0,0 +1,35 @@
+// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <array>
+
+#include "audio_core/common/common.h"
+#include "audio_core/renderer/upsampler/upsampler_state.h"
+#include "common/common_types.h"
+
+namespace AudioCore::AudioRenderer {
+class UpsamplerManager;
+
+/**
+ * Manages information needed to upsample a mix buffer.
+ */
+struct UpsamplerInfo {
+ /// States used by the AudioRenderer across calls.
+ std::array<UpsamplerState, MaxChannels> states{};
+ /// Pointer to the manager
+ UpsamplerManager* manager{};
+ /// Pointer to the samples to be upsampled
+ CpuAddr samples_pos{};
+ /// Target number of samples to upsample to
+ u32 sample_count{};
+ /// Number of channels to upsample
+ u32 input_count{};
+ /// Is this upsampler enabled?
+ bool enabled{};
+ /// Mix buffer indexes to be upsampled
+ std::array<s16, MaxChannels> inputs{};
+};
+
+} // namespace AudioCore::AudioRenderer
diff --git a/src/audio_core/renderer/upsampler/upsampler_manager.cpp b/src/audio_core/renderer/upsampler/upsampler_manager.cpp
new file mode 100644
index 000000000..4c76a5066
--- /dev/null
+++ b/src/audio_core/renderer/upsampler/upsampler_manager.cpp
@@ -0,0 +1,44 @@
+// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "audio_core/renderer/upsampler/upsampler_manager.h"
+
+namespace AudioCore::AudioRenderer {
+
+UpsamplerManager::UpsamplerManager(const u32 count_, std::span<UpsamplerInfo> infos_,
+ std::span<s32> workbuffer_)
+ : count{count_}, upsampler_infos{infos_}, workbuffer{workbuffer_} {}
+
+UpsamplerInfo* UpsamplerManager::Allocate() {
+ std::scoped_lock l{lock};
+
+ if (count == 0) {
+ return nullptr;
+ }
+
+ u32 free_index{0};
+ for (auto& upsampler : upsampler_infos) {
+ if (!upsampler.enabled) {
+ break;
+ }
+ free_index++;
+ }
+
+ if (free_index >= count) {
+ return nullptr;
+ }
+
+ auto& upsampler{upsampler_infos[free_index]};
+ upsampler.manager = this;
+ upsampler.sample_count = TargetSampleCount;
+ upsampler.samples_pos = CpuAddr(&workbuffer[upsampler.sample_count * MaxChannels]);
+ upsampler.enabled = true;
+ return &upsampler;
+}
+
+void UpsamplerManager::Free(UpsamplerInfo* info) {
+ std::scoped_lock l{lock};
+ info->enabled = false;
+}
+
+} // namespace AudioCore::AudioRenderer
diff --git a/src/audio_core/renderer/upsampler/upsampler_manager.h b/src/audio_core/renderer/upsampler/upsampler_manager.h
new file mode 100644
index 000000000..70cd42b08
--- /dev/null
+++ b/src/audio_core/renderer/upsampler/upsampler_manager.h
@@ -0,0 +1,45 @@
+// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <mutex>
+#include <span>
+
+#include "audio_core/renderer/upsampler/upsampler_info.h"
+#include "common/common_types.h"
+
+namespace AudioCore::AudioRenderer {
+/**
+ * Manages and has utility functions for upsampler infos.
+ */
+class UpsamplerManager {
+public:
+ UpsamplerManager(u32 count, std::span<UpsamplerInfo> infos, std::span<s32> workbuffer);
+
+ /**
+ * Allocate a new UpsamplerInfo.
+ *
+ * @return The allocated upsampler, may be nullptr if alloc failed.
+ */
+ UpsamplerInfo* Allocate();
+
+ /**
+ * Free the given upsampler.
+ *
+ * @param The upsampler to be freed.
+ */
+ void Free(UpsamplerInfo* info);
+
+private:
+ /// Maximum number of upsamplers in the buffer
+ const u32 count;
+ /// Upsamplers buffer
+ std::span<UpsamplerInfo> upsampler_infos;
+ /// Workbuffer for upsampling samples
+ std::span<s32> workbuffer;
+ /// Lock for allocate/free
+ std::mutex lock{};
+};
+
+} // namespace AudioCore::AudioRenderer
diff --git a/src/audio_core/renderer/upsampler/upsampler_state.h b/src/audio_core/renderer/upsampler/upsampler_state.h
new file mode 100644
index 000000000..28cebe200
--- /dev/null
+++ b/src/audio_core/renderer/upsampler/upsampler_state.h
@@ -0,0 +1,40 @@
+// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <array>
+
+#include "common/common_types.h"
+#include "common/fixed_point.h"
+
+namespace AudioCore::AudioRenderer {
+/**
+ * Upsampling state used by the AudioRenderer across calls.
+ */
+struct UpsamplerState {
+ static constexpr u16 HistorySize = 20;
+
+ /// Source data to target data ratio. E.g 48'000/32'000 = 1.5
+ Common::FixedPoint<16, 16> ratio;
+ /// Sample history
+ std::array<Common::FixedPoint<24, 8>, HistorySize> history;
+ /// Size of the sinc coefficient window
+ u16 window_size;
+ /// Read index for the history
+ u16 history_output_index;
+ /// Write index for the history
+ u16 history_input_index;
+ /// Start offset within the history, fixed to 0
+ u16 history_start_index;
+ /// Ebd offset within the history, fixed to HistorySize
+ u16 history_end_index;
+ /// Is this state initialized?
+ bool initialized;
+ /// Index of the current sample.
+ /// E.g 16K -> 48K has a ratio of 3, so this will be 0-2.
+ /// See the Upsample command in the AudioRenderer for more information.
+ u8 sample_index;
+};
+
+} // namespace AudioCore::AudioRenderer