Files
XCEngine/engine/include/XCEngine/Audio/HRTF.h
ssdfasd d575532966 docs: update TEST_SPEC.md and README.md to reflect new directory structure
- TEST_SPEC.md: Updated test directory structure to reflect Core/Asset,
  Core/IO, and Resources/<Type> subdirectories
- TEST_SPEC.md: Updated module names and test counts (852 total)
- TEST_SPEC.md: Updated build commands for new Resources subdirectories
- README.md: Updated engine structure with Core/Asset/ and Core/IO/
- README.md: Updated Resources section with layered architecture
- README.md: Updated test coverage table with accurate counts
2026-03-24 16:14:05 +08:00

88 lines
2.6 KiB
C++

#pragma once
#include "AudioTypes.h"
#include <XCEngine/Core/Math/Vector3.h>
#include <XCEngine/Core/Math/Quaternion.h>
#include <vector>
#include <array>
namespace XCEngine {
namespace Audio {
struct HRTFParams {
float azimuth = 0.0f;
float elevation = 0.0f;
float interauralTimeDelay = 0.0f;
float interauralLevelDifference = 0.0f;
float headShadowing = 0.0f;
float pinnaCues = 0.0f;
float torsoShoulderRotation = 0.0f;
};
class HRTF {
public:
HRTF();
~HRTF();
void ProcessAudio(float* buffer, uint32 sampleCount, uint32 channels,
const Math::Vector3& sourcePosition,
const Math::Vector3& listenerPosition,
const Math::Quaternion& listenerRotation);
void SetEnabled(bool enabled) { m_enabled = enabled; }
bool IsEnabled() const { return m_enabled; }
void SetHRTFEnabled(bool enabled) { m_hrtfEnabled = enabled; }
bool IsHRTFEnabled() const { return m_hrtfEnabled; }
void SetQualityLevel(uint32 level);
uint32 GetQualityLevel() const { return m_qualityLevel; }
void SetCrossFeed(float crossFeed);
float GetCrossFeed() const { return m_crossFeed; }
void SetDopplerShiftEnabled(bool enabled) { m_dopplerEnabled = enabled; }
bool IsDopplerShiftEnabled() const { return m_dopplerEnabled; }
void SetSpeedOfSound(float speed);
float GetSpeedOfSound() const { return m_speedOfSound; }
private:
void ComputeDirection(const Math::Vector3& sourcePosition,
const Math::Vector3& listenerPosition,
const Math::Quaternion& listenerRotation,
float& azimuth, float& elevation);
void ComputeITD(float azimuth, float elevation);
void ComputeILD(float azimuth, float elevation);
float ComputePinnaEffect(float azimuth, float elevation);
void ApplyHRTF(float* buffer, uint32 sampleCount, uint32 channels);
void ApplySimplePanning(float* buffer, uint32 sampleCount, uint32 channels, float pan);
private:
bool m_enabled = true;
bool m_hrtfEnabled = true;
bool m_dopplerEnabled = true;
uint32 m_qualityLevel = 2;
float m_crossFeed = 0.0f;
float m_speedOfSound = 343.0f;
HRTFParams m_params;
Math::Vector3 m_prevSourcePosition = Math::Vector3::Zero();
Math::Vector3 m_prevListenerPosition = Math::Vector3::Zero();
float m_prevDopplerShift = 1.0f;
static constexpr uint32 MaxDelaySamples = 1024;
std::vector<float> m_leftDelayLine;
std::vector<float> m_rightDelayLine;
uint32 m_leftDelayIndex = 0;
uint32 m_rightDelayIndex = 0;
uint32 m_sampleRate = 48000;
};
} // namespace Audio
} // namespace XCEngine