AudioPreloadComponent.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/std/containers/unordered_set.h>
  10. #include <AzCore/std/string/string.h>
  11. #include <AzCore/Component/Component.h>
  12. #include <LmbrCentral/Audio/AudioPreloadComponentBus.h>
  13. #include <IAudioSystem.h>
  14. namespace LmbrCentral
  15. {
  16. /*!
  17. * AudioPreloadComponent
  18. * Allows loading and unloading ATL Preloads (Soundbanks).
  19. * A Preload name can be serialized with the component, or it can be manually specified
  20. * at runtime for use in scripting.
  21. */
  22. class AudioPreloadComponent
  23. : public AZ::Component
  24. , public AudioPreloadComponentRequestBus::Handler
  25. , public Audio::AudioPreloadNotificationBus::MultiHandler
  26. {
  27. public:
  28. enum class LoadType : AZ::u32
  29. {
  30. Auto, // Automatically loads / unloads when the component activates / deactivates
  31. Manual, // Loading and unloading is triggered manually
  32. };
  33. /*!
  34. * AZ::Component
  35. */
  36. AZ_COMPONENT(AudioPreloadComponent, "{CBBB1234-4DCA-427E-80FF-E2BB0866EEB1}");
  37. void Activate() override;
  38. void Deactivate() override;
  39. AudioPreloadComponent() = default;
  40. AudioPreloadComponent(AudioPreloadComponent::LoadType loadType, const AZStd::string& preloadName);
  41. /*!
  42. * AudioPreloadComponentRequestBus::Handler Required Interface
  43. */
  44. void Load() override;
  45. void Unload() override;
  46. void LoadPreload(const char* preloadName) override;
  47. void UnloadPreload(const char* preloadName) override;
  48. bool IsLoaded(const char* preloadName) override;
  49. /*!
  50. * Audio::AudioPreloadNotificationBus::Handler interface
  51. */
  52. void OnAudioPreloadCached() override;
  53. void OnAudioPreloadUncached() override;
  54. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  55. {
  56. provided.push_back(AZ_CRC_CE("AudioPreloadService"));
  57. }
  58. static void GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  59. {
  60. }
  61. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  62. {
  63. dependent.push_back(AZ_CRC_CE("AudioProxyService"));
  64. }
  65. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  66. {
  67. incompatible.push_back(AZ_CRC_CE("AudioPreloadService"));
  68. }
  69. static void Reflect(AZ::ReflectContext* context);
  70. private:
  71. //! Utility functions
  72. void LoadPreloadById(Audio::TAudioPreloadRequestID preloadId);
  73. void UnloadPreloadById(Audio::TAudioPreloadRequestID preloadId);
  74. //! Transient data
  75. AZStd::unordered_set<Audio::TAudioPreloadRequestID> m_loadedPreloadIds;
  76. AZStd::mutex m_loadMutex;
  77. //! Serialized data
  78. AZStd::string m_defaultPreloadName;
  79. AudioPreloadComponent::LoadType m_loadType = AudioPreloadComponent::LoadType::Auto;
  80. };
  81. } // namespace LmbrCentral
  82. namespace AZ
  83. {
  84. AZ_TYPE_INFO_SPECIALIZE(LmbrCentral::AudioPreloadComponent::LoadType, "{084969E9-65AB-42FD-8EA2-C1DDDCB7B676}");
  85. } // namespace AZ