AudioAsset.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _AUDIO_ASSET_H_
  23. #include "AudioAsset.h"
  24. #endif
  25. #ifndef _ASSET_PTR_H_
  26. #include "assets/assetPtr.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. ConsoleType( audioAssetPtr, TypeAudioAssetPtr, sizeof(AssetPtr<AudioAsset>), ASSET_ID_FIELD_PREFIX )
  33. //-----------------------------------------------------------------------------
  34. ConsoleGetType( TypeAudioAssetPtr )
  35. {
  36. // Fetch asset Id.
  37. return (*((AssetPtr<AudioAsset>*)dptr)).getAssetId();
  38. }
  39. //-----------------------------------------------------------------------------
  40. ConsoleSetType( TypeAudioAssetPtr )
  41. {
  42. // Was a single argument specified?
  43. if( argc == 1 )
  44. {
  45. // Yes, so fetch field value.
  46. const char* pFieldValue = argv[0];
  47. // Fetch asset pointer.
  48. AssetPtr<AudioAsset>* pAssetPtr = dynamic_cast<AssetPtr<AudioAsset>*>((AssetPtrBase*)(dptr));
  49. // Is the asset pointer the correct type?
  50. if ( pAssetPtr == NULL )
  51. {
  52. // No, so fail.
  53. Con::warnf( "(TypeAudioAssetPtr) - Failed to set asset Id '%d'.", pFieldValue );
  54. return;
  55. }
  56. // Set asset.
  57. pAssetPtr->setAssetId( pFieldValue );
  58. return;
  59. }
  60. // Warn.
  61. Con::warnf( "(TypeAudioAssetPtr) - Cannot set multiple args to a single asset." );
  62. }
  63. //--------------------------------------------------------------------------
  64. IMPLEMENT_CONOBJECT(AudioAsset);
  65. //--------------------------------------------------------------------------
  66. AudioAsset::AudioAsset()
  67. {
  68. mAudioFile = StringTable->EmptyString;
  69. mDescription.mVolume = 1.0f;
  70. mDescription.mVolumeChannel = 0;
  71. mDescription.mIsLooping = false;
  72. mDescription.mIsStreaming = false;
  73. mDescription.mIs3D = false;
  74. mDescription.mReferenceDistance = 1.0f;
  75. mDescription.mMaxDistance = 100.0f;
  76. mDescription.mEnvironmentLevel = 0.0f;
  77. mDescription.mConeInsideAngle = 360;
  78. mDescription.mConeOutsideAngle = 360;
  79. mDescription.mConeOutsideVolume = 1.0f;
  80. mDescription.mConeVector.set(0, 0, 1);
  81. }
  82. //--------------------------------------------------------------------------
  83. void AudioAsset::initPersistFields()
  84. {
  85. Parent::initPersistFields();
  86. addProtectedField("AudioFile", TypeAssetLooseFilePath, Offset(mAudioFile, AudioAsset), &setAudioFile, &getAudioFile, &defaultProtectedWriteFn, "" );
  87. addProtectedField("Volume", TypeF32, Offset(mDescription.mVolume, AudioAsset), &setVolume, &defaultProtectedGetFn, &writeVolume, "");
  88. addProtectedField("VolumeChannel", TypeS32, Offset(mDescription.mVolumeChannel, AudioAsset), &setVolumeChannel, &defaultProtectedGetFn, &writeVolumeChannel, "");
  89. addProtectedField("Looping", TypeBool, Offset(mDescription.mIsLooping, AudioAsset), &setLooping, &defaultProtectedGetFn, &writeLooping, "");
  90. addProtectedField("Streaming", TypeBool, Offset(mDescription.mIsStreaming, AudioAsset), &setStreaming, &defaultProtectedGetFn, &writeStreaming, "");
  91. //addField("is3D", TypeBool, Offset(mDescription.mIs3D, AudioAsset));
  92. //addField("referenceDistance", TypeF32, Offset(mDescription.mReferenceDistance, AudioAsset));
  93. //addField("maxDistance", TypeF32, Offset(mDescription.mMaxDistance, AudioAsset));
  94. //addField("coneInsideAngle", TypeS32, Offset(mDescription.mConeInsideAngle, AudioAsset));
  95. //addField("coneOutsideAngle", TypeS32, Offset(mDescription.mConeOutsideAngle, AudioAsset));
  96. //addField("coneOutsideVolume", TypeF32, Offset(mDescription.mConeOutsideVolume, AudioAsset));
  97. //addField("coneVector", TypePoint3F, Offset(mDescription.mConeVector, AudioAsset));
  98. //addField("environmentLevel", TypeF32, Offset(mDescription.mEnvironmentLevel, AudioAsset));
  99. }
  100. //------------------------------------------------------------------------------
  101. void AudioAsset::copyTo(SimObject* object)
  102. {
  103. // Call to parent.
  104. Parent::copyTo(object);
  105. // Cast to asset.
  106. AudioAsset* pAsset = static_cast<AudioAsset*>(object);
  107. // Sanity!
  108. AssertFatal(pAsset != NULL, "AudioAsset::copyTo() - Object is not the correct type.");
  109. // Copy state.
  110. pAsset->setAudioFile( getAudioFile() );
  111. pAsset->setVolume( getVolume() );
  112. pAsset->setVolumeChannel( getVolumeChannel() );
  113. pAsset->setLooping( getLooping() );
  114. pAsset->setStreaming( getStreaming() );
  115. }
  116. //--------------------------------------------------------------------------
  117. void AudioAsset::initializeAsset( void )
  118. {
  119. // Call parent.
  120. Parent::initializeAsset();
  121. // Ensure the audio-file is expanded.
  122. mAudioFile = expandAssetFilePath( mAudioFile );
  123. // Asset should never auto-unload.
  124. setAssetAutoUnload( false );
  125. // Clamp these for now.
  126. if (mDescription.mIs3D)
  127. {
  128. mDescription.mReferenceDistance = mClampF(mDescription.mReferenceDistance, 0.0f, mDescription.mReferenceDistance);
  129. mDescription.mMaxDistance = mDescription.mMaxDistance > mDescription.mReferenceDistance ? mDescription.mMaxDistance : (mDescription.mReferenceDistance+0.01f);
  130. mDescription.mEnvironmentLevel = mClampF(mDescription.mEnvironmentLevel, 0.0f, 1.0f);
  131. mDescription.mConeInsideAngle = mClamp(mDescription.mConeInsideAngle, 0, 360);
  132. mDescription.mConeOutsideAngle = mClamp(mDescription.mConeOutsideAngle, mDescription.mConeInsideAngle, 360);
  133. mDescription.mConeOutsideVolume = mClampF(mDescription.mConeOutsideVolume, 0.0f, 1.0f);
  134. mDescription.mConeVector.normalize();
  135. }
  136. }
  137. //--------------------------------------------------------------------------
  138. void AudioAsset::setAudioFile( const char* pAudioFile )
  139. {
  140. // Sanity!
  141. AssertFatal( pAudioFile != NULL, "Cannot use a NULL audio filename." );
  142. // Fetch audio filename.
  143. pAudioFile = StringTable->insert( pAudioFile );
  144. // Ignore no change,
  145. if ( pAudioFile == mAudioFile )
  146. return;
  147. // Update.
  148. mAudioFile = getOwned() ? expandAssetFilePath( pAudioFile ) : pAudioFile;
  149. // Refresh the asset.
  150. refreshAsset();
  151. }
  152. //--------------------------------------------------------------------------
  153. void AudioAsset::setVolume( const F32 volume )
  154. {
  155. // Ignore no change.
  156. if ( mIsEqual( volume, mDescription.mVolume ) )
  157. return;
  158. // Update.
  159. mDescription.mVolume = mClampF(volume, 0.0f, 1.0f);;
  160. // Refresh the asset.
  161. refreshAsset();
  162. }
  163. //--------------------------------------------------------------------------
  164. void AudioAsset::setVolumeChannel( const S32 volumeChannel )
  165. {
  166. // Ignore no change.
  167. if ( volumeChannel == mDescription.mVolumeChannel )
  168. return;
  169. // Update.
  170. mDescription.mVolumeChannel = mClamp( volumeChannel, 0, Audio::AudioVolumeChannels-1 );
  171. // Refresh the asset.
  172. refreshAsset();
  173. }
  174. //--------------------------------------------------------------------------
  175. void AudioAsset::setLooping( const bool looping )
  176. {
  177. // Ignore no change.
  178. if ( looping == mDescription.mIsLooping )
  179. return;
  180. // Update.
  181. mDescription.mIsLooping = looping;
  182. // Refresh the asset.
  183. refreshAsset();
  184. }
  185. //--------------------------------------------------------------------------
  186. void AudioAsset::setStreaming( const bool streaming )
  187. {
  188. // Ignore no change.
  189. if ( streaming == mDescription.mIsStreaming )
  190. return;
  191. // UPdate.
  192. mDescription.mIsStreaming = streaming;
  193. // Refresh the asset.
  194. refreshAsset();
  195. }
  196. //--------------------------------------------------------------------------
  197. void AudioAsset::setDescription( const Audio::Description& audioDescription )
  198. {
  199. // Update.
  200. mDescription = audioDescription;
  201. // Refresh the asset.
  202. refreshAsset();
  203. }
  204. //-----------------------------------------------------------------------------
  205. void AudioAsset::onTamlPreWrite( void )
  206. {
  207. // Call parent.
  208. Parent::onTamlPreWrite();
  209. // Ensure the audio-file is collapsed.
  210. mAudioFile = collapseAssetFilePath( mAudioFile );
  211. }
  212. //-----------------------------------------------------------------------------
  213. void AudioAsset::onTamlPostWrite( void )
  214. {
  215. // Call parent.
  216. Parent::onTamlPostWrite();
  217. // Ensure the audio-file is expanded.
  218. mAudioFile = expandAssetFilePath( mAudioFile );
  219. }