369 lines
7.8 KiB
C++
369 lines
7.8 KiB
C++
// Platform implementation of MilesLib/SoundManager.h for port/headless execution.
|
|
#include "MilesLib/Stdafx.h"
|
|
#include "MilesLib/SoundManager.h"
|
|
#include "AudioCommands.h"
|
|
#include <cmath>
|
|
#include <algorithm>
|
|
|
|
decltype(CSoundManager::ms_SoundManager2D) CSoundManager::ms_SoundManager2D{};
|
|
decltype(CSoundManager::ms_SoundManager3D) CSoundManager::ms_SoundManager3D{};
|
|
decltype(CSoundManager::ms_SoundManagerStream) CSoundManager::ms_SoundManagerStream{};
|
|
|
|
CSoundManager::CSoundManager()
|
|
{
|
|
m_bInitialized = FALSE;
|
|
m_isSoundDisable = FALSE;
|
|
|
|
m_fxPosition = 0.0f;
|
|
m_fyPosition = 0.0f;
|
|
m_fzPosition = 0.0f;
|
|
|
|
m_fSoundScale = 200.0f;
|
|
m_fAmbienceSoundScale = 1000.0f;
|
|
m_fSoundVolume = 1.0f;
|
|
m_fMusicVolume = 1.0f;
|
|
|
|
m_fBackupMusicVolume = 0.0f;
|
|
m_fBackupSoundVolume = 0.0f;
|
|
|
|
for (int i = 0; i < CSoundManagerStream::MUSIC_INSTANCE_MAX_NUM; ++i)
|
|
{
|
|
TMusicInstance & rInstance = m_MusicInstances[i];
|
|
rInstance.dwMusicFileNameCRC = 0;
|
|
rInstance.MusicState = MUSIC_STATE_OFF;
|
|
rInstance.fVolume = 0.0f;
|
|
rInstance.fLimitVolume = 0.0f;
|
|
rInstance.fVolumeSpeed = 0.0f;
|
|
}
|
|
}
|
|
|
|
CSoundManager::~CSoundManager()
|
|
{
|
|
}
|
|
|
|
BOOL CSoundManager::Create()
|
|
{
|
|
m_bInitialized = TRUE;
|
|
return TRUE;
|
|
}
|
|
|
|
void CSoundManager::Destroy()
|
|
{
|
|
m_PlaySoundHistoryMap.clear();
|
|
m_bInitialized = FALSE;
|
|
}
|
|
|
|
void CSoundManager::SetPosition(float fx, float fy, float fz)
|
|
{
|
|
m_fxPosition = fx;
|
|
m_fyPosition = fy;
|
|
m_fzPosition = fz;
|
|
}
|
|
|
|
void CSoundManager::SetDirection(float, float, float, float, float, float)
|
|
{
|
|
}
|
|
|
|
void CSoundManager::Update()
|
|
{
|
|
}
|
|
|
|
float CSoundManager::GetSoundScale()
|
|
{
|
|
return m_fSoundScale;
|
|
}
|
|
|
|
void CSoundManager::SetSoundScale(float fScale)
|
|
{
|
|
m_fSoundScale = fScale;
|
|
}
|
|
|
|
void CSoundManager::SetAmbienceSoundScale(float fScale)
|
|
{
|
|
m_fAmbienceSoundScale = fScale;
|
|
}
|
|
|
|
float CSoundManager::__ConvertRatioVolumeToApplyVolume(float fRatioVolume)
|
|
{
|
|
if (0.1f > fRatioVolume)
|
|
return fRatioVolume;
|
|
|
|
return (float)pow(10.0f, (-1.0f + fRatioVolume));
|
|
}
|
|
|
|
float CSoundManager::__ConvertGradeVolumeToApplyVolume(int nGradeVolume)
|
|
{
|
|
return __ConvertRatioVolumeToApplyVolume(nGradeVolume / 5.0f);
|
|
}
|
|
|
|
void CSoundManager::SetSoundVolume(float fVolume)
|
|
{
|
|
if (m_isSoundDisable)
|
|
{
|
|
m_fBackupSoundVolume = fVolume;
|
|
return;
|
|
}
|
|
|
|
if (fVolume < 0.0f) fVolume = 0.0f;
|
|
if (fVolume > 1.0f) fVolume = 1.0f;
|
|
m_fSoundVolume = fVolume;
|
|
|
|
if (!m_isSoundDisable)
|
|
{
|
|
m_fBackupSoundVolume = fVolume;
|
|
}
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::SetSoundVolume;
|
|
cmd.volume = m_fSoundVolume;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::SetSoundVolumeRatio(float fRatio)
|
|
{
|
|
float fVolume = __ConvertRatioVolumeToApplyVolume(fRatio);
|
|
SetSoundVolume(fVolume);
|
|
}
|
|
|
|
void CSoundManager::SetSoundVolumeGrade(int iGrade)
|
|
{
|
|
float fVolume = __ConvertGradeVolumeToApplyVolume(iGrade);
|
|
SetSoundVolume(fVolume);
|
|
}
|
|
|
|
void CSoundManager::__SetMusicVolume(float fVolume)
|
|
{
|
|
if (m_isSoundDisable)
|
|
{
|
|
m_fBackupMusicVolume = fVolume;
|
|
return;
|
|
}
|
|
|
|
if (fVolume < 0.0f) fVolume = 0.0f;
|
|
if (fVolume > 1.0f) fVolume = 1.0f;
|
|
m_fMusicVolume = fVolume;
|
|
|
|
if (!m_isSoundDisable)
|
|
{
|
|
m_fBackupMusicVolume = fVolume;
|
|
}
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::SetMusicVolume;
|
|
cmd.volume = m_fMusicVolume;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::SetMusicVolume(float fVolume)
|
|
{
|
|
__SetMusicVolume(fVolume);
|
|
}
|
|
|
|
void CSoundManager::SetMusicVolumeRatio(float fRatio)
|
|
{
|
|
float fVolume = __ConvertRatioVolumeToApplyVolume(fRatio);
|
|
SetMusicVolume(fVolume);
|
|
}
|
|
|
|
void CSoundManager::SetMusicVolumeGrade(int iGrade)
|
|
{
|
|
float fVolume = __ConvertGradeVolumeToApplyVolume(iGrade);
|
|
SetMusicVolume(fVolume);
|
|
}
|
|
|
|
void CSoundManager::SaveVolume()
|
|
{
|
|
if (m_isSoundDisable)
|
|
return;
|
|
const float musicVolume = m_fMusicVolume;
|
|
const float soundVolume = m_fSoundVolume;
|
|
SetMusicVolume(0.0f);
|
|
SetSoundVolume(0.0f);
|
|
m_fBackupMusicVolume = musicVolume;
|
|
m_fBackupSoundVolume = soundVolume;
|
|
m_isSoundDisable = TRUE;
|
|
}
|
|
|
|
void CSoundManager::RestoreVolume()
|
|
{
|
|
m_isSoundDisable = FALSE;
|
|
SetMusicVolume(m_fBackupMusicVolume);
|
|
SetSoundVolume(m_fBackupSoundVolume);
|
|
}
|
|
|
|
float CSoundManager::GetSoundVolume()
|
|
{
|
|
return m_fSoundVolume;
|
|
}
|
|
|
|
float CSoundManager::GetMusicVolume()
|
|
{
|
|
return m_fMusicVolume;
|
|
}
|
|
|
|
void CSoundManager::PlaySound2D(const char * c_szFileName)
|
|
{
|
|
if (m_isSoundDisable || !c_szFileName || !c_szFileName[0])
|
|
return;
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::PlaySound2D;
|
|
cmd.filename = c_szFileName;
|
|
cmd.volume = m_fSoundVolume;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::PlaySound3D(float fx, float fy, float fz, const char * c_szFileName, int iPlayCount)
|
|
{
|
|
if (m_isSoundDisable || !c_szFileName || !c_szFileName[0])
|
|
return;
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::PlaySound3D;
|
|
cmd.filename = c_szFileName;
|
|
cmd.x = fx;
|
|
cmd.y = fy;
|
|
cmd.z = fz;
|
|
cmd.play_count = iPlayCount;
|
|
cmd.volume = m_fSoundVolume;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::StopSound3D(int iIndex)
|
|
{
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::StopSound3D;
|
|
cmd.id = iIndex;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
int CSoundManager::PlayAmbienceSound3D(float fx, float fy, float fz, const char * c_szFileName, int iPlayCount)
|
|
{
|
|
if (m_isSoundDisable || !c_szFileName || !c_szFileName[0])
|
|
return -1;
|
|
static int s_next_id = 1;
|
|
int id = s_next_id++;
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::PlaySound3D;
|
|
cmd.filename = c_szFileName;
|
|
cmd.x = fx;
|
|
cmd.y = fy;
|
|
cmd.z = fz;
|
|
cmd.play_count = iPlayCount;
|
|
cmd.volume = m_fSoundVolume;
|
|
cmd.id = id;
|
|
PushAudioCommand(cmd);
|
|
return id;
|
|
}
|
|
|
|
void CSoundManager::PlayCharacterSound3D(float fx, float fy, float fz, const char * c_szFileName, BOOL)
|
|
{
|
|
PlaySound3D(fx, fy, fz, c_szFileName, 1);
|
|
}
|
|
|
|
void CSoundManager::SetSoundVolume3D(int index, float volume)
|
|
{
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::SetSoundVolume3D;
|
|
cmd.id = index;
|
|
cmd.volume = volume;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::StopAllSound3D()
|
|
{
|
|
m_PlaySoundHistoryMap.clear();
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::StopAllSound3D;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::PlayMusic(const char * c_szFileName)
|
|
{
|
|
if (!c_szFileName || !c_szFileName[0])
|
|
return;
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::PlayMusic;
|
|
cmd.filename = c_szFileName;
|
|
cmd.volume = m_fMusicVolume;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::FadeInMusic(const char * c_szFileName, float speed)
|
|
{
|
|
if (!c_szFileName || !c_szFileName[0])
|
|
return;
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::FadeInMusic;
|
|
cmd.filename = c_szFileName;
|
|
cmd.volume = m_fMusicVolume;
|
|
cmd.speed = speed;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::FadeOutMusic(const char * c_szFileName, float fSpeed)
|
|
{
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::FadeOutMusic;
|
|
cmd.filename = c_szFileName ? c_szFileName : "";
|
|
cmd.speed = fSpeed;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::FadeLimitOutMusic(const char * c_szFileName, float limit, float speed)
|
|
{
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::FadeLimitOutMusic;
|
|
cmd.filename = c_szFileName ? c_szFileName : "";
|
|
cmd.volume = __ConvertRatioVolumeToApplyVolume(limit);
|
|
cmd.speed = speed;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::FadeOutAllMusic()
|
|
{
|
|
AudioCommand cmd;
|
|
cmd.type = AudioCommand::FadeOutAllMusic;
|
|
PushAudioCommand(cmd);
|
|
}
|
|
|
|
void CSoundManager::FadeAll()
|
|
{
|
|
FadeOutAllMusic();
|
|
}
|
|
|
|
void CSoundManager::UpdateSoundData(DWORD, const NSound::TSoundDataVector *)
|
|
{
|
|
}
|
|
|
|
void CSoundManager::UpdateSoundData(float, float, float, DWORD, const NSound::TSoundDataVector *)
|
|
{
|
|
}
|
|
|
|
void CSoundManager::UpdateSoundInstance(float, float, float, DWORD, const NSound::TSoundInstanceVector *, BOOL)
|
|
{
|
|
}
|
|
|
|
void CSoundManager::UpdateSoundInstance(DWORD, const NSound::TSoundInstanceVector *)
|
|
{
|
|
}
|
|
|
|
void CSoundManager::PlayMusic(DWORD, const char *, float, float)
|
|
{
|
|
}
|
|
|
|
void CSoundManager::StopMusic(DWORD)
|
|
{
|
|
}
|
|
|
|
BOOL CSoundManager::GetMusicIndex(const char *, DWORD *)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
BOOL CSoundManager::GetSoundInstance2D(const char *, ISoundInstance **)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
BOOL CSoundManager::GetSoundInstance3D(const char *, ISoundInstance **)
|
|
{
|
|
return FALSE;
|
|
}
|