sfxFMODEventSource.cpp
Engine/source/sfx/fmod/sfxFMODEventSource.cpp
Public Functions
ConsoleDocClass(SFXFMODEventSource , "@brief A sound <a href="/coding/file/pointer_8h/#pointer_8h_1adb82dfe18535e9a30aa97d275f82bd55">source</a> controller playing an %FMOD Designer event (<a href="/coding/class/classsfxfmodevent/">SFXFMODEvent</a>).\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "%FMOD event sources are internally created by the sound system <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> play events from imported %FMOD Designer <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">projects.\n\n</a>" "@note This class cannot be instantiated directly by the user. Instead, instances of <a href="/coding/class/classsfxfmodeventsource/">SFXFMODEventSource</a> will be " "implicitly created by the sound system when playing an <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">SFXFMODEvent.\n\n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">SFXFMOD\n</a>" )
Detailed Description
Public Functions
ConsoleDocClass(SFXFMODEventSource , "@brief A sound <a href="/coding/file/pointer_8h/#pointer_8h_1adb82dfe18535e9a30aa97d275f82bd55">source</a> controller playing an %FMOD Designer event (<a href="/coding/class/classsfxfmodevent/">SFXFMODEvent</a>).\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "%FMOD event sources are internally created by the sound system <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> play events from imported %FMOD Designer <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">projects.\n\n</a>" "@note This class cannot be instantiated directly by the user. Instead, instances of <a href="/coding/class/classsfxfmodeventsource/">SFXFMODEventSource</a> will be " "implicitly created by the sound system when playing an <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">SFXFMODEvent.\n\n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">SFXFMOD\n</a>" )
IMPLEMENT_CONOBJECT(SFXFMODEventSource )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "sfx/fmod/sfxFMODEventSource.h" 26#include "sfx/fmod/sfxFMODEvent.h" 27#include "sfx/fmod/sfxFMODEventGroup.h" 28#include "sfx/fmod/sfxFMODDevice.h" 29#include "sfx/sfxDescription.h" 30 31 32IMPLEMENT_CONOBJECT( SFXFMODEventSource ); 33 34ConsoleDocClass( SFXFMODEventSource, 35 "@brief A sound source controller playing an %FMOD Designer event (SFXFMODEvent).\n\n" 36 37 "%FMOD event sources are internally created by the sound system to play events from imported %FMOD Designer projects.\n\n" 38 39 "@note This class cannot be instantiated directly by the user. Instead, instances of SFXFMODEventSource will be " 40 "implicitly created by the sound system when playing an SFXFMODEvent.\n\n" 41 42 "@ingroup SFXFMOD\n" 43); 44 45 46//----------------------------------------------------------------------------- 47 48SFXFMODEventSource::SFXFMODEventSource() 49 : mHandle( NULL ) 50{ 51 SFXFMODDevice::instance()->smStatNumEventSources ++; 52} 53 54//----------------------------------------------------------------------------- 55 56SFXFMODEventSource::SFXFMODEventSource( SFXFMODEvent* event ) 57 : Parent( event ), 58 mHandle( NULL ) 59{ 60 SFXFMODDevice::instance()->smStatNumEventSources ++; 61 62 // Make sure the group has its data loaded. 63 64 SFXFMODEventGroup* group = event->getEventGroup(); 65 if( !group->loadData() ) 66 return; 67 68 // Create an event instance. 69 70 if( SFXFMODDevice::smFunc->FMOD_EventGroup_GetEvent( 71 event->getEventGroup()->mHandle, 72 event->getEventName(), 73 FMOD_EVENT_DEFAULT, 74 &mHandle ) != FMOD_OK ) 75 { 76 Con::errorf( "SFXFMODEventSource::SFXFMODEventSource - failed to open event '%s'", event->getQualifiedName().c_str() ); 77 mHandle = NULL; 78 } 79} 80 81//----------------------------------------------------------------------------- 82 83SFXFMODEventSource::~SFXFMODEventSource() 84{ 85 SFXFMODDevice::instance()->smStatNumEventSources --; 86 87 if( mHandle ) 88 SFXFMODDevice::smFunc->FMOD_Event_Release( mHandle, true, true ); 89 90 if( getEvent() ) 91 getEvent()->getEventGroup()->freeData(); 92} 93 94//----------------------------------------------------------------------------- 95 96SFXFMODEventSource* SFXFMODEventSource::create( SFXFMODEvent* event ) 97{ 98 AssertFatal( event != NULL, "SFXFMODEventSource::create - got a NULL event!" ); 99 100 // Create the source. 101 102 SFXFMODEventSource* source = new SFXFMODEventSource( event ); 103 if( source->mHandle ) 104 source->registerObject(); 105 else 106 { 107 delete source; 108 source = NULL; 109 } 110 111 return source; 112} 113 114//----------------------------------------------------------------------------- 115 116void SFXFMODEventSource::play( F32 fadeInTime ) 117{ 118 if( getStatus() == SFXStatusPlaying ) 119 return; 120 121 if( isPaused() ) 122 SFXFMODDevice::smFunc->FMOD_Event_SetPaused( mHandle, false ); 123 else 124 { 125 AssertFatal( getEvent()->getEventGroup()->isDataLoaded(), "SFXFMODEventSource::play() - event data for group not loaded" ); 126 127 if( fadeInTime != -1.f ) 128 { 129 U32 fade = U32( fadeInTime * 1000.f ); 130 SFXFMODDevice::smFunc->FMOD_Event_SetPropertyByIndex( 131 mHandle, FMOD_EVENTPROPERTY_FADEIN, 132 &fade, true 133 ); 134 } 135 136 FMOD_RESULT result = SFXFMODDevice::smFunc->FMOD_Event_Start( mHandle ); 137 if( result != FMOD_OK ) 138 { 139 Con::errorf( "SFXFMODEventSoure::play() - failed to start event: %s", FMODResultToString( result ).c_str() ); 140 return; 141 } 142 } 143 144 mPlayTimer.start(); 145 _setStatus( SFXStatusPlaying ); 146 147 _play(); 148} 149 150//----------------------------------------------------------------------------- 151 152void SFXFMODEventSource::stop( F32 fadeOutTime ) 153{ 154 if( getStatus() == SFXStatusStopped ) 155 return; 156 157 AssertFatal( mHandle, "SFXFMODEvent::stop() - event not acquired" ); 158 159 bool immediate = ( fadeOutTime == 0.f ); 160 161 FMOD_RESULT result = SFXFMODDevice::smFunc->FMOD_Event_Stop( mHandle, immediate ); 162 if( result != FMOD_OK ) 163 Con::errorf( "SFXFMODEventSource::stop() - failed to stop event: %s", FMODResultToString( result ).c_str() ); 164 165 mPlayTimer.stop(); 166 _setStatus( SFXStatusStopped ); 167 168 // Reset fade-in to default in case it got overwritten 169 // in play(). 170 171 U32 fade = U32( mFadeInTime * 1000.f ); 172 SFXFMODDevice::smFunc->FMOD_Event_SetPropertyByIndex( 173 mHandle, FMOD_EVENTPROPERTY_FADEIN, 174 &fade, true 175 ); 176 177 _stop(); 178} 179 180//----------------------------------------------------------------------------- 181 182void SFXFMODEventSource::pause( F32 fadeOutTime ) 183{ 184 if( getStatus() != SFXStatusPlaying ) 185 return; 186 187 SFXFMODDevice::smFunc->FMOD_Event_SetPaused( mHandle, true ); 188 189 mPlayTimer.pause(); 190 _setStatus( SFXStatusPaused ); 191 192 _pause(); 193} 194 195//----------------------------------------------------------------------------- 196 197void SFXFMODEventSource::setTransform( const MatrixF& transform ) 198{ 199 Parent::setTransform( transform ); 200 _update3DAttributes(); 201} 202 203//----------------------------------------------------------------------------- 204 205void SFXFMODEventSource::setVelocity( const VectorF& velocity ) 206{ 207 Parent::setVelocity( velocity ); 208 _update3DAttributes(); 209} 210 211//----------------------------------------------------------------------------- 212 213void SFXFMODEventSource::_update3DAttributes() 214{ 215 FMOD_VECTOR position; 216 FMOD_VECTOR velocity; 217 FMOD_VECTOR orientation; 218 219 Point3F direction; 220 getTransform().getColumn( 1, &direction ); 221 222 TorqueVectorToFMODVector( getTransform().getPosition(), position ); 223 TorqueVectorToFMODVector( getVelocity(), velocity ); 224 TorqueVectorToFMODVector( direction, orientation ); 225 226 SFXFMODDevice::smFunc->FMOD_Event_Set3DAttributes( mHandle, &position, &velocity, &orientation ); 227} 228 229//----------------------------------------------------------------------------- 230 231void SFXFMODEventSource::_updateStatus() 232{ 233 if( mStatus == SFXStatusPlaying ) 234 { 235 if( !getEvent() ) 236 _setStatus( SFXStatusStopped ); 237 else 238 { 239 FMOD_EVENT_STATE state; 240 SFXFMODDevice::smFunc->FMOD_Event_GetState( mHandle, &state ); 241 242 if( !( state & FMOD_EVENT_STATE_PLAYING ) ) 243 _setStatus( SFXStatusStopped ); 244 } 245 } 246} 247 248//----------------------------------------------------------------------------- 249 250void SFXFMODEventSource::_updateVolume( const MatrixF& listener ) 251{ 252 F32 oldPreAttenuatedVolume = mPreAttenuatedVolume; 253 Parent::_updateVolume( listener ); 254 255 if( oldPreAttenuatedVolume != mPreAttenuatedVolume ) 256 SFXFMODDevice::smFunc->FMOD_Event_SetVolume( mHandle, mPreAttenuatedVolume ); 257} 258 259//----------------------------------------------------------------------------- 260 261void SFXFMODEventSource::_updatePitch() 262{ 263 F32 oldEffectivePitch = mEffectivePitch; 264 Parent::_updatePitch(); 265 266 if( mEffectivePitch != oldEffectivePitch ) 267 SFXFMODDevice::smFunc->FMOD_Event_SetPitch( mHandle, mEffectivePitch - 1.0f, FMOD_EVENT_PITCHUNITS_RAW ); 268} 269 270//----------------------------------------------------------------------------- 271 272void SFXFMODEventSource::_updatePriority() 273{ 274 //TODO 275 Parent::_updatePriority(); 276} 277 278//----------------------------------------------------------------------------- 279 280void SFXFMODEventSource::_setMinMaxDistance( F32 min, F32 max ) 281{ 282 Parent::_setMinMaxDistance( min, max ); 283 _update3DAttributes(); 284} 285 286//----------------------------------------------------------------------------- 287 288void SFXFMODEventSource::_setFadeTimes( F32 fadeInTime, F32 fadeOutTime ) 289{ 290 Parent::_setFadeTimes( fadeInTime, fadeOutTime ); 291 292 U32 fadeIn = U32( mFadeInTime * 1000.f ); 293 SFXFMODDevice::smFunc->FMOD_Event_SetPropertyByIndex( 294 mHandle, FMOD_EVENTPROPERTY_FADEIN, 295 &fadeIn, true 296 ); 297 298 U32 fadeOut = U32( mFadeOutTime * 1000.f ); 299 SFXFMODDevice::smFunc->FMOD_Event_SetPropertyByIndex( 300 mHandle, FMOD_EVENTPROPERTY_FADEOUT, 301 &fadeOut, true 302 ); 303} 304 305//----------------------------------------------------------------------------- 306 307void SFXFMODEventSource::_setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume ) 308{ 309 Parent::_setCone( innerAngle, outerAngle, outerVolume ); 310 _update3DAttributes(); 311} 312 313//----------------------------------------------------------------------------- 314 315void SFXFMODEventSource::_onParameterEvent( SFXParameter* parameter, SFXParameterEvent event ) 316{ 317 Parent::_onParameterEvent( parameter, event ); 318 319 // If it's a value-change on a custom parameter, 320 // pass it along to FMOD. 321 322 if( getEvent() 323 && event == SFXParameterEvent_ValueChanged 324 && parameter->getChannel() == SFXChannelUser0 ) 325 { 326 const char* name = parameter->getInternalName(); 327 328 FMOD_EVENTPARAMETER* fmodParameter; 329 if( SFXFMODDevice::smFunc->FMOD_Event_GetParameter( mHandle, name, &fmodParameter ) != FMOD_OK ) 330 { 331 Con::errorf( "SFXFMODEventSource::_onParameterEvent - could not access parameter '%s' of event '%s'", 332 name, getEvent()->getQualifiedName().c_str() ); 333 return; 334 } 335 336 SFXFMODDevice::smFunc->FMOD_EventParameter_SetValue( fmodParameter, parameter->getValue() ); 337 } 338} 339