AudioSpectrumObserverNative.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  5. using AOT;
  6. #endif
  7. namespace Agora.Rtc
  8. {
  9. internal static class AudioSpectrumObserverNative
  10. {
  11. internal static IAudioSpectrumObserver AgoraRtcAudioSpectrumObserver = null;
  12. private static AudioSpectrumData ProcessAudioSpectrumData(IntPtr bufferPtr, int length)
  13. {
  14. AudioSpectrumData spectrumData = new AudioSpectrumData();
  15. spectrumData.dataLength = length;
  16. spectrumData.audioSpectrumData = new float[length];
  17. if (bufferPtr != IntPtr.Zero)
  18. {
  19. Marshal.Copy(bufferPtr, spectrumData.audioSpectrumData, 0, length);
  20. }
  21. return spectrumData;
  22. }
  23. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  24. [MonoPInvokeCallback(typeof(Func_LocalAudioSpectrum_Native))]
  25. #endif
  26. internal static bool OnLocalAudioSpectrum(int playerId, IntPtr data)
  27. {
  28. if (AgoraRtcAudioSpectrumObserver == null || playerId != 0) return true;
  29. var irisAudioSpectrumData = (IrisAudioSpectrumData)Marshal.PtrToStructure(data, typeof(IrisAudioSpectrumData));
  30. try
  31. {
  32. return AgoraRtcAudioSpectrumObserver.OnLocalAudioSpectrum(ProcessAudioSpectrumData(irisAudioSpectrumData.audioSpectrumData, irisAudioSpectrumData.dataLength));
  33. }
  34. catch (Exception e)
  35. {
  36. AgoraLog.LogError("[Exception] IAudioSpectrumObserver.OnLocalAudioSpectrum: " + e);
  37. return true;
  38. }
  39. }
  40. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  41. [MonoPInvokeCallback(typeof(Func_RemoteAudioSpectrum_Native))]
  42. #endif
  43. internal static bool OnRemoteAudioSpectrum(int playerId, IntPtr dataspectrums, uint spectrumNumber)
  44. {
  45. if (AgoraRtcAudioSpectrumObserver == null || playerId != 0) return true;
  46. UserAudioSpectrumInfo[] SpectrumInfos = new UserAudioSpectrumInfo[spectrumNumber];
  47. for (int i = 0; i < spectrumNumber; i++)
  48. {
  49. IntPtr p = new IntPtr(dataspectrums.ToInt64() + Marshal.SizeOf(typeof(IrisUserAudioSpectrumInfo)) * i);
  50. var dataspectrum = (IrisUserAudioSpectrumInfo)Marshal.PtrToStructure(p, typeof(IrisUserAudioSpectrumInfo));
  51. SpectrumInfos[i].uid = dataspectrum.uid;
  52. SpectrumInfos[i].spectrumData = ProcessAudioSpectrumData(dataspectrum.spectrumData.audioSpectrumData, dataspectrum.spectrumData.dataLength);
  53. }
  54. try
  55. {
  56. return AgoraRtcAudioSpectrumObserver.OnRemoteAudioSpectrum(SpectrumInfos, spectrumNumber);
  57. }
  58. catch (Exception e)
  59. {
  60. AgoraLog.LogError("[Exception] IAudioSpectrumObserver.OnRemoteAudioSpectrum: " + e);
  61. return true;
  62. }
  63. }
  64. }
  65. }