00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_PS_SOUND_IMPL_H
00027 #define NL_PS_SOUND_IMPL_H
00028
00029 #include "nel/misc/types_nl.h"
00030 #include <string>
00031 #include "nel/misc/debug.h"
00032 #include "nel/misc/rgba.h"
00033 #include "nel/sound/u_audio_mixer.h"
00034 #include "nel/3d/u_particle_system_sound.h"
00035
00036
00037
00038
00039
00040 #define NL3D_MEM_PS_SOUND NL_ALLOC_CONTEXT( 3dPSSnd )
00041
00042 namespace NL3D
00043 {
00044
00045
00046 inline void SpawnedSourceEndedCallback(NLSOUND::USource *source, void *userParam);
00047
00048
00049 class CPSSoundServImpl;
00050
00051
00053 class CPSSoundInstanceImpl : public UPSSoundInstance
00054 {
00055 public:
00057
00059 CPSSoundInstanceImpl()
00060 : _Source(NULL), _SoundServImpl(NULL), _Spawned(false)
00061 {
00062 NL3D_MEM_PS_SOUND
00063 }
00064
00066 void init(NLSOUND::USource *source, CPSSoundServImpl *soundServImp, bool spawned)
00067 {
00068 NL3D_MEM_PS_SOUND
00069 nlassert(source);
00070 _Source = source;
00071 _Spawned = spawned;
00072 _SoundServImpl = soundServImp;
00073 }
00074
00076 virtual void setSoundParams(float gain
00077 , const NLMISC::CVector &pos
00078 , const NLMISC::CVector &velocity
00079 , float pitch
00080 )
00081 {
00082 NL3D_MEM_PS_SOUND
00083 if (!_Source) return;
00084 if (gain < 0) gain = 0;
00085 if (gain > 1) gain = 1;
00086 if (pitch > 1) pitch = 1;
00087 if (pitch < 0.0001f) pitch = 0.0001f;
00088 _Source->setPos(pos);
00089 _Source->setVelocity(velocity);
00090 _Source->setRelativeGain(gain);
00091 _Source->setPitch(pitch);
00092 }
00093
00095 virtual void play(void)
00096 {
00097 NL3D_MEM_PS_SOUND
00098 if (!_Source) return;
00099 _Source->play();
00100 }
00101
00102
00103 virtual bool isPlaying(void) const
00104 {
00105 NL3D_MEM_PS_SOUND
00106 if (!_Source) return false;
00107 return _Source->isPlaying();
00108 }
00109
00111 virtual void stop(void)
00112 {
00113 NL3D_MEM_PS_SOUND
00114 if (!_Source) return;
00115 _Source->stop();
00116 }
00117
00119 virtual void release(void);
00120
00121 protected:
00122 friend inline void SpawnedSourceEndedCallback(NLSOUND::USource *source, void *userParam);
00123 NLSOUND::USource *_Source;
00124 bool _Spawned;
00125 CPSSoundServImpl *_SoundServImpl;
00126 };
00127
00128
00129
00130
00131
00132
00133
00140 class CPSSoundServImpl : public UPSSoundServer
00141 {
00142 public:
00144 CPSSoundServImpl() : _AudioMixer(NULL)
00145 {
00146 }
00147
00148
00149
00151 void init(NLSOUND::UAudioMixer *audioMixer)
00152 {
00153 _AudioMixer = audioMixer;
00154 }
00155
00156
00158 NLSOUND::UAudioMixer *getAudioMixer(void) { return _AudioMixer;}
00159 const NLSOUND::UAudioMixer *getAudioMixer(void) const { return _AudioMixer;}
00160
00161
00163 UPSSoundInstance *createSound(const std::string &soundName, bool spawned = true)
00164 {
00165 if (!_AudioMixer) return NULL;
00166 CPSSoundInstanceImpl *sound = new CPSSoundInstanceImpl;
00167 NLSOUND::USource *source = _AudioMixer->createSource(soundName, spawned, SpawnedSourceEndedCallback, sound );
00168 if (source)
00169 {
00170 if (spawned)
00171 {
00172 source->setLooping(false);
00173 }
00174 sound->init(source, this, spawned);
00175 return sound;
00176 }
00177 else
00178 {
00179
00180 delete sound;
00181 return NULL;
00182 }
00183 }
00184
00185 protected:
00186
00187 NLSOUND::UAudioMixer *_AudioMixer;
00188
00189 };
00190
00191
00193 inline void SpawnedSourceEndedCallback(NLSOUND::USource *source, void *userParam)
00194 {
00195 nlassert(((CPSSoundInstanceImpl *) userParam)->_Source == source);
00196 ((CPSSoundInstanceImpl *) userParam)->_Source = NULL;
00197 }
00198
00199
00200
00201 void CPSSoundInstanceImpl::release(void)
00202 {
00203 if (!_Spawned)
00204 {
00205 if (_SoundServImpl->getAudioMixer())
00206 {
00207
00208 delete _Source;
00209 }
00210 }
00211 else
00212 {
00213 if (_Source)
00214 {
00215 _Source->unregisterSpawnCallBack();
00216 }
00217 }
00218 delete this;
00219 }
00220
00221
00222
00223
00224 }
00225
00226
00227 #endif // NL_PS_SOUND_IMPL_H
00228
00229