2018-01-28 20:48:02 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2013 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
# ifndef SOUND_ASSET_H
# include "SoundAsset.h"
# endif
# ifndef _ASSET_MANAGER_H_
# include "assets/assetManager.h"
# endif
# ifndef _CONSOLETYPES_H_
# include "console/consoleTypes.h"
# endif
# ifndef _TAML_
# include "persistence/taml/taml.h"
# endif
# ifndef _ASSET_PTR_H_
# include "assets/assetPtr.h"
# endif
2021-08-31 05:54:05 +00:00
# ifndef _SFXSOURCE_H_
# include "sfx/sfxSource.h"
# endif
2018-01-28 20:48:02 +00:00
// Debug Profiling.
# include "platform/profiler.h"
2021-07-19 06:07:08 +00:00
# include "sfx/sfxTypes.h"
2018-01-28 20:48:02 +00:00
//-----------------------------------------------------------------------------
IMPLEMENT_CONOBJECT ( SoundAsset ) ;
2021-07-19 06:07:08 +00:00
ConsoleType ( SoundAssetPtr , TypeSoundAssetPtr , const char * , ASSET_ID_FIELD_PREFIX )
2018-01-28 20:48:02 +00:00
//-----------------------------------------------------------------------------
ConsoleGetType ( TypeSoundAssetPtr )
{
// Fetch asset Id.
2021-07-19 06:07:08 +00:00
return * ( ( const char * * ) ( dptr ) ) ;
2018-01-28 20:48:02 +00:00
}
//-----------------------------------------------------------------------------
ConsoleSetType ( TypeSoundAssetPtr )
{
// Was a single argument specified?
if ( argc = = 1 )
{
// Yes, so fetch field value.
2021-07-19 06:07:08 +00:00
* ( ( const char * * ) dptr ) = StringTable - > insert ( argv [ 0 ] ) ;
2018-01-28 20:48:02 +00:00
2021-07-19 06:07:08 +00:00
return ;
}
2018-01-28 20:48:02 +00:00
2021-07-19 06:07:08 +00:00
// Warn.
Con : : warnf ( " (TypeSoundAssetPtr) - Cannot set multiple args to a single asset. " ) ;
}
//-----------------------------------------------------------------------------
2018-01-28 20:48:02 +00:00
2021-07-19 06:07:08 +00:00
ConsoleType ( assetIdString , TypeSoundAssetId , const char * , ASSET_ID_FIELD_PREFIX )
ConsoleGetType ( TypeSoundAssetId )
{
// Fetch asset Id.
return * ( ( const char * * ) ( dptr ) ) ;
}
ConsoleSetType ( TypeSoundAssetId )
{
// Was a single argument specified?
if ( argc = = 1 )
{
// Yes, so fetch field value.
* ( ( const char * * ) dptr ) = StringTable - > insert ( argv [ 0 ] ) ;
2018-01-28 20:48:02 +00:00
return ;
}
// Warn.
2021-07-19 06:07:08 +00:00
Con : : warnf ( " (TypeAssetId) - Cannot set multiple args to a single asset. " ) ;
2018-01-28 20:48:02 +00:00
}
//-----------------------------------------------------------------------------
SoundAsset : : SoundAsset ( )
{
2019-05-04 16:49:42 +00:00
mSoundFile = StringTable - > EmptyString ( ) ;
2020-08-09 06:32:27 +00:00
mSoundPath = StringTable - > EmptyString ( ) ;
2021-07-19 06:07:08 +00:00
mSubtitleString = StringTable - > EmptyString ( ) ;
mLoadedState = AssetErrCode : : NotLoaded ;
mPreload = false ;
// SFX description inits
// reverb is useless here, reverb is inacted on listener.
mProfileDesc . mPitch = 1 ;
mProfileDesc . mVolume = 1 ;
mProfileDesc . mIs3D = false ;
mProfileDesc . mIsLooping = false ;
mProfileDesc . mIsStreaming = false ;
mProfileDesc . mUseHardware = false ;
mProfileDesc . mMinDistance = 1 ;
mProfileDesc . mMaxDistance = 100 ;
mProfileDesc . mConeInsideAngle = 360 ;
mProfileDesc . mConeOutsideAngle = 360 ;
mProfileDesc . mConeOutsideVolume = 1 ;
mProfileDesc . mRolloffFactor = - 1.0f ;
mProfileDesc . mScatterDistance = Point3F ( 0.f , 0.f , 0.f ) ;
mProfileDesc . mPriority = 1.0f ;
mProfileDesc . mSourceGroup = NULL ;
2018-01-28 20:48:02 +00:00
}
//-----------------------------------------------------------------------------
SoundAsset : : ~ SoundAsset ( )
{
}
//-----------------------------------------------------------------------------
void SoundAsset : : initPersistFields ( )
{
// Call parent.
Parent : : initPersistFields ( ) ;
2019-05-04 16:49:42 +00:00
addProtectedField ( " soundFile " , TypeAssetLooseFilePath , Offset ( mSoundFile , SoundAsset ) ,
& setSoundFile , & getSoundFile , " Path to the sound file. " ) ;
2018-01-28 20:48:02 +00:00
2021-07-19 06:07:08 +00:00
addField ( " pitchAdjust " , TypeF32 , Offset ( mProfileDesc . mPitch , SoundAsset ) , " Adjustment of the pitch value 1 is default. " ) ;
addField ( " volumeAdjust " , TypeF32 , Offset ( mProfileDesc . mVolume , SoundAsset ) , " Adjustment to the volume. " ) ;
addField ( " is3D " , TypeBool , Offset ( mProfileDesc . mIs3D , SoundAsset ) , " Set this sound to 3D. " ) ;
addField ( " isLooping " , TypeBool , Offset ( mProfileDesc . mIsLooping , SoundAsset ) , " Does this sound loop. " ) ;
// if streaming, a default packet size should be chosen for all sounds.
addField ( " isStreaming " , TypeBool , Offset ( mProfileDesc . mIsStreaming , SoundAsset ) , " Use streaming. " ) ;
//....why?
addField ( " useHardware " , TypeBool , Offset ( mProfileDesc . mUseHardware , SoundAsset ) , " Use hardware mixing for this sound. " ) ;
addField ( " minDistance " , TypeF32 , Offset ( mProfileDesc . mMinDistance , SoundAsset ) , " Minimum distance for sound. " ) ;
// more like it.
addField ( " maxDistance " , TypeF32 , Offset ( mProfileDesc . mMaxDistance , SoundAsset ) , " Max distance for sound. " ) ;
addField ( " coneInsideAngle " , TypeS32 , Offset ( mProfileDesc . mConeInsideAngle , SoundAsset ) , " Cone inside angle. " ) ;
addField ( " coneOutsideAngle " , TypeS32 , Offset ( mProfileDesc . mConeOutsideAngle , SoundAsset ) , " Cone outside angle. " ) ;
2021-08-31 05:54:05 +00:00
addField ( " coneOutsideVolume " , TypeF32 , Offset ( mProfileDesc . mConeOutsideVolume , SoundAsset ) , " Cone outside volume. " ) ;
2021-07-19 06:07:08 +00:00
addField ( " rolloffFactor " , TypeF32 , Offset ( mProfileDesc . mRolloffFactor , SoundAsset ) , " Rolloff factor. " ) ;
addField ( " scatterDistance " , TypePoint3F , Offset ( mProfileDesc . mScatterDistance , SoundAsset ) , " Randomization to the spacial position of the sound. " ) ;
addField ( " sourceGroup " , TypeSFXSourceName , Offset ( mProfileDesc . mSourceGroup , SoundAsset ) , " Group that sources playing with this description should be put into. " ) ;
2018-01-28 20:48:02 +00:00
}
//------------------------------------------------------------------------------
void SoundAsset : : copyTo ( SimObject * object )
{
// Call to parent.
Parent : : copyTo ( object ) ;
}
void SoundAsset : : initializeAsset ( void )
{
2021-07-19 06:07:08 +00:00
Parent : : initializeAsset ( ) ;
if ( mSoundFile = = StringTable - > EmptyString ( ) )
return ;
2021-08-22 04:12:37 +00:00
mSoundPath = getOwned ( ) ? expandAssetFilePath ( mSoundFile ) : mSoundPath ;
2021-07-19 06:07:08 +00:00
loadSound ( ) ;
}
void SoundAsset : : _onResourceChanged ( const Torque : : Path & path )
{
if ( path ! = Torque : : Path ( mSoundPath ) )
return ;
refreshAsset ( ) ;
loadSound ( ) ;
2018-01-28 20:48:02 +00:00
}
void SoundAsset : : onAssetRefresh ( void )
{
2021-07-19 06:07:08 +00:00
if ( mSoundFile = = StringTable - > EmptyString ( ) )
return ;
//Update
2021-08-22 04:12:37 +00:00
mSoundPath = getOwned ( ) ? expandAssetFilePath ( mSoundFile ) : mSoundPath ;
2021-07-19 06:07:08 +00:00
loadSound ( ) ;
}
bool SoundAsset : : loadSound ( )
{
if ( mSoundPath )
{
2021-07-23 02:27:13 +00:00
if ( ! Torque : : FS : : IsFile ( mSoundPath ) )
2021-07-19 06:07:08 +00:00
{
Con : : errorf ( " SoundAsset::initializeAsset: Attempted to load file %s but it was not valid! " , mSoundFile ) ;
mLoadedState = BadFileReference ;
return false ;
}
else
{ // = new SFXProfile(mProfileDesc, mSoundFile, mPreload);
mSFXProfile . setDescription ( & mProfileDesc ) ;
2021-08-31 05:54:05 +00:00
mSFXProfile . setSoundFileName ( mSoundPath ) ;
2021-07-19 06:07:08 +00:00
mSFXProfile . setPreload ( mPreload ) ;
}
}
mChangeSignal . trigger ( ) ;
mLoadedState = Ok ;
return true ;
2019-05-04 16:49:42 +00:00
}
void SoundAsset : : setSoundFile ( const char * pSoundFile )
{
// Sanity!
2021-07-19 06:07:08 +00:00
AssertFatal ( pSoundFile ! = NULL , " Cannot use a NULL sound file. " ) ;
2019-05-04 16:49:42 +00:00
2021-07-19 06:07:08 +00:00
// Fetch sound file.
2021-08-22 04:12:37 +00:00
pSoundFile = StringTable - > insert ( pSoundFile , true ) ;
2019-05-04 16:49:42 +00:00
// Ignore no change,
if ( pSoundFile = = mSoundFile )
return ;
// Update.
2021-08-22 04:12:37 +00:00
mSoundFile = getOwned ( ) ? expandAssetFilePath ( pSoundFile ) : pSoundFile ;
2019-05-04 16:49:42 +00:00
// Refresh the asset.
refreshAsset ( ) ;
}
2020-08-25 01:24:50 +00:00
2021-08-31 05:54:05 +00:00
StringTableEntry SoundAsset : : getAssetIdByFileName ( StringTableEntry fileName )
{
if ( fileName = = StringTable - > EmptyString ( ) )
return StringTable - > EmptyString ( ) ;
StringTableEntry materialAssetId = " " ;
AssetQuery query ;
U32 foundCount = AssetDatabase . findAssetType ( & query , " SoundAsset " ) ;
if ( foundCount ! = 0 )
{
for ( U32 i = 0 ; i < foundCount ; i + + )
{
SoundAsset * soundAsset = AssetDatabase . acquireAsset < SoundAsset > ( query . mAssetList [ i ] ) ;
if ( soundAsset & & soundAsset - > getSoundPath ( ) = = fileName )
{
materialAssetId = soundAsset - > getAssetId ( ) ;
AssetDatabase . releaseAsset ( query . mAssetList [ i ] ) ;
break ;
}
AssetDatabase . releaseAsset ( query . mAssetList [ i ] ) ;
}
}
return materialAssetId ;
}
U32 SoundAsset : : getAssetById ( StringTableEntry assetId , AssetPtr < SoundAsset > * materialAsset )
{
( * materialAsset ) = assetId ;
if ( materialAsset - > notNull ( ) )
{
return ( * materialAsset ) - > mLoadedState ;
}
else
{
//Well that's bad, loading the fallback failed.
Con : : warnf ( " MaterialAsset::getAssetById - Finding of asset with id %s failed with no fallback asset " , assetId ) ;
return AssetErrCode : : Failed ;
}
}
U32 SoundAsset : : getAssetByFileName ( StringTableEntry fileName , AssetPtr < SoundAsset > * soundAsset )
{
AssetQuery query ;
U32 foundAssetcount = AssetDatabase . findAssetType ( & query , " SoundAsset " ) ;
if ( foundAssetcount = = 0 )
{
//Well that's bad, loading the fallback failed.
Con : : warnf ( " MaterialAsset::getAssetByMaterialName - Finding of asset associated with filename %s failed with no fallback asset " , fileName ) ;
return AssetErrCode : : Failed ;
}
else
{
for ( U32 i = 0 ; i < foundAssetcount ; i + + )
{
SoundAsset * tSoundAsset = AssetDatabase . acquireAsset < SoundAsset > ( query . mAssetList [ i ] ) ;
if ( tSoundAsset & & tSoundAsset - > getSoundPath ( ) = = fileName )
{
soundAsset - > setAssetId ( query . mAssetList [ i ] ) ;
AssetDatabase . releaseAsset ( query . mAssetList [ i ] ) ;
return ( * soundAsset ) - > mLoadedState ;
}
AssetDatabase . releaseAsset ( query . mAssetList [ i ] ) ; //cleanup if that's not the one we needed
}
}
//No good match
return AssetErrCode : : Failed ;
}
2020-08-25 01:24:50 +00:00
DefineEngineMethod ( SoundAsset , getSoundPath , const char * , ( ) , , " " )
{
return object - > getSoundPath ( ) ;
}
2021-07-19 06:07:08 +00:00
2021-08-31 05:54:05 +00:00
DefineEngineMethod ( SoundAsset , playSound , S32 , ( Point3F position ) , ( Point3F : : Zero ) ,
" Gets the number of materials for this shape asset. \n "
" @return Material count. \n " )
{
if ( object - > getSfxProfile ( ) )
{
MatrixF transform ;
transform . setPosition ( position ) ;
SFXSource * source = SFX - > playOnce ( object - > getSfxProfile ( ) , & transform , NULL , - 1 ) ;
return source - > getId ( ) ;
}
else
return 0 ;
}
2021-07-19 06:07:08 +00:00
IMPLEMENT_CONOBJECT ( GuiInspectorTypeSoundAssetPtr ) ;
ConsoleDocClass ( GuiInspectorTypeSoundAssetPtr ,
" @brief Inspector field type for Sounds \n \n "
" Editor use only. \n \n "
" @internal "
) ;
void GuiInspectorTypeSoundAssetPtr : : consoleInit ( )
{
Parent : : consoleInit ( ) ;
ConsoleBaseType : : getType ( TypeSoundAssetPtr ) - > setInspectorFieldType ( " GuiInspectorTypeSoundAssetPtr " ) ;
}
GuiControl * GuiInspectorTypeSoundAssetPtr : : constructEditControl ( )
{
2021-08-31 05:54:05 +00:00
// Create base filename edit controls
GuiControl * retCtrl = Parent : : constructEditControl ( ) ;
if ( retCtrl = = NULL )
return retCtrl ;
// Change filespec
char szBuffer [ 512 ] ;
dSprintf ( szBuffer , sizeof ( szBuffer ) , " AssetBrowser.showDialog( \" SoundAsset \" , \" AssetBrowser.changeAsset \" , %s, \" \" ); " ,
getIdString ( ) ) ;
mBrowseButton - > setField ( " Command " , szBuffer ) ;
setDataField ( StringTable - > insert ( " targetObject " ) , NULL , mInspector - > getInspectObject ( ) - > getIdString ( ) ) ;
// Create "Open in Editor" button
mEditButton = new GuiBitmapButtonCtrl ( ) ;
dSprintf ( szBuffer , sizeof ( szBuffer ) , " AssetBrowser.editAsset(%d.getText()); " , retCtrl - > getId ( ) ) ;
mEditButton - > setField ( " Command " , szBuffer ) ;
char bitmapName [ 512 ] = " ToolsModule:SFXEmitter_image " ;
mEditButton - > setBitmap ( StringTable - > insert ( bitmapName ) ) ;
mEditButton - > setDataField ( StringTable - > insert ( " Profile " ) , NULL , " GuiButtonProfile " ) ;
mEditButton - > setDataField ( StringTable - > insert ( " tooltipprofile " ) , NULL , " GuiToolTipProfile " ) ;
mEditButton - > setDataField ( StringTable - > insert ( " hovertime " ) , NULL , " 1000 " ) ;
mEditButton - > setDataField ( StringTable - > insert ( " tooltip " ) , NULL , " Test play this sound " ) ;
mEditButton - > registerObject ( ) ;
addObject ( mEditButton ) ;
return retCtrl ;
2021-07-19 06:07:08 +00:00
}
bool GuiInspectorTypeSoundAssetPtr : : updateRects ( )
{
2021-08-31 05:54:05 +00:00
S32 dividerPos , dividerMargin ;
mInspector - > getDivider ( dividerPos , dividerMargin ) ;
Point2I fieldExtent = getExtent ( ) ;
Point2I fieldPos = getPosition ( ) ;
mCaptionRect . set ( 0 , 0 , fieldExtent . x - dividerPos - dividerMargin , fieldExtent . y ) ;
mEditCtrlRect . set ( fieldExtent . x - dividerPos + dividerMargin , 1 , dividerPos - dividerMargin - 34 , fieldExtent . y ) ;
bool resized = mEdit - > resize ( mEditCtrlRect . point , mEditCtrlRect . extent ) ;
if ( mBrowseButton ! = NULL )
{
mBrowseRect . set ( fieldExtent . x - 32 , 2 , 14 , fieldExtent . y - 4 ) ;
resized | = mBrowseButton - > resize ( mBrowseRect . point , mBrowseRect . extent ) ;
}
if ( mEditButton ! = NULL )
{
RectI shapeEdRect ( fieldExtent . x - 16 , 2 , 14 , fieldExtent . y - 4 ) ;
resized | = mEditButton - > resize ( shapeEdRect . point , shapeEdRect . extent ) ;
}
return resized ;
2021-07-19 06:07:08 +00:00
}
IMPLEMENT_CONOBJECT ( GuiInspectorTypeSoundAssetId ) ;
ConsoleDocClass ( GuiInspectorTypeSoundAssetId ,
" @brief Inspector field type for Sounds \n \n "
" Editor use only. \n \n "
" @internal "
) ;
void GuiInspectorTypeSoundAssetId : : consoleInit ( )
{
Parent : : consoleInit ( ) ;
ConsoleBaseType : : getType ( TypeSoundAssetId ) - > setInspectorFieldType ( " GuiInspectorTypeSoundAssetId " ) ;
}