123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- //-----------------------------------------------------------------------------
- // Verve
- // Copyright (C) 2014 - Violent Tulip
- //
- // 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.
- //-----------------------------------------------------------------------------
- #include "Verve/VerveConfig.h"
- #include "Verve/Torque3D/VSoundEffect.h"
- #include "T3D/gameBase/gameConnection.h"
- #include "core/stream/bitStream.h"
- #include "math/mathIO.h"
- #include "sfx/sfxSystem.h"
- #include "sfx/sfxDescription.h"
- //-----------------------------------------------------------------------------
- //
- // Sound Methods.
- //
- //-----------------------------------------------------------------------------
- bool VTorque::isSoundLooping( SoundEffectType *pSoundProfile )
- {
- if ( !pSoundProfile )
- {
- // Sanity!
- return false;
- }
- // Return Looping.
- return pSoundProfile->getDescription()->mIsLooping;
- }
- S32 VTorque::getSoundDuration( SoundEffectType *pSoundProfile )
- {
- if ( !pSoundProfile )
- {
- // Sanity!
- return 0;
- }
- // Return Duration.
- return pSoundProfile->getSoundDuration();
- }
- VTorque::SoundSourceType *VTorque::playSound( SoundEffectType *pSoundProfile, const U32 &pPosition, const F32 &pPitch )
- {
- if ( !pSoundProfile )
- {
- // Sanity!
- return NULL;
- }
- #ifdef VT_EDITOR
- // Play Sound.
- SFXSound *source = ( SFXSound* )SFX->playOnce( pSoundProfile );
- if ( source )
- {
- // Set Position.
- source->setPosition( pPosition );
- // Set Pitch.
- source->setPitch( pPitch );
- }
- // Return Source.
- return source;
- #else
- // Fetch Client Group.
- SimGroup* clientGroup = Sim::getClientGroup();
- for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ )
- {
- NetConnection *connection = static_cast<NetConnection*>( *itr );
- if ( connection )
- {
- // Create Event.
- VSoundEffectNetEvent *event = new VSoundEffectNetEvent();
- // Setup Event.
- event->mProfile = pSoundProfile;
- event->mPosition = pPosition;
- event->mPitch = pPitch;
- event->mIs3D = false;
- // Post Event.
- connection->postNetEvent( event );
- }
- }
- return NULL;
- #endif
- }
- VTorque::SoundSourceType *VTorque::playSound( SoundEffectType *pSoundProfile, SceneObjectType *pObject, const U32 &pPosition, const F32 &pPitch )
- {
- if ( !pSoundProfile )
- {
- // Sanity!
- return NULL;
- }
- #ifdef VT_EDITOR
- // Fetch Reference Transform.
- const MatrixF &transform = pObject->getTransform();
- // Play Sound.
- SFXSound *source = ( SFXSound* )SFX->playOnce( pSoundProfile, &transform );
- if ( source )
- {
- // Set Position.
- source->setPosition( pPosition );
- // Set Pitch.
- source->setPitch( pPitch );
- }
- // Return Source.
- return source;
- #else
- // Fetch Client Group.
- SimGroup* clientGroup = Sim::getClientGroup();
- for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ )
- {
- NetConnection *connection = static_cast<NetConnection*>( *itr );
- if ( connection )
- {
- // Create Event.
- VSoundEffectNetEvent *event = new VSoundEffectNetEvent();
- // Setup Event.
- event->mProfile = pSoundProfile;
- event->mPosition = pPosition;
- event->mPitch = pPitch;
- event->mIs3D = true;
- event->mTransform = pObject->getTransform();
- // Post Event.
- connection->postNetEvent( event );
- }
- }
- return NULL;
- #endif
- }
- void VTorque::playSound( SoundSourceType *pSource )
- {
- if ( !pSource )
- {
- // Sanity!
- return;
- }
- // Play.
- pSource->play();
- }
- void VTorque::pauseSound( SoundSourceType *pSource )
- {
- if ( !pSource )
- {
- // Sanity!
- return;
- }
- // Pause.
- pSource->pause();
- }
- void VTorque::stopSound( SoundSourceType *pSource )
- {
- if ( !pSource )
- {
- // Sanity!
- return;
- }
- // Stop.
- pSource->stop();
- }
- void VTorque::setSoundPosition( SoundSourceType *pSource, const U32 &pPosition )
- {
- if ( !pSource )
- {
- // Sanity!
- return;
- }
- // Set Position.
- pSource->setPosition( pPosition );
- }
- void VTorque::setSoundPitch( SoundSourceType *pSource, const F32 &pPitch )
- {
- if ( !pSource )
- {
- // Sanity!
- return;
- }
- // Set Pitch.
- pSource->setPitch( pPitch );
- }
- //-----------------------------------------------------------------------------
- IMPLEMENT_CO_CLIENTEVENT_V1( VSoundEffectNetEvent );
- //-----------------------------------------------------------------------------
- VSoundEffectNetEvent::VSoundEffectNetEvent( void ) : mProfile( NULL ),
- mPosition( 0.f ),
- mPitch( 1.f ),
- mIs3D( false ),
- mTransform( MatrixF::Identity )
- {
- // Void.
- }
- void VSoundEffectNetEvent::write( NetConnection *pConnection, BitStream *pStream )
- {
- // Void.
- }
- void VSoundEffectNetEvent::pack( NetConnection *pConnection, BitStream *pStream )
- {
- // Valid?
- if ( !pStream->writeFlag( mProfile != NULL ) )
- {
- return;
- }
- // Profile.
- pStream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize );
- // Position.
- pStream->write( mPosition );
- // Pitch.
- pStream->write( mPitch );
- // 3D?
- if ( pStream->writeFlag( mIs3D ) )
- {
- // Rotation.
- SFXDescription* description = mProfile->getDescription();
- if ( pStream->writeFlag( description->mConeInsideAngle || description->mConeOutsideAngle ) )
- {
- // Entire Transform.
- pStream->writeAffineTransform( mTransform );
- }
- else
- {
- // Position.
- mathWrite( *pStream, mTransform.getColumn3F( 3 ) );
- }
- }
- }
- void VSoundEffectNetEvent::unpack( NetConnection *pConnection, BitStream *pStream )
- {
- // Valid?
- if ( !pStream->readFlag() )
- {
- return;
- }
- // Profile.
- Sim::findObject( pStream->readInt( DataBlockObjectIdBitSize ) + DataBlockObjectIdFirst, mProfile );
- // Position.
- pStream->read( &mPosition );
- // Pitch.
- pStream->read( &mPitch );
- // 3D?
- if ( pStream->readFlag() )
- {
- // Yup!
- mIs3D = true;
- // Rotation?
- if ( pStream->readFlag() )
- {
- // Transform.
- pStream->readAffineTransform( &mTransform );
- }
- else
- {
- // Position.
- Point3F pos;
- mathRead( *pStream, &pos );
- mTransform.setColumn( 3, pos );
- }
- }
- }
- void VSoundEffectNetEvent::process( NetConnection *pConnection )
- {
- // Valid?
- if ( !mProfile )
- {
- return;
- }
- SFXSound *source = NULL;
- if ( mIs3D )
- {
- // Play 3D Sound.
- source = ( SFXSound* )SFX->playOnce( mProfile, &mTransform );
- }
- else
- {
- // Play 2D Sound.
- source = ( SFXSound* )SFX->playOnce( mProfile );
- }
- if ( source )
- {
- // Set Position.
- source->setPosition( mPosition );
- // Set Pitch.
- source->setPitch( mPitch );
- }
- }
|