Files
AVA2/src/eter_sound/SoundManager.h
T

160 lines
5.2 KiB
C++

#pragma once
#include <SDL3/SDL.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>
#include <cstdint>
namespace ava2 {
struct CharacterSoundEvent {
float time = 0.0f;
std::string soundFile;
};
// 1:1 Implementation of Metin2 / MilesLib CSoundManager using SDL3 modern Audio Streams
class SoundManager {
public:
static SoundManager& Instance();
SoundManager();
~SoundManager();
bool Initialize();
void Destroy();
bool IsInitialized() const { return m_initialized; }
bool HasAudioDevice() const { return m_deviceId != 0; }
// Frame update for music fades, 3D stream lifetimes, and looping
void Update(float dt = 0.016f);
// 3D Listener Orientation (Camera / Player)
void SetPosition(float fx, float fy, float fz);
void SetDirection(float fxDir, float fyDir, float fzDir, float fxUp = 0.0f, float fyUp = 1.0f, float fzUp = 0.0f);
void GetPosition(float& fx, float& fy, float& fz) const { fx = m_listenerX; fy = m_listenerY; fz = m_listenerZ; }
// Volume & Scale configuration
void SetSoundVolume(float volume);
void SetSoundVolumeRatio(float ratio);
void SetSoundVolumeGrade(int grade);
float GetSoundVolume() const { return m_soundVolume; }
void SetMusicVolume(float volume);
void SetMusicVolumeRatio(float ratio);
void SetMusicVolumeGrade(int grade);
float GetMusicVolume() const { return m_musicVolume; }
void SetSoundScale(float scale) { m_soundScale = (scale > 0.0f) ? scale : 200.0f; }
float GetSoundScale() const { return m_soundScale; }
void SetAmbienceSoundScale(float scale) { m_ambienceScale = (scale > 0.0f) ? scale : 1000.0f; }
float GetAmbienceSoundScale() const { return m_ambienceScale; }
void SaveVolume();
void RestoreVolume();
// 2D & 3D Sound Effects
void PlaySound2D(const std::string& filename);
void PlaySound3D(float fx, float fy, float fz, const std::string& filename, int playCount = 1);
int PlayAmbienceSound3D(float fx, float fy, float fz, const std::string& filename, int playCount = 1);
void StopSound3D(int index);
void StopAllSound3D();
// Background Music (BGM) Streaming & Fading
void PlayMusic(const std::string& filename);
void FadeInMusic(const std::string& filename, float speed = 0.016f);
void FadeOutMusic(const std::string& filename, float speed = 0.016f);
void FadeLimitOutMusic(const std::string& filename, float limitVolume, float speed = 0.016f);
void FadeOutAllMusic();
std::string GetCurrentMusic() const { return m_currentMusicFile; }
// Character Sound Script (.mss) Parser
bool LoadCharacterSoundScript(const std::string& mssPath, std::vector<CharacterSoundEvent>& outEvents) const;
// Cache & diagnostics
std::string ResolveAudioPath(const std::string& filename) const;
size_t GetCachedSoundCount() const { return m_soundCache.size(); }
size_t GetActiveSoundCount() const;
private:
struct CachedAudio {
SDL_AudioSpec spec;
std::vector<uint8_t> data;
};
enum class MusicState {
Off,
Play,
FadeIn,
FadeOut,
FadeLimitOut
};
struct MusicChannel {
std::string filename;
SDL_AudioStream* stream = nullptr;
MusicState state = MusicState::Off;
float volume = 0.0f;
float targetVolume = 1.0f;
float limitVolume = 0.0f;
float volumeSpeed = 0.016f;
CachedAudio cachedPcm;
size_t playOffsetBytes = 0;
};
struct SoundInstance {
int id = 0;
SDL_AudioStream* stream = nullptr;
std::string filename;
bool is3D = false;
float worldX = 0.0f, worldY = 0.0f, worldZ = 0.0f;
float scale = 200.0f;
float volume = 1.0f;
int playCount = 1;
bool inUse = false;
};
const CachedAudio* GetOrLoadAudio(const std::string& filename);
bool LoadWavFile(const std::string& resolvedPath, CachedAudio& outAudio);
bool LoadMp3File(const std::string& resolvedPath, CachedAudio& outAudio);
float ConvertRatioVolumeToApplyVolume(float ratio) const;
float ConvertGradeVolumeToApplyVolume(int grade) const;
bool m_initialized = false;
SDL_AudioDeviceID m_deviceId = 0;
SDL_AudioSpec m_deviceSpec{};
// Listener
float m_listenerX = 0.0f, m_listenerY = 0.0f, m_listenerZ = 0.0f;
float m_listenerDirX = 0.0f, m_listenerDirY = 0.0f, m_listenerDirZ = 1.0f;
float m_listenerUpX = 0.0f, m_listenerUpY = 1.0f, m_listenerUpZ = 0.0f;
// Volume
float m_soundVolume = 1.0f;
float m_musicVolume = 1.0f;
float m_backupSoundVolume = 1.0f;
float m_backupMusicVolume = 1.0f;
float m_soundScale = 200.0f;
float m_ambienceScale = 1000.0f;
// Cache
std::unordered_map<std::string, CachedAudio> m_soundCache;
// Sound instance pool (32 channels for 2D/3D SFX)
static constexpr size_t MAX_SOUND_INSTANCES = 32;
std::vector<SoundInstance> m_instances;
int m_nextInstanceId = 1;
// Music streaming (up to 2 channels for seamless cross-fading)
MusicChannel m_musicChannels[2];
std::string m_currentMusicFile;
};
// Typedef alias for 1:1 legacy Metin2 code compatibility
using CSoundManager = SoundManager;
} // namespace ava2