Advertisement
Guest User

PKFxAudioSourceSampler

a guest
Jun 19th, 2018
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using AOT;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7.  
  8. [RequireComponent(typeof(AudioSource))]
  9. public class PKFxAudioSourceSampler : MonoBehaviour
  10. {
  11.     public string channel;
  12.  
  13.     private static Dictionary<string, AudioSource> m_AudioSources = new Dictionary<string, AudioSource>();
  14.     private static float[] m_Samples;
  15.     private static GCHandle m_SamplesHandle;
  16.     private bool tmp;
  17.  
  18.     void Start()
  19.     {
  20.         // Initialize the data buffer. Expected size is 1024.
  21.         m_Samples = new float[1024];
  22.         // Pin the array
  23.         m_SamplesHandle = GCHandle.Alloc(m_Samples, GCHandleType.Pinned);
  24.         tmp = false;
  25.         string key = channel;
  26.         if (string.IsNullOrEmpty(key))
  27.             key = "Master";
  28.         m_AudioSources.Add(key, GetComponent<AudioSource>());
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.         if (!tmp)
  34.         {
  35.             AudioSource audioSrc = null;
  36.             if (m_AudioSources.TryGetValue(channel, out audioSrc))
  37.             {
  38.                 PKFxManager.SetDelegateOnAudioSpectrumData(Marshal.GetFunctionPointerForDelegate(new Func<IntPtr, IntPtr, IntPtr>(OnAudioSpectrumData)));
  39.                 PKFxManager.SetDelegateOnAudioWaveformData(Marshal.GetFunctionPointerForDelegate(new Func<IntPtr, IntPtr, IntPtr>(OnAudioWaveformData)));
  40.             }
  41.             tmp = true;
  42.         }
  43.     }
  44.  
  45.     // MonoPInvokeCallback is required by IL2CPP (mostly for iOS' static libs)
  46.     [MonoPInvokeCallback(typeof(PKFxManager.AudioCallback))]
  47.     public static IntPtr OnAudioSpectrumData(IntPtr channelName, IntPtr nbSamples)
  48.     {
  49.         string name = Marshal.PtrToStringAnsi(channelName);
  50.         Debug.Log(name);
  51.         AudioSource audioSrc = null;
  52.         if (m_AudioSources.TryGetValue(name, out audioSrc))
  53.         {
  54.             audioSrc.GetSpectrumData(m_Samples, 0, FFTWindow.Rectangular);
  55.             // Last value filled by Unity seems wrong...
  56.             m_Samples[1023] = m_Samples[1022];
  57.             // return the address of the pinned object.
  58.             return m_SamplesHandle.AddrOfPinnedObject();
  59.         }
  60.         return IntPtr.Zero;
  61.     }
  62.  
  63.     [MonoPInvokeCallback(typeof(PKFxManager.AudioCallback))]
  64.     public static IntPtr OnAudioWaveformData(IntPtr channelName, IntPtr nbSamples)
  65.     {
  66.         string name = Marshal.PtrToStringAnsi(channelName);
  67.         Debug.Log(name);
  68.         AudioSource audioSrc = null;
  69.         if (m_AudioSources.TryGetValue(name, out audioSrc))
  70.         {
  71.             audioSrc.GetOutputData(m_Samples, 0);
  72.             // return the address of the pinned object.
  73.             return m_SamplesHandle.AddrOfPinnedObject();
  74.         }
  75.         return IntPtr.Zero;
  76.     }
  77.  
  78.     void OnDestroy()
  79.     {
  80.         // Unpin the object to allow GC.
  81.         m_SamplesHandle.Free();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement