00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "SoundSourceComponent.h"
00018 #ifdef __COMPILE_WITH_SFML_AUDIO__
00019 #include "../../core/GameManager.h"
00020
00021
00022
00023
00024 SoundSourceComponent::SoundSourceComponent(Entity *parent)
00025 : EntityComponent(parent), mSound(NULL), mMusic(NULL), mBuffer(NULL), mAttenuation(1.0f),
00026 mLoop(false), mMinDistance(1.0f), mPitch(1.0f), mPosition(parent->getAbsolutePosition()), mVolume(100)
00027 {
00028 if(pParent != NULL)
00029 {
00030
00031 mName = "SoundSourceComponent";
00032
00033
00034 pParent->connectEventSignal("onPositionChange", this, &SoundSourceComponent::onPositionChange);
00035 pParent->connectEventSignal("onPause", this, &SoundSourceComponent::onPause);
00036 pParent->connectEventSignal("onUnPause", this, &SoundSourceComponent::onUnPause);
00037 }
00038 }
00039
00040
00041 SoundSourceComponent::~SoundSourceComponent()
00042 {
00043 if(pParent != NULL)
00044 {
00045 if(mSound != NULL)
00046 delete mSound;
00047
00048 if(mMusic != NULL)
00049 delete mMusic;
00050
00051 if(mBuffer != NULL)
00052 delete[] mBuffer;
00053 }
00054 }
00055
00056
00057 sf::Sound* SoundSourceComponent::getSound()
00058 {
00059 return mSound;
00060 }
00061
00062
00063 sf::Music* SoundSourceComponent::getMusic()
00064 {
00065 return mMusic;
00066 }
00067
00068
00069 bool SoundSourceComponent::loadMusic(const std::string &fileName)
00070 {
00071
00072 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00073
00074
00075 unloadData();
00076
00077
00078 AssetGroup *assets = pParent->getAssetGroup();
00079
00080 if(assets != NULL)
00081 {
00082
00083 SoundProcessor *processor = static_cast<SoundProcessor*>
00084 (assets->getAssetProcessor("sounds"));
00085
00086 if(processor->getSound(fileName, mBuffer, mBufferSize))
00087 {
00088 assets->connectEventSignal(std::string("sounds/") + fileName, this,
00089 &SoundSourceComponent::onSound);
00090 mSoundFileName = fileName;
00091 }
00092
00093 else
00094 return false;
00095 }
00096
00097 else
00098 {
00099
00100 if(!fileSystem->existFile(fileName.c_str()))
00101 return false;
00102
00103
00104 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00105
00106 mBufferSize = file->getSize();
00107 mBuffer = new c8[mBufferSize+1];
00108 file->read(mBuffer, mBufferSize);
00109 file->drop();
00110 }
00111
00112
00113 mMusic = new sf::Music();
00114 mMusic->OpenFromMemory(mBuffer, mBufferSize);
00115
00116
00117 mMusic->SetAttenuation(mAttenuation);
00118 mMusic->SetLoop(mLoop);
00119 mMusic->SetMinDistance(mMinDistance);
00120 mMusic->SetPitch(mPitch);
00121 mMusic->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00122 mMusic->SetVolume(mVolume);
00123
00124 return true;
00125 }
00126
00127
00128 bool SoundSourceComponent::loadMusicFromMemory(c8 *buffer, long length)
00129 {
00130
00131 unloadData();
00132
00133
00134 mMusic = new sf::Music();
00135 mMusic->OpenFromMemory(buffer, length);
00136
00137
00138 mMusic->SetAttenuation(mAttenuation);
00139 mMusic->SetLoop(mLoop);
00140 mMusic->SetMinDistance(mMinDistance);
00141 mMusic->SetPitch(mPitch);
00142 mMusic->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00143 mMusic->SetVolume(mVolume);
00144
00145 return true;
00146 }
00147
00148
00149 bool SoundSourceComponent::loadSoundBuffer(const std::string &bufferName)
00150 {
00151
00152 unloadData();
00153
00154
00155 mSound = new sf::Sound();
00156
00157 sf::SoundBuffer *soundBuffer = GameManager::Instance()->getSoundManager()->getSoundBuffer(bufferName);
00158 if(soundBuffer == NULL)
00159 {
00160 delete mSound;
00161 mSound = NULL;
00162 return false;
00163 }
00164
00165 mSound->SetBuffer(*soundBuffer);
00166 std::cout << mSound->GetBuffer()->GetDuration() << "\n";
00167
00168 mSound->SetAttenuation(mAttenuation);
00169 mSound->SetLoop(mLoop);
00170 mSound->SetMinDistance(mMinDistance);
00171 mSound->SetPitch(mPitch);
00172 mSound->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00173 mSound->SetVolume(mVolume);
00174
00175 return true;
00176 }
00177
00178
00179 void SoundSourceComponent::unloadData()
00180 {
00181
00182 AssetGroup *assets = pParent->getAssetGroup();
00183
00184
00185
00186
00187
00188
00189 if(mSound != NULL)
00190 {
00191 delete mSound;
00192 mSound = NULL;
00193 }
00194
00195 if(mMusic != NULL)
00196 {
00197 delete mMusic;
00198 mMusic = NULL;
00199 }
00200
00201 if(assets == NULL && mBuffer != NULL)
00202 {
00203 delete[] mBuffer;
00204 mBuffer = NULL;
00205 mBufferSize = 0;
00206 }
00207 }
00208
00209
00210 void SoundSourceComponent::play()
00211 {
00212 if(mSound != NULL)
00213 mSound->Play();
00214
00215 else if(mMusic != NULL)
00216 mMusic->Play();
00217 }
00218
00219
00220 void SoundSourceComponent::pause()
00221 {
00222 if(mSound != NULL)
00223 mSound->Pause();
00224
00225 else if(mMusic != NULL)
00226 mMusic->Pause();
00227 }
00228
00229
00230 void SoundSourceComponent::stop()
00231 {
00232 if(mSound != NULL)
00233 mSound->Stop();
00234
00235 else if(mMusic != NULL)
00236 mMusic->Stop();
00237 }
00238
00239
00240 f32 SoundSourceComponent::getAttenuation() const
00241 {
00242 return mAttenuation;
00243 }
00244
00245
00246 bool SoundSourceComponent::getIsPaused() const
00247 {
00248 if(mSound != NULL)
00249 {
00250 if(mSound->GetStatus() == sf::Sound::Paused)
00251 return true;
00252 }
00253
00254 else if(mMusic != NULL)
00255 {
00256 if(mMusic->GetStatus() == sf::Sound::Paused)
00257 return true;
00258 }
00259
00260 return false;
00261 }
00262
00263
00264 bool SoundSourceComponent::getIsPlaying() const
00265 {
00266 if(mSound != NULL)
00267 {
00268 if(mSound->GetStatus() == sf::Sound::Playing)
00269 return true;
00270 }
00271
00272 else if(mMusic != NULL)
00273 {
00274 if(mMusic->GetStatus() == sf::Sound::Playing)
00275 return true;
00276 }
00277
00278 return false;
00279 }
00280
00281
00282 bool SoundSourceComponent::getIsStopped() const
00283 {
00284 if(mSound != NULL)
00285 {
00286 if(mSound->GetStatus() == sf::Sound::Stopped)
00287 return true;
00288 }
00289
00290 else if(mMusic != NULL)
00291 {
00292 if(mMusic->GetStatus() == sf::Sound::Stopped)
00293 return true;
00294 }
00295
00296 return false;
00297 }
00298
00299
00300 bool SoundSourceComponent::getLoop() const
00301 {
00302 return mLoop;
00303 }
00304
00305
00306 f32 SoundSourceComponent::getMinDistance() const
00307 {
00308 return mMinDistance;
00309 }
00310
00311
00312
00313 f32 SoundSourceComponent::getOffset() const
00314 {
00315 if(mSound != NULL)
00316 return mSound->GetPlayingOffset();
00317
00318 return 0.0f;
00319 }
00320
00321
00322 f32 SoundSourceComponent::getPitch() const
00323 {
00324 return mPitch;
00325 }
00326
00327
00328 f32 SoundSourceComponent::getVolume() const
00329 {
00330 return mVolume;
00331 }
00332
00333
00334
00335 void SoundSourceComponent::setAttenuation(f32 attenuation)
00336 {
00337 if(mSound != NULL)
00338 mSound->SetAttenuation(attenuation);
00339
00340 else if(mMusic != NULL)
00341 mMusic->SetAttenuation(attenuation);
00342
00343 mAttenuation = attenuation;
00344 }
00345
00346
00347 void SoundSourceComponent::setLoop(bool value)
00348 {
00349 if(mSound != NULL)
00350 mSound->SetLoop(value);
00351
00352 else if(mMusic != NULL)
00353 mMusic->SetLoop(value);
00354
00355 mLoop = value;
00356 }
00357
00358
00359
00360 void SoundSourceComponent::setMinDistance(f32 minDistance)
00361 {
00362 if(mSound != NULL)
00363 mSound->SetMinDistance(minDistance);
00364
00365 else if(mMusic != NULL)
00366 mMusic->SetMinDistance(minDistance);
00367
00368 mMinDistance = minDistance;
00369 }
00370
00371
00372
00373 void SoundSourceComponent::setOffset(f32 offset)
00374 {
00375 if(mSound != NULL)
00376 mSound->SetPlayingOffset(offset);
00377 }
00378
00379
00380 void SoundSourceComponent::setPitch(f32 pitch)
00381 {
00382 if(mSound != NULL)
00383 mSound->SetPitch(pitch);
00384
00385 else if(mMusic != NULL)
00386 mMusic->SetPitch(pitch);
00387
00388 mPitch = pitch;
00389 }
00390
00391
00392 void SoundSourceComponent::setVolume(f32 volume)
00393 {
00394 if(mSound != NULL)
00395 mSound->SetVolume(volume);
00396
00397 else if(mMusic != NULL)
00398 mMusic->SetVolume(volume);
00399
00400 mVolume = volume;
00401 }
00402
00403
00404
00405 void SoundSourceComponent::onPositionChange(void *p)
00406 {
00407 mPosition += reinterpret_cast<vector3df*>(p)[0];
00408
00409 if(mSound != NULL)
00410 mSound->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00411
00412 else if(mMusic != NULL)
00413 mMusic->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00414 }
00415
00416
00417 void SoundSourceComponent::onPause(void *p)
00418 {
00419 mWasPaused = getIsPaused();
00420 pause();
00421 }
00422
00423
00424 void SoundSourceComponent::onUnPause(void *p)
00425 {
00426 if(!mWasPaused)
00427 play();
00428 }
00429
00430
00431 void SoundSourceComponent::onSound(void *p)
00432 {
00433
00434 std::pair<c8*, long> *soundData = reinterpret_cast<std::pair<c8*, long>*>(p);
00435
00436
00437 unloadData();
00438
00439
00440 mBuffer = soundData->first;
00441 mBufferSize = soundData->second;
00442
00443
00444 if(mBufferSize != 0)
00445 {
00446
00447 mMusic = new sf::Music();
00448 mMusic->OpenFromMemory(mBuffer, mBufferSize);
00449
00450
00451 mMusic->SetAttenuation(mAttenuation);
00452 mMusic->SetLoop(mLoop);
00453 mMusic->SetMinDistance(mMinDistance);
00454 mMusic->SetPitch(mPitch);
00455 mMusic->SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00456 mMusic->SetVolume(mVolume);
00457 }
00458 }
00459
00460
00461 bool SoundSourceComponent::parseXML(IXMLReader *file, Entity *entity)
00462 {
00463
00464 SoundSourceComponent *component = NULL;
00465
00466
00467 if(file->getNodeType() == io::EXN_ELEMENT)
00468 {
00469 if(stringw("SoundSourceComponent") == file->getNodeName())
00470 {
00471
00472 component = new SoundSourceComponent(entity);
00473 }
00474 }
00475
00476 if(component == NULL)
00477 return false;
00478
00479
00480 while(file->read())
00481 {
00482 switch(file->getNodeType())
00483 {
00484 case io::EXN_ELEMENT:
00485
00486
00487 if(stringw("attenuation") == file->getNodeName())
00488 component->setAttenuation(file->getAttributeValueAsFloat(L"value"));
00489
00490
00491 else if(stringw("loop") == file->getNodeName())
00492 {
00493 stringc value = file->getAttributeValue(L"value");
00494 component->setLoop( (value == "true") ? true : false );
00495 }
00496
00497
00498 else if(stringw("minDistance") == file->getNodeName())
00499 component->setMinDistance(file->getAttributeValueAsFloat(L"value"));
00500
00501
00502 else if(stringw("music") == file->getNodeName())
00503 {
00504 stringc fileName = file->getAttributeValue(L"fileName");
00505 component->loadMusic(fileName.c_str());
00506 }
00507
00508
00509 else if(stringw("pitch") == file->getNodeName())
00510 component->setPitch(file->getAttributeValueAsFloat(L"value"));
00511
00512 else if(stringw("soundBuffer") == file->getNodeName())
00513 {
00514 stringc bufferName = file->getAttributeValue(L"bufferName");
00515 component->loadSoundBuffer(bufferName.c_str());
00516 }
00517
00518
00519 else if(stringw("volume") == file->getNodeName())
00520 component->setVolume(file->getAttributeValueAsFloat(L"value"));
00521
00522 break;
00523
00524 case io::EXN_ELEMENT_END:
00525
00526
00527 if(stringw("SoundSourceComponent") == file->getNodeName())
00528 return true;
00529
00530 break;
00531
00532 default:
00533
00534 break;
00535 }
00536
00537 }
00538
00539
00540 return false;
00541 }
00542
00543 #endif // __COMPILE_WITH_SFML_AUDIO__
00544
00545