Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AudioFFT : MonoBehaviour {
- // fmod variables
- private FMOD.System fmodSystem;
- private FMOD.Channel fmodChannel;
- private FMOD.ChannelGroup fmodChannelGroup;
- private FMOD.DSP fftDSP;
- private FMOD.Sound exampleSong;
- private FMOD.RESULT fmodResult;
- // sound variable
- private string soundName;
- // spectrum variables
- IntPtr unmanagedData;
- uint unmanagedDataLen = 0;
- FMOD.DSP_PARAMETER_FFT waveFormBuffer = new FMOD.DSP_PARAMETER_FFT();
- //float spectrum[];
- // Use this for initialization
- void Start () {
- soundName = "bensound-happyrock";
- FMODInitialize();
- LoadSong(soundName);
- Play();
- GetSpectrum();
- }
- private void FMODInitialize()
- {
- fmodResult = FMOD.Factory.System_Create(out fmodSystem); // creates the fmod system object
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorSystem");
- //fmodResult = fmodSystem.setDSPBufferSize(1024, 4); // sets the internal mixing buffer, controls mixer latency or granularity
- fmodResult = fmodSystem.init(32, FMOD.INITFLAGS.NORMAL, IntPtr.Zero); // initializes fmodSystem normally
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorInit");
- fmodResult = fmodSystem.getMasterChannelGroup(out fmodChannelGroup);
- fmodResult = fmodSystem.createDSPByType(FMOD.DSP_TYPE.FFT, out fftDSP);
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorInit");
- fmodResult = fmodChannelGroup.addDSP(0, fftDSP);
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorAddDSP");
- FMODerrorCheck(fmodResult);
- }
- private void LoadSong(string name)
- {
- fmodResult = fmodSystem.createStream(Application.dataPath + "/Sounds/" + name + ".mp3",
- FMOD.MODE.LOOP_NORMAL, out exampleSong); // creates a stream for the sound
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorLoad");
- }
- private void Play()
- {
- fmodResult = fmodSystem.playSound(exampleSong, fmodChannelGroup, false, out fmodChannel); // plays the sound
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorPlay");
- fmodResult = fmodChannel.setChannelGroup(fmodChannelGroup);
- fmodResult = fmodChannel.setMode(FMOD.MODE.LOOP_NORMAL);
- fmodResult = fmodChannel.setVolume(0.1f);
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorAddDSP");
- }
- private void GetSpectrum()
- {
- fmodResult = fftDSP.getParameterData((int)FMOD.DSP_FFT.SPECTRUMDATA, out unmanagedData, out unmanagedDataLen);
- FMODerrorCheck(fmodResult);
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log("ErrorGetParameter");
- waveFormBuffer = (FMOD.DSP_PARAMETER_FFT)Marshal.PtrToStructure(unmanagedData, typeof(FMOD.DSP_PARAMETER_FFT));
- Debug.Log(waveFormBuffer.numchannels);
- for (int channel = 0; channel < waveFormBuffer.numchannels; channel++)
- {
- for (int bin = 0; bin < waveFormBuffer.length; bin++)
- {
- float val = waveFormBuffer.spectrum[channel][bin];
- Debug.Log(val);
- }
- }
- }
- private void Update()
- {
- fmodSystem.update();
- }
- private void OnApplicationQuit()
- {
- fmodResult = fmodSystem.release();
- }
- private void FMODerrorCheck(FMOD.RESULT res)
- {
- if (fmodResult != FMOD.RESULT.OK)
- Debug.Log(FMOD.Error.String(res));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement