Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. public class SpectrumDataTest : MonoBehaviour {
  6.  
  7. private AudioSource audioSource;
  8.  
  9. // These variables are meant to get the left and right channels of the music
  10. private float[] samples0Channel;
  11. private float[] samples1Channel;
  12.  
  13. // Inside the History buffer are the previous instant energies
  14. // Using a history buffer saves a lot of CPU
  15. // Refer to 1.2 in references to get more info
  16. private float[] historyBuffer;
  17.  
  18. // The size of our sampled arrays. Usually : 1024
  19. public int bufferSize;
  20.  
  21. // The type of window we are going to apply at the Spectrum
  22. public FFTWindow window;
  23.  
  24. public GameObject[] samplesObjects;
  25.  
  26. private MeshRenderer m;
  27.  
  28. void Start () {
  29.  
  30. // Init all things
  31. audioSource = GetComponent<AudioSource> ();
  32. samples0Channel = new float[bufferSize];
  33. samples1Channel = new float[bufferSize];
  34. historyBuffer = new float[43];
  35. m = samplesObjects [8].GetComponent<MeshRenderer> ();
  36. }
  37.  
  38. void Update () {
  39.  
  40. // Get the instant energy of the song this frame
  41. float instantEnergy = GetInstantEnergy ();
  42.  
  43. // Use the History Buffer to compute the average energy of the sound the past 1 second
  44. // Refer to reference to more info
  45. float localAverageEnergy = GetLocalAverageEnergy ();
  46.  
  47. // Compute the variance with the history buffer and the average energy
  48. float varianceEnergies = ComputeVariance (localAverageEnergy);
  49.  
  50. // Use the linear equation described in the reference to compute the constant C
  51. double constantC = (-0.0025714 * varianceEnergies) + 1.5142857;
  52.  
  53. // Shift the history buffer one position to the right
  54. // Save it in another array
  55. float[] shiftedHistoryBuffer = ShiftArray (historyBuffer, 1);
  56.  
  57. // Make the first position to be our instantEnergy
  58. shiftedHistoryBuffer [0] = instantEnergy;
  59.  
  60. // Override the elements of the new array on the old one
  61. OverrideElementsToAnotherArray (shiftedHistoryBuffer, historyBuffer);
  62.  
  63. // Ask if a beat has been done
  64. if (instantEnergy > constantC * localAverageEnergy) {
  65. // Beat!
  66. m.material.color = Color.red;
  67. } else {
  68. // Not Beat!
  69. m.material.color = Color.blue;
  70. }
  71.  
  72.  
  73. }
  74.  
  75. #region ALGORITHM_USE
  76. private float GetInstantEnergy(){
  77.  
  78. float result = 0;
  79.  
  80. // Fill the samples arrays
  81. // Each value of these arrays are floats between 0 and 1
  82. // and are the frequency percentage of use
  83. audioSource.GetSpectrumData (samples0Channel, 0, FFTWindow.Hamming);
  84. audioSource.GetSpectrumData (samples1Channel, 1, FFTWindow.Hamming);
  85.  
  86. // Refer to reference: e = sum(a[i]^2 + b[i]^2)
  87. for (int i = 0; i < bufferSize; i++) {
  88. result += (float) System.Math.Pow(samples0Channel [i],2) + (float) System.Math.Pow(samples1Channel [i],2);
  89. }
  90.  
  91. return result;
  92. }
  93.  
  94. private float GetLocalAverageEnergy(){
  95. float result = 0;
  96.  
  97. // Refer to reference: <E> = sum(E[i]^2) / 43
  98.  
  99. for (int i = 0; i < historyBuffer.Length; i++) {
  100. result += (float) System.Math.Pow (historyBuffer [i], 2);
  101. }
  102.  
  103. return result / historyBuffer.Length;
  104. }
  105.  
  106. private float ComputeVariance(float _averageEnery){
  107. float result = 0;
  108.  
  109. // Refer to reference: V = sum( (E[i] - <E>)^2 ) / 43
  110.  
  111. for (int i = 0; i < historyBuffer.Length; i++) {
  112. result += (float) System.Math.Pow ( historyBuffer[i] - _averageEnery , 2);
  113. }
  114.  
  115. return result / historyBuffer.Length;
  116. }
  117.  
  118. #endregion
  119.  
  120. #region UTILITY_USE
  121. private void OverrideElementsToAnotherArray(float[] _from, float[] _to){
  122. for (int i = 0; i < _from.Length; i++) {
  123. _to [i] = _from [i];
  124. }
  125. }
  126.  
  127. private float[] ShiftArray(float[] _array, int amount){
  128.  
  129. float[] result = new float[_array.Length];
  130.  
  131. for (int i = 0; i < _array.Length - amount; i++) {
  132. result [i + amount] = _array [i];
  133. }
  134.  
  135. return result;
  136.  
  137. }
  138.  
  139. private string historybuffer() {
  140. string s = "";
  141. for (int i = 0; i<historyBuffer.Length; i++) {
  142. s += (historyBuffer[i] + ",");
  143. }
  144. return s;
  145. }
  146.  
  147. #endregion
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement