From 8ac51752d348ee5ee580b7f16b31e546e762934f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 9 Mar 2024 12:15:11 +0300 Subject: [PATCH] audio fixes --- .../base/block_materials/grass_block.json | 5 +++++ res/content/base/block_materials/ground.json | 4 ++-- res/content/base/blocks/flower.json | 1 + res/content/base/blocks/grass.json | 1 + res/content/base/blocks/grass_block.json | 2 +- res/content/base/blocks/leaves.json | 3 ++- src/audio/AL/ALAudio.cpp | 17 ++++++++++++++++- src/audio/AL/alutil.h | 9 ++++++++- src/audio/audio.cpp | 4 ++++ src/audio/audio.h | 3 +++ src/frontend/debug_panel.cpp | 5 +++++ src/logic/PlayerController.cpp | 2 +- 12 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 res/content/base/block_materials/grass_block.json diff --git a/res/content/base/block_materials/grass_block.json b/res/content/base/block_materials/grass_block.json new file mode 100644 index 00000000..d866ff40 --- /dev/null +++ b/res/content/base/block_materials/grass_block.json @@ -0,0 +1,5 @@ +{ + "steps-sound": "steps/grass", + "place-sound": "blocks/ground_place", + "break-sound": "blocks/ground_break" +} diff --git a/res/content/base/block_materials/ground.json b/res/content/base/block_materials/ground.json index af6eba38..36b15aed 100644 --- a/res/content/base/block_materials/ground.json +++ b/res/content/base/block_materials/ground.json @@ -1,5 +1,5 @@ { "steps-sound": "steps/ground", - "place-sound": "steps/ground_place", - "break-sound": "steps/ground_break" + "place-sound": "blocks/ground_place", + "break-sound": "blocks/ground_break" } diff --git a/res/content/base/blocks/flower.json b/res/content/base/blocks/flower.json index 7d2f9c73..db0609f8 100644 --- a/res/content/base/blocks/flower.json +++ b/res/content/base/blocks/flower.json @@ -1,5 +1,6 @@ { "texture": "flower", + "material": "base:grass", "draw-group": 1, "light-passing": true, "obstacle": false, diff --git a/res/content/base/blocks/grass.json b/res/content/base/blocks/grass.json index 844d7baa..8d43b1a7 100644 --- a/res/content/base/blocks/grass.json +++ b/res/content/base/blocks/grass.json @@ -1,5 +1,6 @@ { "texture": "grass", + "material": "base:grass", "draw-group": 1, "light-passing": true, "obstacle": false, diff --git a/res/content/base/blocks/grass_block.json b/res/content/base/blocks/grass_block.json index 4c7ecf7b..c0f6fa48 100644 --- a/res/content/base/blocks/grass_block.json +++ b/res/content/base/blocks/grass_block.json @@ -1,5 +1,5 @@ { - "material": "base:grass", + "material": "base:grass_block", "texture-faces": [ "grass_side", "grass_side", diff --git a/res/content/base/blocks/leaves.json b/res/content/base/blocks/leaves.json index ee0e38de..d8731480 100644 --- a/res/content/base/blocks/leaves.json +++ b/res/content/base/blocks/leaves.json @@ -1,3 +1,4 @@ { - "texture": "leaves" + "texture": "leaves", + "material": "base:grass" } \ No newline at end of file diff --git a/src/audio/AL/ALAudio.cpp b/src/audio/AL/ALAudio.cpp index 72627dae..0b4e76a3 100644 --- a/src/audio/AL/ALAudio.cpp +++ b/src/audio/AL/ALAudio.cpp @@ -38,6 +38,11 @@ ALStream::ALStream(ALAudio* al, std::shared_ptr source, bool keepSour ALStream::~ALStream() { bindSpeaker(0); source = nullptr; + + while (!unusedBuffers.empty()) { + al->freeBuffer(unusedBuffers.front()); + unusedBuffers.pop(); + } } std::shared_ptr ALStream::getSource() const { @@ -135,6 +140,11 @@ void ALStream::update(double delta) { return; } ALSpeaker* alspeaker = dynamic_cast(speaker); + if (alspeaker->stopped) { + speaker = 0; + return; + } + uint alsource = alspeaker->source; unqueueBuffers(alsource); @@ -194,6 +204,8 @@ ALSpeaker::~ALSpeaker() { } void ALSpeaker::update(const Channel* channel, float masterVolume) { + if (source == 0) + return; float gain = this->volume * channel->getVolume()*masterVolume; AL_CHECK(alSourcef(source, AL_GAIN, gain)); @@ -265,7 +277,9 @@ void ALSpeaker::stop() { AL_CHECK(alSourceUnqueueBuffers(source, 1, &buffer)); al->freeBuffer(buffer); } + AL_CHECK(alSourcei(source, AL_BUFFER, 0)); al->freeSource(source); + source = 0; } } @@ -395,8 +409,9 @@ uint ALAudio::getFreeSource(){ } ALuint id; alGenSources(1, &id); - if (!AL_GET_ERROR()) + if (!AL_GET_ERROR()) { return 0; + } allsources.push_back(id); return id; } diff --git a/src/audio/AL/alutil.h b/src/audio/AL/alutil.h index f07dbc46..193fa0d3 100644 --- a/src/audio/AL/alutil.h +++ b/src/audio/AL/alutil.h @@ -17,7 +17,8 @@ #define AL_CHECK(STATEMENT) STATEMENT; AL::check_errors(__FILE__, __LINE__) #define AL_GET_ERROR() AL::check_errors(__FILE__, __LINE__) -namespace AL { +namespace AL { + /// @return true if no errors bool check_errors(const std::string& filename, const std::uint_fast32_t line); /// @brief alGetSourcef wrapper @@ -27,6 +28,8 @@ namespace AL { /// @return field value or default inline float getSourcef(uint source, ALenum field, float def=0.0f) { float value = def; + if (source == 0) + return def; AL_CHECK(alGetSourcef(source, field, &value)); return value; } @@ -38,6 +41,8 @@ namespace AL { /// @return field value or default inline glm::vec3 getSource3f(uint source, ALenum field, glm::vec3 def={}) { glm::vec3 value = def; + if (source == 0) + return def; AL_CHECK(alGetSource3f(source, field, &value.x, &value.y, &value.z)); return value; } @@ -49,6 +54,8 @@ namespace AL { /// @return field value or default inline float getSourcei(uint source, ALenum field, int def=0) { int value = def; + if (source == 0) + return def; AL_CHECK(alGetSourcei(source, field, &value)); return value; } diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 6476aeb0..ebcd4e9c 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -368,6 +368,10 @@ size_t audio::count_speakers() { return speakers.size(); } +size_t audio::count_streams() { + return streams.size(); +} + void audio::update(double delta) { backend->update(delta); diff --git a/src/audio/audio.h b/src/audio/audio.h index 61609b41..63407cb4 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -484,6 +484,9 @@ namespace audio { /// @brief Get alive speakers number (including paused) extern size_t count_speakers(); + /// @brief Get playing streams number (including paused) + extern size_t count_streams(); + /// @brief Update audio streams and sound instanced /// @param delta time elapsed since the last update (seconds) extern void update(double delta); diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index 0fb20427..bb41880b 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -2,6 +2,7 @@ #include #include "gui/controls.h" +#include "../audio/audio.h" #include "../graphics/Mesh.h" #include "../objects/Player.h" #include "../physics/Hitbox.h" @@ -52,6 +53,10 @@ std::shared_ptr create_debug_panel( panel->add(create_label([](){ return L"meshes: " + std::to_wstring(Mesh::meshesCount); })); + panel->add(create_label([](){ + return L"speakers: " + std::to_wstring(audio::count_speakers())+ + L" streams: " + std::to_wstring(audio::count_streams()); + })); panel->add(create_label([=](){ auto& settings = engine->getSettings(); bool culling = settings.graphics.frustumCulling; diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index b7cfdffa..0c0daca4 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -382,7 +382,7 @@ void PlayerController::updateInteraction(){ Block* target = indices->getBlockDef(vox->id); if (lclick && target->breakable){ onBlockInteraction( - glm::ivec3(x, y, z), def, + glm::ivec3(x, y, z), target, BlockInteraction::destruction ); blocksController->breakBlock(player.get(), target, x, y, z);