MicrophoneSystemComponent.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include "MicrophoneSystemComponent.h"
  11. namespace Audio
  12. {
  13. void MicrophoneSystemComponent::Reflect(AZ::ReflectContext* context)
  14. {
  15. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  16. {
  17. serializeContext->Class<MicrophoneSystemComponent, AZ::Component>()
  18. ->Version(1)
  19. ;
  20. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  21. {
  22. editContext->Class<MicrophoneSystemComponent>("Microphone", "Provides access to a connected Microphone Device to capture and read the data")
  23. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  24. ->Attribute(AZ::Edit::Attributes::Category, "Audio")
  25. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  26. ;
  27. }
  28. }
  29. }
  30. void MicrophoneSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  31. {
  32. provided.push_back(AZ_CRC_CE("MicrophoneService"));
  33. }
  34. void MicrophoneSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  35. {
  36. incompatible.push_back(AZ_CRC_CE("MicrophoneService"));
  37. }
  38. void MicrophoneSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  39. {
  40. AZ_UNUSED(required);
  41. }
  42. void MicrophoneSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  43. {
  44. AZ_UNUSED(dependent);
  45. }
  46. MicrophoneSystemComponent::MicrophoneSystemComponent()
  47. : m_impl(Implementation::Create())
  48. {
  49. }
  50. MicrophoneSystemComponent::~MicrophoneSystemComponent()
  51. {
  52. delete m_impl;
  53. m_impl = nullptr;
  54. }
  55. void MicrophoneSystemComponent::Init()
  56. {
  57. }
  58. void MicrophoneSystemComponent::Activate()
  59. {
  60. InitializeDevice();
  61. MicrophoneRequestBus::Handler::BusConnect();
  62. }
  63. void MicrophoneSystemComponent::Deactivate()
  64. {
  65. MicrophoneRequestBus::Handler::BusDisconnect();
  66. EndSession();
  67. ShutdownDevice();
  68. }
  69. bool MicrophoneSystemComponent::InitializeDevice()
  70. {
  71. if (m_impl)
  72. {
  73. m_initialized = m_impl->InitializeDevice();
  74. if (!m_initialized)
  75. {
  76. AZ_TracePrintf("MicrophoneSystemComponent", "Failed to initialize a Microphone device, check your OS audio device settings.\n");
  77. ShutdownDevice();
  78. }
  79. return m_initialized;
  80. }
  81. return false;
  82. }
  83. void MicrophoneSystemComponent::ShutdownDevice()
  84. {
  85. if (m_impl)
  86. {
  87. m_impl->ShutdownDevice();
  88. m_initialized = false;
  89. }
  90. }
  91. bool MicrophoneSystemComponent::StartSession()
  92. {
  93. if (m_impl && m_initialized)
  94. {
  95. return m_impl->StartSession();
  96. }
  97. return false;
  98. }
  99. void MicrophoneSystemComponent::EndSession()
  100. {
  101. if (m_impl && m_initialized)
  102. {
  103. m_impl->EndSession();
  104. }
  105. }
  106. bool MicrophoneSystemComponent::IsCapturing()
  107. {
  108. if (m_impl && m_initialized)
  109. {
  110. return m_impl->IsCapturing();
  111. }
  112. return false;
  113. }
  114. SAudioInputConfig MicrophoneSystemComponent::GetFormatConfig() const
  115. {
  116. if (m_impl)
  117. {
  118. return m_impl->GetFormatConfig();
  119. }
  120. return SAudioInputConfig();
  121. }
  122. AZStd::size_t MicrophoneSystemComponent::GetData(void** outputData, AZStd::size_t numFrames, const SAudioInputConfig& targetConfig, bool shouldDeinterleave)
  123. {
  124. if (m_impl && IsCapturing())
  125. {
  126. return m_impl->GetData(outputData, numFrames, targetConfig, shouldDeinterleave);
  127. }
  128. return 0;
  129. }
  130. } // namespace Audio