UnityAudioCapture.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProMovieCapture
  6. {
  7. /// <summary>
  8. /// Base class for capturing audio from Unity
  9. /// Two classes derive from this:
  10. /// 1) CaptureAudioFromAudioListener - used for real-time capture
  11. /// 2) CaptureAudioFromAudioRenderer - used for offline rendering
  12. /// </summary>
  13. public abstract class UnityAudioCapture : MonoBehaviour
  14. {
  15. public virtual int OverflowCount
  16. {
  17. get { return 0; }
  18. }
  19. public abstract int SampleRate
  20. {
  21. get;
  22. }
  23. public abstract int ChannelCount
  24. {
  25. get;
  26. }
  27. public abstract void PrepareCapture();
  28. public abstract void StartCapture();
  29. public abstract void StopCapture();
  30. public abstract void FlushBuffer();
  31. public virtual System.IntPtr ReadData(out int length) { length = 0; return System.IntPtr.Zero; }
  32. public static int GetUnityAudioChannelCount()
  33. {
  34. int result = GetChannelCount(AudioSettings.driverCapabilities);
  35. if (
  36. #if !UNITY_2019_2_OR_NEWER
  37. AudioSettings.speakerMode != AudioSpeakerMode.Raw &&
  38. #endif
  39. AudioSettings.speakerMode < AudioSettings.driverCapabilities)
  40. {
  41. result = GetChannelCount(AudioSettings.speakerMode);
  42. }
  43. return result;
  44. }
  45. private static int GetChannelCount(AudioSpeakerMode mode)
  46. {
  47. int result = 0;
  48. switch (mode)
  49. {
  50. #if !UNITY_2019_2_OR_NEWER
  51. case AudioSpeakerMode.Raw:
  52. break;
  53. #endif
  54. case AudioSpeakerMode.Mono:
  55. result = 1;
  56. break;
  57. case AudioSpeakerMode.Stereo:
  58. result = 2;
  59. break;
  60. case AudioSpeakerMode.Quad:
  61. result = 4;
  62. break;
  63. case AudioSpeakerMode.Surround:
  64. result = 5;
  65. break;
  66. case AudioSpeakerMode.Mode5point1:
  67. result = 6;
  68. break;
  69. case AudioSpeakerMode.Mode7point1:
  70. result = 8;
  71. break;
  72. case AudioSpeakerMode.Prologic:
  73. result = 2;
  74. break;
  75. }
  76. return result;
  77. }
  78. }
  79. }