GetSpectrumData error
This feels like I'm missing something basic, but I'm a bit stuck.
I'm just trying to do a vanilla AudioSource.GetSpectrumData() call but I'm getting an error in the console that looks deep:
/Users/builduser/buildslave/unity/build/Runtime/Audio/AudioSource.cpp(1621) : Error executing m_dryGroup->getSpectrum (samples, numSamples, channelOffset, windowType) (An invalid parameter was passed to this function. )
UnityEngine.AudioSource:GetSpectrumData(Single[], Int32, FFTWindow)
AudioMediator:Update() (at Assets/Scripts/AudioMediator.cs:24)
my code looks like this:
public AudioSource MainAudioSource;
void Update () {
float[] b = new float[32];
MainAudioSource.GetSpectrumData (b, 0, FFTWindow.BlackmanHarris);
}
Here is a sample project
Answer by julianwitte · Sep 14, 2016 at 10:19 PM
I found this post looking for an answer, but at the end I found an answer myself. Apparently 32 is not enough for the GetSpectrumData function, the minimum length for the float array is 64.
Answer by Red_Nut · Dec 01, 2015 at 12:15 AM
Replacing AudioSource with AudioListener seems to stop that error. I'm currently getting a bunch of 0's though so not sure.
I modified the supplied example code and got the following to produce some squiggly lines. http://docs.unity3d.com/ScriptReference/AudioSource.GetSpectrumData.html
#pragma strict
var Audio: AudioSource;
function Start() {
}
function Update() {
var spectrum: float[] = new float[1024];
AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
var i: int = 1;
while ( i < 1023 ) {
Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red);
Debug.DrawLine(new Vector3(i - 1, $$anonymous$$athf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, $$anonymous$$athf.Log(spectrum[i]) + 10, 2), Color.cyan);
Debug.DrawLine(new Vector3($$anonymous$$athf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3($$anonymous$$athf.Log(i), spectrum[i] - 10, 1), Color.green);
Debug.DrawLine(new Vector3($$anonymous$$athf.Log(i - 1), $$anonymous$$athf.Log(spectrum[i - 1]), 3), new Vector3($$anonymous$$athf.Log(i), $$anonymous$$athf.Log(spectrum[i]), 3), Color.yellow);
i++;
}
}
Four things that may be obvious:
The DrawLine function draws in the scene window not the game window.
I had to zoom out to see the lines
I had to increase the number of samples to 1024 in order to get any values. 32 was too small.
You'll need to keep your audiosource to make the sound and will need an audiolistener for the AudioListener.GetSpectrumData to work.
This is strange as the above project works out of the box in 5.1.4. Was there an undocumented breaking change in 5.2?
Answer by cresquin · Dec 01, 2015 at 12:31 AM
I downgraded to 5.1.3 and it works as expected. I think this bug was introduced in 5.2
Status update: I'm seeing the same errors in 5.3.4f1.
It would be nice if this bug could either be fixed, or the error message updated to contain workaround options, or the feature be marked as deprecated (if it's failing triage).
In the meantime, it's time to use GetOutputData and research how to implement an FFT...
Answer by Sami_AlEsh · Sep 14, 2019 at 12:38 PM
how to catch this exception in code ?? i cant catch it !!!
Your answer
Follow this Question
Related Questions
All my spectrums from GetSpectrumData are the same 0 Answers
GetSpectrumData from an audio source when Volume is 0? 0 Answers
I want the audio to play when the player nears the object, but fade away when they walk away. 0 Answers
Audio playing at hightspeed and simoultanesly various times! 1 Answer
Stop AudioSource on Trigger!!! 1 Answer