Sound support changes

* removed 2d sound
* fixed listener orientation (propably issue #235)
* removed unused code and minor refactoring
dev-ui
erihel 2013-12-19 22:41:16 +01:00
parent 114cbec8a9
commit fe3f9ea38c
8 changed files with 42 additions and 249 deletions

View File

@ -433,7 +433,7 @@ bool CApplication::Create()
m_sound = new CSoundInterface();
#endif
m_sound->Create(true);
m_sound->Create();
if (!m_customDataPath && GetProfile().GetLocalProfileString("Resources", "Sound", path))
{

View File

@ -26,7 +26,6 @@
ALSound::ALSound()
{
m_enabled = false;
m_3D = false;
m_audioVolume = 1.0f;
m_musicVolume = 1.0f;
m_currentMusic = nullptr;
@ -77,7 +76,7 @@ void ALSound::CleanUp()
}
bool ALSound::Create(bool b3D)
bool ALSound::Create()
{
CleanUp();
@ -109,25 +108,6 @@ bool ALSound::Create(bool b3D)
}
void ALSound::SetSound3D(bool bMode)
{
m_3D = bMode;
}
bool ALSound::GetSound3D()
{
return m_3D;
}
bool ALSound::GetSound3DCap()
{
// TODO stub! need to be implemented
return true;
}
bool ALSound::GetEnable()
{
return m_enabled;
@ -168,7 +148,7 @@ int ALSound::GetMusicVolume()
}
bool ALSound::Cache(Sound sound, std::string filename)
bool ALSound::Cache(Sound sound, const std::string &filename)
{
Buffer *buffer = new Buffer();
if (buffer->LoadFromFile(filename, sound))
@ -179,7 +159,7 @@ bool ALSound::Cache(Sound sound, std::string filename)
return false;
}
bool ALSound::CacheMusic(std::string filename)
bool ALSound::CacheMusic(const std::string &filename)
{
if (m_music.find(filename) == m_music.end())
{
@ -337,7 +317,7 @@ int ALSound::Play(Sound sound, float amplitude, float frequency, bool bLoop)
}
int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequency, bool bLoop)
int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float frequency, bool bLoop)
{
if (!m_enabled)
{
@ -364,14 +344,7 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc
}
Position(channel, pos);
if (!m_3D)
{
ComputeVolumePan2D(channel, pos);
}
else
{
m_channels[channel]->SetVolumeAtrib(1.0f);
}
m_channels[channel]->SetVolumeAtrib(1.0f);
// setting initial values
m_channels[channel]->SetStartAmplitude(amplitude);
@ -421,7 +394,7 @@ bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float t
}
bool ALSound::Position(int channel, Math::Vector pos)
bool ALSound::Position(int channel, const Math::Vector &pos)
{
if (!m_enabled)
return false;
@ -431,20 +404,7 @@ bool ALSound::Position(int channel, Math::Vector pos)
return false;
}
if (m_3D)
{
m_channels[channel]->SetPan(pos);
}
else
{
ComputeVolumePan2D(channel, pos);
if (!m_channels[channel]->HasEnvelope())
{
float volume = m_channels[channel]->GetStartAmplitude();
m_channels[channel]->SetVolume(powf(volume * m_channels[channel]->GetVolumeAtrib(), 0.2f) * m_audioVolume);
}
}
m_channels[channel]->SetPosition(pos);
return true;
}
@ -585,38 +545,16 @@ void ALSound::FrameMove(float delta)
}
void ALSound::SetListener(Math::Vector eye, Math::Vector lookat)
void ALSound::SetListener(const Math::Vector &eye, const Math::Vector &lookat)
{
m_eye = eye;
m_lookat = lookat;
if (m_3D)
{
float orientation[] = {lookat.x, lookat.y, lookat.z, 0.f, 1.f, 0.f};
alListener3f(AL_POSITION, eye.x, eye.y, eye.z);
alListenerfv(AL_ORIENTATION, orientation);
}
else
{
float orientation[] = {0.0f, 0.0f, 0.0f, 0.f, 1.f, 0.f};
alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f);
alListenerfv(AL_ORIENTATION, orientation);
Math::Vector forward = lookat - eye;
forward.Normalize();
float orientation[] = {forward.x, forward.y, forward.z, 0.f, -1.0f, 0.0f};
// recalculate sound position
for (auto it : m_channels)
{
if (it.second->IsPlaying())
{
Math::Vector pos = it.second->GetPosition();
ComputeVolumePan2D(it.first, pos);
if (!it.second->HasEnvelope())
{
float volume = it.second->GetStartAmplitude();
it.second->SetVolume(powf(volume * it.second->GetVolumeAtrib(), 0.2f) * m_audioVolume);
}
}
}
}
alListener3f(AL_POSITION, eye.x, eye.y, eye.z);
alListenerfv(AL_ORIENTATION, orientation);
}
bool ALSound::PlayMusic(int rank, bool bRepeat)
@ -626,7 +564,7 @@ bool ALSound::PlayMusic(int rank, bool bRepeat)
return PlayMusic(filename.str(), bRepeat);
}
bool ALSound::PlayMusic(std::string filename, bool bRepeat)
bool ALSound::PlayMusic(const std::string &filename, bool bRepeat)
{
if (!m_enabled)
{
@ -706,66 +644,3 @@ void ALSound::SuspendMusic()
m_currentMusic->Stop();
}
void ALSound::ComputeVolumePan2D(int channel, Math::Vector &pos)
{
float dist, a, g;
m_channels[channel]->SetPosition(pos);
if (VectorsEqual(pos, m_eye))
{
m_channels[channel]->SetVolumeAtrib(1.0f); // maximum volume
m_channels[channel]->SetPan(Math::Vector()); // at the center
return;
}
dist = Distance(pos, m_eye);
if ( dist >= 110.0f ) // very far?
{
m_channels[channel]->SetVolumeAtrib(0.0f); // silence
m_channels[channel]->SetPan(Math::Vector()); // at the center
return;
}
else if ( dist <= 10.0f ) // very close?
{
m_channels[channel]->SetVolumeAtrib(1.0f); // maximum volume
m_channels[channel]->SetPan(Math::Vector()); // at the center
return;
}
m_channels[channel]->SetVolumeAtrib(1.0f - ((dist - 10.0f) / 100.0f));
Math::Vector one = Math::Vector(1.0f, 0.0f, 0.0f);
float angle_a = Angle(Math::Vector(m_lookat.x - m_eye.x, m_lookat.z - m_eye.z, 0.0f), one);
float angle_g = Angle(Math::Vector(pos.x - m_eye.x, pos.z - m_eye.z, 0.0f), one);
a = fmodf(angle_a, Math::PI * 2.0f);
g = fmodf(angle_g, Math::PI * 2.0f);
if ( a < 0.0f )
{
a += Math::PI * 2.0f;
}
if ( g < 0.0f )
{
g += Math::PI * 2.0f;
}
if ( a < g )
{
if (a + Math::PI * 2.0f - g < g - a )
{
a += Math::PI * 2.0f;
}
}
else
{
if ( g + Math::PI * 2.0f - a < a - g )
{
g += Math::PI * 2.0f;
}
}
m_channels[channel]->SetPan( Math::Vector(0.0f, 0.0f, sinf(g - a)) );
}

View File

@ -41,55 +41,43 @@ public:
ALSound();
~ALSound();
bool Create(bool b3D);
bool Cache(Sound, std::string);
bool CacheMusic(std::string);
bool Create();
bool Cache(Sound, const std::string &);
bool CacheMusic(const std::string &);
bool GetEnable();
void SetSound3D(bool bMode);
bool GetSound3D();
bool GetSound3DCap();
void SetAudioVolume(int volume);
int GetAudioVolume();
void SetMusicVolume(int volume);
int GetMusicVolume();
void SetListener(Math::Vector eye, Math::Vector lookat);
void SetListener(const Math::Vector &eye, const Math::Vector &lookat);
void FrameMove(float rTime);
int Play(Sound sound, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
int Play(Sound sound, const Math::Vector &pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
bool FlushEnvelope(int channel);
bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper);
bool Position(int channel, Math::Vector pos);
bool Position(int channel, const Math::Vector &pos);
bool Frequency(int channel, float frequency);
bool Stop(int channel);
bool StopAll();
bool MuteAll(bool bMute);
bool PlayMusic(int rank, bool bRepeat);
bool PlayMusic(std::string filename, bool bRepeat);
bool PlayMusic(const std::string &filename, bool bRepeat);
bool RestartMusic();
void SuspendMusic();
void StopMusic();
bool IsPlayingMusic();
// plugin interface
std::string PluginName();
int PluginVersion();
void InstallPlugin();
bool UninstallPlugin(std::string &);
private:
void CleanUp();
int GetPriority(Sound);
bool SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded);
void ComputeVolumePan2D(int channel, Math::Vector &pos);
bool m_enabled;
bool m_3D;
float m_audioVolume;
float m_musicVolume;
ALCdevice* m_device;

View File

@ -75,7 +75,7 @@ bool Channel::Play()
}
bool Channel::SetPan(Math::Vector pos)
bool Channel::SetPosition(const Math::Vector &pos)
{
if (!m_ready || m_buffer == nullptr)
{
@ -92,18 +92,6 @@ bool Channel::SetPan(Math::Vector pos)
}
void Channel::SetPosition(Math::Vector pos)
{
m_position = pos;
}
Math::Vector Channel::GetPosition()
{
return m_position;
}
bool Channel::SetFrequency(float freq)
{
if (!m_ready || m_buffer == nullptr)

View File

@ -52,9 +52,7 @@ public:
bool Play();
bool Stop();
bool SetPan(Math::Vector);
void SetPosition(Math::Vector);
Math::Vector GetPosition();
bool SetPosition(const Math::Vector &);
bool SetFrequency(float);
float GetFrequency();

View File

@ -36,12 +36,12 @@ CSoundInterface::~CSoundInterface()
{
}
bool CSoundInterface::Create(bool b3D)
bool CSoundInterface::Create()
{
return true;
}
void CSoundInterface::CacheAll(std::string path)
void CSoundInterface::CacheAll(const std::string &path)
{
for ( int i = 1; i < SOUND_MAX; i++ )
{
@ -52,7 +52,7 @@ void CSoundInterface::CacheAll(std::string path)
}
}
void CSoundInterface::AddMusicFiles(std::string path)
void CSoundInterface::AddMusicFiles(const std::string &path)
{
m_soundPath = path;
CacheMusic("Intro1.ogg");
@ -61,12 +61,12 @@ void CSoundInterface::AddMusicFiles(std::string path)
CacheMusic("music011.ogg");
}
bool CSoundInterface::Cache(Sound bSound, std::string bFile)
bool CSoundInterface::Cache(Sound bSound, const std::string &bFile)
{
return true;
}
bool CSoundInterface::CacheMusic(std::string bFile)
bool CSoundInterface::CacheMusic(const std::string &bFile)
{
return true;
}
@ -76,20 +76,6 @@ bool CSoundInterface::GetEnable()
return true;
}
void CSoundInterface::SetSound3D(bool bMode)
{
}
bool CSoundInterface::GetSound3D()
{
return true;
}
bool CSoundInterface::GetSound3DCap()
{
return true;
}
void CSoundInterface::SetAudioVolume(int volume)
{
}
@ -108,7 +94,7 @@ int CSoundInterface::GetMusicVolume()
return 0;
}
void CSoundInterface::SetListener(Math::Vector eye, Math::Vector lookat)
void CSoundInterface::SetListener(const Math::Vector &eye, const Math::Vector &lookat)
{
}
@ -121,7 +107,7 @@ int CSoundInterface::Play(Sound sound, float amplitude, float frequency, bool bL
return 0;
}
int CSoundInterface::Play(Sound sound, Math::Vector pos, float amplitude, float frequency, bool bLoop)
int CSoundInterface::Play(Sound sound, const Math::Vector &pos, float amplitude, float frequency, bool bLoop)
{
return 0;
}
@ -136,7 +122,7 @@ bool CSoundInterface::AddEnvelope(int channel, float amplitude, float frequency,
return true;
}
bool CSoundInterface::Position(int channel, Math::Vector pos)
bool CSoundInterface::Position(int channel, const Math::Vector &pos)
{
return true;
}
@ -166,7 +152,7 @@ bool CSoundInterface::PlayMusic(int rank, bool bRepeat)
return true;
}
bool CSoundInterface::PlayMusic(std::string filename, bool bRepeat)
bool CSoundInterface::PlayMusic(const std::string &filename, bool bRepeat)
{
return true;
}

