Fix crash
[ryzomcore.git] / snowballs2 / client / src / sound.cpp
blobbbefd943524a069198cd0c603f65be904fbd9838
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) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
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/>.
21 // Includes
24 #include <nel/misc/types_nl.h>
26 #include <cmath>
27 #include <vector>
29 #include <nel/misc/vector.h>
30 #include <nel/misc/command.h>
32 #include <nel/sound/u_audio_mixer.h>
33 #include <nel/sound/u_listener.h>
34 #include <nel/sound/u_source.h>
36 #include "snowballs_client.h"
37 #include "sound.h"
38 #include "entities.h"
39 #include "configuration.h"
42 // Namespaces
45 using namespace std;
46 using namespace NLMISC;
47 using namespace NLSOUND;
49 namespace SBCLIENT
53 // Variables
54 ///
56 #ifdef SBCLIENT_WITH_SOUND
57 UAudioMixer *AudioMixer = NULL;
58 //TSoundId SoundId;
59 //const vector<TSoundId> *SoundIdArray;
60 static bool SoundEnabled;
61 #endif
64 // Functions
67 #ifdef SBCLIENT_WITH_SOUND
69 void initSound2();
70 void releaseSound2();
72 void cbConfigFileSoundMaxTracks(NLMISC::CConfigFile::CVar &var)
74 if (AudioMixer)
75 AudioMixer->changeMaxTrack(var.asInt());
78 void cbConfigFileSoundEnabled(NLMISC::CConfigFile::CVar &var)
80 if (var.asBool() != SoundEnabled)
82 if (var.asBool())
83 initSound2();
84 else
85 releaseSound2();
89 void cbConfigFileMusicVolume(NLMISC::CConfigFile::CVar &var)
91 if (AudioMixer)
92 AudioMixer->setMusicVolume(var.asFloat());
95 void cbConfigFileFail(NLMISC::CConfigFile::CVar &var)
97 nlwarning("You can't modify the config variable '%s' at runtime for now, please restart the game", var.asString().c_str());
100 void initSound2()
102 AudioMixer = UAudioMixer::createAudioMixer();
103 std::string driverName;
104 NLSOUND::UAudioMixer::TDriver driverType;
105 if (!ConfigFile->exists("SoundDriver"))
106 #ifdef NL_OS_WINDOWS
107 driverType = NLSOUND::UAudioMixer::DriverFMod;
108 #elif defined(NL_OS_UNIX)
109 driverType = NLSOUND::UAudioMixer::DriverOpenAl;
110 #else
111 driverType = NLSOUND::UAudioMixer::DriverAuto;
112 #endif
113 else
115 driverName = ConfigFile->getVar("SoundDriver").asString();
116 if (driverName == "Auto")
117 driverType = NLSOUND::UAudioMixer::DriverAuto;
118 else if (driverName == "FMod")
119 driverType = NLSOUND::UAudioMixer::DriverFMod;
120 else if (driverName == "DSound")
121 driverType = NLSOUND::UAudioMixer::DriverDSound;
122 else if (driverName == "OpenAl")
123 driverType = NLSOUND::UAudioMixer::DriverOpenAl;
124 else
125 nlerror("SoundDriver value '%s' is invalid.", driverName.c_str());
128 AudioMixer->init(
129 ConfigFile->exists("SoundMaxTracks")
130 ? ConfigFile->getVar("SoundMaxTracks").asInt()
131 : 32,
132 ConfigFile->exists("SoundUseEax")
133 ? ConfigFile->getVar("SoundUseEax").asBool()
134 : true,
135 ConfigFile->exists("SoundUseADPCM")
136 ? ConfigFile->getVar("SoundUseADPCM").asBool()
137 : true,
138 NULL, true, driverType,
139 ConfigFile->exists("SoundForceSoftware")
140 ? ConfigFile->getVar("SoundForceSoftware").asBool()
141 : true);
143 ConfigFile->setCallback("SoundMaxTracks", cbConfigFileSoundMaxTracks);
144 ConfigFile->setCallback("SoundUseEax", cbConfigFileFail);
145 ConfigFile->setCallback("SoundUseADPCM", cbConfigFileFail);
146 ConfigFile->setCallback("SoundForceSoftware", cbConfigFileFail);
147 ConfigFile->setCallback("SoundDriver", cbConfigFileFail);
148 CConfiguration::setAndCallback("MusicVolume", cbConfigFileMusicVolume);
150 // PlaylistManager = new SBCLIENT::CMusicPlaylistManager(AudioMixer, ConfigFile, "SoundPlaylist");
152 /* AudioMixer->loadSoundBuffers ("sounds.nss", &SoundIdArray);
153 nlassert( SoundIdArray->size() == 2 );
154 SoundId = (*SoundIdArray)[0];
155 // StSoundId = (*SoundIdArray)[1]; */
157 SoundEnabled = true;
160 void releaseSound2()
162 SoundEnabled = false;
163 ConfigFile->setCallback("SoundMaxTracks", NULL);
164 ConfigFile->setCallback("SoundUseEax", NULL);
165 ConfigFile->setCallback("SoundUseADPCM", NULL);
166 ConfigFile->setCallback("SoundForceSoftware", NULL);
167 ConfigFile->setCallback("SoundDriver", NULL);
168 // delete PlaylistManager;
169 // PlaylistManager = NULL;
170 delete AudioMixer;
171 AudioMixer = NULL;
174 #endif
176 void initSound()
178 #ifdef SBCLIENT_WITH_SOUND
179 if (ConfigFile->exists("SoundEnabled") ? ConfigFile->getVar("SoundEnabled").asBool() : false)
180 initSound2();
181 ConfigFile->setCallback("SoundEnabled", cbConfigFileSoundEnabled);
182 #endif
185 //void playSound (CEntity &entity, TSoundId id)
187 ///* entity.Source = AudioMixer->createSource (id);
188 // entity.Source->setLooping (true);
189 // entity.Source->play ();
190 //*/}
192 //void deleteSound (CEntity &entity)
194 ///* if (entity.Source != NULL)
195 // {
196 // if (entity.Source->isPlaying ())
197 // entity.Source->stop ();
199 // AudioMixer->removeSource (entity.Source);
200 // entity.Source = NULL;
201 // }
202 //*/}
204 void updateSound()
206 #ifdef SBCLIENT_WITH_SOUND
207 if (SoundEnabled)
209 // PlaylistManager->update(DiffTime);
210 AudioMixer->update();
212 #endif
215 void releaseSound()
217 #ifdef SBCLIENT_WITH_SOUND
218 ConfigFile->setCallback("SoundEnabled", NULL);
219 if (SoundEnabled) releaseSound2();
220 #endif
223 void playMusic(const char *file)
225 #ifdef SBCLIENT_WITH_SOUND
226 if (AudioMixer)
227 AudioMixer->playMusic(file, 1000, true, true);
228 #endif
231 } /* namespace SBCLIENT */
233 ////#ifdef NL_OS_WINDOWS
234 ////
235 ////void playMusic(sint32 playlist, sint32 track)
236 ////{
237 //// if (SoundEnabled)
238 //// PlaylistManager->playMusic(playlist, track);
239 ////}
240 ////
241 ////void setMusicVolume(sint32 playlist, float volume)
242 ////{
243 //// if (SoundEnabled)
244 //// PlaylistManager->setVolume(playlist, volume);
245 ////}
247 NLMISC_COMMAND(music_bg,"background music","")
249 if (args.size() != 0) return false;
250 SBCLIENT::playMusic(SBCLIENT_MUSIC_BACKGROUND);
251 return true;
254 NLMISC_COMMAND(music_bg_beat,"background music with beat","")
256 if (args.size() != 0)
257 return false;
258 SBCLIENT::playMusic(SBCLIENT_MUSIC_BACKGROUND_BEAT);
259 return true;
262 NLMISC_COMMAND(music_wait,"loading music","")
264 if (args.size() != 0)
265 return false;
266 SBCLIENT::playMusic(SBCLIENT_MUSIC_WAIT);
267 return true;
270 NLMISC_COMMAND(music_login,"login screen music","")
272 if (args.size() != 0)
273 return false;
274 SBCLIENT::playMusic(SBCLIENT_MUSIC_LOGIN);
275 return true;
278 ////#endif
280 /* end of file */