audio: reuse audio system scratch buffers

This commit is contained in:
2026-04-14 16:25:11 +08:00
parent c495581878
commit 2eaab2481f
2 changed files with 270 additions and 33 deletions

View File

@@ -1,13 +1,15 @@
#pragma once
#include "AudioMixer.h"
#include "IAudioBackend.h"
#include "AudioConfig.h"
#include "AudioTypes.h"
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Core/Math/Quaternion.h>
#include <map>
#include <vector>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace XCEngine {
namespace Components {
@@ -27,6 +29,8 @@ public:
void SetBackend(std::unique_ptr<IAudioBackend> backend);
IAudioBackend* GetBackend() const { return m_backend.get(); }
AudioMixer& GetMasterMixer() { return m_masterMixer; }
const AudioMixer& GetMasterMixer() const { return m_masterMixer; }
std::string GetCurrentDevice() const;
void SetDevice(const std::string& deviceName);
@@ -38,7 +42,16 @@ public:
bool IsMuted() const;
void SetMuted(bool muted);
void ProcessAudio(float* buffer, uint32 sampleCount, uint32 channels);
void SetListenerReverbMixer(AudioMixer* mixer);
AudioMixer* GetListenerReverbMixer() const { return m_listenerReverbMixer; }
void SetListenerReverbLevel(float level);
float GetListenerReverbLevel() const { return m_listenerReverbLevel; }
void SetListenerDopplerLevel(float level);
float GetListenerDopplerLevel() const { return m_listenerDopplerLevel; }
void SetSpeedOfSound(float metersPerSecond);
float GetSpeedOfSound() const { return m_speedOfSound; }
void ProcessAudio(float* buffer, uint32 frameCount, uint32 channels);
void SetListenerTransform(const Math::Vector3& position, const Math::Quaternion& rotation);
void SetListenerVelocity(const Math::Vector3& velocity);
@@ -65,16 +78,29 @@ private:
AudioSystem(const AudioSystem&) = delete;
AudioSystem& operator=(const AudioSystem&) = delete;
void ProcessSource(Components::AudioSourceComponent* source, float* buffer, uint32 sampleCount, uint32 channels);
void ProcessSource(Components::AudioSourceComponent* source, float* buffer, uint32 frameCount, uint32 channels, uint32 sampleRate);
private:
std::unique_ptr<IAudioBackend> m_backend;
AudioMixer m_masterMixer;
AudioMixer* m_listenerReverbMixer = nullptr;
float m_listenerReverbLevel = 1.0f;
float m_listenerDopplerLevel = 1.0f;
float m_speedOfSound = 343.0f;
Math::Vector3 m_listenerPosition = Math::Vector3::Zero();
Math::Quaternion m_listenerRotation = Math::Quaternion::Identity();
Math::Vector3 m_listenerVelocity = Math::Vector3::Zero();
std::vector<Components::AudioSourceComponent*> m_activeSources;
std::vector<Components::AudioSourceComponent*> m_activeSourceSnapshot;
std::vector<float> m_mixScratchBuffer;
std::vector<float> m_sourceScratchBuffer;
std::unordered_map<AudioMixer*, std::vector<float>> m_mixerScratchBuffers;
std::unordered_map<AudioMixer*, std::vector<AudioMixer*>> m_mixerScratchChildren;
std::unordered_set<AudioMixer*> m_mixerScratchActiveMixers;
std::unordered_set<AudioMixer*> m_mixerScratchVisiting;
std::unordered_set<AudioMixer*> m_mixerScratchRendered;
Stats m_stats = {};
float m_deltaTime = 0.0f;