From 56ab3de5148081fb8510cd5166fbd6ff80da9528 Mon Sep 17 00:00:00 2001 From: ostermine Date: Thu, 12 Nov 2026 01:19:21 +0300 Subject: [PATCH] make audio positions relative to the listener OpenAL takes positions as floats. Positions of the listener and of the speakers were passed in the world space, so far from the world origin both were rounded to the float step (2 m at 30M blocks): a sound next to the player came from a wrong side and with a wrong volume. A speaker keeps its position as worldvec3, the listener stays in the OpenAL origin, and a source is placed at "speaker - listener". Sources are refreshed in the speaker update, once per frame. --- src/audio/AL/ALAudio.cpp | 20 ++++++++++++++----- src/audio/AL/ALAudio.hpp | 11 ++++++++--- src/audio/NoAudio.hpp | 2 +- src/audio/audio.cpp | 8 ++++---- src/audio/audio.hpp | 14 ++++++------- src/frontend/LevelFrontend.cpp | 4 ++-- src/logic/scripting/lua/libs/libaudio.cpp | 24 ++++------------------- 7 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/audio/AL/ALAudio.cpp b/src/audio/AL/ALAudio.cpp index 1aaeb8f1a..8458a2b4b 100644 --- a/src/audio/AL/ALAudio.cpp +++ b/src/audio/AL/ALAudio.cpp @@ -342,6 +342,7 @@ void ALSpeaker::update(const Channel* channel) { if (source == 0) return; float gain = this->volume * channel->getVolume(); AL_CHECK(alSourcef(source, AL_GAIN, gain)); + refreshPosition(); if (!paused) { if (isPaused() && !channel->isPaused()) { @@ -451,12 +452,18 @@ void ALSpeaker::setTime(duration_t time) { AL_CHECK(alSourcef(source, AL_SEC_OFFSET, static_cast(time))); } -void ALSpeaker::setPosition(glm::vec3 pos) { +void ALSpeaker::refreshPosition() { + glm::vec3 pos(relative ? position : position - al->listenerPosition); AL_CHECK(alSource3f(source, AL_POSITION, pos.x, pos.y, pos.z)); } -glm::vec3 ALSpeaker::getPosition() const { - return AL::getSource3f(source, AL_POSITION); +void ALSpeaker::setPosition(worldvec3 pos) { + position = pos; + refreshPosition(); +} + +worldvec3 ALSpeaker::getPosition() const { + return position; } void ALSpeaker::setVelocity(glm::vec3 vel) { @@ -468,9 +475,11 @@ glm::vec3 ALSpeaker::getVelocity() const { } void ALSpeaker::setRelative(bool relative) { + this->relative = relative; AL_CHECK( alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE) ); + refreshPosition(); } bool ALSpeaker::isRelative() const { @@ -782,11 +791,12 @@ void ALAudio::freeBuffer(uint buffer) { } void ALAudio::setListener( - glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up + worldvec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up ) { ALfloat listenerOri[] = {at.x, at.y, at.z, up.x, up.y, up.z}; - AL_CHECK(alListener3f(AL_POSITION, position.x, position.y, position.z)); + listenerPosition = position; + AL_CHECK(alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f)); AL_CHECK(alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z)); AL_CHECK(alListenerfv(AL_ORIENTATION, listenerOri)); AL_CHECK(alListenerf(AL_GAIN, get_channel(0)->getVolume())); diff --git a/src/audio/AL/ALAudio.hpp b/src/audio/AL/ALAudio.hpp index b405d1473..3ca08c671 100644 --- a/src/audio/AL/ALAudio.hpp +++ b/src/audio/AL/ALAudio.hpp @@ -121,6 +121,10 @@ namespace audio { int priority; int channel; float volume = 0.0f; + worldvec3 position {}; + bool relative = false; + + void refreshPosition(); public: ALStream* stream = nullptr; bool manuallyStopped = true; @@ -153,8 +157,8 @@ namespace audio { duration_t getDuration() const override; void setTime(duration_t time) override; - void setPosition(glm::vec3 pos) override; - glm::vec3 getPosition() const override; + void setPosition(worldvec3 pos) override; + worldvec3 getPosition() const override; void setVelocity(glm::vec3 vel) override; glm::vec3 getVelocity() const override; @@ -188,6 +192,7 @@ namespace audio { std::array filters; bool useEffects; + worldvec3 listenerPosition {}; ALAudio( ALCdevice* device, @@ -221,7 +226,7 @@ namespace audio { std::vector getInputDeviceNames() override; void setListener( - glm::vec3 position, + worldvec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up diff --git a/src/audio/NoAudio.hpp b/src/audio/NoAudio.hpp index c97794f36..c4e803c23 100644 --- a/src/audio/NoAudio.hpp +++ b/src/audio/NoAudio.hpp @@ -97,7 +97,7 @@ namespace audio { } void setListener( - glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up + worldvec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up ) override { } diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index a20020ced..370991f55 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -352,7 +352,7 @@ void audio::set_input_device(const std::string& deviceName) { } void audio::set_listener( - glm::vec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up + worldvec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up ) { backend->setListener(position, velocity, lookAt, up); } @@ -380,7 +380,7 @@ void remove_lower_priority_speaker(int priority) { speakerid_t audio::play( Sound* sound, - glm::vec3 position, + worldvec3 position, bool relative, float volume, float pitch, @@ -419,7 +419,7 @@ speakerid_t audio::play( speakerid_t audio::play( const std::shared_ptr& stream, - glm::vec3 position, + worldvec3 position, bool relative, float volume, float pitch, @@ -451,7 +451,7 @@ speakerid_t audio::play( speakerid_t audio::play_stream( const io::path& file, - glm::vec3 position, + worldvec3 position, bool relative, float volume, float pitch, diff --git a/src/audio/audio.hpp b/src/audio/audio.hpp index 9af27dde1..a9c035274 100644 --- a/src/audio/audio.hpp +++ b/src/audio/audio.hpp @@ -331,11 +331,11 @@ namespace audio { /// @brief Set speaker 3D position in the world /// @param pos new position - virtual void setPosition(glm::vec3 pos) = 0; + virtual void setPosition(worldvec3 pos) = 0; /// @brief Get speaker 3D position in the world /// @return position - virtual glm::vec3 getPosition() const = 0; + virtual worldvec3 getPosition() const = 0; /// @brief Set speaker movement velocity used for Doppler effect /// @param vel velocity vector @@ -391,7 +391,7 @@ namespace audio { uint bitsPerSample ) = 0; virtual void setListener( - glm::vec3 position, + worldvec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up @@ -482,7 +482,7 @@ namespace audio { /// @param lookAt point the listener look at /// @param up camera up vector void set_listener( - glm::vec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up + worldvec3 position, glm::vec3 velocity, glm::vec3 lookAt, glm::vec3 up ); /// @brief Play 3D sound in the world @@ -498,7 +498,7 @@ namespace audio { /// @return speaker id or 0 speakerid_t play( Sound* sound, - glm::vec3 position, + worldvec3 position, bool relative, float volume, float pitch, @@ -518,7 +518,7 @@ namespace audio { /// @return speaker id or 0 speakerid_t play( const std::shared_ptr& stream, - glm::vec3 position, + worldvec3 position, bool relative, float volume, float pitch, @@ -537,7 +537,7 @@ namespace audio { /// @return speaker id or 0 speakerid_t play_stream( const io::path& file, - glm::vec3 position, + worldvec3 position, bool relative, float volume, float pitch, diff --git a/src/frontend/LevelFrontend.cpp b/src/frontend/LevelFrontend.cpp index 2143a4778..b5b640a62 100644 --- a/src/frontend/LevelFrontend.cpp +++ b/src/frontend/LevelFrontend.cpp @@ -74,7 +74,7 @@ LevelFrontend::LevelFrontend( } auto sound = assets.get(material->stepsSound); - glm::vec3 pos {}; + worldvec3 pos {}; auto soundsCamera = currentPlayer.currentCamera.get(); if (currentPlayer.isCurrentCameraBuiltin()) { soundsCamera = currentPlayer.fpCamera.get(); @@ -117,7 +117,7 @@ LevelFrontend::LevelFrontend( } audio::play( sound, - glm::vec3(pos.x, pos.y, pos.z) + 0.5f, + worldvec3(pos.x, pos.y, pos.z) + 0.5, false, 1.0f, 1.0f + (rand() % 6 - 3) * 0.05f, diff --git a/src/logic/scripting/lua/libs/libaudio.cpp b/src/logic/scripting/lua/libs/libaudio.cpp index bda2784e8..ce5d3b307 100644 --- a/src/logic/scripting/lua/libs/libaudio.cpp +++ b/src/logic/scripting/lua/libs/libaudio.cpp @@ -42,9 +42,7 @@ static audio::speakerid_t play_sound( } return audio::play( sound, - glm::vec3( - static_cast(x), static_cast(y), static_cast(z) - ), + worldvec3(x, y, z), relative, volume, pitch, @@ -75,11 +73,7 @@ static audio::speakerid_t play_stream( if (stream) { return audio::play( audio::open_stream(std::move(stream), true), - glm::vec3( - static_cast(x), - static_cast(y), - static_cast(z) - ), + worldvec3(x, y, z), relative, volume, pitch, @@ -96,15 +90,7 @@ static audio::speakerid_t play_stream( file = paths.find(filename); } return audio::play_stream( - file, - glm::vec3( - static_cast(x), static_cast(y), static_cast(z) - ), - relative, - volume, - pitch, - loop, - channel + file, worldvec3(x, y, z), relative, volume, pitch, loop, channel ); } @@ -277,9 +263,7 @@ static int l_audio_set_position(lua::State* L) { auto x = lua::tonumber(L, 2); auto y = lua::tonumber(L, 3); auto z = lua::tonumber(L, 4); - speaker->setPosition(glm::vec3( - static_cast(x), static_cast(y), static_cast(z) - )); + speaker->setPosition(worldvec3(x, y, z)); } return 0; }