VSoundEffectEvent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  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. #include "Verve/Core/VGroup.h"
  24. #include "Verve/Extension/SoundEffect/VSoundEffectEvent.h"
  25. #include "Verve/Extension/SoundEffect/VSoundEffectTrack.h"
  26. #include "console/consoleTypes.h"
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT( VSoundEffectEvent );
  29. //-----------------------------------------------------------------------------
  30. VSoundEffectEvent::VSoundEffectEvent( void ) :
  31. mSoundEffect( NULL )
  32. {
  33. setLabel( "SoundEvent" );
  34. }
  35. void VSoundEffectEvent::initPersistFields( void )
  36. {
  37. Parent::initPersistFields();
  38. addProtectedField( "SoundEffect", TYPEID<VTorque::SoundEffectType>(), Offset( mSoundEffect, VSoundEffectEvent ), &setSoundData, &defaultProtectedGetFn, "" );
  39. }
  40. //-----------------------------------------------------------------------------
  41. //
  42. // Callback Methods.
  43. //
  44. //-----------------------------------------------------------------------------
  45. //-----------------------------------------------------------------------------
  46. //
  47. // VSoundEffectEvent::onTrigger( pTime, pDelta );
  48. //
  49. // Play the target sound effect. If this track belongs to a SceneObjectGroup,
  50. // then the sound will play with the reference object's transform. If this is
  51. // not the case, then a 2D sound will be played.
  52. //
  53. //-----------------------------------------------------------------------------
  54. void VSoundEffectEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
  55. {
  56. Parent::onTrigger( pTime, pDelta );
  57. // Fetch Track.
  58. VSoundEffectTrack *track;
  59. if ( !getTrack( track ) )
  60. {
  61. return;
  62. }
  63. // Position & Pitch.
  64. U32 position = mAbs( ( pTime + pDelta ) - getStartTime() );
  65. F32 pitch = mFabs( getControllerTimeScale() );
  66. if ( position < SFXStartBuffer )
  67. {
  68. // Zero.
  69. position = 0;
  70. }
  71. VSceneObjectGroup *group;
  72. if ( getGroup( group ) )
  73. {
  74. // Play Sound With Reference.
  75. track->mSource = VTorque::playSound( mSoundEffect, group->getSceneObject(), position, pitch );
  76. }
  77. else
  78. {
  79. // Play Sound.
  80. track->mSource = VTorque::playSound( mSoundEffect, position, pitch );
  81. }
  82. }
  83. //-----------------------------------------------------------------------------
  84. //
  85. // Property Methods.
  86. //
  87. //-----------------------------------------------------------------------------
  88. //-----------------------------------------------------------------------------
  89. //
  90. // VSoundEffectEvent::setDuration( pDuration );
  91. //
  92. // This event's duration is always set to the sound object's duration.
  93. //
  94. //-----------------------------------------------------------------------------
  95. void VSoundEffectEvent::setDuration( const S32 &pDuration )
  96. {
  97. // Clear Duration.
  98. mDuration = VTorque::getSoundDuration( mSoundEffect );
  99. }
  100. //-----------------------------------------------------------------------------
  101. //
  102. // Static Field Methods.
  103. //
  104. //-----------------------------------------------------------------------------
  105. bool VSoundEffectEvent::setSoundData( void *pObject, const char *pArray, const char *pData )
  106. {
  107. // Fetch Event.
  108. VSoundEffectEvent *event = static_cast<VSoundEffectEvent*>( pObject );
  109. // Use Object.
  110. event->mSoundEffect = dynamic_cast<VTorque::SoundEffectType*>( Sim::findObject( pData ) );
  111. // Set Duration.
  112. event->setDuration( 0 );
  113. return false;
  114. }