feat(audio): add 3D audio playback with source pool and auto-loading

This commit is contained in:
2026-07-06 20:26:20 +08:00
parent 0f60d578fb
commit 765f5ca714
10 changed files with 150 additions and 12 deletions

View File

@@ -12,6 +12,7 @@ AudioEngine::~AudioEngine() {
}
m_bgm.reset();
m_pool.reset();
m_sounds.clear();
alcMakeContextCurrent(nullptr);
@@ -37,6 +38,19 @@ void AudioEngine::init() {
m_fade_map.try_emplace("bgm", m_bgm.get(), 5.0f, 2.0f);
ALCint max_mono = 0;
alcGetIntegerv(device, ALC_MONO_SOURCES, 1, &max_mono);
if (max_mono <= 1) {
Logger::error("Can't get max mono");
max_mono = 4;
}
Logger::info("Set Source Pool Size {}", static_cast<int>(max_mono));
// Reserve a source for BGM
m_pool = std::make_shared<SourcePool>(max_mono - 1);
Logger::info("Audio Engine Init Success");
m_init = true;
@@ -44,11 +58,25 @@ void AudioEngine::init() {
void AudioEngine::play_bgm() { m_bgm->play(); }
void AudioEngine::play_3d(const std::string& sound, const glm::vec3& pos) {
if (!m_pool) {
Logger::error("Source Pool is nullptr");
return;
}
auto* source = m_pool->acquire();
if (!source) {
Logger::error("Source is Full");
}
auto& buffer = m_sounds.get_buffer(sound);
source->play_3d(buffer, pos);
}
void AudioEngine::update_listener(glm::vec3 listener_pos) {}
void AudioEngine::update(float dt) {
for (auto& [key, fade] : m_fade_map) {
fade.update();
}
m_pool->update();
}
} // namespace Cubed