View File

@ -153,17 +153,16 @@ public:
virtual ~CSoundInterface();
/** Function to initialize sound device
* \param b3D - enable support for 3D sound
*/
virtual bool Create(bool b3D);
virtual bool Create();
/** Function called to cache all sound effect files.
* Function calls \link CSoundInterface::Cache() \endlink for each file
*/
void CacheAll(std::string path);
void CacheAll(const std::string &path);
/** Function called to add all music files to list */
void AddMusicFiles(std::string path);
void AddMusicFiles(const std::string &path);
/** Function called to cache sound effect file.
* This function is called by plugin interface for each file.
@ -171,35 +170,20 @@ public:
* \param bFile - file to load
* \return return true on success
*/
virtual bool Cache(Sound bSound, std::string bFile);
virtual bool Cache(Sound bSound, const std::string &bFile);
/** Function called to cache music file.
* This function is called by CRobotMain for each file used in the mission.
* \param bFile - file to load
* \return return true on success
*/
virtual bool CacheMusic(std::string bFile);
virtual bool CacheMusic(const std::string &bFile);
/** Return if plugin is enabled
* \return return true if plugin is enabled
*/
virtual bool GetEnable();
/** Change sound mode to 2D/3D
* \param bMode - true to enable 3D sound
*/
virtual void SetSound3D(bool bMode);
/** Return if we use 3D sound
* \return true if we have 3D sound enabled
*/
virtual bool GetSound3D();
/** Return if we have 3D sound capable card
* \return true for 3D sound support
*/
virtual bool GetSound3DCap();
/** Change global sound volume
* \param volume - range from 0 to MAXVOLUME
*/
@ -224,7 +208,7 @@ public:
* \param eye - position of listener
* \param lookat - direction listener is looking at
*/
virtual void SetListener(Math::Vector eye, Math::Vector lookat);
virtual void SetListener(const Math::Vector &eye, const Math::Vector &lookat);
/** Update data each frame
* \param rTime - time since last update
@ -248,7 +232,7 @@ public:
* \param bLoop - loop sound
* \return identifier of channel that sound will be played on
*/
virtual int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
virtual int Play(Sound sound, const Math::Vector &pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
/** Remove all operations that would be made on sound in channel.
* \param channel - channel to work on
@ -271,7 +255,7 @@ public:
* \param pos - new positino of a sound
* \return return true on success
*/
virtual bool Position(int channel, Math::Vector pos);
virtual bool Position(int channel, const Math::Vector &pos);
/** Set sound frequency
* \param channel - channel to work on
@ -309,7 +293,7 @@ public:
* \param bRepeat - repeat playing
* \return return true on success
*/
virtual bool PlayMusic(std::string filename, bool bRepeat);
virtual bool PlayMusic(const std::string &filename, bool bRepeat);
/** Restart music
* @return return true on success

View File

@ -1522,13 +1522,6 @@ pos.y -= 0.048f;
pl->SetTextAlign(Gfx::TEXT_ALIGN_LEFT);
// #endif
ddim.x = dim.x*6;
ddim.y = dim.y*0.5f;
pos.x = ox+sx*10;
pos.y = 0.55f;
pc = pw->CreateCheck(pos, ddim, -1, EVENT_INTERFACE_SOUND3D);
pc->SetState(STATE_SHADOW);
ddim.x = dim.x*3;
ddim.y = dim.y*1;
pos.x = ox+sx*10;
@ -2913,12 +2906,6 @@ bool CMainDialog::EventProcess(const Event &event)
ChangeSetupButtons();
break;
case EVENT_INTERFACE_SOUND3D:
m_sound->SetSound3D(!m_sound->GetSound3D());
ChangeSetupButtons();
UpdateSetupButtons();
break;
case EVENT_INTERFACE_SILENT:
m_sound->SetAudioVolume(0);
m_sound->SetMusicVolume(0);
@ -5456,13 +5443,6 @@ void CMainDialog::UpdateSetupButtons()
value = static_cast<float>(m_sound->GetMusicVolume());
ps->SetVisibleValue(value);
}
pc = static_cast<CCheck*>(pw->SearchControl(EVENT_INTERFACE_SOUND3D));
if ( pc != 0 )
{
pc->SetState(STATE_CHECK, m_sound->GetSound3D());
pc->SetState(STATE_ENABLE, m_sound->GetSound3DCap());
}
}
// Updates the engine function of the buttons after the setup phase.
@ -5564,7 +5544,6 @@ void CMainDialog::SetupMemorize()
GetProfile().SetLocalProfileInt("Setup", "TotoMode", m_engine->GetTotoMode());
GetProfile().SetLocalProfileInt("Setup", "AudioVolume", m_sound->GetAudioVolume());
GetProfile().SetLocalProfileInt("Setup", "MusicVolume", m_sound->GetMusicVolume());
GetProfile().SetLocalProfileInt("Setup", "Sound3D", m_sound->GetSound3D());
GetProfile().SetLocalProfileInt("Setup", "EditIndentMode", m_engine->GetEditIndentMode());
GetProfile().SetLocalProfileInt("Setup", "EditIndentValue", m_engine->GetEditIndentValue());
@ -5802,11 +5781,6 @@ void CMainDialog::SetupRecall()
m_sound->SetMusicVolume(iValue);
}
if ( GetProfile().GetLocalProfileInt("Setup", "Sound3D", iValue) )
{
m_sound->SetSound3D(iValue == 1);
}
if ( GetProfile().GetLocalProfileInt("Setup", "EditIndentMode", iValue) )
{
m_engine->SetEditIndentMode(iValue);