New lua versions
[ryzomcore.git] / studio / src / plugins / object_viewer / sound_system.cpp
blob702e963f091281639ca2b419aa14e1164a1d84c2
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-2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
6 // Copyright (C) 2012 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/>.
22 #include "stdpch.h"
23 #include "sound_system.h"
25 // Project includes
26 #include "modules.h"
27 #include "object_viewer_constants.h"
28 #include "../core/icore.h"
30 // NeL includes
31 #include <nel/sound/u_audio_mixer.h>
32 #include <nel/sound/u_listener.h>
33 #include <nel/sound/sound_anim_manager.h>
34 #include <nel/sound/sound_animation.h>
35 #include <nel/3d/u_particle_system_sound.h>
36 #include <nel/misc/path.h>
37 #include <nel/misc/sheet_id.h>
40 // Qt includes
41 #include <QtCore/QSettings>
43 namespace NLQT
46 CSoundSystem::CSoundSystem()
47 :_AudioMixer(NULL),
48 _AnimManager(NULL),
49 _Zero(NLMISC::CVector::Null)
54 CSoundSystem::~CSoundSystem()
58 void CSoundSystem::setListenerMatrix(const NLMISC::CMatrix &m)
60 if (_AudioMixer)
62 if(m.getPos() != oldMatrix.getPos() || m.getJ() != oldMatrix.getJ() || m.getK() != oldMatrix.getK())
64 NLSOUND::UListener *l = _AudioMixer->getListener();
65 l->setPos(m.getPos());
66 l->setOrientation(m.getJ(), m.getK());
67 oldMatrix = m;
72 void CSoundSystem::init()
74 //H_AUTO2
75 nldebug("CSoundSystem::init");
77 // require sheet id without sheet id bin
78 NLMISC::CSheetId::initWithoutSheet();
80 // create audiomixer
81 _AudioMixer = NULL;
82 _AnimManager = NULL;
83 NL3D::UParticleSystemSound::setPSSound(NULL);
84 _AudioMixer = NLSOUND::UAudioMixer::createAudioMixer();
85 nlassert(_AudioMixer);
87 try
89 // init audiomixer
90 QSettings *settings = Core::ICore::instance()->settings();
91 settings->beginGroup(Constants::OBJECT_VIEWER_SECTION);
93 _PackedSheetPath = settings->value(Constants::SOUND_PACKED_SHEET_PATH, "").toString().toUtf8().constData();
94 _SamplePath = settings->value(Constants::SOUND_SAMPLE_PATH, "").toString().toUtf8().constData();
95 _AudioMixer->setSamplePath(_SamplePath);
96 _AudioMixer->setPackedSheetOption(_PackedSheetPath, true);
97 std::vector<std::string> devices;
98 _AudioMixer->initDriver(settings->value(Constants::SOUND_DRIVER, "Auto").toString().toUtf8().constData());
99 _AudioMixer->getDevices(devices);
100 NLSOUND::UAudioMixer::CInitInfo audioInfo;
101 audioInfo.AutoLoadSample = settings->value(Constants::SOUND_AUTO_LOAD_SAMPLE, true).toBool();
102 audioInfo.EnableOccludeObstruct = settings->value(Constants::SOUND_ENABLE_OCCLUDE_OBSTRUCT, true).toBool();
103 audioInfo.EnableReverb = settings->value(Constants::SOUND_ENABLE_REVERB, true).toBool();
104 audioInfo.ManualRolloff = settings->value(Constants::SOUND_MANUAL_ROLL_OFF, true).toBool();
105 audioInfo.ForceSoftware = settings->value(Constants::SOUND_FORCE_SOFTWARE, false).toBool();
106 audioInfo.MaxTrack = settings->value(Constants::SOUND_MAX_TRACK, 48).toInt();
107 audioInfo.UseADPCM = settings->value(Constants::SOUND_USE_ADCPM, false).toBool();
108 _AudioMixer->initDevice(settings->value(Constants::SOUND_DEVICE, "").toString().toUtf8().constData(), audioInfo, NULL);
109 _AudioMixer->setLowWaterMark(1);
111 settings->endGroup();
113 catch(NLMISC::Exception &e)
115 // in case of exeption during mixer init, the mixer is destroyed !
116 std::string mess = std::string("Unable to init sound :") + e.what();
117 nlwarning ("Init sound: %s", mess.c_str());
118 _AudioMixer = NULL;
119 QSettings *settings = Core::ICore::instance()->settings();
120 if (settings->group() == Constants::OBJECT_VIEWER_SECTION)
121 settings->endGroup();
122 return;
125 // ok for the mixer, now create the sound anim manager
128 // TODO : boris : Hum, as far as I know, throwing exeption in constructor is a very BAD idea...
129 _AnimManager = new NLSOUND::CSoundAnimManager(_AudioMixer);
131 catch (NLMISC::Exception &e)
133 std::string mess = std::string("Unable to init sound :") + e.what();
134 nlwarning ("Init sound: %s", mess.c_str());
135 delete _AudioMixer;
136 _AudioMixer = NULL;
138 return;
140 NL3D::UParticleSystemSound::setPSSound(_AudioMixer);
143 void CSoundSystem::release(void)
145 //H_AUTO2
146 nldebug("CSoundSystem::release");
148 NL3D::UParticleSystemSound::setPSSound(NULL);
149 if (_AnimManager)
151 delete _AnimManager;
152 _AnimManager = NULL;
154 if (_AudioMixer)
156 delete _AudioMixer;
157 _AudioMixer = NULL;
161 void CSoundSystem::play(const std::string &soundName)
163 if (_AudioMixer)
165 NLSOUND::USource *src = _AudioMixer->createSource(NLMISC::CSheetId(soundName, "sound"), true);
166 if (src)
168 // FIXME: Use relative positioning, and set pos to 0,0,0
169 src->setLooping(false);
170 const NLMISC::CVector &pos = _AudioMixer->getListener()->getPos();
171 src->setPos(pos);
172 src->play();
174 else
176 nlwarning("Can't play the sound (perhaps it's contextual sound)");
181 NLSOUND::USource *CSoundSystem::create(const std::string &soundName)
183 if (_AudioMixer)
185 NLSOUND::USource *src = _AudioMixer->createSource(NLMISC::CSheetId(soundName, "sound"), false);
186 if (src)
188 // FIXME: Use relative positioning, and set pos to 0,0,0
189 src->setLooping(false);
190 const NLMISC::CVector &pos = _AudioMixer->getListener()->getPos();
191 src->setPos(pos);
192 src->play();
193 return src;
195 else
197 nlwarning("Can't play the sound (perhaps it's contextual sound)");
199 return NULL;
201 return NULL;
204 void CSoundSystem::playAnimation(std::string &name, float lastTime, float curTime, NLSOUND::CSoundContext &context)
206 if (_AnimManager == NULL)
208 return;
211 NLSOUND::TSoundAnimId id = _AnimManager->getAnimationFromName(name);
213 if (id != NLSOUND::CSoundAnimationNoId)
215 _AnimManager->playAnimation(id, lastTime, curTime, NULL, context);
219 void CSoundSystem::update()
221 if (_AudioMixer)
223 _AudioMixer->update();
227 void CSoundSystem::initGraphics()
229 //H_AUTO2
230 nldebug("CSoundSystem::initGraphics");
232 // set particle system sound
233 NL3D::UParticleSystemSound::setPSSound(_AudioMixer);
235 // ...
236 // todo: displayers for all the test sound sources :)
239 void CSoundSystem::releaseGraphics()
241 //H_AUTO2
242 nldebug("CSoundSystem::releaseGraphics");
244 // ..
246 // clear particle system sound
247 NL3D::UParticleSystemSound::setPSSound(NULL);
250 } /* namespace NLQT */