From 458da8a94877677f086f06cdeecf959ec4283a33 Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Sat, 16 Jul 2022 23:48:45 +0100 Subject: Project Andio --- .../renderer/upsampler/upsampler_info.cpp | 6 +++ src/audio_core/renderer/upsampler/upsampler_info.h | 35 +++++++++++++++++ .../renderer/upsampler/upsampler_manager.cpp | 44 +++++++++++++++++++++ .../renderer/upsampler/upsampler_manager.h | 45 ++++++++++++++++++++++ .../renderer/upsampler/upsampler_state.h | 40 +++++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 src/audio_core/renderer/upsampler/upsampler_info.cpp create mode 100644 src/audio_core/renderer/upsampler/upsampler_info.h create mode 100644 src/audio_core/renderer/upsampler/upsampler_manager.cpp create mode 100644 src/audio_core/renderer/upsampler/upsampler_manager.h create mode 100644 src/audio_core/renderer/upsampler/upsampler_state.h (limited to 'src/audio_core/renderer/upsampler') 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 + +#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 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 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 infos_, + std::span 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 +#include + +#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 infos, std::span 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 upsampler_infos; + /// Workbuffer for upsampling samples + std::span 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 + +#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, 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 -- cgit v1.2.3