afxEA_AudioBank.cpp
Engine/source/afx/ea/afxEA_AudioBank.cpp
Classes:
class
class
Detailed Description
1 2 3//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 4// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames 5// Copyright (C) 2015 Faust Logic, Inc. 6// 7// Permission is hereby granted, free of charge, to any person obtaining a copy 8// of this software and associated documentation files (the "Software"), to 9// deal in the Software without restriction, including without limitation the 10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11// sell copies of the Software, and to permit persons to whom the Software is 12// furnished to do so, subject to the following conditions: 13// 14// The above copyright notice and this permission notice shall be included in 15// all copies or substantial portions of the Software. 16// 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23// IN THE SOFTWARE. 24// 25//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 26 27#include <typeinfo> 28#include "afx/arcaneFX.h" 29 30#include "sfx/sfxSystem.h" 31#include "sfx/sfxSource.h" 32#include "sfx/sfxProfile.h" 33 34#include "afx/afxEffectDefs.h" 35#include "afx/afxEffectWrapper.h" 36#include "afx/afxChoreographer.h" 37#include "afx/ce/afxAudioBank.h" 38 39//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 40// afxEA_AudioBank 41 42class afxEA_AudioBank : public afxEffectWrapper 43{ 44 typedef afxEffectWrapper Parent; 45 46 SFXSource* sound_handle; 47 afxAudioBank* sound_bank; 48 49 S32 play_index; 50 51 void do_runtime_substitutions(); 52 53public: 54 /*C*/ afxEA_AudioBank(); 55 /*D*/ ~afxEA_AudioBank(); 56 57 virtual void ea_set_datablock(SimDataBlock*); 58 virtual bool ea_start(); 59 virtual bool ea_update(F32 dt); 60 virtual void ea_finish(bool was_stopped); 61}; 62 63//~~~~~~~~~~~~~~~~~~~~// 64 65afxEA_AudioBank::afxEA_AudioBank() 66{ 67 sound_handle = 0; 68 sound_bank = 0; 69 play_index = -1; 70} 71 72afxEA_AudioBank::~afxEA_AudioBank() 73{ 74 if (sound_bank && sound_bank->isTempClone()) 75 delete sound_bank; 76 sound_bank = 0; 77 sound_handle = 0; 78} 79 80void afxEA_AudioBank::ea_set_datablock(SimDataBlock* db) 81{ 82 sound_bank = dynamic_cast<afxAudioBank*>(db); 83} 84 85bool afxEA_AudioBank::ea_start() 86{ 87 if (!sound_bank) 88 { 89 Con::errorf("afxEA_AudioBank::ea_start() -- missing or incompatible afxAudioBank."); 90 return false; 91 } 92 93 do_runtime_substitutions(); 94 95 if (sound_bank->mPath == ST_NULLSTRING) 96 return false; 97 98 play_index = sound_bank->play_index; 99 100 return true; 101} 102 103bool afxEA_AudioBank::ea_update(F32 dt) 104{ 105 if (!sound_handle) 106 { 107 if (play_index >= 0 && play_index < 32 && sound_bank->mFilenames[play_index] != ST_NULLSTRING) 108 { 109 SFXProfile* temp_prof = new SFXProfile(sound_bank->mDescriptionObject, 110 avar("%s/%s", sound_bank->mPath, sound_bank->mFilenames[play_index]), 111 true); 112 if (!temp_prof->registerObject()) 113 { 114 Con::errorf( "afxEA_AudioBank::ea_update() -- unable to create profile"); 115 delete temp_prof; 116 } 117 else 118 { 119 sound_handle = SFX->createSource(temp_prof); 120 if (sound_handle) 121 sound_handle->play(); 122 temp_prof->setAutoDelete(true); 123 } 124 } 125 } 126 127 if (sound_handle) 128 { 129 sound_handle->setTransform(mUpdated_xfm); 130 sound_handle->setVolume(mUpdated_scale.x*mFade_value); 131 } 132 133 return true; 134} 135 136void afxEA_AudioBank::ea_finish(bool was_stopped) 137{ 138 if (sound_handle) 139 { 140 sound_handle->stop(); 141 SFX_DELETE(sound_handle); 142 } 143} 144 145void afxEA_AudioBank::do_runtime_substitutions() 146{ 147 sound_bank = sound_bank->cloneAndPerformSubstitutions(mChoreographer, mGroup_index); 148} 149 150//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 151 152class afxEA_SoundBankDesc : public afxEffectAdapterDesc, public afxEffectDefs 153{ 154 static afxEA_SoundBankDesc mDesc; 155 156public: 157 virtual bool testEffectType(const SimDataBlock*) const; 158 virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const; 159 virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; } 160 virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; } 161 //virtual void prepEffect(afxEffectWrapperData*) const; 162 163 virtual afxEffectWrapper* create() const { return new afxEA_AudioBank; } 164}; 165 166afxEA_SoundBankDesc afxEA_SoundBankDesc::mDesc; 167 168bool afxEA_SoundBankDesc::testEffectType(const SimDataBlock* db) const 169{ 170 return (typeid(afxAudioBank) == typeid(*db)); 171} 172 173bool afxEA_SoundBankDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const 174{ 175 SFXDescription* desc = ((SFXProfile*)ew->effect_data)->getDescription(); 176 return (desc && desc->mIsLooping) ? (timing.lifetime < 0) : false; 177} 178 179//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 180