sfxTrack.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "sfx/sfxTrack.h"
  27. #include "sfx/sfxTypes.h"
  28. #include "sfx/sfxDescription.h"
  29. #include "sfx/sfxSystem.h"
  30. #include "core/stream/bitStream.h"
  31. IMPLEMENT_CO_DATABLOCK_V1( SFXTrack );
  32. ConsoleDocClass( SFXTrack,
  33. "@brief Abstract base class for sound data that can be played back by the sound system.\n\n"
  34. "The term \"track\" is used in the sound system to refer to any entity that can be played "
  35. "back as a sound source. These can be individual files (SFXProfile), patterns of other tracks "
  36. "(SFXPlayList).\n\n"
  37. "Any track must be paired with a SFXDescription that tells the sound system how to set up "
  38. "playback for the track.\n\n"
  39. "All objects that are of type SFXTrack will automatically be added to @c SFXTrackSet.\n\n"
  40. "@note This class cannot be instantiated directly.\n\n"
  41. "@ingroup SFX\n"
  42. "@ingroup Datablocks\n"
  43. );
  44. //-----------------------------------------------------------------------------
  45. SFXTrack::SFXTrack()
  46. : mDescription( NULL )
  47. {
  48. dMemset( mParameters, 0, sizeof( mParameters ) );
  49. }
  50. //-----------------------------------------------------------------------------
  51. SFXTrack::SFXTrack( SFXDescription* description )
  52. : mDescription( description )
  53. {
  54. dMemset( mParameters, 0, sizeof( mParameters ) );
  55. }
  56. SFXTrack::SFXTrack(const SFXTrack& other, bool temp_clone) : SimDataBlock(other, temp_clone)
  57. {
  58. mDescription = other.mDescription;
  59. dMemcpy(mParameters, other.mParameters, sizeof(mParameters));
  60. }
  61. //-----------------------------------------------------------------------------
  62. void SFXTrack::initPersistFields()
  63. {
  64. addGroup( "Sound" );
  65. addField( "description", TypeSFXDescriptionName, Offset( mDescription, SFXTrack ),
  66. "Playback setup description for this track.\n\n"
  67. "If unassigned, the description named \"AudioEffects\" will automatically be assigned to the track. If this description "
  68. "is not defined, track creation will fail." );
  69. addField( "parameters", TypeSFXParameterName, Offset( mParameters, SFXTrack ), MaxNumParameters,
  70. "Parameters to automatically attach to SFXSources created from this track.\n"
  71. "Individual parameters are identified by their #internalName." );
  72. endGroup( "Sound" );
  73. Parent::initPersistFields();
  74. }
  75. //-----------------------------------------------------------------------------
  76. bool SFXTrack::processArguments( S32 argc, ConsoleValue *argv )
  77. {
  78. if( typeid( *this ) == typeid( SFXTrack ) )
  79. {
  80. Con::errorf( ConsoleLogEntry::Script, "SFXTrack is an abstract base class that cannot be instantiated directly!" );
  81. return false;
  82. }
  83. return Parent::processArguments( argc, argv );
  84. }
  85. //-----------------------------------------------------------------------------
  86. void SFXTrack::setParameter( U32 index, const char* name )
  87. {
  88. AssertFatal( index < MaxNumParameters, "SFXTrack::setParameter() - index out of range" );
  89. mParameters[ index ] = StringTable->insert( name );
  90. }
  91. //-----------------------------------------------------------------------------
  92. void SFXTrack::packData( BitStream* stream )
  93. {
  94. Parent::packData( stream );
  95. sfxWrite( stream, mDescription );
  96. for( U32 i = 0; i < MaxNumParameters; ++ i )
  97. if( stream->writeFlag( mParameters[ i ] ) )
  98. stream->writeString( mParameters[ i ] );
  99. }
  100. //-----------------------------------------------------------------------------
  101. void SFXTrack::unpackData( BitStream* stream )
  102. {
  103. Parent::unpackData( stream );
  104. sfxRead( stream, &mDescription );
  105. for( U32 i = 0; i < MaxNumParameters; ++ i )
  106. if( stream->readFlag() )
  107. mParameters[ i ] = stream->readSTString();
  108. else
  109. mParameters[ i ] = NULL;
  110. }
  111. //-----------------------------------------------------------------------------
  112. bool SFXTrack::preload( bool server, String& errorStr )
  113. {
  114. if( !Parent::preload( server, errorStr ) )
  115. return false;
  116. if( !server )
  117. {
  118. if( !sfxResolve( &mDescription, errorStr ) )
  119. return false;
  120. }
  121. return true;
  122. }
  123. //-----------------------------------------------------------------------------
  124. bool SFXTrack::onAdd()
  125. {
  126. if( !Parent::onAdd() )
  127. return false;
  128. // If we have no SFXDescription, try to grab a default.
  129. if( !mDescription )
  130. {
  131. if( !Sim::findObject( "AudioEffects", mDescription ) && Sim::getSFXDescriptionSet()->size() > 0 )
  132. mDescription = dynamic_cast< SFXDescription* >( Sim::getSFXDescriptionSet()->at( 0 ) );
  133. if( !mDescription )
  134. {
  135. Con::errorf(
  136. "SFXTrack(%s)::onAdd: The profile is missing a description!",
  137. getName() );
  138. return false;
  139. }
  140. }
  141. Sim::getSFXTrackSet()->addObject( this );
  142. return true;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void SFXTrack::inspectPostApply()
  146. {
  147. Parent::inspectPostApply();
  148. if( SFX )
  149. SFX->notifyTrackChanged( this );
  150. }