MiniAudioSystemComponent.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "MiniAudioIncludes.h"
  13. #include "SoundAssetHandler.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>()->Version(0);
  31. if (AZ::EditContext* ec = serialize->GetEditContext())
  32. {
  33. ec->Class<MiniAudioSystemComponent>("MiniAudio", "[Description of functionality provided by this System Component]")
  34. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  35. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
  36. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  37. }
  38. }
  39. }
  40. void MiniAudioSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  41. {
  42. provided.push_back(AZ_CRC_CE("MiniAudioService"));
  43. }
  44. void MiniAudioSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  45. {
  46. incompatible.push_back(AZ_CRC_CE("MiniAudioService"));
  47. }
  48. void MiniAudioSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  49. {
  50. }
  51. void MiniAudioSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  52. {
  53. }
  54. MiniAudioSystemComponent::MiniAudioSystemComponent()
  55. {
  56. if (MiniAudioInterface::Get() == nullptr)
  57. {
  58. MiniAudioInterface::Register(this);
  59. }
  60. }
  61. MiniAudioSystemComponent::~MiniAudioSystemComponent()
  62. {
  63. if (MiniAudioInterface::Get() == this)
  64. {
  65. MiniAudioInterface::Unregister(this);
  66. }
  67. }
  68. void MiniAudioSystemComponent::Init()
  69. {
  70. }
  71. void MiniAudioSystemComponent::Activate()
  72. {
  73. m_engine = AZStd::make_unique<ma_engine>();
  74. ma_engine_config engineConfig = ma_engine_config_init();
  75. // The number of audio output channels cannot be dynamically changed during runtime yet.
  76. // The engine configuration setting is done here for future reference.
  77. engineConfig.channels = m_channelCount;
  78. const ma_result result = ma_engine_init(&engineConfig, m_engine.get());
  79. if (result != MA_SUCCESS)
  80. {
  81. AZ_Error("MiniAudio", false, "Failed to initialize audio engine, error %d", result);
  82. }
  83. MiniAudioRequestBus::Handler::BusConnect();
  84. {
  85. SoundAssetHandler* handler = aznew SoundAssetHandler();
  86. AZ::Data::AssetCatalogRequestBus::Broadcast(
  87. &AZ::Data::AssetCatalogRequests::EnableCatalogForAsset, AZ::AzTypeInfo<SoundAsset>::Uuid());
  88. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::AddExtension, SoundAsset::FileExtension);
  89. m_assetHandlers.emplace_back(handler);
  90. }
  91. }
  92. void MiniAudioSystemComponent::Deactivate()
  93. {
  94. m_assetHandlers.clear();
  95. ma_engine_uninit(m_engine.get());
  96. m_engine.reset();
  97. MiniAudioRequestBus::Handler::BusDisconnect();
  98. }
  99. ma_engine* MiniAudioSystemComponent::GetSoundEngine()
  100. {
  101. return m_engine.get();
  102. }
  103. void MiniAudioSystemComponent::SetGlobalVolume(float scale)
  104. {
  105. m_globalVolume = scale;
  106. ma_engine_set_volume(m_engine.get(), m_globalVolume);
  107. }
  108. float MiniAudioSystemComponent::GetGlobalVolume() const
  109. {
  110. return m_globalVolume;
  111. }
  112. void MiniAudioSystemComponent::SetGlobalVolumeInDecibels(float decibels)
  113. {
  114. m_globalVolume = ma_volume_db_to_linear(decibels);
  115. SetGlobalVolume(m_globalVolume);
  116. }
  117. AZ::u32 MiniAudioSystemComponent::GetChannelCount() const
  118. {
  119. return ma_engine_get_channels(m_engine.get());
  120. }
  121. } // namespace MiniAudio