BVB Source Codes

CRYENGINE Show VoiceListener.cpp Source code

Return Download CRYENGINE: download VoiceListener.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "GameContext.h"
  5. #include "VoiceListener.h"
  6. #include "CryAction.h"
  7. #include "Network/NetworkCVars.h"
  8.  
  9. #ifndef OLD_VOICE_SYSTEM_DEPRECATED
  10.  
  11.         #define DUMP_RECEIVED_VOICE 0
  12.  
  13. CVoiceListener::CVoiceListener()
  14.         : m_pSound(0)
  15.         , m_min2dDistance(15.0f)
  16.         , m_max3dDistance(18.0f)
  17.         , m_3dPan(1.0f)
  18. {
  19. }
  20.  
  21. CVoiceListener::~CVoiceListener()
  22. {
  23.         if (m_pSound)
  24.         {
  25.                 m_pSound->RemoveEventListener(this);
  26.                 m_pSound->Stop();
  27.         }
  28. }
  29.  
  30. bool CVoiceListener::Init(IGameObject* pGameObject)
  31. {
  32.         SetGameObject(pGameObject);
  33.  
  34.         StartPlaying(true);
  35.  
  36.         return true;
  37. }
  38.  
  39. void CVoiceListener::PostInit(IGameObject* pGameObject)
  40. {
  41.         pGameObject->EnableUpdateSlot(this, 0);
  42.         pGameObject->SetUpdateSlotEnableCondition(this, 0, eUEC_InRange);
  43. }
  44.  
  45. void CVoiceListener::InitClient(int channelId)
  46. {
  47. }
  48.  
  49. void CVoiceListener::PostInitClient(int channelId)
  50. {
  51. }
  52.  
  53. void CVoiceListener::Release()
  54. {
  55.         delete this;
  56. }
  57.  
  58. void CVoiceListener::Update(SEntityUpdateContext& ctx, int updateSlot)
  59. {
  60.         // this will happen whenever entity is within range (150m).
  61.         UpdateSound3dPan();
  62. }
  63.  
  64. void CVoiceListener::HandleEvent(const SGameObjectEvent& event)
  65. {
  66. }
  67.  
  68. void CVoiceListener::ProcessEvent(SEntityEvent& evt)
  69. {
  70.         FUNCTION_PROFILER(gEnv->pSystem, PROFILE_ACTION);
  71.  
  72.         if (evt.event != ENTITY_EVENT_START_GAME || m_pSound)
  73.                 return;
  74.  
  75.         StartPlaying(false);
  76. }
  77.  
  78. void CVoiceListener::StartPlaying(bool checkStarted)
  79. {
  80.         m_pVoiceContext = CCryAction::GetCryAction()->GetGameContext()->GetNetContext()->GetVoiceContext();
  81.  
  82.         if (!m_pVoiceContext)
  83.                 return;
  84.  
  85.         if (!m_pVoiceContext->IsEnabled())
  86.                 return;
  87.  
  88.         if (checkStarted)
  89.                 if (!CCryAction::GetCryAction()->IsGameStarted())
  90.                         return;
  91.  
  92.         // TODO: figure out the correct parameters with the network system...
  93.  
  94.         m_pSound = gEnv->pAudioSystem->CreateNetworkSound(this, 16, 8000, 160 * 10, GetEntityId());
  95.         if (!m_pSound)
  96.         {
  97.                 //CryLog("Listener failed to create sound");
  98.                 return;
  99.         }
  100.  
  101.         m_pSound->AddEventListener(this, "VoiceListener");
  102.  
  103.         IEntityAudioComponent* pIEntityAudioComponent = GetGameObject()->GetEntity()->GetOrCreateComponent<IEntityAudioComponent>();
  104.  
  105.         if (pIEntityAudioComponent)
  106.                 pIEntityAudioComponent->PlaySound(m_pSound);
  107.         //pIEntityAudioComponent->PlaySound(m_pSound,Vec3(0.0f,0.0f,1.0f),FORWARD_DIRECTION,1.0f);
  108.  
  109.         IActor* act = CCryAction::GetCryAction()->GetClientActor();
  110.         if (act && (act->GetEntityId() != GetGameObject()->GetEntityId()))
  111.         {
  112.                 m_pSound->GetInterfaceExtended()->SetFlags(m_pSound->GetFlags() | FLAG_SOUND_3D);// | FLAG_SOUND_RADIUS);
  113.                 //              m_pSound->SetMinMaxDistance(1.5f,30.0f);
  114.                 //              m_pSound->Set3DPan(1.0f);
  115.         }
  116.  
  117.         UpdateSound3dPan();
  118. }
  119.  
  120. void CVoiceListener::SetAuthority(bool auth)
  121. {
  122. }
  123.  
  124. void CVoiceListener::PostUpdate(float frameTime)
  125. {
  126.         CRY_ASSERT(false);
  127. }
  128.  
  129. bool CVoiceListener::FillDataBuffer(unsigned int nBitsPerSample, unsigned int nSamplesPerSecond, unsigned int nNumSamples, void* pData)
  130. {
  131.         if (!m_pVoiceContext)
  132.                 return false;
  133.  
  134.         //request decoder for sound data
  135.         if (m_pVoiceContext->GetDataFor(GetEntityId(), nNumSamples, (int16*)pData))
  136.         {
  137.                 // keep sound volume correct
  138.                 if (m_pSound)
  139.                         m_pSound->GetInterfaceExtended()->SetVolume(CNetworkCVars::Get().VoiceVolume);
  140.         }
  141.         else
  142.                 memset(pData, 0, nNumSamples * sizeof(int16));
  143.  
  144.         return true;
  145. }
  146.  
  147. void CVoiceListener::OnActivate(bool active)
  148. {
  149.         if (!m_pVoiceContext)
  150.                 return;
  151.         m_pVoiceContext->PauseDecodingFor(GetEntityId(), !active);
  152. }
  153.  
  154. void CVoiceListener::SetSoundPlaybackDistances(float max3dDistance, float min2dDistance)
  155. {
  156.         CRY_ASSERT(max3dDistance >= min2dDistance);
  157.  
  158.         m_max3dDistance = max3dDistance;
  159.         m_min2dDistance = min2dDistance;
  160. }
  161.  
  162. void CVoiceListener::UpdateSound3dPan()
  163. {
  164.         // if player is still talking, don't change their sound
  165.         INetChannel* pChannel = CCryAction::GetCryAction()->GetClientChannel();
  166.         if (pChannel)
  167.         {
  168.                 CTimeValue tm = pChannel->TimeSinceVoiceReceipt(GetEntity()->GetId());
  169.                 if (tm.GetSeconds() < 0.5f)
  170.                 {
  171.                         return;
  172.                 }
  173.         }
  174.  
  175.         // find the distance of the source entity from the local player, and compare to
  176.         // max3d and min2d distances
  177.         if (!m_pSound)
  178.                 return;
  179.         float new3dpan = m_pSound->GetInterfaceExtended()->Get3DPan();
  180.  
  181.         // TEMP TEMP TEMP until 3dpan is working correctly
  182.         //      assume players are far apart always
  183.  
  184.         Vec3 sourcePos = GetEntity()->GetWorldPos();
  185.         IActor* playerActor = gEnv->pGameFramework->GetClientActor();
  186.         if (playerActor)
  187.         {
  188.                 //    Vec3 playerPos = playerActor->GetEntity()->GetWorldPos();
  189.  
  190.                 //    Vec3 dir = playerPos - sourcePos;
  191.                 //    float dist = dir.GetLength();
  192.  
  193.                 //    if(dist < m_min2dDistance)
  194.                 //      new3dpan = 1.0f;
  195.                 //    else if(dist > m_max3dDistance)
  196.                 //      new3dpan = 0.0f;
  197.  
  198.                 new3dpan = 0.0f;
  199.  
  200.                 //    if(new3dpan != m_3dPan)
  201.                 {
  202.                         m_3dPan = new3dpan;
  203.                         //      m_pSound->Set3DPan(new3dpan);
  204.  
  205.                         uint32 flags = m_pSound->GetFlags();
  206.                         if (new3dpan > 0.5f)
  207.                         {
  208.                                 flags &= ~FLAG_SOUND_RELATIVE;
  209.                         }
  210.                         else
  211.                         {
  212.                                 flags |= FLAG_SOUND_RELATIVE;
  213.  
  214.                                 // disable reverb for that sound
  215.                                 float fReverb = 0.0f;
  216.                                 ptParamF32 fParam(fReverb);
  217.                                 m_pSound->SetParam(spREVERBWET, &fParam);
  218.                         }
  219.                         m_pSound->GetInterfaceExtended()->SetFlags(flags);
  220.                 }
  221.         }
  222. }
  223.  
  224. void CVoiceListener::OnSoundEvent(ESoundCallbackEvent event, ISound* pSound)
  225. {
  226.         CRY_ASSERT(pSound == m_pSound);
  227.  
  228.         switch (event)
  229.         {
  230.         case SOUND_EVENT_ON_PLAYBACK_STARTED:
  231.                 //              CryLog("Activating listener");
  232.                 OnActivate(true);
  233.                 break;
  234.  
  235.         case SOUND_EVENT_ON_PLAYBACK_STOPPED:
  236.                 //              CryLog("Deactivating listener");
  237.                 OnActivate(false);
  238.                 break;
  239.         }
  240. }
  241. #endif
  242.  
downloadVoiceListener.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top