- Home /
Returning spectrumdata to an array of 64 floats in good framerate
I am making a parametric equalizer, and the script underneath works fine with returning 8 floats, based on the incoming audio. What I can't seem to fix, is returning a higher number of floats than 8 without unity framerate going superlow, or it just doesnt work. I need abit help to get it to work returning to 64values with good speed. Maybe there's a better solution than mine?!
using UnityEngine; using System.Collections;
public class AudioToFloats : MonoBehaviour {
private float[] samples = new float[512];
public static float[] curValues = new float[8];
string ss = "";
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
int count = 0;
float diff = 0;
AudioListener.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris);
ss = "";
for (int i = 0; i < 8; ++i)
{
float average = 0;
int sampleCount = (int)Mathf.Pow(2, i) * 2;
for (int j = 0; j < sampleCount; ++j)
{
average += samples[count] * (count + 1);
++count;
}
average /= count;
diff = Mathf.Clamp(average * 10 - curValues[i], 0, 4);
curValues[i] = average * 10;
ss += curValues[i].ToString("0.000")+",";
}
}
void OnGUI()
{
GUI.Label(new Rect(150, 0, 600, 500), ss);
int ImageHeight = 300;
for (int i = 0; i < 8; i++ )
{
float Height = ImageHeight * (1 - curValues[i]);
}
}
}
Answer by drudiverse · Dec 01, 2014 at 07:50 AM
you can try different FFT settings than blackman harris, see documents.
i believe that you can do 100 bandpass filters in real time with a streaming kind of set up, i.e. analysing 4410 samples every 100ms or so. you should be able to get 100 realtime freq band readouts, and 400 on a quad core.
i ll see your FCT, only way to hasten it up is normally, multicore, jump between frames, and different fft window settings. at it's a unity function, dunno if you can jump frames and multicore :(
Aldonetto sais:
Unity generates the audio spectrum incredibly fast, so this whole script consumes less than 1 mS at Update in my machine (a 1.8GHz Intel Core Duo PC). If you want to try this script with a reference sound, download this
he has a code version here: http://answers.unity3d.com/questions/157940/getoutputdata-and-getspectrumdata-they-represent-t.html
Your answer
Follow this Question
Related Questions
How to determine the pitch from GetSpectrumData() to make a polyphonic tuner? 1 Answer
GetSpectrumData doesn't update 2 Answers
How to get GetSpectrumData of an audioClip before playing it? 2 Answers
GetSpectrumData From A Muted AudioSource? 1 Answer
For the spectrum data for the return value of GetSpectrumData 0 Answers