Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class AudioFFT : MonoBehaviour {
  8.  
  9. // fmod variables
  10. private FMOD.System fmodSystem;
  11. private FMOD.Channel fmodChannel;
  12. private FMOD.ChannelGroup fmodChannelGroup;
  13. private FMOD.DSP fftDSP;
  14. private FMOD.Sound exampleSong;
  15. private FMOD.RESULT fmodResult;
  16.  
  17. // sound variable
  18. private string soundName;
  19.  
  20. // spectrum variables
  21. IntPtr unmanagedData;
  22. uint unmanagedDataLen = 0;
  23. FMOD.DSP_PARAMETER_FFT waveFormBuffer = new FMOD.DSP_PARAMETER_FFT();
  24. //float spectrum[];
  25.  
  26. // Use this for initialization
  27. void Start () {
  28. soundName = "bensound-happyrock";
  29.  
  30.  
  31. FMODInitialize();
  32. LoadSong(soundName);
  33. Play();
  34. GetSpectrum();
  35. }
  36.  
  37. private void FMODInitialize()
  38. {
  39. fmodResult = FMOD.Factory.System_Create(out fmodSystem); // creates the fmod system object
  40. FMODerrorCheck(fmodResult);
  41. if (fmodResult != FMOD.RESULT.OK)
  42. Debug.Log("ErrorSystem");
  43.  
  44. //fmodResult = fmodSystem.setDSPBufferSize(1024, 4); // sets the internal mixing buffer, controls mixer latency or granularity
  45. fmodResult = fmodSystem.init(32, FMOD.INITFLAGS.NORMAL, IntPtr.Zero); // initializes fmodSystem normally
  46. FMODerrorCheck(fmodResult);
  47. if (fmodResult != FMOD.RESULT.OK)
  48. Debug.Log("ErrorInit");
  49.  
  50. fmodResult = fmodSystem.getMasterChannelGroup(out fmodChannelGroup);
  51.  
  52. fmodResult = fmodSystem.createDSPByType(FMOD.DSP_TYPE.FFT, out fftDSP);
  53. FMODerrorCheck(fmodResult);
  54. if (fmodResult != FMOD.RESULT.OK)
  55. Debug.Log("ErrorInit");
  56.  
  57. fmodResult = fmodChannelGroup.addDSP(0, fftDSP);
  58. FMODerrorCheck(fmodResult);
  59. if (fmodResult != FMOD.RESULT.OK)
  60. Debug.Log("ErrorAddDSP");
  61.  
  62. FMODerrorCheck(fmodResult);
  63. }
  64.  
  65. private void LoadSong(string name)
  66. {
  67. fmodResult = fmodSystem.createStream(Application.dataPath + "/Sounds/" + name + ".mp3",
  68. FMOD.MODE.LOOP_NORMAL, out exampleSong); // creates a stream for the sound
  69. FMODerrorCheck(fmodResult);
  70. if (fmodResult != FMOD.RESULT.OK)
  71. Debug.Log("ErrorLoad");
  72. }
  73.  
  74. private void Play()
  75. {
  76. fmodResult = fmodSystem.playSound(exampleSong, fmodChannelGroup, false, out fmodChannel); // plays the sound
  77. FMODerrorCheck(fmodResult);
  78. if (fmodResult != FMOD.RESULT.OK)
  79. Debug.Log("ErrorPlay");
  80.  
  81.  
  82. fmodResult = fmodChannel.setChannelGroup(fmodChannelGroup);
  83. fmodResult = fmodChannel.setMode(FMOD.MODE.LOOP_NORMAL);
  84. fmodResult = fmodChannel.setVolume(0.1f);
  85. FMODerrorCheck(fmodResult);
  86. if (fmodResult != FMOD.RESULT.OK)
  87. Debug.Log("ErrorAddDSP");
  88. }
  89.  
  90. private void GetSpectrum()
  91. {
  92. fmodResult = fftDSP.getParameterData((int)FMOD.DSP_FFT.SPECTRUMDATA, out unmanagedData, out unmanagedDataLen);
  93. FMODerrorCheck(fmodResult);
  94. if (fmodResult != FMOD.RESULT.OK)
  95. Debug.Log("ErrorGetParameter");
  96.  
  97. waveFormBuffer = (FMOD.DSP_PARAMETER_FFT)Marshal.PtrToStructure(unmanagedData, typeof(FMOD.DSP_PARAMETER_FFT));
  98.  
  99. Debug.Log(waveFormBuffer.numchannels);
  100. for (int channel = 0; channel < waveFormBuffer.numchannels; channel++)
  101. {
  102. for (int bin = 0; bin < waveFormBuffer.length; bin++)
  103. {
  104. float val = waveFormBuffer.spectrum[channel][bin];
  105. Debug.Log(val);
  106. }
  107. }
  108. }
  109.  
  110. private void Update()
  111. {
  112. fmodSystem.update();
  113. }
  114.  
  115. private void OnApplicationQuit()
  116. {
  117. fmodResult = fmodSystem.release();
  118. }
  119.  
  120. private void FMODerrorCheck(FMOD.RESULT res)
  121. {
  122. if (fmodResult != FMOD.RESULT.OK)
  123. Debug.Log(FMOD.Error.String(res));
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement