Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. struct AudioEngine::EventInstanceId
  2.     {
  3.         FMOD::Studio::EventInstance* myEventInstance;
  4.         u32 myId;
  5.     };
  6.     struct AudioEngine::Implementation {
  7.         Implementation();
  8.         ~Implementation();
  9.  
  10.         void Update();
  11.         FMOD::Studio::System* myStudioSystem;
  12.         FMOD::System* mySystem;
  13.  
  14.         int myNextChannelId = 0;
  15.         typedef std::map<string, FMOD::Sound*> SoundMap;
  16.         /*typedef map<int, FMOD::Channel*> ChannelMap;*/
  17.         typedef std::map<string,FMOD::Studio::EventInstance*> EventMap;
  18.         typedef std::map<string, FMOD::Studio::Bank*> BankMap;
  19.         typedef std::map<string, FMOD::Studio::VCA*> VcaMap;
  20.         typedef std::map<string, Core::Vector<EventInstanceId>> EventMap3D;
  21.  
  22.         BankMap myBanks;
  23.         EventMap myEvents;
  24.         EventMap3D my3DEvents;
  25.         SoundMap mySounds;
  26.         //ChannelMap myChannels;
  27.         VcaMap myVcas;
  28.     };
  29.  
  30. void AudioEngine::Set3DAttributes(const string& aEventName, u32 aId, Vec3f aPos, Vec3f aForward, Vec3f aUp, Vec3f aVel)
  31.     {
  32.         FMOD_VECTOR pos = { aPos.x,aPos.y, aPos.z };
  33.         FMOD_VECTOR vel = { aVel.x,aVel.y, aVel.z };
  34.         FMOD_VECTOR forward = { aForward.x,aForward.y, aForward.z };
  35.         FMOD_VECTOR up = { aUp.x,aUp.y, aUp.z };
  36.         FMOD_3D_ATTRIBUTES atr{ pos,vel,forward,up };
  37.         auto foundIt = myImplementation->my3DEvents.find(aEventName);
  38.         if (foundIt == myImplementation->my3DEvents.end())
  39.         {
  40.             return;
  41.         }
  42.         for (i32 i = 0; i < foundIt->second.Size(); i++)
  43.         {
  44.             if (foundIt->second[i].myId == aId)
  45.             {
  46.                 foundIt->second[i].myEventInstance->set3DAttributes(&atr);
  47.                 return;
  48.             }
  49.         }
  50.        
  51.     }
  52.  
  53. void AudioEngine::Load3DEvent(const string & aEventName, u32 aId)
  54.     {
  55.         auto foundIt = myImplementation->my3DEvents.find(aEventName);
  56.         i32 size = foundIt->second.Size();
  57.         for (i32 i = 0; i < size; i++)
  58.         {
  59.             if (foundIt->second[i].myId == aId)
  60.             {
  61.                 return;
  62.             }
  63.  
  64.         }
  65.         FMOD::Studio::EventDescription* eventDescription = nullptr;
  66.  
  67.         AudioEngine::ErrorCheck(myImplementation->myStudioSystem->getEvent(aEventName.c_str(), &eventDescription));
  68.  
  69.  
  70.         if (eventDescription)
  71.         {
  72.             FMOD::Studio::EventInstance* eventInstance = nullptr;
  73.  
  74.             AudioEngine::ErrorCheck(eventDescription->createInstance(&eventInstance));
  75.             if (eventInstance)
  76.             {
  77.                 for (i32 i = 0; i < size; i++)
  78.                 {
  79.                     if (foundIt->second[i].myEventInstance == nullptr)
  80.                     {
  81.                         myImplementation->my3DEvents[aEventName][i].myEventInstance = eventInstance;
  82.                         myImplementation->my3DEvents[aEventName][i].myId = aId;
  83.                         return;
  84.                     }
  85.                 }
  86.             }
  87.  
  88.         }
  89.     }
  90.  
  91. void AudioEngine::Set3DEventVectorSize(const string & aEventName, u32 aSize)
  92.     {
  93.         Core::Vector<EventInstanceId> v;
  94.         v.Resize(aSize);
  95.         for (i32 i = 0; i < v.Size(); i++)
  96.         {
  97.             v[i].myEventInstance = nullptr;
  98.             v[i].myId = 0;
  99.         }
  100.         myImplementation->my3DEvents.insert_or_assign(aEventName, v);
  101.     }
  102.  
  103. void AudioEngine::PlayMoving3DEvent(const string & aEventName, float aVolume, Vec3f aPos, Vec3f aForward, Vec3f aUp, u32 aId, Vec3f aVel)
  104.     {
  105.         FMOD_VECTOR pos = { aPos.x,aPos.y, aPos.z };
  106.         FMOD_VECTOR vel = { aVel.x,aVel.y, aVel.z };
  107.         FMOD_VECTOR forward = { aForward.x,aForward.y, aForward.z };
  108.         FMOD_VECTOR up = { aUp.x,aUp.y, aUp.z };
  109.         FMOD_3D_ATTRIBUTES atr{ pos,vel,forward,up };
  110.  
  111.         auto foundIt = myImplementation->my3DEvents.find(aEventName);
  112.  
  113.         Load3DEvent(aEventName, aId);
  114.         foundIt = myImplementation->my3DEvents.find(aEventName);
  115.        
  116.         if (foundIt == myImplementation->my3DEvents.end())
  117.         {
  118.             return;
  119.         }
  120.        
  121.         for (i32 i = 0; i < foundIt->second.Size(); i++)
  122.         {
  123.             if (foundIt->second[i].myId == aId)
  124.             {
  125.                 foundIt->second[i].myEventInstance->set3DAttributes(&atr);
  126.                 foundIt->second[i].myEventInstance->start();
  127.                 foundIt->second[i].myEventInstance->setVolume(aVolume);
  128.             }
  129.         }
  130.        
  131.  
  132.  
  133.     }
  134.  
  135. void AudioEngine::Stop3DEvent(const string & aEventName, bool isImmediate, u32 aId)
  136.     {
  137.         auto foundIt = myImplementation->my3DEvents.find(aEventName);
  138.         if (foundIt == myImplementation->my3DEvents.end())
  139.         {
  140.             return;
  141.         }
  142.         FMOD_STUDIO_STOP_MODE stopMode;
  143.         stopMode = isImmediate ? FMOD_STUDIO_STOP_IMMEDIATE : FMOD_STUDIO_STOP_ALLOWFADEOUT;
  144.  
  145.         for (i32 i = 0; i < foundIt->second.Size(); i++)
  146.         {
  147.             if (foundIt->second[i].myId == aId)
  148.             {
  149.                 AudioEngine::ErrorCheck(foundIt->second[i].myEventInstance->stop(stopMode));
  150.                 foundIt->second[i].myEventInstance = nullptr;
  151.                 foundIt->second[i].myId = 0;
  152.             }
  153.         }
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement