mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(audio): add 3D audio playback with source pool and auto-loading
This commit is contained in:
@@ -13,10 +13,12 @@ public:
|
||||
~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
|
||||
@@ -3,6 +3,7 @@
|
||||
#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>
|
||||
@@ -25,11 +26,11 @@ public:
|
||||
|
||||
void init();
|
||||
void play_bgm();
|
||||
void play_3d(const std::string& sound, const glm::vec3& pos);
|
||||
void update_listener(glm::vec3 listener_pos);
|
||||
void update(float dt);
|
||||
|
||||
private:
|
||||
using SourcePool = std::unordered_map<std::string, ALuint>;
|
||||
using FadeMap = std::unordered_map<std::string, AudioFade>;
|
||||
bool m_init{false};
|
||||
ALCdevice* device{nullptr};
|
||||
@@ -38,5 +39,6 @@ private:
|
||||
std::unique_ptr<AudioSource> m_bgm;
|
||||
FadeMap m_fade_map;
|
||||
SoundManager m_sounds;
|
||||
std::shared_ptr<SourcePool> m_pool;
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Cubed/audio/audio_buffer.hpp"
|
||||
|
||||
#include <AL/al.h>
|
||||
#include <glm/glm.hpp>
|
||||
namespace Cubed {
|
||||
|
||||
enum class AudioState { INITIAL, PLAYING, PAUSED, STOPPED };
|
||||
@@ -12,9 +13,14 @@ public:
|
||||
~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 play();
|
||||
void play_2d(const AudioBuffer& buffer);
|
||||
void play_3d(const AudioBuffer& buffer, const glm::vec3& pos);
|
||||
|
||||
void stop();
|
||||
void pause();
|
||||
float duration() const;
|
||||
@@ -22,9 +28,14 @@ public:
|
||||
float volume() const;
|
||||
AudioState state() const;
|
||||
|
||||
void mark_in_use();
|
||||
bool in_use() const;
|
||||
void reset();
|
||||
|
||||
private:
|
||||
ALuint m_source = 0;
|
||||
float m_volume = 1.0f;
|
||||
float m_duration = 0.0f;
|
||||
bool m_using;
|
||||
};
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -8,8 +8,8 @@ class SoundManager {
|
||||
public:
|
||||
SoundManager();
|
||||
~SoundManager();
|
||||
void load(const std::string& name);
|
||||
const AudioBuffer& get_buffer(const std::string& name) const;
|
||||
const AudioBuffer& load(const std::string& name);
|
||||
const AudioBuffer& get_buffer(const std::string& name);
|
||||
void init();
|
||||
void clear();
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#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();
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user