Advertisement
Guest User

sound_render.cpp

a guest
May 24th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.56 KB | None | 0 0
  1. #include "stdafx.h"
  2. #pragma hdrstop
  3.  
  4. #include "soundrender_coreA.h"
  5. #include "soundrender_targetA.h"
  6. #include <al.h>
  7. CSoundRender_CoreA* SoundRenderA= 0;
  8.  
  9. CSoundRender_CoreA::CSoundRender_CoreA  ():CSoundRender_Core()
  10. {
  11.     pDevice                     = 0;
  12.     pDeviceList                 = 0;
  13.     pContext                    = 0;
  14.     eaxSet                      = 0;
  15.     eaxGet                      = 0;
  16. }
  17.  
  18. CSoundRender_CoreA::~CSoundRender_CoreA ()
  19. {
  20. }
  21.  
  22. BOOL CSoundRender_CoreA::EAXQuerySupport(BOOL bDeferred, const GUID* guid, u32 prop, void* val, u32 sz)
  23. {
  24.     if (AL_NO_ERROR!=eaxGet(guid, prop, 0, val, sz)) return FALSE;
  25.     if (AL_NO_ERROR!=eaxSet(guid, (bDeferred?DSPROPERTY_EAXLISTENER_DEFERRED:0) | prop, 0, val, sz)) return FALSE;
  26.     return TRUE;
  27. }
  28.  
  29. BOOL CSoundRender_CoreA::EAXTestSupport (BOOL bDeferred)
  30. {
  31.     EAXLISTENERPROPERTIES       ep;
  32.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_ROOM,               &ep.lRoom,                  sizeof(LONG)))  return FALSE;
  33.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_ROOMHF,             &ep.lRoomHF,                sizeof(LONG)))  return FALSE;
  34.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_ROOMROLLOFFFACTOR,  &ep.flRoomRolloffFactor,    sizeof(float))) return FALSE;
  35.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_DECAYTIME,          &ep.flDecayTime,            sizeof(float))) return FALSE;
  36.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_DECAYHFRATIO,       &ep.flDecayHFRatio,         sizeof(float))) return FALSE;
  37.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_REFLECTIONS,        &ep.lReflections,           sizeof(LONG)))  return FALSE;
  38.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_REFLECTIONSDELAY,   &ep.flReflectionsDelay,     sizeof(float))) return FALSE;
  39.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_REVERB,             &ep.lReverb,                sizeof(LONG)))  return FALSE;
  40.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_REVERBDELAY,        &ep.flReverbDelay,          sizeof(float))) return FALSE;
  41.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_ENVIRONMENTDIFFUSION,&ep.flEnvironmentDiffusion,sizeof(float))) return FALSE;
  42.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_AIRABSORPTIONHF,    &ep.flAirAbsorptionHF,      sizeof(float))) return FALSE;
  43.     if (!EAXQuerySupport(bDeferred, &DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_FLAGS,              &ep.dwFlags,                sizeof(DWORD))) return FALSE;
  44.     return TRUE;
  45. }
  46.  
  47. void  CSoundRender_CoreA::_restart()
  48. {
  49.     inherited::_restart();
  50. /*
  51.     CSoundRender_Target*    T   = 0;
  52.     for (u32 tit=0; tit<s_targets.size(); tit++)
  53.     {
  54.         T                       = s_targets[tit];
  55.         T->_destroy             ();
  56.     }
  57.  
  58.     // Reset the current context to NULL.
  59.     alcMakeContextCurrent       (NULL);        
  60.     // Release the context and the device.
  61.     alcDestroyContext           (pContext);    
  62.     pContext                    = NULL;
  63.     alcCloseDevice              (pDevice);     
  64.     pDevice                     = NULL;
  65.  
  66.     _initialize                 (2);
  67.  
  68.     for (u32 tit=0; tit<s_targets.size(); tit++)
  69.     {
  70.         T                       = s_targets[tit];
  71.         T->_initialize              ();
  72.     }
  73. */
  74. }
  75.  
  76. void CSoundRender_CoreA::_initialize(int stage)
  77. {
  78.     if(stage==0)
  79.     {
  80.         pDeviceList                 = xr_new<ALDeviceList>();
  81.  
  82.         if (0==pDeviceList->GetNumDevices())
  83.         {
  84.             CHECK_OR_EXIT           (0,"OpenAL: Can't create sound device.");
  85.             xr_delete               (pDeviceList);
  86.         }
  87.         return;
  88.     }
  89.    
  90.     pDeviceList->SelectBestDevice   ();
  91.     R_ASSERT                        (snd_device_id>=0 && snd_device_id<pDeviceList->GetNumDevices());
  92.     const ALDeviceDesc& deviceDesc  = pDeviceList->GetDeviceDesc(snd_device_id);
  93.     // OpenAL device
  94.     pDevice                     = alcOpenDevice     (deviceDesc.name);
  95.     if (pDevice == NULL)
  96.     {
  97.         CHECK_OR_EXIT           (0,"SOUND: OpenAL: Failed to create device.");
  98.         bPresent                = FALSE;
  99.         return;
  100.     }
  101.  
  102.     // Get the device specifier.
  103.     const ALCchar*              deviceSpecifier;
  104.     deviceSpecifier             = alcGetString      (pDevice, ALC_DEVICE_SPECIFIER);
  105.  
  106.     // Create context
  107.     pContext                    = alcCreateContext  (pDevice,NULL);
  108.     if (0==pContext){
  109.         CHECK_OR_EXIT           (0,"SOUND: OpenAL: Failed to create context.");
  110.         bPresent                = FALSE;
  111.         alcCloseDevice          (pDevice); pDevice = 0;
  112.         return;
  113.     }
  114.    
  115.     // clear errors
  116.     alGetError                  ();
  117.     alcGetError                 (pDevice);
  118.    
  119.     // Set active context
  120.     AC_CHK                      (alcMakeContextCurrent(pContext));
  121.  
  122.     // initialize listener
  123.     A_CHK                       (alListener3f       (AL_POSITION,0.f,0.f,0.f));
  124.     A_CHK                       (alListener3f       (AL_VELOCITY,0.f,0.f,0.f));
  125.     Fvector orient[2]           = {{0.f,0.f,1.f},{0.f,1.f,0.f}};
  126.     A_CHK                       (alListenerfv       (AL_ORIENTATION,&orient[0].x));
  127.     A_CHK                       (alListenerf        (AL_GAIN,1.f));
  128. alGetFloat(AL_SPEED_OF_SOUND);  
  129. alGetFloat(AL_DOPPLER_FACTOR);
  130.     Listener.doppler.set(AL_DOPPLER_FACTOR);
  131.     Listener.speed.set(AL_SPEED_OF_SOUND);    
  132.     // Check for EAX extension
  133.     bEAX                        = deviceDesc.props.eax && !deviceDesc.props.eax_unwanted;
  134.  
  135.     eaxSet                      = (EAXSet)alGetProcAddress  ((const ALchar*)"EAXSet");
  136.     if (eaxSet==NULL) bEAX      = false;
  137.     eaxGet                      = (EAXGet)alGetProcAddress  ((const ALchar*)"EAXGet");
  138.     if (eaxGet==NULL) bEAX      = false;
  139.  
  140.     if (bEAX)
  141.     {
  142.         bDeferredEAX            = EAXTestSupport(TRUE);
  143.         bEAX                    = EAXTestSupport(FALSE);
  144.     }
  145.  
  146.     inherited::_initialize      (stage);
  147.  
  148.     if(stage==1)//first initialize
  149.     {
  150.         // Pre-create targets
  151.         CSoundRender_Target*    T   = 0;
  152.         for (u32 tit=0; tit<u32(psSoundTargets); tit++)
  153.         {
  154.             T                       =   xr_new<CSoundRender_TargetA>();
  155.             if (T->_initialize())
  156.             {
  157.                 s_targets.push_back (T);
  158.             }else
  159.             {
  160.                 Log                 ("! SOUND: OpenAL: Max targets - ",tit);
  161.                 T->_destroy         ();
  162.                 xr_delete           (T);
  163.                 break;
  164.             }
  165.         }
  166.     }
  167. }
  168.  
  169. void CSoundRender_CoreA::set_master_volume(float f )
  170. {
  171.     if (bPresent)               {
  172.         A_CHK                   (alListenerf    (AL_GAIN,f));
  173.     }
  174. }
  175.  
  176. void CSoundRender_CoreA::_clear ()
  177. {
  178.     inherited::_clear           ();
  179.     // remove targets
  180.     CSoundRender_Target*    T   = 0;
  181.     for (u32 tit=0; tit<s_targets.size(); tit++)
  182.     {
  183.         T                       = s_targets[tit];
  184.         T->_destroy             ();
  185.         xr_delete               (T);
  186.     }
  187.     // Reset the current context to NULL.
  188.     alcMakeContextCurrent       (NULL);        
  189.     // Release the context and the device.
  190.     alcDestroyContext           (pContext);     pContext    = 0;
  191.     alcCloseDevice              (pDevice);      pDevice     = 0;
  192.     xr_delete                   (pDeviceList);
  193. }
  194.  
  195. void    CSoundRender_CoreA::i_eax_set           (const GUID* guid, u32 prop, void* val, u32 sz)
  196. {
  197.     eaxSet                      (guid, prop, 0, val, sz);
  198. }
  199. void    CSoundRender_CoreA::i_eax_get           (const GUID* guid, u32 prop, void* val, u32 sz)
  200. {
  201.     eaxGet                      (guid, prop, 0, val, sz);
  202. }
  203.  
  204. void CSoundRender_CoreA::update_listener        ( const Fvector& P, const Fvector& D, const Fvector& N, float dt )
  205. {
  206.     inherited::update_listener(P,D,N,dt);
  207.  
  208.     if (!Listener.position.similar(P)){
  209.         Listener.position.set   (P);
  210.         bListenerMoved          = TRUE;
  211.     }
  212.     Listener.orientation[0].set (D.x,D.y,-D.z);
  213.     Listener.orientation[1].set (N.x,N.y,-N.z);
  214.  
  215.     A_CHK                       (alListener3f   (AL_POSITION,Listener.position.x,Listener.position.y,-Listener.position.z));
  216.     A_CHK                       (alListener3f   (AL_VELOCITY,0.f,0.f,0.f));
  217.     A_CHK                       (alListenerfv   (AL_ORIENTATION,&Listener.orientation[0].x));
  218.     alGetFloat(AL_SPEED_OF_SOUND);
  219.     alGetFloat(AL_DOPPLER_FACTOR);
  220.     Listener.doppler.set(AL_DOPPLER_FACTOR);
  221.     Listener.speed.set(AL_SPEED_OF_SOUND);  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement