"L. Spiro Engine"
|
00001 00021 #ifndef __LSS_SOUNDSOURCE_H__ 00022 #define __LSS_SOUNDSOURCE_H__ 00023 00024 #include "../LSSSoundLib.h" 00025 #include "LSSSoundSourceTypes.h" 00026 #include "Vector/LSMVector3.h" 00027 #include <cassert> 00028 00029 namespace lss { 00030 00034 enum { 00035 LSS_UNIQUE_ID = 0x10000 00036 }; 00037 00049 class CSoundSource { 00050 friend class CSoundManager; 00051 00052 public : 00053 // == Various constructors. 00054 LSE_INLINE LSE_CALLCTOR CSoundSource( LSUINT32 _ui32Id ); 00055 virtual LSE_INLINE LSE_CALLCTOR ~CSoundSource(); 00056 00057 00058 // == Enumerations. 00062 enum LSS_ERROR { 00063 LSS_E_INVALID_BUFFER = 0, 00064 }; 00065 00069 enum LSS_SOUND_STATE { 00070 LSS_SS_PLAYING, 00071 LSS_SS_PAUSED, 00072 LSS_SS_STOPPED, 00073 }; 00074 00078 enum LSS_LOOP_MODE { 00079 LSS_LM_INFINITE = ~0, 00080 LSS_LM_FORCEDWORD = 0x7FFFFFFFUL, 00081 }; 00082 00083 // Maximum QWORD value is used to represent a non-expirable sound. 00084 #define LSS_NONEXPIRABLE static_cast<LSUINT64>(~0ULL) 00085 00086 00087 // == Functions. 00094 // 00095 virtual LSS_SOUND_SOURCE LSE_CALL GetType() const = 0; 00096 00102 virtual LSBOOL LSE_CALL Static() const = 0; 00103 00109 LSUINT32 LSE_CALL GetId() const; 00110 00116 LSS_SOUND_STATE LSE_CALL GetState() const; 00117 00128 virtual LSBOOL LSE_CALL Play( LSUINT32 _ui32LoopCount = 1 ) = 0; 00129 00135 virtual LSBOOL LSE_CALL Pause() = 0; 00136 00142 virtual LSBOOL LSE_CALL Stop() = 0; 00143 00150 const CVector3 & LSE_CALL GetPos() const; 00151 00157 LSVOID LSE_CALL SetPos( const CVector3 &_vPos ); 00158 00164 const CVector3 & LSE_CALL GetVel() const; 00165 00171 LSVOID LSE_CALL SetVel( const CVector3 &_vVel ); 00172 00178 const CVector3 & LSE_CALL GetDir() const; 00179 00185 LSVOID LSE_CALL SetDir( const CVector3 &_vDir ); 00186 00192 LSSREAL LSE_CALL GetMaxDist() const; 00193 00199 LSVOID LSE_CALL SetMaxDist( LSSREAL _fDist ); 00200 00206 LSSREAL LSE_CALL GetPitchMultiplier() const; 00207 00213 LSVOID LSE_CALL SetPitchMultiplier( LSSREAL _fPitch ); 00214 00220 LSSREAL LSE_CALL GetGain() const; 00221 00227 LSVOID LSE_CALL SetGain( LSSREAL _fGain ); 00228 00234 LSSREAL LSE_CALL GetRolloff() const; 00235 00241 LSVOID LSE_CALL SetRolloff( LSSREAL _fRolloff ); 00242 00248 LSSREAL LSE_CALL GetMinGain() const; 00249 00255 LSVOID LSE_CALL SetMinGain( LSSREAL _fMinGain ); 00256 00262 LSSREAL LSE_CALL GetMaxGain() const; 00263 00269 LSVOID LSE_CALL SetMaxGain( LSSREAL _fMaxGain ); 00270 00276 LSSREAL LSE_CALL GetOuterConeGain() const; 00277 00283 LSVOID LSE_CALL SetOuterConeGain( LSSREAL _fOuterConeGain ); 00284 00290 LSSREAL LSE_CALL GetInnerConeAngle() const; 00291 00297 LSVOID LSE_CALL SetInnerConeAngle( LSSREAL _fInnerConeGain ); 00298 00304 LSSREAL LSE_CALL GetConeAngle() const; 00305 00311 LSVOID LSE_CALL SetConeAngle( LSSREAL _fConeAngle ); 00312 00319 virtual LSVOID LSE_CALL Tick( LSUINT64 _ui64TimeSinceLastUpdate ); 00320 00327 virtual LSVOID LSE_CALL SynchronousUpdate( LSUINTPTR _uiptrUserPtr ); 00328 00334 LSVOID LSE_CALL SetLoopCount( LSUINT32 _ui32Count ); 00335 00342 LSBOOL LSE_CALL Loop() const; 00343 00349 LSUINT32 LSE_CALL TotalLoops() const; 00350 00358 LSBOOL LSE_CALL Expired() const; 00359 00367 LSVOID LSE_CALL SetExpirationDate( const LSUINT64 &_ui64Micros ); 00368 00378 virtual LSBOOL LSE_CALL CanSoftReset() const; 00379 00386 virtual LSVOID LSE_CALL EventSoundEndedAsync(); 00387 00393 LSBOOL LSE_CALL JustStarted() const; 00394 00400 LSBOOL LSE_CALL JustStopped() const; 00401 00402 00403 protected : 00404 // == Enumerations. 00405 #ifdef LSS_USEOPENAL 00406 // An invalid OpenAL buffer ID. Used internally. 00407 enum { 00408 LSS_INVALID_BUFFER = 0, 00409 }; 00410 #endif // #ifdef LSS_USEOPENAL 00411 00412 00413 // == Members. 00414 // Live time for this sound. In microseconds. 00415 LSUINT64 m_ui64Time; 00416 00417 // Dead time for this sound. In microseconds. Resets on each play command. 00418 // Only accumulates while stopped. 00419 LSUINT64 m_ui64DeadTime; 00420 00421 // Length of the sound we are playing. 00422 LSUINT64 m_ui64SoundTime; 00423 00424 // Expiration time. If we are dead for this amount of time then we are eligable 00425 // for automatic removal. 00426 LSUINT64 m_ui64ExpirationDate; 00427 00428 // Position of the source. 00429 CVector3 m_vPos; 00430 00431 // Velocity of the source. 00432 CVector3 m_vVel; 00433 00434 // Direction of the source. 00435 CVector3 m_vDir; 00436 00437 // Pitch multiplier. 00438 LSSREAL m_fPitch; 00439 00440 // Gain. 00441 LSSREAL m_fGain; 00442 00443 // Max distance. 00444 LSSREAL m_fDist; 00445 00446 // Rolloff. 00447 LSSREAL m_fRolloff; 00448 00449 // Minimum gain. 00450 LSSREAL m_fMinGain; 00451 00452 // Maximum gain. 00453 LSSREAL m_fMaxGain; 00454 00455 // Gain outside of the oriented cone. 00456 LSSREAL m_fOuterGain; 00457 00458 // Inner cone angle. 00459 LSSREAL m_fInnerAngle; 00460 00461 // Cone angle in degrees. 00462 LSSREAL m_fConeAngle; 00463 00464 // Are we dirty? Update on the next tick if we are. 00465 LSBOOL m_bDirty; 00466 00467 // Our unique ID. 00468 LSUINT32 m_ui32Id; 00469 00470 // Our state. 00471 LSS_SOUND_STATE m_ssState; 00472 00473 // Last state (synchronous with the main game thread). 00474 LSS_SOUND_STATE m_ssLastStateSynchronous; 00475 00476 // Did we just begin (synchronous with the main game thread)? 00477 LSBOOL m_bJustBeganSynchronous; 00478 00479 // Did we just end (synchronous with the main game thread)? 00480 LSBOOL m_bJustEndedSynchronous; 00481 00482 // Repeat count. Use 1 for no looping. Use LSS_LM_INFINITE 00483 // for infinite looping. 00484 LSUINT32 m_ui32LoopCount; 00485 00486 // Last loop mode sent to the sound system. Sound systems only have loop or not loop. 00487 LSBOOL m_bSoundSystemLooping; 00488 00489 #ifdef LSS_USEOPENAL 00490 // The OpenAL ID for this buffer. 00491 // Created with ::alGenBuffers(). 00492 ALuint m_uiAlSourceId; 00493 00494 #endif // #ifdef LSS_USEOPENAL 00495 00496 // The unique ID counter. 00497 static LSUINT32 m_ui32IdCounter; 00498 00499 00500 // == Functions. 00505 virtual LSVOID LSE_CALL ApplyStates(); 00506 00510 LSVOID LSE_CALL UpdateLoop(); 00511 00512 00513 // == Functions. 00522 virtual LSBOOL LSE_CALL DeviceActivated(); 00523 00529 virtual LSVOID LSE_CALL DeviceWillClose(); 00530 00537 virtual LSVOID LSE_CALL EnterDeletableState() const; 00538 }; 00539 00540 00541 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00542 // DEFINITIONS 00543 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00544 // == Various constructors. 00545 LSE_INLINE LSE_CALLCTOR CSoundSource::CSoundSource( LSUINT32 _ui32Id ) : 00546 m_fPitch( static_cast<LSSREAL>(1.0) ), 00547 m_fGain( static_cast<LSSREAL>(0.0) ), 00548 m_fDist( static_cast<LSSREAL>(100.0) ), 00549 m_fRolloff( static_cast<LSSREAL>(1.0) ), 00550 m_fMinGain( static_cast<LSSREAL>(0.0) ), 00551 m_fMaxGain( static_cast<LSSREAL>(1.0) ), 00552 m_fOuterGain( static_cast<LSSREAL>(0.0) ), 00553 m_fInnerAngle( static_cast<LSSREAL>(360.0) ), 00554 m_fConeAngle( static_cast<LSSREAL>(360.0) ), 00555 m_bDirty( true ), 00556 m_ui32Id( LSS_E_INVALID_BUFFER ), 00557 m_ui64Time( 0ULL ), 00558 m_ui64DeadTime( 0ULL ), 00559 m_ui64SoundTime( 0ULL ), 00560 m_ui64ExpirationDate( LSS_NONEXPIRABLE ), 00561 m_ssState( LSS_SS_STOPPED ), 00562 m_bJustBeganSynchronous( false ), 00563 m_bJustEndedSynchronous( false ), 00564 m_ui32LoopCount( 1UL ), 00565 m_bSoundSystemLooping( false ), 00566 #ifdef LSS_USEOPENAL 00567 m_uiAlSourceId( LSS_INVALID_BUFFER ) 00568 #endif // #ifdef LSS_USEOPENAL 00569 { 00570 m_vPos[0] = m_vPos[1] = m_vPos[2] = LSM_ZERO; 00571 m_vVel[0] = m_vVel[1] = m_vVel[2] = LSM_ZERO; 00572 m_vDir[0] = m_vDir[1] = LSM_ZERO; 00573 m_vDir[2] = -LSM_ONE; 00574 00575 if ( _ui32Id != 0ULL ) { 00576 m_ui32IdCounter = _ui32Id; 00577 if ( _ui32Id < LSS_UNIQUE_ID ) { 00578 CStd::DebugPrintA( "CSoundSource::CSoundSource(): The unique ID given must be equal to or above LSS_UNIQUE_ID.\r\n" ); 00579 assert( 0 ); 00580 } 00581 } 00582 else { 00583 m_ui32IdCounter = ((m_ui32IdCounter + 1UL) % (LSS_UNIQUE_ID - 1)) + 1UL; 00584 m_ui32Id = m_ui32IdCounter; 00585 } 00586 } 00587 LSE_INLINE LSE_CALLCTOR CSoundSource::~CSoundSource() { 00588 EnterDeletableState(); 00589 #ifdef LSS_USEOPENAL 00590 // Delete existing source. 00591 if ( m_uiAlSourceId != LSS_INVALID_BUFFER ) { 00592 ::alDeleteSources( 1, &m_uiAlSourceId ); 00593 assert( ::alIsSource( m_uiAlSourceId ) == AL_FALSE ); 00594 m_uiAlSourceId = LSS_INVALID_BUFFER; 00595 } 00596 #endif // #ifdef LSS_USEOPENAL 00597 } 00598 00599 // == Functions. 00605 LSE_INLINE LSBOOL LSE_CALL CSoundSource::JustStarted() const { 00606 return m_bJustBeganSynchronous; 00607 } 00608 00614 LSE_INLINE LSBOOL LSE_CALL CSoundSource::JustStopped() const { 00615 return m_bJustEndedSynchronous; 00616 } 00617 00618 } // namespace lss 00619 00620 #endif // __LSS_SOUNDSOURCE_H__