Advertisement
Guest User

Untitled

a guest
May 27th, 2017
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.95 KB | None | 0 0
  1. /*****************************************************************************
  2. *
  3. *  PROJECT:     Multi Theft Auto v1.0
  4. *               (Shared logic for modifications)
  5. *  LICENSE:     See LICENSE in the top level directory
  6. *  FILE:        mods/shared_logic/CClientSound.h
  7. *  PURPOSE:     Sound entity class header
  8. *  DEVELOPERS:  Stanislav Bobrov <lil_Toady@hotmail.com>
  9. *               Florian Busse <flobu@gmx.net>
  10. *
  11. *****************************************************************************/
  12.  
  13. class CClientSound;
  14.  
  15. #ifndef __CCLIENTSOUND_H
  16. #define __CCLIENTSOUND_H
  17.  
  18. #include "CClientSoundManager.h"
  19. #include "CClientEntity.h"
  20.  
  21. //#define MAX_SOUND_DISTANCE 100
  22. #define SOUND_PAN_THRESHOLD 0.85f
  23.  
  24. typedef struct ST
  25. {
  26.     CClientSound* pClientSound;
  27.     SString strURL;
  28.     bool bIs3D;
  29.     long lFlags;
  30.     CVector vecPosition;
  31. } thestruct;
  32.  
  33. class CClientSound : public CClientEntity
  34. {
  35.     friend CClientSoundManager;
  36.  
  37. public:
  38.  
  39.                             CClientSound            ( CClientManager* pManager, ElementID ID );
  40.                             ~CClientSound           ( void );
  41.  
  42.     eClientEntityType       GetType                 ( void ) const                      { return CCLIENTSOUND; }
  43.  
  44.     void                    PlayStream              ( const SString& strURL, bool bIs3D, bool bLoop, const CVector& vecPosition = CVector () );
  45.     bool                    Play                    ( const SString& strPath, bool bLoop );
  46.     bool                    Play3D                  ( const SString& strPath, const CVector& vecPosition, bool bLoop, int iangle, int oangle, float outvol );
  47.  
  48.     static void             PlayStreamIntern        ( void* arguments );
  49.  
  50.     void                    Stop                    ( void );
  51.  
  52.     void                    Set3DAttributes         ( float fMinDistance, float fMaxDistance, int iIAngle, int iOAngle, float fOutVol );
  53.  
  54.     void                    SetPaused               ( bool bPaused );
  55.     bool                    IsPaused                ( void );
  56.  
  57.     bool                    IsFinished              ( void );
  58.  
  59.     void                    SetPlayPosition         ( unsigned int uiPosition );
  60.     unsigned int            GetPlayPosition         ( void );
  61.  
  62.     unsigned int            GetLength               ( void );
  63.  
  64.     void                    SetVolume               ( float fVolume );
  65.     float                   GetVolume               ( void );
  66.  
  67.     void                    SetPlaybackSpeed        ( float fSpeed );
  68.     float                   GetPlaybackSpeed        ( void );
  69.  
  70.     void                    SetMinDistance          ( float fDistance );
  71.     float                   GetMinDistance          ( void );
  72.  
  73.     void                    SetMaxDistance          ( float fDistance );
  74.     float                   GetMaxDistance          ( void );
  75.  
  76.     void                    Unlink                  ( void ) {};
  77.     void                    GetPosition             ( CVector& vecPosition ) const;
  78.     void                    SetPosition             ( const CVector& vecPosition );
  79.  
  80.     void                    SetDimension            ( unsigned short usDimension );
  81.     void                    RelateDimension         ( unsigned short usDimension );
  82.  
  83.     DWORD                   m_pSound;
  84.  
  85.     bool                    m_b3D;
  86.     float                   m_fVolume;
  87.     CVector                 m_vecPosition;
  88.  
  89. protected:
  90.  
  91.     DWORD                   GetSound                ( void )                            { return m_pSound; };
  92.     void                    UpdatePosition          ( void );
  93.  
  94. private:
  95.  
  96.     CClientSoundManager*    m_pSoundManager;
  97.  
  98.     HANDLE                  m_pThread;
  99.  
  100.     thestruct*              m_pArguments;
  101. };
  102.  
  103. #endif
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /*****************************************************************************
  110. *
  111. *  PROJECT:     Multi Theft Auto v1.0
  112. *               (Shared logic for modifications)
  113. *  LICENSE:     See LICENSE in the top level directory
  114. *  FILE:        mods/shared_logic/CClientSound.h
  115. *  PURPOSE:     Sound entity class
  116. *  DEVELOPERS:  Stanislav Bobrov <lil_Toady@hotmail.com>
  117. *               arc_
  118. *               Florian Busse <flobu@gmx.net>
  119. *
  120. *****************************************************************************/
  121.  
  122. #include <StdInc.h>
  123. #include <process.h>
  124.  
  125. extern CClientGame* g_pClientGame;
  126.  
  127. CClientSound::CClientSound ( CClientManager* pManager, ElementID ID ) : CClientEntity ( ID )
  128. {
  129.     m_pManager = pManager;
  130.     m_pSoundManager = pManager->GetSoundManager();
  131.     m_pSound = NULL;
  132.  
  133.     SetTypeName ( "sound" );
  134.  
  135.     m_pSoundManager->AddToList ( this );
  136.  
  137.     RelateDimension ( pManager->GetSoundManager ()->GetDimension () );
  138.  
  139.     m_fVolume = 1.0f;
  140.     m_usDimension = 0;
  141.     m_b3D = false;
  142.     m_pThread = 0;
  143. }
  144.  
  145. CClientSound::~CClientSound ( void )
  146. {
  147.     if ( m_pSound )
  148.         BASS_ChannelStop ( m_pSound );
  149.  
  150.     m_pSoundManager->RemoveFromList ( this );
  151.     _endthread();
  152. }
  153.  
  154. bool CClientSound::Play ( const SString& strPath, bool bLoop )
  155. {
  156.     long lFlags = BASS_STREAM_AUTOFREE;
  157.     if ( bLoop )
  158.         lFlags = BASS_SAMPLE_LOOP;
  159.  
  160.     if (
  161.         ( m_pSound = BASS_StreamCreateFile ( false, strPath, 0, 0, lFlags ) ) ||
  162.         ( m_pSound = BASS_MusicLoad ( false, strPath, 0, 0, lFlags, 0) ) ||
  163.         ( m_pSound = BASS_StreamCreateURL ( strPath, 0, lFlags, NULL, NULL ) )
  164.         )
  165.     {
  166.         BASS_ChannelPlay ( m_pSound, false );
  167.         return true;
  168.     }
  169.     g_pCore->GetConsole()->Printf( "BASS ERROR %d in Play  path = %s", BASS_ErrorGetCode(), strPath );
  170.     return false;
  171. }
  172.  
  173. bool CClientSound::Play3D ( const SString& strPath, const CVector& vecPosition, bool bLoop, int iangle, int oangle, float outvol )
  174. {
  175.     long lFlags = BASS_STREAM_AUTOFREE | BASS_SAMPLE_3D | BASS_SAMPLE_MONO;
  176.     if ( bLoop )
  177.         lFlags |= BASS_SAMPLE_LOOP;
  178.  
  179.     if (
  180.         ( m_pSound = BASS_StreamCreateFile ( false, strPath, 0, 0, lFlags ) ) ||
  181.         ( m_pSound = BASS_MusicLoad ( false, strPath, 0, 0, lFlags, 0) ) ||
  182.         ( m_pSound = BASS_StreamCreateURL ( strPath, 0, lFlags, NULL, NULL ) )
  183.         )
  184.     {
  185.         m_b3D = true;
  186.         m_vecPosition = vecPosition;
  187.         BASS_3DVECTOR pos ( vecPosition.fX, vecPosition.fY, vecPosition.fZ );
  188.         BASS_ChannelSet3DPosition ( m_pSound, &pos, NULL, NULL );
  189.         g_pCore->GetConsole()->Printf( "BASS  iangle = %d  oangle = %d  outvol = %f", iangle, oangle, outvol );
  190.         BASS_ChannelSet3DAttributes ( m_pSound, BASS_3DMODE_NORMAL, 2, 10, iangle, oangle, outvol );
  191.         BASS_ChannelPlay ( m_pSound, false );
  192.         return true;
  193.     }
  194.     g_pCore->GetConsole()->Printf( "BASS ERROR %d in Play3D  path = %s", BASS_ErrorGetCode(), strPath );
  195.     return false;
  196. }
  197.  
  198. void CClientSound::PlayStream ( const SString& strURL, bool bIs3D, bool bLoop, const CVector& vecPosition )
  199. {
  200.     long lFlags = BASS_STREAM_AUTOFREE;
  201.     if ( bLoop )
  202.         lFlags |= BASS_SAMPLE_LOOP;
  203.     if ( bIs3D )
  204.         lFlags |= BASS_SAMPLE_3D | BASS_SAMPLE_MONO;
  205.  
  206.     thestruct* m_pArguments = new thestruct;
  207.     m_pArguments->pClientSound = this;
  208.     m_pArguments->strURL = strURL;
  209.     m_pArguments->bIs3D = bIs3D;
  210.     m_pArguments->lFlags = lFlags;
  211.     m_pArguments->vecPosition = vecPosition;
  212.  
  213.     m_pThread = (HANDLE)_beginthread ( &CClientSound::PlayStreamIntern, 0, &m_pArguments );
  214.     //WaitForSingleObject( m_pThread, INFINITE );
  215.     //if ( m_pSound )
  216.     //    return true;
  217.  
  218.     //g_pCore->GetConsole()->Printf( "BASS ERROR %d in PlayStream  bIs3D = %s  path = %s", BASS_ErrorGetCode(), bIs3D ? "true" : "false", strURL );
  219.     //return false;
  220. }
  221.  
  222. void CClientSound::PlayStreamIntern ( void* arguments )
  223. {
  224.     thestruct* args = (thestruct*)arguments;
  225.     CClientSound* pClientSound = (CClientSound*)args->pClientSound;
  226.     DWORD pSound = pClientSound->m_pSound;
  227.     SString strURL = args->strURL;
  228.     pClientSound->m_pSound = BASS_StreamCreateURL ( strURL, 0, args->lFlags, NULL, NULL );
  229.     DWORD errorid = BASS_ErrorGetCode();
  230.     if ( pClientSound->m_pSound )
  231.     {
  232.         if ( args->bIs3D )
  233.         {
  234.             pClientSound->m_b3D = true;
  235.             pClientSound->m_vecPosition = args->vecPosition;
  236.             BASS_3DVECTOR pos (args->vecPosition.fX, args->vecPosition.fY, args->vecPosition.fZ );
  237.             BASS_ChannelSet3DPosition ( pClientSound->m_pSound, &pos, NULL, NULL );
  238.             BASS_ChannelSet3DAttributes ( pClientSound->m_pSound, BASS_3DMODE_NORMAL, 2, 10, 360, 0, 0 );
  239.         }
  240.         BASS_ChannelPlay ( pClientSound->m_pSound, false );
  241.     }
  242.     delete arguments;
  243.     _endthread ();
  244. }
  245.  
  246. void CClientSound::Stop ( void )
  247. {
  248.     if ( m_pSound )
  249.         BASS_ChannelStop ( m_pSound );
  250.  
  251.     g_pClientGame->GetElementDeleter()->Delete ( this );
  252. }
  253.  
  254. void CClientSound::Set3DAttributes ( float fMinDistance, float fMaxDistance, int iIAngle, int iOAngle, float fOutVol )
  255. {
  256.     if ( m_pSound )
  257.     {
  258.         if ( !BASS_ChannelSet3DAttributes( m_pSound, -1, fMinDistance, fMaxDistance, iIAngle, iOAngle, fOutVol ) )
  259.             g_pCore->GetConsole()->Printf( "BASS ERROR %d in Set3DAttributes  fMinDistance = %f  fMinDistance = %f  iIAngle = %d  iOAngle = %d  fOutVol = %f", BASS_ErrorGetCode(), fMinDistance, fMaxDistance, iIAngle, iOAngle, fOutVol );
  260.     }
  261. }
  262.  
  263. void CClientSound::SetPaused ( bool bPaused )
  264. {
  265.     if ( m_pSound )
  266.     {
  267.         if ( bPaused )
  268.             BASS_ChannelPause ( m_pSound );
  269.         else
  270.             BASS_ChannelPlay ( m_pSound, false );
  271.     }
  272. }
  273.  
  274. bool CClientSound::IsPaused ( void )
  275. {
  276.     if ( m_pSound )
  277.     {
  278.         return BASS_ChannelIsActive( m_pSound ) == BASS_ACTIVE_PAUSED;
  279.     }
  280.     return false;
  281. }
  282.  
  283. bool CClientSound::IsFinished ( void )
  284. {
  285.     if ( m_pSound )
  286.     {
  287.         return BASS_ChannelIsActive( m_pSound ) == BASS_ACTIVE_STOPPED;
  288.     }
  289.     return false;
  290. }
  291.  
  292. void CClientSound::SetPlayPosition ( unsigned int uiPosition )
  293. {
  294.     if ( m_pSound )
  295.     {
  296.         BASS_ChannelSetPosition( m_pSound, BASS_ChannelSeconds2Bytes( m_pSound, uiPosition/1000 ), BASS_POS_BYTE );
  297.     }
  298. }
  299.  
  300. unsigned int CClientSound::GetPlayPosition ( void )
  301. {
  302.     if ( m_pSound )
  303.     {
  304.         QWORD pos = BASS_ChannelGetPosition( m_pSound, BASS_POS_BYTE );
  305.         if ( pos != -1 )
  306.             return BASS_ChannelBytes2Seconds( m_pSound, pos )*1000;
  307.     }
  308.     return 0;
  309. }
  310.  
  311. unsigned int CClientSound::GetLength ( void )
  312. {
  313.     if ( m_pSound )
  314.     {
  315.         QWORD length = BASS_ChannelGetLength( m_pSound, BASS_POS_BYTE );
  316.         if ( length != -1 )
  317.             return BASS_ChannelBytes2Seconds( m_pSound, length )*1000;
  318.     }
  319.     return 0;
  320. }
  321.  
  322. float CClientSound::GetVolume ( void )
  323. {
  324.     float fVolume = -1;
  325.     BASS_ChannelGetAttribute( m_pSound, BASS_ATTRIB_VOL, &fVolume );
  326.     return fVolume;
  327. }
  328.  
  329. void CClientSound::SetVolume ( float fVolume )
  330. {
  331.     if ( m_pSound && m_usDimension == m_pManager->GetSoundManager ()->GetDimension () )
  332.     {
  333.         if ( BASS_ChannelSetAttribute( m_pSound, BASS_ATTRIB_VOL, fVolume ) )
  334.             m_fVolume = fVolume;
  335.     }
  336. }
  337.  
  338. float CClientSound::GetPlaybackSpeed ( void )
  339. {
  340.     float fSpeed = -1;
  341.     BASS_ChannelGetAttribute(m_pSound, BASS_ATTRIB_FREQ, &fSpeed);
  342.     return fSpeed;
  343. }
  344.  
  345. void CClientSound::SetPlaybackSpeed ( float fSpeed )
  346. {
  347.     if ( m_pSound )
  348.     {
  349.         BASS_ChannelSetAttribute(m_pSound, BASS_ATTRIB_FREQ, fSpeed);
  350.     }
  351. }
  352.  
  353. void CClientSound::SetPosition ( const CVector& vecPosition )
  354. {
  355.     if ( m_pSound )
  356.     {
  357.         m_vecPosition = vecPosition;
  358.         BASS_3DVECTOR pos ( vecPosition.fX, vecPosition.fY, vecPosition.fZ );
  359.         BASS_ChannelSet3DPosition ( m_pSound, &pos, NULL, NULL);
  360.     }
  361. }
  362.  
  363. void CClientSound::GetPosition ( CVector& vecPosition ) const
  364. {
  365.     vecPosition = m_vecPosition;
  366. }
  367.  
  368. void CClientSound::SetDimension ( unsigned short usDimension )
  369. {
  370.     m_usDimension = usDimension;
  371.     RelateDimension ( m_pManager->GetSoundManager ()->GetDimension () );
  372. }
  373.  
  374. void CClientSound::RelateDimension ( unsigned short usDimension )
  375. {
  376.     if ( usDimension == m_usDimension )
  377.     {
  378.         SetVolume ( m_fVolume );
  379.     }
  380.     else
  381.     {
  382.         m_fVolume = GetVolume ();
  383.         SetVolume ( 0.0f );
  384.     }
  385. }
  386.  
  387. void CClientSound::SetMinDistance ( float fDistance )
  388. {
  389.     if ( m_pSound )
  390.     {
  391.         BASS_ChannelSet3DAttributes( m_pSound, -1, fDistance, -1, -1, -1, -1 );
  392.     }
  393. }
  394.  
  395. float CClientSound::GetMinDistance ( void )
  396. {
  397.     float fMinDistance;
  398.     BASS_ChannelGet3DAttributes( m_pSound, NULL, &fMinDistance, NULL, NULL, NULL, NULL );
  399.     return fMinDistance;
  400. }
  401.  
  402. void CClientSound::SetMaxDistance ( float fDistance )
  403. {
  404.     if ( m_pSound )
  405.     {
  406.         BASS_ChannelSet3DAttributes( m_pSound, -1, -1, fDistance, -1, -1, -1 );
  407.     }
  408. }
  409.  
  410. float CClientSound::GetMaxDistance ( void )
  411. {
  412.     float fMaxDistance;
  413.     BASS_ChannelGet3DAttributes( m_pSound, NULL, NULL, &fMaxDistance, NULL, NULL, NULL );
  414.     return fMaxDistance;
  415. }
  416.  
  417. void CClientSound::UpdatePosition ( void )
  418. {
  419.     if ( !m_b3D ) return;
  420.  
  421.     // Update our position/rotation if we're attached
  422.     DoAttaching ();
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement