mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat: sound (#28)
* build: add OpenAL dependency * feat(deps): add dr_libs audio library * feat(audio): integrate OpenAL audio engine with WAV/MP3/FLAC support * feat: add bgm001 * feat(audio): add audio fade in/out and fix delta time naming * fix(audio-source): improve state, volume, and const correctness * feat(audio): add 3D audio playback with source pool and auto-loading * feat: add ogg loader * feat(audio): add footstep sounds and fix audio listener orientation * feat(sound): add block place and break sounds * feat(audio): add per-channel volume control and config reload * fix(audio): set distance model, reduce max distance, fix sound pos - Set OpenAL distance model to AL_INVERSE_DISTANCE_CLAMPED in engine. - Reduced AL_MAX_DISTANCE from 100 to 48 for better falloff. - Fixed block sound position to use world coordinates instead of local block coordinates. * refactor(game_time): introduce Timer for time-based updates and rename existing to TickTimer Add a new `Timer` class that operates in seconds (using `TimeType`) alongside the existing `TickTimer` which uses `TickType`. Rename the original `Timer` to `TickTimer` throughout the codebase. Update `ClientWorld` to maintain separate maps for tick-based and time-based timers, and replace the hardcoded footstep timer in `ClientPlayer` with a time-based callback timer. Also add `play_2d` to `AudioEngine` and load an ambient bird sound to demonstrate the new timer functionality. * feat(gameplay): add ocean wave ambient sounds with random selection Add four ocean wave FLAC sound files and play one randomly when player is within 10 blocks of sea level and in an ocean biome. Include a Random member initialized from ChunkGenerator seed for consistent randomization. * feat(gameplay): detect underwater state and play transition/bubble sounds - Add is_underwater() and set_underwater() to ClientPlayer. - Change get_world() to return non-const reference for audio access. - Update Camera::update_move_camera() to detect water block changes and trigger transition sound. - Add repeating timer in ClientWorld to play random bubble ambient sounds while underwater. - Include new ambient sound assets for water transitions and bubbles. - Also add sand walk sound asset (unused in this commit). * feat(assets): add and update block break/place/walk sounds Add sounds for dirt, grass_block, leaf, log, sand, snowy_grass_block, stone. Update existing leaf/walk, sand/place, sand/walk, and stone/place sounds. * feat(gameplay): add walking sound for players and adjust underwater bubble interval - Move walk/run sound interval constants from anonymous namespace to ClientPlayer header - Add moving_time field to PlayerInfo to track continuous movement - Implement play_walk_sound callback triggered by WALK and RUN gaits - Increase underwater bubble timer from 1.0s to 1.5s * feat(asset): add grass block break and place sound effects * feat(renderer): add --no-debug flag to disable OpenGL debug output * feat(audio): add day/night-based BGM switching * feat(audio): add underwater low-pass filter effect * feat(audio): add pitch control to audio sources Implement set_pitch method that clamps pitch between 0.0 and 1.0 and applies it via AL_PITCH. Includes minor whitespace cleanup in audio_engine.cpp. * feat(audio): implement reverb effect and integrate into underwater system * asset(sound): update dirt and grass block break/place sounds * feat(network): synchronize player water sound * fix: include <numbers> * fix(audio): play ambient water bubble sound in 3D at player position
This commit is contained in:
24
include/Cubed/audio/audio_buffer.hpp
Normal file
24
include/Cubed/audio/audio_buffer.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_data.hpp"
|
||||
|
||||
#include <AL/al.h>
|
||||
namespace Cubed {
|
||||
class AudioBuffer {
|
||||
public:
|
||||
AudioBuffer(const AudioData& data);
|
||||
AudioBuffer(const AudioBuffer&) = delete;
|
||||
AudioBuffer(AudioBuffer&&) = delete;
|
||||
AudioBuffer& operator=(const AudioBuffer&) = delete;
|
||||
AudioBuffer& operator=(AudioBuffer&&) = delete;
|
||||
~AudioBuffer();
|
||||
ALuint buffer() const;
|
||||
float duration() const;
|
||||
uint32_t channels() const;
|
||||
|
||||
private:
|
||||
ALuint m_buffer = 0;
|
||||
float m_duration = 0.0f;
|
||||
uint32_t m_channels = 0;
|
||||
void set_data(const AudioData& data);
|
||||
};
|
||||
} // namespace Cubed
|
||||
10
include/Cubed/audio/audio_data.hpp
Normal file
10
include/Cubed/audio/audio_data.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
struct AudioData {
|
||||
std::vector<int16_t> pcm;
|
||||
uint32_t channels;
|
||||
uint32_t sample_rate;
|
||||
};
|
||||
} // namespace Cubed
|
||||
19
include/Cubed/audio/audio_effect.hpp
Normal file
19
include/Cubed/audio/audio_effect.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <AL/al.h>
|
||||
namespace Cubed {
|
||||
class AudioEffect {
|
||||
public:
|
||||
AudioEffect();
|
||||
AudioEffect(const AudioEffect&) = delete;
|
||||
AudioEffect(AudioEffect&&) = delete;
|
||||
AudioEffect& operator=(const AudioEffect&) = default;
|
||||
AudioEffect& operator=(AudioEffect&&) = delete;
|
||||
~AudioEffect();
|
||||
void set_reverb(float decay_time, float reverb_gain, float gain_hf,
|
||||
float density = 1.0f, float diffusion = 1.0f);
|
||||
ALuint effect() const;
|
||||
|
||||
private:
|
||||
ALuint m_effect = 0;
|
||||
};
|
||||
} // namespace Cubed
|
||||
18
include/Cubed/audio/audio_effect_slot.hpp
Normal file
18
include/Cubed/audio/audio_effect_slot.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_effect.hpp"
|
||||
namespace Cubed {
|
||||
class AudioEffectSlot {
|
||||
public:
|
||||
AudioEffectSlot();
|
||||
AudioEffectSlot(const AudioEffectSlot&) = delete;
|
||||
AudioEffectSlot(AudioEffectSlot&&) = delete;
|
||||
AudioEffectSlot& operator=(const AudioEffectSlot&) = delete;
|
||||
AudioEffectSlot& operator=(AudioEffectSlot&&) = delete;
|
||||
~AudioEffectSlot();
|
||||
void set_effect(const AudioEffect& effect);
|
||||
ALuint slot() const;
|
||||
|
||||
private:
|
||||
ALuint m_slot = 0;
|
||||
};
|
||||
} // namespace Cubed
|
||||
60
include/Cubed/audio/audio_engine.hpp
Normal file
60
include/Cubed/audio/audio_engine.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/audio/audio_fade.hpp"
|
||||
#include "Cubed/audio/audio_source.hpp"
|
||||
#include "Cubed/audio/sound_manager.hpp"
|
||||
#include "Cubed/audio/source_pool.hpp"
|
||||
|
||||
#include <AL/al.h>
|
||||
#include <AL/alc.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
namespace Cubed {
|
||||
|
||||
class ClientWorld;
|
||||
|
||||
class AudioEngine {
|
||||
public:
|
||||
AudioEngine();
|
||||
AudioEngine(const AudioEngine&) = delete;
|
||||
AudioEngine(AudioEngine&&) = delete;
|
||||
AudioEngine& operator=(const AudioEngine&) = delete;
|
||||
AudioEngine& operator=(AudioEngine&&) = delete;
|
||||
~AudioEngine();
|
||||
|
||||
void init();
|
||||
void play_bgm();
|
||||
void change_bgm(const std::string& sound);
|
||||
void play_3d(const std::string& sound, const glm::vec3& pos,
|
||||
bool check = false);
|
||||
void play_2d(const std::string& sound, bool check = false);
|
||||
void update_listener(const glm::vec3& pos, const glm::vec3& forward,
|
||||
const glm::vec3& up);
|
||||
void update();
|
||||
void reload_config();
|
||||
|
||||
void underwater_change(bool underwater);
|
||||
|
||||
float& bgm_target_volume();
|
||||
|
||||
private:
|
||||
using FadeMap = std::unordered_map<std::string, AudioFade>;
|
||||
bool m_init{false};
|
||||
ALCdevice* device{nullptr};
|
||||
ALCcontext* context{nullptr};
|
||||
glm::vec3 listener_pos;
|
||||
std::unique_ptr<AudioSource> m_bgm;
|
||||
FadeMap m_fade_map;
|
||||
SoundManager m_sounds;
|
||||
std::shared_ptr<SourcePool> m_pool;
|
||||
bool m_efx_supported = false;
|
||||
bool m_underwater = false;
|
||||
float m_music_volume = 1.0f;
|
||||
float m_sfx_volume = 1.0f;
|
||||
std::unique_ptr<AudioFilter> m_low_pass_filter;
|
||||
std::unique_ptr<AudioEffect> m_underwater_effect;
|
||||
std::unique_ptr<AudioEffectSlot> m_underwater_slot;
|
||||
};
|
||||
} // namespace Cubed
|
||||
15
include/Cubed/audio/audio_error.hpp
Normal file
15
include/Cubed/audio/audio_error.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <AL/al.h>
|
||||
namespace Cubed {
|
||||
inline void check_al_error(
|
||||
std::source_location loaction = std::source_location::current()) {
|
||||
ALenum error = alGetError();
|
||||
if (error != AL_NO_ERROR) {
|
||||
Logger::error("File {} Line {} Function {} OpenAL Error {} ",
|
||||
loaction.file_name(), loaction.line(),
|
||||
loaction.function_name(), alGetString(error));
|
||||
}
|
||||
}
|
||||
} // namespace Cubed
|
||||
19
include/Cubed/audio/audio_fade.hpp
Normal file
19
include/Cubed/audio/audio_fade.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_source.hpp"
|
||||
namespace Cubed {
|
||||
class AudioFade {
|
||||
public:
|
||||
AudioFade(AudioSource* source, float fade_in = 0.0f, float fade_out = 0.0f);
|
||||
~AudioFade();
|
||||
void update();
|
||||
void reset();
|
||||
|
||||
private:
|
||||
AudioSource* m_source = nullptr;
|
||||
float m_in_duration = 0.0f;
|
||||
float m_out_duration = 0.0f;
|
||||
float m_start_gain = 0.0f;
|
||||
bool m_fade_in = true;
|
||||
bool m_active = true;
|
||||
};
|
||||
} // namespace Cubed
|
||||
19
include/Cubed/audio/audio_filter.hpp
Normal file
19
include/Cubed/audio/audio_filter.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <AL/al.h>
|
||||
namespace Cubed {
|
||||
class AudioFilter {
|
||||
public:
|
||||
AudioFilter();
|
||||
AudioFilter(const AudioFilter&) = delete;
|
||||
AudioFilter(AudioFilter&&) = delete;
|
||||
AudioFilter& operator=(const AudioFilter&) = delete;
|
||||
AudioFilter& operator=(AudioFilter&&) = delete;
|
||||
~AudioFilter();
|
||||
|
||||
void set_lowpass(float gain, float gain_hf);
|
||||
ALuint filter() const;
|
||||
|
||||
private:
|
||||
ALuint m_filter = 0;
|
||||
};
|
||||
} // namespace Cubed
|
||||
17
include/Cubed/audio/audio_loader.hpp
Normal file
17
include/Cubed/audio/audio_loader.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_data.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
namespace Cubed {
|
||||
|
||||
class AudioLoader {
|
||||
public:
|
||||
static AudioData load(const std::filesystem::path& path);
|
||||
|
||||
private:
|
||||
static AudioData load_wav(const std::filesystem::path& path);
|
||||
static AudioData load_mp3(const std::filesystem::path& path);
|
||||
static AudioData load_flac(const std::filesystem::path& path);
|
||||
static AudioData load_ogg(const std::filesystem::path& path);
|
||||
};
|
||||
} // namespace Cubed
|
||||
50
include/Cubed/audio/audio_source.hpp
Normal file
50
include/Cubed/audio/audio_source.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_buffer.hpp"
|
||||
#include "Cubed/audio/audio_effect_slot.hpp"
|
||||
#include "Cubed/audio/audio_filter.hpp"
|
||||
|
||||
#include <AL/al.h>
|
||||
#include <glm/glm.hpp>
|
||||
namespace Cubed {
|
||||
|
||||
enum class AudioState { INITIAL, PLAYING, PAUSED, STOPPED };
|
||||
|
||||
class AudioSource {
|
||||
public:
|
||||
AudioSource(float volume = 1.0f);
|
||||
~AudioSource();
|
||||
|
||||
void set_buffer_2d(const AudioBuffer& buffer);
|
||||
void set_buffer_3d(const AudioBuffer& buffer, const glm::vec3& vec3);
|
||||
void set_loop(bool on = true);
|
||||
void set_volume(float volume);
|
||||
void set_pitch(float pitch);
|
||||
void play();
|
||||
void play_2d(const AudioBuffer& buffer);
|
||||
void play_3d(const AudioBuffer& buffer, const glm::vec3& pos);
|
||||
|
||||
void stop();
|
||||
void pause();
|
||||
float duration() const;
|
||||
float current_time() const;
|
||||
float target_volume() const;
|
||||
float& target_volume();
|
||||
void set_target_volume(float volume);
|
||||
AudioState state() const;
|
||||
|
||||
void mark_in_use();
|
||||
bool in_use() const;
|
||||
void reset();
|
||||
|
||||
void set_filter(const AudioFilter& filter);
|
||||
void clear_filter();
|
||||
void set_effect_slot(const AudioEffectSlot& slot);
|
||||
void clear_effect_slot();
|
||||
|
||||
private:
|
||||
ALuint m_source = 0;
|
||||
float m_target_volume = 1.0f;
|
||||
float m_duration = 0.0f;
|
||||
bool m_using;
|
||||
};
|
||||
} // namespace Cubed
|
||||
19
include/Cubed/audio/sound_manager.hpp
Normal file
19
include/Cubed/audio/sound_manager.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_buffer.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
namespace Cubed {
|
||||
class SoundManager {
|
||||
public:
|
||||
SoundManager();
|
||||
~SoundManager();
|
||||
const AudioBuffer& load(const std::string& name);
|
||||
const AudioBuffer& get_buffer(const std::string& name);
|
||||
void init();
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, AudioBuffer> m_buffers;
|
||||
};
|
||||
} // namespace Cubed
|
||||
23
include/Cubed/audio/source_pool.hpp
Normal file
23
include/Cubed/audio/source_pool.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_source.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
class SourcePool {
|
||||
private:
|
||||
std::vector<AudioSource> m_sources;
|
||||
|
||||
public:
|
||||
explicit SourcePool(size_t size);
|
||||
~SourcePool();
|
||||
SourcePool(const SourcePool&) = delete;
|
||||
SourcePool(SourcePool&&) = delete;
|
||||
SourcePool& operator=(const SourcePool&) = delete;
|
||||
SourcePool& operator=(SourcePool&&) = delete;
|
||||
|
||||
void update();
|
||||
AudioSource* acquire();
|
||||
|
||||
std::vector<AudioSource>& sources();
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user