1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "sound_system.h"
23 #include "nel/sound/u_audio_mixer.h"
24 #include "nel/sound/u_listener.h"
25 #include "nel/sound/sound_anim_manager.h"
26 #include "nel/sound/sound_animation.h"
27 #include "nel/misc/path.h"
28 #include "edit_ps_sound.h"
31 using namespace NLMISC
;
32 using namespace NLSOUND
;
35 UAudioMixer
*CSoundSystem::_AudioMixer
= NULL
;
36 //set<string> CSoundSystem::_SoundBanksFileName;
37 set
<string
> CSoundSystem::_SampleBanksFileName
;
38 CSoundAnimManager
*CSoundSystem::_AnimManager
= NULL
;
39 //TSoundAnimId CSoundSystem::_CurrentAnimation = CSoundAnimation::NoId;
40 //sint32 CSoundSystem::_CurrentPlayback = -1;
41 CVector
CSoundSystem::_Zero
= CVector::Null
;
42 string
CSoundSystem::_SamplePath
;
43 string
CSoundSystem::_PackedSheetPath
;
44 //sint CSoundSystem::_AnimIndex = -1;
46 void CSoundSystem::setListenerMatrix(const NLMISC::CMatrix
&m
)
50 static CMatrix oldMatrix
;
51 if(m
.getPos() != oldMatrix
.getPos() || m
.getJ() != oldMatrix
.getJ() || m
.getK() != oldMatrix
.getK())
53 UListener
*l
= _AudioMixer
->getListener();
54 l
->setPos(m
.getPos());
55 l
->setOrientation(m
.getJ(), m
.getK());
62 void CSoundSystem::initSoundSystem ()
66 _AudioMixer
= NLSOUND::UAudioMixer::createAudioMixer();
69 _AudioMixer
->setSamplePath(_SamplePath
);
70 _AudioMixer
->setPackedSheetOption(_PackedSheetPath
, true);
71 _AudioMixer
->init(32, true, false, NULL
, true);
73 catch(const NLMISC::Exception
&e
)
75 // in case of exeption during mixer init, the mixer is destroyed !
76 string mess
= string("Unable to init sound :") + e
.what();
77 nlwarning ("Init sound: %s", mess
.c_str());
82 // ok for the mixer, now create the sound anim manager
85 // TODO : boris : Hum, as far as I know, throwing exeption in constructor is a very BAD idea...
86 _AnimManager
= new CSoundAnimManager(_AudioMixer
);
88 catch (const NLMISC::Exception
&e
)
90 string mess
= string("Unable to init sound :") + e
.what();
91 nlwarning ("Init sound: %s", mess
.c_str());
103 setPSSoundSystem(_AudioMixer
);
106 for (set<string>::const_iterator it1 = _SampleBanksFileName.begin();
107 it1 != _SampleBanksFileName.end();
112 //_AudioMixer->loadSampleBank(NLMISC::CPath::lookup(*it).c_str());
113 _AudioMixer->loadSampleBank(false, (*it1));
115 catch (const NLMISC::Exception &e)
117 string mess = "Unable to load sound file :" + *it1
119 nlwarning ("Init sound: %s", mess.c_str());
123 /* for (set<string>::const_iterator it2 = _SoundBanksFileName.begin();
124 it2 != _SoundBanksFileName.end();
129 //_AudioMixer->loadSoundBank(NLMISC::CPath::lookup(*it).c_str());
130 _AudioMixer->loadSoundBank((*it2).c_str());
132 catch (const NLMISC::Exception &e)
134 string mess = "Unable to load sound file :" + *it2
136 nlwarning ("Init sound: %s", mess.c_str());
144 void CSoundSystem::poll()
148 _AudioMixer
->update();
154 void CSoundSystem::releaseSoundSystem(void)
156 setPSSoundSystem(NULL
);
170 void CSoundSystem::play(const string
&soundName
)
174 NLSOUND::USource
*src
= _AudioMixer
->createSource(CStringMapper::map(soundName
), true);
177 src
->setLooping(false);
178 const CVector
&pos
= _AudioMixer
->getListener()->getPos();
184 MessageBox(NULL
, _T("Can't play the sound (perhaps it's contextual sound)"), _T("warning"), MB_OK
|MB_ICONWARNING
);
189 USource
*CSoundSystem::create(const std::string
&soundName
)
193 NLSOUND::USource
*src
= _AudioMixer
->createSource(CStringMapper::map(soundName
), false);
196 src
->setLooping(false);
197 const CVector
&pos
= _AudioMixer
->getListener()->getPos();
204 MessageBox(NULL
, _T("Can't play the sound (perhaps it's contextual sound)"), _T("warning"), MB_OK
|MB_ICONWARNING
);
211 void CSoundSystem::playAnimation(string
& name
, float lastTime
, float curTime
, CSoundContext
&context
)
213 if (_AnimManager
== NULL
)
218 TSoundAnimId id
= _AnimManager
->getAnimationFromName(name
);
220 if (id
!= CSoundAnimationNoId
)
222 _AnimManager
->playAnimation(id
, lastTime
, curTime
, NULL
, context
);