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) 2010-2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
6 // Copyright (C) 2012 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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/>.
23 #include "sound_system.h"
27 #include "object_viewer_constants.h"
28 #include "../core/icore.h"
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>
41 #include <QtCore/QSettings>
46 CSoundSystem::CSoundSystem()
49 _Zero(NLMISC::CVector::Null
)
54 CSoundSystem::~CSoundSystem()
58 void CSoundSystem::setListenerMatrix(const NLMISC::CMatrix
&m
)
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());
72 void CSoundSystem::init()
75 nldebug("CSoundSystem::init");
77 // require sheet id without sheet id bin
78 NLMISC::CSheetId::initWithoutSheet();
83 NL3D::UParticleSystemSound::setPSSound(NULL
);
84 _AudioMixer
= NLSOUND::UAudioMixer::createAudioMixer();
85 nlassert(_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());
119 QSettings
*settings
= Core::ICore::instance()->settings();
120 if (settings
->group() == Constants::OBJECT_VIEWER_SECTION
)
121 settings
->endGroup();
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());
140 NL3D::UParticleSystemSound::setPSSound(_AudioMixer
);
143 void CSoundSystem::release(void)
146 nldebug("CSoundSystem::release");
148 NL3D::UParticleSystemSound::setPSSound(NULL
);
161 void CSoundSystem::play(const std::string
&soundName
)
165 NLSOUND::USource
*src
= _AudioMixer
->createSource(NLMISC::CSheetId(soundName
, "sound"), true);
168 // FIXME: Use relative positioning, and set pos to 0,0,0
169 src
->setLooping(false);
170 const NLMISC::CVector
&pos
= _AudioMixer
->getListener()->getPos();
176 nlwarning("Can't play the sound (perhaps it's contextual sound)");
181 NLSOUND::USource
*CSoundSystem::create(const std::string
&soundName
)
185 NLSOUND::USource
*src
= _AudioMixer
->createSource(NLMISC::CSheetId(soundName
, "sound"), false);
188 // FIXME: Use relative positioning, and set pos to 0,0,0
189 src
->setLooping(false);
190 const NLMISC::CVector
&pos
= _AudioMixer
->getListener()->getPos();
197 nlwarning("Can't play the sound (perhaps it's contextual sound)");
204 void CSoundSystem::playAnimation(std::string
&name
, float lastTime
, float curTime
, NLSOUND::CSoundContext
&context
)
206 if (_AnimManager
== NULL
)
211 NLSOUND::TSoundAnimId id
= _AnimManager
->getAnimationFromName(name
);
213 if (id
!= NLSOUND::CSoundAnimationNoId
)
215 _AnimManager
->playAnimation(id
, lastTime
, curTime
, NULL
, context
);
219 void CSoundSystem::update()
223 _AudioMixer
->update();
227 void CSoundSystem::initGraphics()
230 nldebug("CSoundSystem::initGraphics");
232 // set particle system sound
233 NL3D::UParticleSystemSound::setPSSound(_AudioMixer
);
236 // todo: displayers for all the test sound sources :)
239 void CSoundSystem::releaseGraphics()
242 nldebug("CSoundSystem::releaseGraphics");
246 // clear particle system sound
247 NL3D::UParticleSystemSound::setPSSound(NULL
);
250 } /* namespace NLQT */