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 Matt RAYKOWSKI (sfb) <matt.raykowski@gmail.com>
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 "nel/sound/source_common.h"
24 #include "nel/sound/audio_mixer_user.h"
27 using namespace NLMISC
;
32 CSourceCommon::CSourceCommon(TSoundId id
, bool spawn
, TSpawnEndCallback cb
, void *cbUserParam
, NL3D::CCluster
*cluster
, CGroupController
*groupController
)
36 _Position(CVector::Null
),
37 _Velocity(CVector::Null
),
38 _Direction(CVector::Null
),
46 _CbUserParam(cbUserParam
),
48 _UserVarControler(id
->getUserVarControler()),
49 _GroupController(groupController
? groupController
: id
->getGroupController())
51 CAudioMixerUser::instance()->addSource(this);
52 _GroupController
->addSource(this);
54 // get a local copy of the sound parameter
55 _InitialGain
= _Gain
= id
->getGain();
56 _Pitch
= id
->getPitch();
57 _Looping
= id
->getLooping();
58 _Priority
= id
->getPriority();
59 _Direction
= id
->getDirectionVector();
62 CSourceCommon::~CSourceCommon()
64 _GroupController
->removeSource(this);
65 CAudioMixerUser::instance()->removeSource(this);
70 * Change the priority of the source
72 void CSourceCommon::setPriority( TSoundPriority pr
)
76 // The AudioMixer redispatches as necessary in the update() function [PH]
77 // Redispatch the tracks if needed
80 // CAudioMixerUser::instance()->balanceSources();
85 * Set looping on/off for future playbacks (default: off)
87 void CSourceCommon::setLooping( bool l
)
93 * Return the looping state
95 bool CSourceCommon::getLooping() const
103 void CSourceCommon::play()
105 CAudioMixerUser::instance()->incPlayingSource();
107 _PlayStart
= CTime::getLocalTime();
109 if (_UserVarControler
!= CStringMapper::emptyId())
110 CAudioMixerUser::instance()->addUserControledSource(this, _UserVarControler
);
116 void CSourceCommon::stop()
118 CAudioMixerUser::instance()->decPlayingSource();
121 if (_UserVarControler
!= CStringMapper::emptyId())
122 CAudioMixerUser::instance()->removeUserControledSource(this, _UserVarControler
);
125 /* Set the position vector (default: (0,0,0)).
126 * 3D mode -> 3D position
127 * st mode -> x is the pan value (from left (-1) to right (1)), set y and z to 0
129 void CSourceCommon::setPos( const NLMISC::CVector
& pos
)
134 /* Get the position vector.
135 * If the source is stereo, return the position vector which reference was passed to set3DPositionVector()
137 const NLMISC::CVector
&CSourceCommon::getPos() const
139 //if ( _3DPosition == NULL )
145 // return *_3DPosition;
150 /* Shift the frequency. 1.0f equals identity, each reduction of 50% equals a pitch shift
151 * of one octave. 0 is not a legal value.
153 void CSourceCommon::setPitch( float pitch
)
155 // nlassert( (pitch > 0) && (pitch <= 1.0f ) );
161 * Set the velocity vector (3D mode only, ignored in stereo mode) (default: (0,0,0))
163 void CSourceCommon::setVelocity( const NLMISC::CVector
& vel
)
169 * Set the direction vector (3D mode only, ignored in stereo mode) (default: (0,0,0) as non-directional)
171 void CSourceCommon::setDirection( const NLMISC::CVector
& dir
)
176 /* Set the gain (volume value inside [0 , 1]). (default: 1)
179 * 1.0 -> no attenuation
180 * values > 1 (amplification) not supported by most drivers
182 void CSourceCommon::setGain( float gain
)
184 clamp(gain
, 0.0f
, 1.0f
);
185 _InitialGain
= _Gain
= gain
;
189 /* Set the gain amount (value inside [0, 1]) to map between 0 and the nominal gain
190 * (which is getSource()->getGain()). Does nothing if getSource() is null.
192 void CSourceCommon::setRelativeGain( float gain
)
194 clamp(gain
, 0.0f
, 1.0f
);
195 _Gain
= _InitialGain
* gain
;
200 * Return the relative gain (see setRelativeGain()), or the absolute gain if getSource() is null.
202 float CSourceCommon::getRelativeGain() const
204 if (_InitialGain
== 0.0f
)
207 return _Gain
/ _InitialGain
;
211 * Set the source relative mode. If true, positions are interpreted relative to the listener position (default: false)
213 void CSourceCommon::setSourceRelativeMode( bool mode
)
215 _RelativeMode
= mode
;
219 uint32
CSourceCommon::getTime()
223 // default implementation
224 return uint32(CTime::getLocalTime() - _PlayStart
);