CaptureAudioFromWwise.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using UnityEngine;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProMovieCapture
  6. {
  7. /// <summary>
  8. /// Encodes audio directly from Wwise in offline render mode
  9. /// </summary>
  10. [AddComponentMenu("AVPro Movie Capture/Audio/Capture Audio (From Wwise)", 500)]
  11. public class CaptureAudioFromWwise : UnityAudioCapture
  12. {
  13. [SerializeField] CaptureBase _capture = null;
  14. public CaptureBase Capture { get { return _capture; } set { _capture = value; } }
  15. #if AVPRO_MOVIECAPTURE_WWISE_SUPPORT
  16. private int _audioChannelCount;
  17. private int _audioSampleRate;
  18. private ulong _outputDeviceId;
  19. private bool _isRendererRecording;
  20. public override int SampleRate { get { return _audioSampleRate; } }
  21. public override int ChannelCount { get { return _audioChannelCount; } }
  22. public override void PrepareCapture()
  23. {
  24. if (_capture == null)
  25. {
  26. Debug.LogWarning("[AVProMovieCapture] CaptureAudioFromWwise has no Capture source set");
  27. return;
  28. }
  29. var sampleRate = AkSoundEngine.GetSampleRate();
  30. var channelConfig = new AkChannelConfig();
  31. var audioSinkCapabilities = new Ak3DAudioSinkCapabilities();
  32. AkSoundEngine.GetOutputDeviceConfiguration(_outputDeviceId, channelConfig, audioSinkCapabilities);
  33. _audioSampleRate = (int)sampleRate;
  34. _audioChannelCount = (int)channelConfig.uNumChannels;
  35. _outputDeviceId = AkSoundEngine.GetOutputID(AkSoundEngine.AK_INVALID_UNIQUE_ID, 0);
  36. #if UNITY_EDITOR
  37. // Ensure that the editor update does not call AkSoundEngine.RenderAudio().
  38. AkSoundEngineController.Instance.DisableEditorLateUpdate();
  39. #endif
  40. AkSoundEngine.StartDeviceCapture(_outputDeviceId);
  41. AkSoundEngine.SetOfflineRenderingFrameTime(1f / _capture.FrameRate);
  42. AkSoundEngine.SetOfflineRendering(true);
  43. }
  44. public override void StartCapture()
  45. {
  46. if (_capture == null)
  47. {
  48. Debug.LogWarning("[AVProMovieCapture] CaptureAudioFromWwise has no Capture source set");
  49. return;
  50. }
  51. if (!_isRendererRecording)
  52. {
  53. _isRendererRecording = true;
  54. }
  55. FlushBuffer();
  56. }
  57. public override void StopCapture()
  58. {
  59. if (_isRendererRecording)
  60. {
  61. _isRendererRecording = false;
  62. AkSoundEngine.StopDeviceCapture(_outputDeviceId);
  63. #if UNITY_EDITOR
  64. // Bring back editor update calls to AkSoundEngine.RenderAudio().
  65. AkSoundEngineController.Instance.EnableEditorLateUpdate();
  66. #endif
  67. AkSoundEngine.SetOfflineRenderingFrameTime(0f);
  68. AkSoundEngine.SetOfflineRendering(false);
  69. }
  70. }
  71. public override void FlushBuffer()
  72. {
  73. AkSoundEngine.ClearCaptureData();
  74. }
  75. void Update()
  76. {
  77. if (_isRendererRecording && _capture != null && _capture.IsCapturing() && !_capture.IsPaused())
  78. {
  79. var sampleCount = AkSoundEngine.UpdateCaptureSampleCount(_outputDeviceId);
  80. if (sampleCount <= 0)
  81. {
  82. return;
  83. }
  84. var buffer = new float[sampleCount];
  85. var count = AkSoundEngine.GetCaptureSamples(_outputDeviceId, buffer, (uint)buffer.Length);
  86. if (count <= 0)
  87. {
  88. return;
  89. }
  90. _capture.EncodeAudio(buffer);
  91. }
  92. }
  93. #else
  94. void Awake()
  95. {
  96. Debug.LogError("[AVProMovieCapture] CaptureAudioFromWise component requires AVPRO_MOVIECAPTURE_WWISE_SUPPORT to be added to Script Define Symbols");
  97. }
  98. public override int SampleRate { get { return 0; } }
  99. public override int ChannelCount { get { return 0; } }
  100. public override void PrepareCapture() {}
  101. public override void FlushBuffer() {}
  102. public override void StartCapture() {}
  103. public override void StopCapture() {}
  104. #endif // AVPRO_MOVIECAPTURE_WWISE_SUPPORT
  105. }
  106. }