MiniAudioSystemComponent.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "MiniAudioSystemComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <MiniAudio/SoundAsset.h>
  11. #include <MiniAudio/SoundAssetRef.h>
  12. #include "SoundAssetHandler.h"
  13. #include "MiniAudioIncludes.h"
  14. namespace MiniAudio
  15. {
  16. AZ::ComponentDescriptor* MiniAudioSystemComponent_CreateDescriptor()
  17. {
  18. return MiniAudioSystemComponent::CreateDescriptor();
  19. }
  20. AZ::TypeId MiniAudioSystemComponent_GetTypeId()
  21. {
  22. return azrtti_typeid<MiniAudioSystemComponent>();
  23. }
  24. void MiniAudioSystemComponent::Reflect(AZ::ReflectContext* context)
  25. {
  26. SoundAsset::Reflect(context);
  27. SoundAssetRef::Reflect(context);
  28. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  29. {
  30. serialize->Class<MiniAudioSystemComponent, AZ::Component>()
  31. ->Version(0)
  32. ;
  33. if (AZ::EditContext* ec = serialize->GetEditContext())
  34. {
  35. ec->Class<MiniAudioSystemComponent>("MiniAudio", "[Description of functionality provided by this System Component]")
  36. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  37. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
  38. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  39. ;
  40. }
  41. }
  42. }
  43. void MiniAudioSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  44. {
  45. provided.push_back(AZ_CRC_CE("MiniAudioService"));
  46. }
  47. void MiniAudioSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  48. {
  49. incompatible.push_back(AZ_CRC_CE("MiniAudioService"));
  50. }
  51. void MiniAudioSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  52. {
  53. }
  54. void MiniAudioSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  55. {
  56. }
  57. MiniAudioSystemComponent::MiniAudioSystemComponent()
  58. {
  59. if (MiniAudioInterface::Get() == nullptr)
  60. {
  61. MiniAudioInterface::Register(this);
  62. }
  63. }
  64. MiniAudioSystemComponent::~MiniAudioSystemComponent()
  65. {
  66. if (MiniAudioInterface::Get() == this)
  67. {
  68. MiniAudioInterface::Unregister(this);
  69. }
  70. }
  71. void MiniAudioSystemComponent::Init()
  72. {
  73. }
  74. void MiniAudioSystemComponent::Activate()
  75. {
  76. m_engine = AZStd::make_unique<ma_engine>();
  77. const ma_result result = ma_engine_init(nullptr, m_engine.get());
  78. if (result != MA_SUCCESS)
  79. {
  80. AZ_Error("MiniAudio", false, "Failed to initialize audio engine, error %d", result);
  81. }
  82. MiniAudioRequestBus::Handler::BusConnect();
  83. {
  84. SoundAssetHandler* handler = aznew SoundAssetHandler();
  85. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::EnableCatalogForAsset, AZ::AzTypeInfo<SoundAsset>::Uuid());
  86. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::AddExtension, SoundAsset::FileExtension);
  87. m_assetHandlers.emplace_back(handler);
  88. }
  89. }
  90. void MiniAudioSystemComponent::Deactivate()
  91. {
  92. m_assetHandlers.clear();
  93. ma_engine_uninit(m_engine.get());
  94. m_engine.reset();
  95. MiniAudioRequestBus::Handler::BusDisconnect();
  96. }
  97. ma_engine* MiniAudioSystemComponent::GetSoundEngine()
  98. {
  99. return m_engine.get();
  100. }
  101. void MiniAudioSystemComponent::SetGlobalVolume(float scale)
  102. {
  103. m_globalVolume = scale;
  104. ma_engine_set_volume(m_engine.get(), m_globalVolume);
  105. }
  106. float MiniAudioSystemComponent::GetGlobalVolume() const
  107. {
  108. return m_globalVolume;
  109. }
  110. void MiniAudioSystemComponent::SetGlobalVolumeInDecibels(float decibels)
  111. {
  112. m_globalVolume = ma_volume_db_to_linear(decibels);
  113. SetGlobalVolume(m_globalVolume);
  114. }
  115. } // namespace MiniAudio