Fix "no remove aqua speed" bug when player leaves the water
[ryzomcore.git] / nel / src / sound / sound.cpp
blobc3ac77e23396585251721e5288e6bcdbd27468e5
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Matt RAYKOWSKI (sfb) <matt.raykowski@gmail.com>
6 // Copyright (C) 2012-2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "stdsound.h"
23 #include "nel/sound/sound.h"
24 #include "nel/misc/path.h"
25 #include "nel/sound/sound_bank.h"
27 #include "nel/sound/simple_sound.h"
28 #include "nel/sound/complex_sound.h"
29 #include "nel/sound/background_sound.h"
30 #include "nel/sound/context_sound.h"
31 #include "nel/sound/music_sound.h"
32 #include "nel/sound/stream_sound.h"
33 #include "nel/sound/stream_file_sound.h"
35 #include "nel/sound/group_controller.h"
36 #include "nel/sound/group_controller_root.h"
38 using namespace std;
39 using namespace NLMISC;
42 namespace NLSOUND {
44 CSound *CSound::createSound(const std::string &filename, NLGEORGES::UFormElm& formRoot)
46 CSound *ret = NULL;
47 string soundType;
49 NLGEORGES::UFormElm *psoundType;
51 if (!formRoot.getNodeByName(&psoundType, ".SoundType"))
53 nlwarning("No SoundType in : %s", filename.c_str());
54 return 0;
57 if (psoundType != NULL)
59 std::string dfnName;
60 psoundType->getDfnName(dfnName);
62 if (dfnName == "simple_sound.dfn")
64 ret = new CSimpleSound();
65 ret->importForm(filename, formRoot);
67 else if (dfnName == "complex_sound.dfn")
69 ret = new CComplexSound();
70 ret->importForm(filename, formRoot);
72 else if (dfnName == "background_sound.dfn")
74 ret = new CBackgroundSound();
75 ret->importForm(filename, formRoot);
77 else if (dfnName == "context_sound.dfn")
79 ret = new CContextSound();
80 ret->importForm(filename, formRoot);
82 else if (dfnName == "music_sound.dfn")
84 ret = new CMusicSound();
85 ret->importForm(filename, formRoot);
87 else if (dfnName == "stream_sound.dfn")
89 ret = new CStreamSound();
90 ret->importForm(filename, formRoot);
92 else if (dfnName == "stream_file_sound.dfn")
94 ret = new CStreamFileSound();
95 ret->importForm(filename, formRoot);
97 else
99 nlassertex(false, ("SoundType unsupported: %s", dfnName.c_str()));
104 return ret;
110 * Constructor
112 CSound::CSound() :
113 _Gain(1.0f),
114 _Pitch(1.0f),
115 _Priority(MidPri),
116 _ConeInnerAngle(6.283185f),
117 _ConeOuterAngle(6.283185f),
118 _ConeOuterGain( 1.0f ),
119 _Looping(false),
120 _MinDist(1.0f),
121 _MaxDist(1000000.0f),
122 _UserVarControler(CStringMapper::emptyId()),
123 _GroupController(NULL)
127 CSound::~CSound()
131 void CSound::serial(NLMISC::IStream &s)
133 s.serial(_Gain);
134 s.serial(_Pitch);
135 s.serialEnum(_Priority);
136 s.serial(_ConeInnerAngle, _ConeOuterAngle, _ConeOuterGain);
137 s.serial(_Direction);
138 s.serial(_Looping);
139 s.serial(_MaxDist);
140 if (s.isReading())
142 std::string name;
143 s.serial(name);
144 _Name = CStringMapper::map(name);
146 else
148 std::string name = CStringMapper::unmap(_Name);
149 s.serial(name);
152 nlassert(CGroupControllerRoot::isInitialized()); // not sure
153 #if NLSOUND_SHEET_VERSION_BUILT < 2
154 if (s.isReading()) _GroupController = CGroupControllerRoot::getInstance()->getGroupController(NLSOUND_SHEET_V1_DEFAULT_SOUND_GROUP_CONTROLLER);
155 #else
156 if (s.isReading())
158 std::string groupControllerPath;
159 s.serial(groupControllerPath);
160 _GroupController = CGroupControllerRoot::getInstance()->getGroupController(groupControllerPath);
162 else
164 std::string groupControllerPath = _GroupController->getPath();
165 s.serial(groupControllerPath);
167 #endif
172 * Load the sound parameters from georges' form
174 void CSound::importForm(const std::string& filename, NLGEORGES::UFormElm& root)
176 // Name
177 _Name = CStringMapper::map(CFile::getFilenameWithoutExtension(filename));
179 // InternalConeAngle
180 uint32 inner;
181 root.getValueByName(inner, ".InternalConeAngle");
182 if (inner > 360)
184 inner = 360;
186 _ConeInnerAngle = (float) (Pi * inner / 180.0f); // convert to radians
188 // ExternalConeAngle
189 uint32 outer;
190 root.getValueByName(outer, ".ExternalConeAngle");
191 if (outer > 360)
193 outer = 360;
195 _ConeOuterAngle= (float) (Pi * outer / 180.0f); // convert to radians
197 // Loop
198 root.getValueByName(_Looping, ".Loop");
200 // Gain
201 sint32 gain;
202 root.getValueByName(gain, ".Gain");
203 if (gain > 0)
205 gain = 0;
207 if (gain < -100)
209 gain = -100;
211 _Gain = (float) pow(10.0, gain / 20.0); // convert dB to linear gain
213 // External gain
214 root.getValueByName(gain, ".ExternalGain");
215 if (gain > 0)
217 gain = 0;
219 if (gain < -100)
221 gain = -100;
223 _ConeOuterGain = (float) pow(10.0, gain / 20.0); // convert dB to linear gain
225 // Direction
226 float x, y, z;
228 root.getValueByName(x, ".Direction.X");
229 root.getValueByName(y, ".Direction.Y");
230 root.getValueByName(z, ".Direction.Z");
231 _Direction = CVector(x, y, z);
234 // Pitch
235 sint32 trans;
236 root.getValueByName(trans, ".Transpose");
237 _Pitch = (float) pow(Sqrt12_2, trans); // convert semi-tones to playback speed
239 // Priority
240 uint32 prio = 0;
241 root.getValueByName(prio, ".Priority");
242 switch (prio)
244 case 0:
245 _Priority = LowPri;
246 break;
247 case 1:
248 _Priority = MidPri;
249 break;
250 case 2:
251 _Priority = HighPri;
252 break;
253 case 3:
254 _Priority = HighestPri;
255 break;
256 default:
257 _Priority = MidPri;
260 nlassert(CGroupControllerRoot::isInitialized()); // not sure
261 #if NLSOUND_SHEET_VERSION_BUILT < 2
262 _GroupController = CGroupControllerRoot::getInstance()->getGroupController(NLSOUND_SHEET_V1_DEFAULT_SOUND_GROUP_CONTROLLER);
263 #else
264 std::string groupControllerPath;
265 root.getValueByName(groupControllerPath, ".GroupControllerPath");
266 _GroupController = CGroupControllerRoot::getInstance()->getGroupController(groupControllerPath);
267 #endif
273 } // NLSOUND