Add HRTF 3D spatialization audio effect
This commit is contained in:
87
engine/include/XCEngine/Audio/HRTF.h
Normal file
87
engine/include/XCEngine/Audio/HRTF.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include "AudioTypes.h"
|
||||
#include <XCEngine/Math/Vector3.h>
|
||||
#include <XCEngine/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
|
||||
Reference in New Issue
Block a user