AudioSystem.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #pragma once
  9. #include <unordered_map>
  10. #include <string>
  11. #include "SoundEvent.h"
  12. #include "Math.h"
  13. // Forward declarations to avoid including FMOD header
  14. namespace FMOD
  15. {
  16. class System;
  17. namespace Studio
  18. {
  19. class Bank;
  20. class EventDescription;
  21. class EventInstance;
  22. class System;
  23. class Bus;
  24. };
  25. };
  26. class AudioSystem
  27. {
  28. public:
  29. AudioSystem(class Game* game);
  30. ~AudioSystem();
  31. bool Initialize();
  32. void Shutdown();
  33. // Load/unload banks
  34. void LoadBank(const std::string& name);
  35. void UnloadBank(const std::string& name);
  36. void UnloadAllBanks();
  37. SoundEvent PlayEvent(const std::string& name);
  38. void Update(float deltaTime);
  39. // For positional audio
  40. void SetListener(const Matrix4& viewMatrix);
  41. // Control buses
  42. float GetBusVolume(const std::string& name) const;
  43. bool GetBusPaused(const std::string& name) const;
  44. void SetBusVolume(const std::string& name, float volume);
  45. void SetBusPaused(const std::string& name, bool pause);
  46. protected:
  47. friend class SoundEvent;
  48. FMOD::Studio::EventInstance* GetEventInstance(unsigned int id);
  49. private:
  50. // Tracks the next ID to use for event instances
  51. static unsigned int sNextID;
  52. class Game* mGame;
  53. // Map of loaded banks
  54. std::unordered_map<std::string, FMOD::Studio::Bank*> mBanks;
  55. // Map of event name to EventDescription
  56. std::unordered_map<std::string, FMOD::Studio::EventDescription*> mEvents;
  57. // Map of event id to EventInstance
  58. std::unordered_map<unsigned int, FMOD::Studio::EventInstance*> mEventInstances;
  59. // Map of buses
  60. std::unordered_map<std::string, FMOD::Studio::Bus*> mBuses;
  61. // FMOD studio system
  62. FMOD::Studio::System* mSystem;
  63. // FMOD Low-level system (in case needed)
  64. FMOD::System* mLowLevelSystem;
  65. };