VSoundEffect.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/VerveConfig.h"
  24. #include "Verve/Torque3D/VSoundEffect.h"
  25. #include "T3D/gameBase/gameConnection.h"
  26. #include "core/stream/bitStream.h"
  27. #include "math/mathIO.h"
  28. #include "sfx/sfxSystem.h"
  29. #include "sfx/sfxDescription.h"
  30. //-----------------------------------------------------------------------------
  31. //
  32. // Sound Methods.
  33. //
  34. //-----------------------------------------------------------------------------
  35. bool VTorque::isSoundLooping( SoundEffectType *pSoundProfile )
  36. {
  37. if ( !pSoundProfile )
  38. {
  39. // Sanity!
  40. return false;
  41. }
  42. // Return Looping.
  43. return pSoundProfile->getDescription()->mIsLooping;
  44. }
  45. S32 VTorque::getSoundDuration( SoundEffectType *pSoundProfile )
  46. {
  47. if ( !pSoundProfile )
  48. {
  49. // Sanity!
  50. return 0;
  51. }
  52. // Return Duration.
  53. return pSoundProfile->getSoundDuration();
  54. }
  55. VTorque::SoundSourceType *VTorque::playSound( SoundEffectType *pSoundProfile, const U32 &pPosition, const F32 &pPitch )
  56. {
  57. if ( !pSoundProfile )
  58. {
  59. // Sanity!
  60. return NULL;
  61. }
  62. #ifdef VT_EDITOR
  63. // Play Sound.
  64. SFXSound *source = ( SFXSound* )SFX->playOnce( pSoundProfile );
  65. if ( source )
  66. {
  67. // Set Position.
  68. source->setPosition( pPosition );
  69. // Set Pitch.
  70. source->setPitch( pPitch );
  71. }
  72. // Return Source.
  73. return source;
  74. #else
  75. // Fetch Client Group.
  76. SimGroup* clientGroup = Sim::getClientGroup();
  77. for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ )
  78. {
  79. NetConnection *connection = static_cast<NetConnection*>( *itr );
  80. if ( connection )
  81. {
  82. // Create Event.
  83. VSoundEffectNetEvent *event = new VSoundEffectNetEvent();
  84. // Setup Event.
  85. event->mProfile = pSoundProfile;
  86. event->mPosition = pPosition;
  87. event->mPitch = pPitch;
  88. event->mIs3D = false;
  89. // Post Event.
  90. connection->postNetEvent( event );
  91. }
  92. }
  93. return NULL;
  94. #endif
  95. }
  96. VTorque::SoundSourceType *VTorque::playSound( SoundEffectType *pSoundProfile, SceneObjectType *pObject, const U32 &pPosition, const F32 &pPitch )
  97. {
  98. if ( !pSoundProfile )
  99. {
  100. // Sanity!
  101. return NULL;
  102. }
  103. #ifdef VT_EDITOR
  104. // Fetch Reference Transform.
  105. const MatrixF &transform = pObject->getTransform();
  106. // Play Sound.
  107. SFXSound *source = ( SFXSound* )SFX->playOnce( pSoundProfile, &transform );
  108. if ( source )
  109. {
  110. // Set Position.
  111. source->setPosition( pPosition );
  112. // Set Pitch.
  113. source->setPitch( pPitch );
  114. }
  115. // Return Source.
  116. return source;
  117. #else
  118. // Fetch Client Group.
  119. SimGroup* clientGroup = Sim::getClientGroup();
  120. for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ )
  121. {
  122. NetConnection *connection = static_cast<NetConnection*>( *itr );
  123. if ( connection )
  124. {
  125. // Create Event.
  126. VSoundEffectNetEvent *event = new VSoundEffectNetEvent();
  127. // Setup Event.
  128. event->mProfile = pSoundProfile;
  129. event->mPosition = pPosition;
  130. event->mPitch = pPitch;
  131. event->mIs3D = true;
  132. event->mTransform = pObject->getTransform();
  133. // Post Event.
  134. connection->postNetEvent( event );
  135. }
  136. }
  137. return NULL;
  138. #endif
  139. }
  140. void VTorque::playSound( SoundSourceType *pSource )
  141. {
  142. if ( !pSource )
  143. {
  144. // Sanity!
  145. return;
  146. }
  147. // Play.
  148. pSource->play();
  149. }
  150. void VTorque::pauseSound( SoundSourceType *pSource )
  151. {
  152. if ( !pSource )
  153. {
  154. // Sanity!
  155. return;
  156. }
  157. // Pause.
  158. pSource->pause();
  159. }
  160. void VTorque::stopSound( SoundSourceType *pSource )
  161. {
  162. if ( !pSource )
  163. {
  164. // Sanity!
  165. return;
  166. }
  167. // Stop.
  168. pSource->stop();
  169. }
  170. void VTorque::setSoundPosition( SoundSourceType *pSource, const U32 &pPosition )
  171. {
  172. if ( !pSource )
  173. {
  174. // Sanity!
  175. return;
  176. }
  177. // Set Position.
  178. pSource->setPosition( pPosition );
  179. }
  180. void VTorque::setSoundPitch( SoundSourceType *pSource, const F32 &pPitch )
  181. {
  182. if ( !pSource )
  183. {
  184. // Sanity!
  185. return;
  186. }
  187. // Set Pitch.
  188. pSource->setPitch( pPitch );
  189. }
  190. //-----------------------------------------------------------------------------
  191. IMPLEMENT_CO_CLIENTEVENT_V1( VSoundEffectNetEvent );
  192. //-----------------------------------------------------------------------------
  193. VSoundEffectNetEvent::VSoundEffectNetEvent( void ) : mProfile( NULL ),
  194. mPosition( 0.f ),
  195. mPitch( 1.f ),
  196. mIs3D( false ),
  197. mTransform( MatrixF::Identity )
  198. {
  199. // Void.
  200. }
  201. void VSoundEffectNetEvent::write( NetConnection *pConnection, BitStream *pStream )
  202. {
  203. // Void.
  204. }
  205. void VSoundEffectNetEvent::pack( NetConnection *pConnection, BitStream *pStream )
  206. {
  207. // Valid?
  208. if ( !pStream->writeFlag( mProfile != NULL ) )
  209. {
  210. return;
  211. }
  212. // Profile.
  213. pStream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize );
  214. // Position.
  215. pStream->write( mPosition );
  216. // Pitch.
  217. pStream->write( mPitch );
  218. // 3D?
  219. if ( pStream->writeFlag( mIs3D ) )
  220. {
  221. // Rotation.
  222. SFXDescription* description = mProfile->getDescription();
  223. if ( pStream->writeFlag( description->mConeInsideAngle || description->mConeOutsideAngle ) )
  224. {
  225. // Entire Transform.
  226. pStream->writeAffineTransform( mTransform );
  227. }
  228. else
  229. {
  230. // Position.
  231. mathWrite( *pStream, mTransform.getColumn3F( 3 ) );
  232. }
  233. }
  234. }
  235. void VSoundEffectNetEvent::unpack( NetConnection *pConnection, BitStream *pStream )
  236. {
  237. // Valid?
  238. if ( !pStream->readFlag() )
  239. {
  240. return;
  241. }
  242. // Profile.
  243. Sim::findObject( pStream->readInt( DataBlockObjectIdBitSize ) + DataBlockObjectIdFirst, mProfile );
  244. // Position.
  245. pStream->read( &mPosition );
  246. // Pitch.
  247. pStream->read( &mPitch );
  248. // 3D?
  249. if ( pStream->readFlag() )
  250. {
  251. // Yup!
  252. mIs3D = true;
  253. // Rotation?
  254. if ( pStream->readFlag() )
  255. {
  256. // Transform.
  257. pStream->readAffineTransform( &mTransform );
  258. }
  259. else
  260. {
  261. // Position.
  262. Point3F pos;
  263. mathRead( *pStream, &pos );
  264. mTransform.setColumn( 3, pos );
  265. }
  266. }
  267. }
  268. void VSoundEffectNetEvent::process( NetConnection *pConnection )
  269. {
  270. // Valid?
  271. if ( !mProfile )
  272. {
  273. return;
  274. }
  275. SFXSound *source = NULL;
  276. if ( mIs3D )
  277. {
  278. // Play 3D Sound.
  279. source = ( SFXSound* )SFX->playOnce( mProfile, &mTransform );
  280. }
  281. else
  282. {
  283. // Play 2D Sound.
  284. source = ( SFXSound* )SFX->playOnce( mProfile );
  285. }
  286. if ( source )
  287. {
  288. // Set Position.
  289. source->setPosition( mPosition );
  290. // Set Pitch.
  291. source->setPitch( mPitch );
  292. }
  293. }