AudioInputMicrophone.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <AudioInput/AudioInputMicrophone.h>
  9. #include <AzCore/Casting/numeric_cast.h>
  10. #include <MicrophoneBus.h>
  11. namespace Audio
  12. {
  13. ///////////////////////////////////////////////////////////////////////////////////////////////
  14. // Audio Input Source : Microphone
  15. ///////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////
  17. AudioInputMicrophone::AudioInputMicrophone(const SAudioInputConfig& sourceConfig)
  18. {
  19. m_config = sourceConfig;
  20. }
  21. ///////////////////////////////////////////////////////////////////////////////////////////////
  22. AudioInputMicrophone::~AudioInputMicrophone()
  23. {
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////////////////////
  26. void AudioInputMicrophone::ReadInput([[maybe_unused]] const AudioStreamData& data)
  27. {
  28. // ReadInput only used when PUSHing source data in, and would need an internal buffer to store
  29. // the data temporarily. For Microphone, the microphone impl has its own internal buffer, so
  30. // we only need to PULL data in WriteOutput.
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////////////////////
  33. void AudioInputMicrophone::WriteOutput(AkAudioBuffer* akBuffer)
  34. {
  35. AZ::u32 numSampleFramesRequested = (akBuffer->MaxFrames() - akBuffer->uValidFrames);
  36. AkSampleType* channelData[2] = { nullptr, nullptr };
  37. for (AZ::u32 channel = 0; channel < akBuffer->NumChannels(); ++channel)
  38. {
  39. channelData[channel] = akBuffer->GetChannel(channel);
  40. }
  41. size_t numSampleFramesCopied = 0;
  42. MicrophoneRequestBus::BroadcastResult(numSampleFramesCopied, &MicrophoneRequestBus::Events::GetData, reinterpret_cast<void**>(channelData), numSampleFramesRequested, m_config, true);
  43. akBuffer->uValidFrames += aznumeric_cast<AkUInt16>(numSampleFramesCopied);
  44. akBuffer->eState = (numSampleFramesCopied > 0) ? AK_DataReady : AK_NoDataReady;
  45. // handle the AK_NoMoreData condition?
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////////////////////
  48. void AudioInputMicrophone::OnDeactivated()
  49. {
  50. m_config.m_numChannels = 0;
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////////////////////
  53. bool AudioInputMicrophone::IsOk() const
  54. {
  55. // Mono and Stereo only
  56. bool ok = (m_config.m_numChannels == 1 || m_config.m_numChannels == 2);
  57. // 32-bit float or 16-bit int only
  58. ok &= (m_config.m_sampleType == AudioInputSampleType::Float && m_config.m_bitsPerSample == 32)
  59. || (m_config.m_sampleType == AudioInputSampleType::Int && m_config.m_bitsPerSample == 16);
  60. return ok;
  61. }
  62. } // namespace Audio