Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / sound / source_common.cpp
blobddf1b5a84b74fb6e8bb56931011313cff490c8d7
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 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/source_common.h"
24 #include "nel/sound/audio_mixer_user.h"
27 using namespace NLMISC;
29 namespace NLSOUND
32 CSourceCommon::CSourceCommon(TSoundId id, bool spawn, TSpawnEndCallback cb, void *cbUserParam, NL3D::CCluster *cluster, CGroupController *groupController)
33 : _Priority(MidPri),
34 _Playing(false),
35 _Looping(false),
36 _Position(CVector::Null),
37 _Velocity(CVector::Null),
38 _Direction(CVector::Null),
39 _Gain(1.0f),
40 _Pitch(1.0f),
41 _RelativeMode(false),
42 _InitialGain(1.0f),
43 _PlayStart(0),
44 _Spawn(spawn),
45 _SpawnEndCb(cb),
46 _CbUserParam(cbUserParam),
47 _Cluster(cluster),
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)
74 _Priority = pr;
76 // The AudioMixer redispatches as necessary in the update() function [PH]
77 // Redispatch the tracks if needed
78 //if ( redispatch )
79 //{
80 // CAudioMixerUser::instance()->balanceSources();
81 //}
85 * Set looping on/off for future playbacks (default: off)
87 void CSourceCommon::setLooping( bool l )
89 _Looping = l;
93 * Return the looping state
95 bool CSourceCommon::getLooping() const
97 return _Looping;
101 * Play
103 void CSourceCommon::play()
105 CAudioMixerUser::instance()->incPlayingSource();
106 _Playing = true;
107 _PlayStart = CTime::getLocalTime();
109 if (_UserVarControler != CStringMapper::emptyId())
110 CAudioMixerUser::instance()->addUserControledSource(this, _UserVarControler);
114 * Stop playing
116 void CSourceCommon::stop()
118 CAudioMixerUser::instance()->decPlayingSource();
119 _Playing = false;
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 )
131 _Position = 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 )
141 return _Position;
143 //else
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 ) );
156 _Pitch = pitch;
161 * Set the velocity vector (3D mode only, ignored in stereo mode) (default: (0,0,0))
163 void CSourceCommon::setVelocity( const NLMISC::CVector& vel )
165 _Velocity = 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 )
173 _Direction = dir;
176 /* Set the gain (volume value inside [0 , 1]). (default: 1)
177 * 0.0 -> silence
178 * 0.5 -> -6dB
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;
186 updateFinalGain();
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;
196 updateFinalGain();
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)
205 return 0.0f;
206 else
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()
221 if (!_Playing)
222 return 0;
223 // default implementation
224 return uint32(CTime::getLocalTime() - _PlayStart);
228 } // NLSOUND