CaptureAudioFromAudioRenderer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #if UNITY_2017_3_OR_NEWER
  2. #define AVPRO_MOVIECAPTURE_OFFLINE_AUDIOCAPTURE
  3. #endif
  4. #if UNITY_2019_3_OR_NEWER
  5. #define UNITY_NATIVEARRAY_GETSUBARRAY_SUPPORT
  6. #endif
  7. #if AVPRO_MOVIECAPTURE_OFFLINE_AUDIOCAPTURE
  8. using UnityEngine;
  9. #if UNITY_2018_1_OR_NEWER
  10. using Unity.Collections;
  11. #else
  12. using UnityEngine.Collections;
  13. #endif
  14. //-----------------------------------------------------------------------------
  15. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  16. //-----------------------------------------------------------------------------
  17. namespace RenderHeads.Media.AVProMovieCapture
  18. {
  19. /// <summary>
  20. /// Encodes audio directly from AudioRenderer (https://docs.unity3d.com/ScriptReference/AudioRenderer.html)
  21. /// While capturing, audio playback in Unity becomes muted
  22. /// </summary>
  23. [AddComponentMenu("AVPro Movie Capture/Audio/Capture Audio (From AudioRenderer)", 500)]
  24. public class CaptureAudioFromAudioRenderer : UnityAudioCapture
  25. {
  26. [SerializeField] CaptureBase _capture = null;
  27. private int _unityAudioChannelCount;
  28. private bool _isRendererRecording;
  29. public CaptureBase Capture { get { return _capture; } set { _capture = value; } }
  30. public override int SampleRate { get { return AudioSettings.outputSampleRate; } }
  31. public override int ChannelCount { get { return _unityAudioChannelCount; } }
  32. public override void PrepareCapture()
  33. {
  34. _unityAudioChannelCount = GetUnityAudioChannelCount();
  35. }
  36. #if UNITY_NATIVEARRAY_GETSUBARRAY_SUPPORT
  37. private NativeArray<float> _audioBuffer;
  38. #endif
  39. private NativeArray<float> GetAudioBufferOfLength(int length)
  40. {
  41. #if UNITY_NATIVEARRAY_GETSUBARRAY_SUPPORT
  42. if (_audioBuffer.Length < length)
  43. {
  44. Debug.Log("Creating new audio buffer with length: " + length);
  45. if (_audioBuffer.IsCreated)
  46. _audioBuffer.Dispose();
  47. _audioBuffer = new NativeArray<float>(length, Allocator.Persistent);
  48. }
  49. return _audioBuffer.GetSubArray(0, length);
  50. #else
  51. return new NativeArray<float>(length, Allocator.TempJob);
  52. #endif
  53. }
  54. private void DisposeAudioBuffer(NativeArray<float> buffer)
  55. {
  56. #if !UNITY_NATIVEARRAY_GETSUBARRAY_SUPPORT
  57. buffer.Dispose();
  58. #endif
  59. }
  60. public override void StartCapture()
  61. {
  62. if (_capture == null)
  63. {
  64. Debug.LogWarning("[AVProMovieCapture] CaptureAudioFromAudioRenderer has no Capture source set");
  65. return;
  66. }
  67. if (!_isRendererRecording)
  68. {
  69. #if UNITY_NATIVEARRAY_GETSUBARRAY_SUPPORT
  70. // Allocate a big buffer for capturing the audio into
  71. _audioBuffer = new NativeArray<float>(65536, Allocator.Persistent);
  72. #endif
  73. AudioRenderer.Start();
  74. _isRendererRecording = true;
  75. }
  76. FlushBuffer();
  77. }
  78. public override void StopCapture()
  79. {
  80. if (_isRendererRecording)
  81. {
  82. _isRendererRecording = false;
  83. AudioRenderer.Stop();
  84. #if UNITY_NATIVEARRAY_GETSUBARRAY_SUPPORT
  85. _audioBuffer.Dispose();
  86. #endif
  87. }
  88. }
  89. public override void FlushBuffer()
  90. {
  91. int sampleFrameCount = AudioRenderer.GetSampleCountForCaptureFrame();
  92. int sampleCount = sampleFrameCount * _unityAudioChannelCount;
  93. NativeArray<float> buffer = GetAudioBufferOfLength(sampleCount);
  94. AudioRenderer.Render(buffer);
  95. DisposeAudioBuffer(buffer);
  96. }
  97. void Update()
  98. {
  99. if (_isRendererRecording && _capture != null && _capture.IsCapturing() && !_capture.IsPaused())
  100. {
  101. int sampleFrameCount = AudioRenderer.GetSampleCountForCaptureFrame();
  102. int sampleCount = sampleFrameCount * _unityAudioChannelCount;
  103. NativeArray<float> buffer = GetAudioBufferOfLength(sampleCount);
  104. if (AudioRenderer.Render(buffer))
  105. {
  106. _capture.EncodeAudio(buffer);
  107. }
  108. DisposeAudioBuffer(buffer);
  109. }
  110. }
  111. }
  112. }
  113. #endif // AVPRO_MOVIECAPTURE_OFFLINE_AUDIOCAPTURE