test acoustics

This commit is contained in:
MihailRis 2026-01-05 23:46:00 +03:00
parent 3523816378
commit 72aba4d31d
8 changed files with 80 additions and 18 deletions

View file

@ -780,15 +780,18 @@ void ALAudio::setListener(
AL_CHECK(alListenerf(AL_GAIN, get_channel(0)->getVolume()));
}
void ALAudio::update(double delta) {
void ALAudio::update(double) {
}
void ALAudio::setAcoustics(Acoustics acoustics) {
if (!useEffects) {
return;
}
int reverbEffect = effects[REVERB_EFFECT];
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_DECAY_TIME, 1.0f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_ROOM_ROLLOFF_FACTOR, 0.07f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_GAIN, 0.0f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_GAINHF, 0.0f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_REFLECTIONS_GAIN, 0.0f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_DECAY_TIME, std::max<float>(0.1f, acoustics.reverbDecayTime)));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_ROOM_ROLLOFF_FACTOR, 0.14f));
// AL_CHECK(alEffectf(reverbEffect, AL_REVERB_GAIN, 0.0f));
// AL_CHECK(alEffectf(reverbEffect, AL_REVERB_GAINHF, 0.0f));
AL_CHECK(alEffectf(reverbEffect, AL_REVERB_REFLECTIONS_GAIN, 0.5f));
alAuxiliaryEffectSloti(effectSlots[0], AL_EFFECTSLOT_EFFECT, reverbEffect);
}

View file

@ -226,6 +226,8 @@ namespace audio {
void update(double delta) override;
void setAcoustics(Acoustics acoustics) override;
bool isDummy() const override {
return false;
}

View file

@ -104,6 +104,9 @@ namespace audio {
void update(double delta) override {
}
void setAcoustics(Acoustics) override {
}
bool isDummy() const override {
return true;
}

View file

@ -496,6 +496,10 @@ void audio::update(double delta) {
}
}
void audio::set_acoustics(Acoustics acoustics) {
backend->setAcoustics(std::move(acoustics));
}
void audio::reset_channel(int index) {
auto channel = get_channel(index);
if (channel == nullptr) {

View file

@ -7,6 +7,7 @@
#include "typedefs.hpp"
#include "settings.hpp"
#include "io/fwd.hpp"
#include "effects.hpp"
namespace audio {
/// @brief playing speaker uid
@ -394,6 +395,7 @@ namespace audio {
virtual std::vector<std::string> getInputDeviceNames() = 0;
virtual std::vector<std::string> getOutputDeviceNames() = 0;
virtual void update(double delta) = 0;
virtual void setAcoustics(Acoustics acoustics) = 0;
/// @brief Check if backend is an abstraction that does not internally
/// work with actual audio data or play anything
@ -580,6 +582,8 @@ namespace audio {
/// @param delta time elapsed since the last update (seconds)
void update(double delta);
void set_acoustics(Acoustics acoustics);
/// @brief Stop all playing audio in channel, reset channel state
void reset_channel(int channel);

View file

@ -26,4 +26,8 @@ namespace audio {
protected:
const Effect* effect = nullptr;
};
struct Acoustics {
float reverbDecayTime;
};
}

View file

@ -23,9 +23,12 @@
#include "io/io.hpp"
#include "audio/audio.hpp"
#include "maths/util.hpp"
#include "debug/Logger.hpp"
namespace fs = std::filesystem;
static debug::Logger logger("decorator");
/// @brief Not greater than 64 for this BIG_PRIME value
inline constexpr int UPDATE_AREA_DIAMETER = 32;
/// @brief Number of blocks in the volume
@ -70,19 +73,56 @@ Decorator::Decorator(
void Decorator::addParticles(const Block& def, const glm::ivec3& pos) {
const auto& found = blockEmitters.find(pos);
if (found == blockEmitters.end()) {
auto treg = util::get_texture_region(
assets, def.particles->texture, ""
);
blockEmitters[pos] = renderer.particles->add(std::make_unique<Emitter>(
level,
glm::vec3{pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
*def.particles,
treg.texture,
treg.region,
-1
));
if (found != blockEmitters.end()) {
return;
}
auto treg = util::get_texture_region(
assets, def.particles->texture, ""
);
blockEmitters[pos] = renderer.particles->add(std::make_unique<Emitter>(
level,
glm::vec3{pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
*def.particles,
treg.texture,
treg.region,
-1
));
}
void Decorator::updateAcoustics(const Camera& camera) {
audio::Acoustics acoustics {};
util::PseudoRandom random(0x34621B361);
auto& chunks = *player.chunks;
const auto& start = camera.position;
float rayLength = 40.0f;
int rays = 100;
int hit = 0;
float averageDistance = 0.0f;
for (int i = 0; i < rays; i++) {
glm::vec3 dir {
random.randFloat() - 0.5f,
random.randFloat() - 0.5f,
random.randFloat() - 0.5f,
};
dir = glm::normalize(dir);
auto end = chunks.rayCastToObstacle(start, dir, rayLength);
auto distance = glm::distance(start, end);
if (distance >= rayLength * 0.98f) {
continue;
}
hit++;
averageDistance += distance * 0.2f;
}
float decayTime = (averageDistance / hit) * ((hit / static_cast<float>(rays)) - 0.6f) * 1.6f;
decayTime *= 2.5f;
decayTime *= decayTime;
logger.info() << "rays hit " << hit << "/" << rays << " decay-time: " << decayTime;
acoustics.reverbDecayTime = decayTime;
audio::set_acoustics(std::move(acoustics));
}
void Decorator::updateRandom(
@ -283,4 +323,5 @@ void Decorator::update(
}
updateBlockEmitters(camera);
updateTextNotes();
updateAcoustics(camera);
}

View file

@ -48,6 +48,7 @@ class Decorator {
void updateBlockEmitters(const Camera& camera);
void updateTextNotes();
void addParticles(const Block& def, const glm::ivec3& pos);
void updateAcoustics(const Camera& camera);
public:
Decorator(
Engine& engine,