AmbisonicWavWriter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProMovieCapture
  8. {
  9. /// <summary>
  10. /// Combine multiple AmbisonicSource streams into a single ambisonic mix
  11. /// We had to use an overcomplicated buffer queue system because the with the audio thread
  12. /// we have no idea which frame we are on, and so no reliable way to know when to write
  13. /// or accumulate the audio into the mix.
  14. /// </summary>
  15. [AddComponentMenu("AVPro Movie Capture/Audio/Ambisonic WAV Writer", 601)]
  16. public class AmbisonicWavWriter : MonoBehaviour
  17. {
  18. [SerializeField] CaptureBase _capture = null;
  19. [SerializeField] AmbisonicOrder _order = AmbisonicOrder.Third;
  20. [SerializeField] AmbisonicFormat _format = AmbisonicFormat.ACN_SN3D;
  21. [SerializeField] string _filename = "output.wav";
  22. [SerializeField, Range(4, 32)] int _bufferCount = 16;
  23. private float[] _outSamples = null;
  24. private WavWriter _wavWriter = null;
  25. private List<AmbisonicSource> _sources = new List<AmbisonicSource>(8);
  26. private int _pendingSampleCount = 0;
  27. public AmbisonicOrder Order { get { return _order; } }
  28. public AmbisonicFormat Format { get { return _format; } }
  29. internal void AddSource(AmbisonicSource source)
  30. {
  31. lock (_sources)
  32. {
  33. SetupSource(source);
  34. _sources.Add(source);
  35. }
  36. }
  37. internal void RemoveSource(AmbisonicSource source)
  38. {
  39. lock (_sources)
  40. {
  41. _sources.Remove(source);
  42. }
  43. }
  44. void OnDisable()
  45. {
  46. StopCapture();
  47. }
  48. void SetupSource(AmbisonicSource source)
  49. {
  50. source.Setup(_order, Ambisonic.GetChannelOrder(_format), Ambisonic.GetNormalisation(_format), _bufferCount);
  51. source.FlushBuffers();
  52. }
  53. void ToggleCapturing(bool isCapturing)
  54. {
  55. if (isCapturing && !IsCapturing())
  56. {
  57. StartCapture();
  58. }
  59. else if (!isCapturing && IsCapturing())
  60. {
  61. StopCapture();
  62. }
  63. }
  64. void StartCapture()
  65. {
  66. #if UNITY_EDITOR
  67. if (UnityEditor.EditorApplication.isPaused) return;
  68. #endif
  69. Debug.Assert(_outSamples == null);
  70. Debug.Assert(_wavWriter == null);
  71. _pendingSampleCount = 0;
  72. int coeffCount = Ambisonic.GetCoeffCount(_order);
  73. Debug.Assert(coeffCount == 4 || coeffCount == 9 || coeffCount == 16);
  74. lock (this)
  75. {
  76. foreach (AmbisonicSource source in _sources)
  77. {
  78. SetupSource(source);
  79. }
  80. string path = Path.Combine(Application.persistentDataPath, _filename);
  81. if (_capture)
  82. {
  83. path = _capture.LastFilePath + ".wav";
  84. }
  85. Debug.Log("[AVProMovieCapture] Writing Ambisonic WAV to " + path);
  86. _wavWriter = new WavWriter(path, coeffCount, AudioSettings.outputSampleRate, WavWriter.SampleFormat.Float32);
  87. _outSamples = new float[coeffCount * AudioSettings.outputSampleRate * 1]; // 1 second buffer
  88. }
  89. }
  90. void StopCapture()
  91. {
  92. if (_wavWriter != null)
  93. {
  94. lock (_wavWriter)
  95. {
  96. FlushWavWriter();
  97. _wavWriter.Dispose();
  98. _wavWriter = null;
  99. _outSamples = null;
  100. }
  101. }
  102. }
  103. bool IsCapturing()
  104. {
  105. return (_wavWriter != null && _outSamples != null);
  106. }
  107. void LateUpdate()
  108. {
  109. ToggleCapturing(_capture != null && _capture.IsCapturing());
  110. ProcessSources(isDraining:false);
  111. }
  112. void ProcessSources(bool isDraining)
  113. {
  114. if (!IsCapturing() || _capture.IsPaused()) return;
  115. lock(this)
  116. {
  117. if (_sources.Count > 0)
  118. {
  119. // Find the minimum number of full buffers across all sources
  120. int minBuffers = int.MaxValue;
  121. for (int i = 0; i < _sources.Count; i++)
  122. {
  123. int bufferCount = _sources[i].GetFullBufferCount();
  124. minBuffers = Mathf.Min(bufferCount, minBuffers);
  125. }
  126. // Process the minimum number of full buffers
  127. for (int j = 0; j < minBuffers; j++)
  128. {
  129. for (int i = 0; i < _sources.Count; i++)
  130. {
  131. // TODO: fix this draining - it doesn't take into account cases where some of the sources
  132. // still have a full buffer, but other's don't.
  133. _sources[i].SendSamplesToSink(i != 0, isDraining);
  134. }
  135. FlushWavWriter();
  136. }
  137. }
  138. }
  139. }
  140. internal void MixSamples(float[] samples, int sampleCount, bool addSamples)
  141. {
  142. Debug.Assert(sampleCount < _outSamples.Length);
  143. if (sampleCount < _outSamples.Length)
  144. {
  145. if (!addSamples)
  146. {
  147. _pendingSampleCount = sampleCount;
  148. System.Buffer.BlockCopy(samples, 0, _outSamples, 0, sampleCount * sizeof(float));
  149. }
  150. else
  151. {
  152. // Accumulate samples into the mix
  153. for (int i = 0; i < sampleCount; i++)
  154. {
  155. _outSamples[i] += samples[i];
  156. }
  157. }
  158. }
  159. else
  160. {
  161. Debug.LogError("too many samples");
  162. }
  163. }
  164. void FlushWavWriter()
  165. {
  166. if (_pendingSampleCount > 0)
  167. {
  168. _wavWriter.WriteInterleaved(_outSamples, _pendingSampleCount);
  169. _pendingSampleCount = 0;
  170. }
  171. }
  172. }
  173. }