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