audio: clear mixer routes on destruction

This commit is contained in:
2026-04-14 16:48:39 +08:00
parent ee03f7035b
commit a4c48c1b3f
6 changed files with 241 additions and 8 deletions

View File

@@ -1,15 +1,19 @@
#include <XCEngine/Components/AudioListenerComponent.h>
#include <XCEngine/Audio/AudioSystem.h>
#include <algorithm>
#include <cmath>
#include <sstream>
namespace XCEngine {
namespace Components {
AudioListenerComponent::AudioListenerComponent()
{
Audio::AudioSystem::Get().RegisterListenerComponent(this);
}
AudioListenerComponent::~AudioListenerComponent() {
Audio::AudioSystem::Get().UnregisterListenerComponent(this);
}
void AudioListenerComponent::SetMasterVolume(float volume) {
@@ -24,18 +28,22 @@ void AudioListenerComponent::SetMute(bool mute) {
void AudioListenerComponent::SetDopplerLevel(float level) {
m_dopplerLevel = std::max(0.0f, std::min(5.0f, level));
Audio::AudioSystem::Get().SetListenerDopplerLevel(m_dopplerLevel);
}
void AudioListenerComponent::SetSpeedOfSound(float metersPerSecond) {
m_speedOfSound = std::max(1.0f, metersPerSecond);
Audio::AudioSystem::Get().SetSpeedOfSound(m_speedOfSound);
}
void AudioListenerComponent::SetReverbLevel(float level) {
m_reverbLevel = std::max(0.0f, std::min(1.0f, level));
Audio::AudioSystem::Get().SetListenerReverbLevel(m_reverbLevel);
}
void AudioListenerComponent::SetReverb(Audio::AudioMixer* reverb) {
m_reverb = reverb;
Audio::AudioSystem::Get().SetListenerReverbMixer(m_reverb);
}
void AudioListenerComponent::Update(float deltaTime) {
@@ -43,10 +51,62 @@ void AudioListenerComponent::Update(float deltaTime) {
return;
}
Math::Vector3 position = transform().GetPosition();
Math::Quaternion rotation = transform().GetRotation();
const Math::Vector3 position = transform().GetPosition();
const Math::Quaternion rotation = transform().GetRotation();
Math::Vector3 velocity = Math::Vector3::Zero();
if (m_hasLastPosition && deltaTime > 0.0f) {
velocity = (position - m_lastPosition) / deltaTime;
}
Audio::AudioSystem::Get().SetListenerTransform(position, rotation);
Audio::AudioSystem::Get().SetListenerVelocity(velocity);
m_lastPosition = position;
m_hasLastPosition = true;
}
void AudioListenerComponent::Serialize(std::ostream& os) const {
os << "masterVolume=" << m_masterVolume << ";";
os << "mute=" << (m_mute ? 1 : 0) << ";";
os << "dopplerLevel=" << m_dopplerLevel << ";";
os << "speedOfSound=" << m_speedOfSound << ";";
os << "reverbLevel=" << m_reverbLevel << ";";
}
void AudioListenerComponent::Deserialize(std::istream& is) {
SetMasterVolume(1.0f);
SetMute(false);
SetDopplerLevel(1.0f);
SetSpeedOfSound(343.0f);
SetReverbLevel(1.0f);
std::string token;
while (std::getline(is, token, ';')) {
if (token.empty()) {
continue;
}
const size_t eqPos = token.find('=');
if (eqPos == std::string::npos) {
continue;
}
const std::string key = token.substr(0, eqPos);
const std::string value = token.substr(eqPos + 1);
if (key == "masterVolume") {
SetMasterVolume(std::stof(value));
} else if (key == "mute") {
SetMute(std::stoi(value) != 0);
} else if (key == "dopplerLevel") {
SetDopplerLevel(std::stof(value));
} else if (key == "speedOfSound") {
SetSpeedOfSound(std::stof(value));
} else if (key == "reverbLevel") {
SetReverbLevel(std::stof(value));
}
}
}
} // namespace Components

View File

@@ -146,9 +146,12 @@ void ApplyPanToBuffer(float* buffer, Audio::uint32 frameCount, Audio::uint32 cha
} // namespace
AudioSourceComponent::AudioSourceComponent() = default;
AudioSourceComponent::AudioSourceComponent() {
Audio::AudioSystem::Get().RegisterSourceComponent(this);
}
AudioSourceComponent::~AudioSourceComponent() {
Audio::AudioSystem::Get().UnregisterSourceComponent(this);
if (m_playState == Audio::PlayState::Playing) {
Audio::AudioSystem::Get().UnregisterSource(this);
}