feat(audio): integrate OpenAL audio engine with WAV/MP3/FLAC support

This commit is contained in:
2026-07-06 15:56:42 +08:00
parent c098fdbc72
commit d23540ff86
18 changed files with 400 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
#pragma once
#include "Cubed/audio/audio_engine.hpp"
#include "Cubed/gameplay/client_world.hpp"
#include "Cubed/gameplay/network_server.hpp"
#include "Cubed/gameplay/server_world.hpp"
@@ -62,6 +63,8 @@ private:
Window m_window{m_renderer};
AudioEngine m_audio;
inline static double last_time = glfwGetTime();
inline static double current_time = glfwGetTime();
inline static double delta_time = 0.0f;

View File

@@ -0,0 +1,20 @@
#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;
private:
ALuint m_buffer = 0;
void set_data(const AudioData& data);
};
} // namespace Cubed

View 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

View File

@@ -0,0 +1,38 @@
#pragma once
#include "Cubed/audio/audio_source.hpp"
#include "Cubed/audio/sound_manager.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 update(glm::vec3 listener_pos);
private:
using SourcePool = std::unordered_map<std::string, ALuint>;
bool m_init{false};
ALCdevice* device{nullptr};
ALCcontext* context{nullptr};
glm::vec3 listener_pos;
std::unique_ptr<AudioSource> m_bgm;
SoundManager m_sounds;
};
} // namespace Cubed

View File

@@ -0,0 +1,16 @@
#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);
};
} // namespace Cubed

View File

@@ -0,0 +1,26 @@
#pragma once
#include "Cubed/audio/audio_buffer.hpp"
#include <AL/al.h>
namespace Cubed {
enum class AudioState { INITIAL, PLAYING, PAUSED, STOPPED };
class AudioSource {
public:
AudioSource();
~AudioSource();
void set_buffer(const AudioBuffer& buffer);
void set_loop(bool on = true);
void play();
void stop();
void pause();
AudioState state();
private:
ALuint m_source = 0;
};
} // namespace Cubed

View 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();
void load(const std::string& name);
const AudioBuffer& get_buffer(const std::string& name) const;
void init();
void clear();
private:
std::unordered_map<std::string, AudioBuffer> m_buffers;
};
} // namespace Cubed

View File

@@ -1,4 +1,5 @@
#pragma once
#include "Cubed/audio/audio_engine.hpp"
#include "Cubed/gameplay/block.hpp"
#include "Cubed/gameplay/chunk_pos.hpp"
#include "Cubed/gameplay/client_chunk.hpp"
@@ -40,7 +41,7 @@ struct PlayerRenderData {
class ClientWorld {
public:
ClientWorld();
ClientWorld(AudioEngine& auido);
~ClientWorld();
void init(std::string_view player_name,
std::shared_ptr<NetworkClient> client);
@@ -112,6 +113,7 @@ private:
ClientPlayer m_player;
OtherPlayerHashMap m_player_info;
ChunkHashMap m_chunks;
AudioEngine& m_audio;
std::vector<glm::vec4> m_planes;
std::jthread m_client_thread;