[Audio] GetOutputData/GetSpectrumData?

There’s not much in the way of examples or usage in the scripting reference as of yet, so I was curious what sort of data can potentially be returned. I actually only realised you could get any data at all from the recent blog post on new features that the Unity devs liked the most.

I did up a really simple thing with an AudioSource playing an MP3, scaling a cube in the Y axis, just for kicks, and to try and wrap my head around the data that gets returned.

function Update() {
	var tempAudio = audio.GetOutputData(64, 0);
	transform.localScale.y = tempAudio[0] * 5;
}

I’m not quite sure how, for example, I’d get separate data values for various parts of the frequency spectrum (say, bass, mids, and treble).

Anyone able to shed some light on this?

ps. On a somewhat related note, for some reason I’m not able to access AudioListener in script. I tried attaching the script to the main camera, which has an Audio Listener component, but calling AudioListener.GetSpectrumData netted me an error (needs type AudioListener). Same issue with AudioSource, and the only way I was able to access the audio source was as above (audio.GetSpectrumData).

Nothing?

Near as I can figure, GetOutputData just gets the overall volume of the source/listener (number of samples appears to allow you to get smoother changes in value, I think?).

Still can’t figure out how to get proper data from GetSpectrumData. It seems to return an array, but I’m not seeing any value changes in anything other than the first element. Is the array in fact the spectrum spread (various frequencies), or is it something else entirely?

I would like to revive this thread because I have not been able to find anything helpful about GetSpectrumData. There just isn’t much documentation on it at all. I have found some example code here and there, but when I try to use them I get all kinds of errors. The documentation in the Unity Scripting Reference about this is less than almost helpful.

Anyone? I guess there are quite a few people that don’t know much about this either.

Here, made this a while back, it uses Vectosity. But if you don’t have it or don’t want to buy it, this should still give you a good start.

var object : Transform; //Just to keep the wave attached to the object when using Vectrosity.
var Audio : AudioListener;

var Samples : float[];
var Channel : int;
var Window : FFTWindow;

var xSize : float;
var ySize : float;

var material : Material;
var lineWidth : float;
var layer = 26;

private var line : VectorLine;

function Start()
{
	Vector.SetCamera3D(camera); // Set cam for lines to draw correctly.
}

function Update()
{
	Audio.GetSpectrumData (Samples, Channel, Window);
	
	
	if(Samples.length>2)
	{
		var DrawSamples = new Vector3[Samples.length];
	
		var i = 0.0;
		while(i<Samples.length)
		{
			DrawSamples[i] = Vector3(i*xSize, Samples[i]*ySize, 0);
			i++;
		}
	
		Vector.DestroyLine(line);
	
		line = VectorLine("Sound Wave", 
				DrawSamples, 
				material, 
				lineWidth, 
				LineType.Continuous, 
				Joins.Open);
	
		Vector.SetLayer(line, layer);
		Vector.DrawLine3D(line, object);
	}
}

You could also instantiate objects for every DrawSamples and then delete them, or use particles. but that should give you a good idea.
Main thing is you need Audio.GetSpectrumData to get kind of an oscilloscope like data from the sound. As for assigning the audio listener, I just do it in the inspector, just drag it on to Audio variable like you would drag a transform from the hierarchy.

Thanks! This helps tremendously! What object did you attach this script to?

Yep, np.

Pretty sure any old object works, just as long as you give it a reference to the audioListener.

You could always delete the object variable at the top and just use “transform” (if you want it to follow the object you have it attached to, makes it easier to move around/resize/rotate).

I agree though, this isn’t the best documented function in the script reference (or any where).

btw, music gets pretty entertaining with this function, especially if you turn up the pitch! :smile:

Ok. Thanks again. I should be able to get it working now.

Is GetSpectrumData a Pro only feature? Whenever I use FFTWindow I get an error say it is not a valid type. Btw I didn’t just copy and paste your code. Here is what I was using.

var Audio : AudioListener;

var Samples : float[];
var Channel : int;
var Window : FFTWindow;

function Update () {

	Audio.GetSpectrumData (Samples, Channel, Window);
	
	Debug.Log("Samples: " + Samples + " Channel: " + Channel);

}

AudioListener isn’t pro only, have the free version running my code to “johnny be good” right now.

Honestly I have no idea why your getting an error with FFTWindow.

I don’t think Debug.Log("Samples: " + Samples + " Channel: " + Channel); will return any thing useful though, you’ll have to parse the Samples array your self if you want to debug it.

Make sure the inspector is picking a default for the FFTWindow though, should be a drop down menu you can pick from when you click the variable.

I’m not getting any error with my code or yours though. When do you get the error (right after it compiles, or at runtime)? What line does it show on?

The script won’t compile because of the error, which is in line 5, so the variables don’t even show up yet.

Only other thing I can think to ask is what version of unity are you on? FFTWindow requires Unity 3.0 and GetSpectrumData requires Unity 3.2 if you use audio listeners, and 3.0 if you use AudioSources.

http://unity3d.com/support/documentation/ScriptReference/40_history.html#3.3.0

That might be the problem. How do I find what version I am using? Is there somewhere in the editor that shows the version? I check the Unity file that I downloaded but it doesn’t say.

In Unity click on Unity, and “about unity” if your on mac. Windows I’m not sure, just flip through the the stuff on the menu bar, look for about unity.

Ah! I forgot about the splash screen! I am using Unity 2.6. I guess that is the problem. Sigh Time to upgrade I guess.

cool, yep thats the problem.

If your on the free version the 3.4 version is well worth the download time any way! Especially vs 2.6!

I here 3.5 is going to be huge too!