00001 #include "audio.h"
00002
00003 extern HWND HScol;
00004 extern int AUDIO_END_CB;
00005
00006 SAudioManager::SAudioManager()
00007 {
00008
00009 manager = cAudio::createAudioManager(true);
00010 }
00011
00012 SAudioManager::~SAudioManager()
00013 {
00014
00015 if (manager != 0)
00016 {
00017 validSounds.clear();
00018 manager->releaseAllSources();
00019 manager->shutDown();
00020 cAudio::destroyAudioManager(manager);
00021 manager = 0;
00022 }
00023 }
00024
00025 SAudioManager* SAudioManager::GetInstance()
00026 {
00027 if (NULL == _singleton)
00028 {
00029 _singleton = new SAudioManager;
00030 }
00031
00032 return _singleton;
00033 }
00034
00035 void SAudioManager::Kill()
00036 {
00037 SAFE_DELETE(_singleton);
00038 }
00039
00040 cAudio::IAudioManager* SAudioManager::GetManager()
00041 {
00042 return manager;
00043 }
00044
00045 void SAudioManager::SetListenerPosition(cAudio::cVector3 pos, cAudio::cVector3 dir)
00046 {
00047 if (manager != 0)
00048 {
00049 cAudio::IListener* listener = manager->getListener();
00050 listener->setPosition(pos);
00051 listener->setDirection(dir);
00052 }
00053 }
00054
00055 void SAudioManager::AddValidSound(SAudioSound* sound)
00056 {
00057 validSounds.insert(sound);
00058 }
00059
00060 void SAudioManager::RemoveValidSound(SAudioSound* sound)
00061 {
00062 if (validSounds.find(sound) != validSounds.end())
00063 validSounds.erase(sound);
00064 }
00065
00066 bool SAudioManager::GetValidSound(SAudioSound* sound)
00067 {
00068 if (validSounds.find(sound) != validSounds.end())
00069 return true;
00070 else
00071 return false;
00072 }
00073
00074 void SAudioManager::RemoveSound(SAudioSound* sound)
00075 {
00076 if ((manager != 0) && (GetValidSound(sound)))
00077 {
00078 RemoveValidSound(sound);
00079 SAFE_DELETE(sound);
00080 }
00081 }
00082
00083 SAudioSound* SAudioManager::AddSound(std::string name, std::string path, bool stream)
00084 {
00085 if (manager != 0)
00086 {
00087 SAudioSound* sound = new SAudioSound(name, path, stream);
00088 AddValidSound(sound);
00089 return sound;
00090 }
00091 else
00092 return 0;
00093 }
00094
00095
00096
00097 SAudioManager* SAudioManager::_singleton = NULL;
00098
00099
00100 SAudioSound::SAudioSound(std::string name, std::string path, bool stream)
00101 {
00102 mName = name;
00103 mSound = SAudioManager::GetInstance()->GetManager()->create(name.c_str(), path.c_str(), stream);
00104 if (mSound)
00105 mSound->registerEventHandler(this);
00106 }
00107
00108 SAudioSound::~SAudioSound()
00109 {
00110 if (mSound)
00111 {
00112 mSound->unRegisterEventHandler(this);
00113 SAudioManager::GetInstance()->GetManager()->release(mSound);
00114 mSound = 0;
00115 }
00116 }
00117
00118 bool SAudioSound::IsValid()
00119 {
00120 if (mSound)
00121 return true;
00122 else
00123 return false;
00124 }
00125
00126 std::string SAudioSound::GetName()
00127 {
00128 return mName;
00129 }
00130
00131 bool SAudioSound::Play(bool loop)
00132 {
00133 if (mSound)
00134 return mSound->play2d(loop);
00135 else
00136 return false;
00137 }
00138
00139 bool SAudioSound::Play3d(float attenuation, bool loop)
00140 {
00141 if (mSound)
00142 return mSound->play3d(mSound->getPosition(), attenuation, loop);
00143 else
00144 return false;
00145 }
00146
00147 void SAudioSound::Stop()
00148 {
00149 if (mSound)
00150 mSound->stop();
00151 }
00152
00153 void SAudioSound::Pause()
00154 {
00155 if (mSound)
00156 mSound->pause();
00157 }
00158
00159 void SAudioSound::SetVolume(float volume)
00160 {
00161 if (mSound)
00162 mSound->setVolume(volume);
00163 }
00164
00165 float SAudioSound::GetVolume()
00166 {
00167 if (mSound)
00168 return mSound->getVolume();
00169 else
00170 return 0.0f;
00171 }
00172
00173 bool SAudioSound::IsPlaying()
00174 {
00175 if (mSound)
00176 return mSound->isPlaying();
00177 else
00178 return false;
00179 }
00180
00181 float SAudioSound::GetTime()
00182 {
00183 if (mSound)
00184 return mSound->getCurrentAudioTime();
00185 else
00186 return 0.0f;
00187 }
00188
00189 float SAudioSound::GetTotalTime()
00190 {
00191 if (mSound)
00192 return mSound->getTotalAudioTime();
00193 else
00194 return 0.0f;
00195 }
00196
00197 void SAudioSound::SetPitch(float pitch)
00198 {
00199 if (mSound)
00200 mSound->setPitch(pitch);
00201 }
00202
00203 float SAudioSound::GetPitch()
00204 {
00205 if (mSound)
00206 return mSound->getPitch();
00207 else
00208 return 0.0f;
00209 }
00210
00211 void SAudioSound::Seek(float pos, bool relative)
00212 {
00213 if (mSound)
00214 mSound->seek(pos, relative);
00215 }
00216
00217
00218 void SAudioSound::SetPositionAndDirection(cAudio::cVector3 pos, cAudio::cVector3 dir)
00219 {
00220 if (mSound)
00221 {
00222 mSound->setPosition(pos);
00223 mSound->setDirection(dir);
00224 }
00225 }
00226
00227 cAudio::cVector3 SAudioSound::GetPosition()
00228 {
00229 if (mSound)
00230 return mSound->getPosition();
00231 else
00232 return cAudio::cVector3(0.0f, 0.0f, 0.0f);
00233 }
00234
00235 cAudio::cVector3 SAudioSound::GetDirection()
00236 {
00237 if (mSound)
00238 return mSound->getDirection();
00239 else
00240 return cAudio::cVector3(0.0f, 0.0f, 0.0f);
00241 }
00242
00243 void SAudioSound::SetCone(float inner, float outer, float outvol)
00244 {
00245 if (mSound)
00246 {
00247 mSound->setInnerConeAngle(inner);
00248 mSound->setOuterConeAngle(outer);
00249 mSound->setOuterConeVolume(outvol);
00250 }
00251 }
00252
00253 float SAudioSound::GetInnerCone()
00254 {
00255 if (mSound)
00256 return mSound->getInnerConeAngle();
00257 else
00258 return 0.0f;
00259 }
00260
00261 float SAudioSound::GetOuterCone()
00262 {
00263 if (mSound)
00264 return mSound->getOuterConeAngle();
00265 else
00266 return 0.0f;
00267 }
00268
00269 float SAudioSound::GetOuterConeVolume()
00270 {
00271 if (mSound)
00272 return mSound->getOuterConeVolume();
00273 else
00274 return 0.0f;
00275 }
00276
00277 void SAudioSound::SetRolloffFactor(float rolloff)
00278 {
00279 if (mSound)
00280 mSound->setRolloffFactor(rolloff);
00281 }
00282
00283 float SAudioSound::GetRolloffFactor()
00284 {
00285 if (mSound)
00286 return mSound->getRolloffFactor();
00287 else
00288 return 0.0f;
00289 }
00290
00291
00292 void SAudioSound::onUpdate()
00293 {
00294 }
00295
00296 void SAudioSound::onRelease()
00297 {
00298 }
00299
00300 void SAudioSound::onPlay()
00301 {
00302 }
00303
00304 void SAudioSound::onStop()
00305 {
00306 PostMessage(HScol, AUDIO_END_CB, (int)this, (LPARAM)0);
00307 }
00308
00309 void SAudioSound::onPause()
00310 {
00311 }