VSoundEffect.cpp
Engine/source/Verve/Torque3D/VSoundEffect.cpp
Public Functions
Detailed Description
Public Functions
IMPLEMENT_CO_CLIENTEVENT_V1(VSoundEffectNetEvent )
1 2//----------------------------------------------------------------------------- 3// Verve 4// Copyright (C) 2014 - Violent Tulip 5// 6// Permission is hereby granted, free of charge, to any person obtaining a copy 7// of this software and associated documentation files (the "Software"), to 8// deal in the Software without restriction, including without limitation the 9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10// sell copies of the Software, and to permit persons to whom the Software is 11// furnished to do so, subject to the following conditions: 12// 13// The above copyright notice and this permission notice shall be included in 14// all copies or substantial portions of the Software. 15// 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22// IN THE SOFTWARE. 23//----------------------------------------------------------------------------- 24#include "Verve/VerveConfig.h" 25#include "Verve/Torque3D/VSoundEffect.h" 26 27#include "T3D/gameBase/gameConnection.h" 28#include "core/stream/bitStream.h" 29#include "math/mathIO.h" 30#include "sfx/sfxSystem.h" 31#include "sfx/sfxDescription.h" 32 33//----------------------------------------------------------------------------- 34// 35// Sound Methods. 36// 37//----------------------------------------------------------------------------- 38 39bool VTorque::isSoundLooping( SoundEffectType *pSoundProfile ) 40{ 41 if ( !pSoundProfile ) 42 { 43 // Sanity! 44 return false; 45 } 46 47 // Return Looping. 48 return pSoundProfile->getDescription()->mIsLooping; 49} 50 51S32 VTorque::getSoundDuration( SoundEffectType *pSoundProfile ) 52{ 53 if ( !pSoundProfile ) 54 { 55 // Sanity! 56 return 0; 57 } 58 59 // Return Duration. 60 return pSoundProfile->getSoundDuration(); 61} 62 63VTorque::SoundSourceType *VTorque::playSound( SoundEffectType *pSoundProfile, const U32 &pPosition, const F32 &pPitch ) 64{ 65 if ( !pSoundProfile ) 66 { 67 // Sanity! 68 return NULL; 69 } 70 71#ifdef VT_EDITOR 72 73 // Play Sound. 74 SFXSound *source = ( SFXSound* )SFX->playOnce( pSoundProfile ); 75 76 if ( source ) 77 { 78 // Set Position. 79 source->setPosition( pPosition ); 80 81 // Set Pitch. 82 source->setPitch( pPitch ); 83 } 84 85 // Return Source. 86 return source; 87 88#else 89 90 // Fetch Client Group. 91 SimGroup* clientGroup = Sim::getClientGroup(); 92 93 for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ ) 94 { 95 NetConnection *connection = static_cast<NetConnection*>( *itr ); 96 if ( connection ) 97 { 98 // Create Event. 99 VSoundEffectNetEvent *event = new VSoundEffectNetEvent(); 100 101 // Setup Event. 102 event->mProfile = pSoundProfile; 103 event->mPosition = pPosition; 104 event->mPitch = pPitch; 105 event->mIs3D = false; 106 107 // Post Event. 108 connection->postNetEvent( event ); 109 } 110 } 111 112 return NULL; 113 114#endif 115} 116 117VTorque::SoundSourceType *VTorque::playSound( SoundEffectType *pSoundProfile, SceneObjectType *pObject, const U32 &pPosition, const F32 &pPitch ) 118{ 119 if ( !pSoundProfile ) 120 { 121 // Sanity! 122 return NULL; 123 } 124 125#ifdef VT_EDITOR 126 127 // Fetch Reference Transform. 128 const MatrixF &transform = pObject->getTransform(); 129 130 // Play Sound. 131 SFXSound *source = ( SFXSound* )SFX->playOnce( pSoundProfile, &transform ); 132 133 if ( source ) 134 { 135 // Set Position. 136 source->setPosition( pPosition ); 137 138 // Set Pitch. 139 source->setPitch( pPitch ); 140 } 141 142 // Return Source. 143 return source; 144 145#else 146 147 // Fetch Client Group. 148 SimGroup* clientGroup = Sim::getClientGroup(); 149 150 for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ ) 151 { 152 NetConnection *connection = static_cast<NetConnection*>( *itr ); 153 if ( connection ) 154 { 155 // Create Event. 156 VSoundEffectNetEvent *event = new VSoundEffectNetEvent(); 157 158 // Setup Event. 159 event->mProfile = pSoundProfile; 160 event->mPosition = pPosition; 161 event->mPitch = pPitch; 162 event->mIs3D = true; 163 event->mTransform = pObject->getTransform(); 164 165 // Post Event. 166 connection->postNetEvent( event ); 167 } 168 } 169 170 return NULL; 171 172#endif 173} 174 175void VTorque::playSound( SoundSourceType *pSource ) 176{ 177 if ( !pSource ) 178 { 179 // Sanity! 180 return; 181 } 182 183 // Play. 184 pSource->play(); 185} 186 187void VTorque::pauseSound( SoundSourceType *pSource ) 188{ 189 if ( !pSource ) 190 { 191 // Sanity! 192 return; 193 } 194 195 // Pause. 196 pSource->pause(); 197} 198 199void VTorque::stopSound( SoundSourceType *pSource ) 200{ 201 if ( !pSource ) 202 { 203 // Sanity! 204 return; 205 } 206 207 // Stop. 208 pSource->stop(); 209} 210 211void VTorque::setSoundPosition( SoundSourceType *pSource, const U32 &pPosition ) 212{ 213 if ( !pSource ) 214 { 215 // Sanity! 216 return; 217 } 218 219 // Set Position. 220 pSource->setPosition( pPosition ); 221} 222 223void VTorque::setSoundPitch( SoundSourceType *pSource, const F32 &pPitch ) 224{ 225 if ( !pSource ) 226 { 227 // Sanity! 228 return; 229 } 230 231 // Set Pitch. 232 pSource->setPitch( pPitch ); 233} 234 235//----------------------------------------------------------------------------- 236IMPLEMENT_CO_CLIENTEVENT_V1( VSoundEffectNetEvent ); 237//----------------------------------------------------------------------------- 238 239VSoundEffectNetEvent::VSoundEffectNetEvent( void ) : mProfile( NULL ), 240 mPosition( 0.f ), 241 mPitch( 1.f ), 242 mIs3D( false ), 243 mTransform( MatrixF::Identity ) 244{ 245 // Void. 246} 247 248void VSoundEffectNetEvent::write( NetConnection *pConnection, BitStream *pStream ) 249{ 250 // Void. 251} 252 253void VSoundEffectNetEvent::pack( NetConnection *pConnection, BitStream *pStream ) 254{ 255 // Valid? 256 if ( !pStream->writeFlag( mProfile != NULL ) ) 257 { 258 return; 259 } 260 261 // Profile. 262 pStream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize ); 263 264 // Position. 265 pStream->write( mPosition ); 266 267 // Pitch. 268 pStream->write( mPitch ); 269 270 // 3D? 271 if ( pStream->writeFlag( mIs3D ) ) 272 { 273 // Rotation. 274 SFXDescription* description = mProfile->getDescription(); 275 if ( pStream->writeFlag( description->mConeInsideAngle || description->mConeOutsideAngle ) ) 276 { 277 // Entire Transform. 278 pStream->writeAffineTransform( mTransform ); 279 } 280 else 281 { 282 // Position. 283 mathWrite( *pStream, mTransform.getColumn3F( 3 ) ); 284 } 285 } 286} 287 288void VSoundEffectNetEvent::unpack( NetConnection *pConnection, BitStream *pStream ) 289{ 290 // Valid? 291 if ( !pStream->readFlag() ) 292 { 293 return; 294 } 295 296 // Profile. 297 Sim::findObject( pStream->readInt( DataBlockObjectIdBitSize ) + DataBlockObjectIdFirst, mProfile ); 298 299 // Position. 300 pStream->read( &mPosition ); 301 302 // Pitch. 303 pStream->read( &mPitch ); 304 305 // 3D? 306 if ( pStream->readFlag() ) 307 { 308 // Yup! 309 mIs3D = true; 310 311 // Rotation? 312 if ( pStream->readFlag() ) 313 { 314 // Transform. 315 pStream->readAffineTransform( &mTransform ); 316 } 317 else 318 { 319 // Position. 320 Point3F pos; 321 mathRead( *pStream, &pos ); 322 mTransform.setColumn( 3, pos ); 323 } 324 } 325} 326 327void VSoundEffectNetEvent::process( NetConnection *pConnection ) 328{ 329 // Valid? 330 if ( !mProfile ) 331 { 332 return; 333 } 334 335 SFXSound *source = NULL; 336 if ( mIs3D ) 337 { 338 // Play 3D Sound. 339 source = ( SFXSound* )SFX->playOnce( mProfile, &mTransform ); 340 } 341 else 342 { 343 // Play 2D Sound. 344 source = ( SFXSound* )SFX->playOnce( mProfile ); 345 } 346 347 if ( source ) 348 { 349 // Set Position. 350 source->setPosition( mPosition ); 351 352 // Set Pitch. 353 source->setPitch( mPitch ); 354 } 355} 356