sfxAmbience.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "sfx/sfxAmbience.h"
  23. #include "sfx/sfxEnvironment.h"
  24. #include "sfx/sfxTrack.h"
  25. #include "sfx/sfxSystem.h"
  26. #include "sfx/sfxTypes.h"
  27. #include "math/mathTypes.h"
  28. #include "core/stream/bitStream.h"
  29. IMPLEMENT_CO_DATABLOCK_V1( SFXAmbience );
  30. ConsoleDocClass( SFXAmbience,
  31. "@brief A datablock that describes an ambient sound space.\n\n"
  32. "Each ambience datablock captures the properties of a unique ambient sound space. A sound space is comprised of:\n"
  33. "- an ambient audio track that is played when the listener is inside the space,\n"
  34. "- a reverb environment that is active inside the space, and\n"
  35. "- a number of SFXStates that are activated when entering the space and deactivated when exiting it.\n"
  36. "\n"
  37. "Each of these properties is optional.\n\n"
  38. "An important characteristic of ambient audio spaces is that their unique nature is not determined by their location "
  39. "in space but rather by their SFXAmbience datablock. This means that the same SFXAmbience datablock assigned to "
  40. "multiple locations in a level represents the same unique audio space to the sound system.\n\n"
  41. "This is an important distinction for the ambient sound mixer which will activate a given ambient audio space only "
  42. "once at any one time regardless of how many intersecting audio spaces with the same SFXAmbience datablock assigned "
  43. "the listener may currently be in.\n\n"
  44. "All SFXAmbience instances are automatically added to the global @c SFXAmbienceSet.\n\n"
  45. "At the moment, transitions between reverb environments are not blended and different reverb environments from multiple "
  46. "active SFXAmbiences will not be blended together. This will be added in a future version.\n\n"
  47. "@tsexample\n"
  48. "singleton SFXAmbience( Underwater )\n"
  49. "{\n"
  50. " environment = AudioEnvUnderwater;\n"
  51. " soundTrack = ScubaSoundList;\n"
  52. " states[ 0 ] = AudioLocationUnderwater;\n"
  53. "};\n"
  54. "@endtsexample\n\n"
  55. "@see SFXEnvironment\n"
  56. "@see SFXTrack\n"
  57. "@see SFXState\n"
  58. "@see LevelInfo::soundAmbience\n"
  59. "@see Zone::soundAmbience\n\n"
  60. "@ref Datablock_Networking\n"
  61. "@ingroup SFX\n"
  62. "@ingroup Datablocks\n"
  63. );
  64. SFXAmbience::ChangeSignal SFXAmbience::smChangeSignal;
  65. //-----------------------------------------------------------------------------
  66. SFXAmbience::SFXAmbience()
  67. : mDopplerFactor( 0.5f ),
  68. mRolloffFactor( 1.f ),
  69. mEnvironment( NULL )
  70. {
  71. dMemset( mState, 0, sizeof( mState ) );
  72. INIT_ASSET(SoundTrack);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void SFXAmbience::initPersistFields()
  76. {
  77. addGroup( "Sound" );
  78. addField( "environment", TypeSFXEnvironmentName, Offset( mEnvironment, SFXAmbience ),
  79. "Reverb environment active in the ambience zone.\n"
  80. "@ref SFX_reverb" );
  81. INITPERSISTFIELD_SOUNDASSET(SoundTrack, SFXAmbience,
  82. "Sound track to play in the ambience zone." );
  83. addField( "rolloffFactor", TypeF32, Offset( mRolloffFactor, SFXAmbience ),
  84. "The rolloff factor to apply to distance-based volume attenuation in this space.\n"
  85. "Defaults to 1.0.\n\n"
  86. "@note This applies to the logarithmic distance model only.\n\n"
  87. "@ref SFXSource_volume" );
  88. addField( "dopplerFactor", TypeF32, Offset( mDopplerFactor, SFXAmbience ),
  89. "The factor to apply to the doppler affect in this space.\n"
  90. "Defaults to 0.5.\n\n"
  91. "@ref SFXSource_doppler" );
  92. addField( "states", TypeSFXStateName, Offset( mState, SFXAmbience ),
  93. MaxStates,
  94. "States to activate when the ambient zone is entered.\n"
  95. "When the ambient sound state is entered, all states associated with the state will "
  96. "be activated (given that they are not disabled) and deactivated when the space "
  97. "is exited again." );
  98. endGroup( "Sound" );
  99. Parent::initPersistFields();
  100. }
  101. //-----------------------------------------------------------------------------
  102. bool SFXAmbience::onAdd()
  103. {
  104. if( !Parent::onAdd() )
  105. return false;
  106. Sim::getSFXAmbienceSet()->addObject( this );
  107. _setSoundTrack(getSoundTrack());
  108. return true;
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool SFXAmbience::preload( bool server, String& errorStr )
  112. {
  113. if( !Parent::preload( server, errorStr ) )
  114. return false;
  115. validate();
  116. // Resolve datablocks on client.
  117. if( !server )
  118. {
  119. if( !sfxResolve( &mEnvironment, errorStr ) )
  120. return false;
  121. _setSoundTrack(getSoundTrack());
  122. if (!getSoundTrackProfile())
  123. {
  124. Con::errorf("SFXAmbience::Preload() - unable to find sfxProfile for asset %s", mSoundTrackAssetId);
  125. return false;
  126. }
  127. for( U32 i = 0; i < MaxStates; ++ i )
  128. if( !sfxResolve( &mState[ i ], errorStr ) )
  129. return false;
  130. }
  131. return true;
  132. }
  133. //-----------------------------------------------------------------------------
  134. void SFXAmbience::packData( BitStream* stream )
  135. {
  136. Parent::packData( stream );
  137. sfxWrite( stream, mEnvironment );
  138. PACKDATA_ASSET(SoundTrack);
  139. stream->write( mRolloffFactor );
  140. stream->write( mDopplerFactor );
  141. for( U32 i = 0; i < MaxStates; ++ i )
  142. sfxWrite( stream, mState[ i ] );
  143. }
  144. //-----------------------------------------------------------------------------
  145. void SFXAmbience::unpackData( BitStream* stream )
  146. {
  147. Parent::unpackData( stream );
  148. sfxRead( stream, &mEnvironment );
  149. UNPACKDATA_ASSET(SoundTrack);
  150. stream->read( &mRolloffFactor );
  151. stream->read( &mDopplerFactor );
  152. for( U32 i = 0; i < MaxStates; ++ i )
  153. sfxRead( stream, &mState[ i ] );
  154. }
  155. //-----------------------------------------------------------------------------
  156. void SFXAmbience::inspectPostApply()
  157. {
  158. Parent::inspectPostApply();
  159. validate();
  160. smChangeSignal.trigger( this );
  161. }
  162. //-----------------------------------------------------------------------------
  163. void SFXAmbience::validate()
  164. {
  165. }