Unity 3 Audio Spectrum Analysis

Well, I’ve been trying to get this to work in Unity, but I think I got something that somewhat works. If you want to try it out, download it here! :smile:

MAC: http://dl.dropbox.com/u/7155207/AudioReact.zip
WINDOWS: http://dl.dropbox.com/u/7155207/AudioReactWIN.zip

Looks like a mess!

SYNC TO VBL

:!:

Why is it changing from red to green?!

That music sounds completely broken! Does a person really listen to this? :shock:

:sweat_smile: … sorry about that. I can update it so it will Sync TO VBL, want me to change the music? I only picked something that you could actually see the changes being made.

Who cares what anybody else wants! Let us all complain while you make something you think is good!

1 Like

Update: Ok, so I added Sync to VBL and I changed the music. Hopefully it’s less annoying now. Anyone know a way to average these values so it’s no so erratic? Thanks :smile:

I didn’t think it was that good, I was just showing the spectrum analysis. Since no one else has posted anything about it, I thought someone would think it was interesting… but I guess I thought wrong :confused:

thank you!
finally a first little demo/example of spectrum analysis
it’s my most wanted features needed for U3… and i didn’t know it was already into v3 until today!

i have it… but the docs are really a few words sentence!

anyway (on imac 2.8) i see several bad rendering with U3 betas…
v2.6 is perfect… so i hope there’s still some bug somewhere

pelase keep on with sharing prototypes!
some code would be welcome also!

ciao
stefano

:slight_smile: Awesome, so someone does appreciate it! I’d be glad to share some code, which isn’t the cleanest looking code, but it works!

Placed on Main Camera, with an audio source in the scene playing the music:

var Audio : AudioListener;

var colorStart : Color = Color.red;
var colorEnd : Color = Color.green;
var duration : float = 1.0;

var ball : GameObject;
var ball2 : GameObject;
var pointLight : Light;
var channel : int;
var numSamples : int;
var freq : int;
var number = new Array ();

function Update () {    

var number = Audio.GetSpectrumData(numSamples, channel, FFTWindow.BlackmanHarris);
ball.transform.localScale.y = 5 + number[freq] * 5;
ball.transform.localScale.x = 5 + number[freq] * 5;
ball.transform.localScale.z = 5 + number[freq] * 5;

Camera.main.fieldOfView = 60 + number[0] * 15;
pointLight.light.intensity = 0.7 + number[freq] * 1.2;

var lerp : float = Mathf.PingPong (number[freq], duration) / duration;
ball2.renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
}

hey, that’s really cool. Does Unity3 also allow you to write to the audio buffer like FMod does?

Very nice!

Hey has anyone used Prime31’s AudioRecorder plugin to get audio fed to Audio.GetSpectrumData?

I’m trying to look into it, but it looks like it’s not trivial.

David

Why would you need a plug in to get that data?

I can’t download your project at work.

What are some good settings for:

var channel : int;
var numSamples : int;
var freq : int;

It will be dependent on material. Run your audio through a spectral analyzer of some sort to get an idea of what you’l need. (Logic and Soundtrack would be my choices.) Channel should be easy enough to find; just pick the one that lights up prettiest when you hear what you want to trigger something. Freq will be in the middle of the hump that corresponds to what you’re listening for. numSamples will have to be bigger, the lower freq is.

I don’t know anything about window functions but Wikipedia seems to have a good entry, so I’ll start there when I have some time to read up.

I use either 0 or 1 for channel, try both and test to see which works out better for you. Number of samples must be a power of 2, I have mine set at 256. Frequency you should just change values at runtime and adjust to your liking. Hope that helps?

what I was saying is how to get this kind of thing to get audio spectrum in -real time- from incoming audio (mic), not as it is now, from playing buffers of audio…

i’m about to look into the same topic… any news?

s

Here’s a little test Digital Transformation Consulting - Sonicviz

reposted fm http://forum.unity3d.com/threads/61884-Audiovisual-Ruffmix

Not sure on the real time input side of things…looking…

fm http://forum.unity3d.com/threads/60493-Passing-audio-buffer-to-Unity

So, Audio.GetSpectrumData() - That’s a .net class?

All the unity classes I’ve been able to find are under AudioSouce or AudioListener.

Is there more goodness under Audio.?? :)?

Another thing. Loading the new .mod files off of WWW . It seems there is no built in way to do it. (There is one for Ogg). Though there is a way to load bytes or text. But that isn’t straight forward. Maybe through the AssetBundles? I was wondering are the assetBundle functions no longer pro only? The manual doesn’t state that they are pro only, so I’m wondering.

Thanks

Ok. I now see. BuildPipeline.BuildAssetBundle seems to be PRO only, so building the assetbundles would need a pro license but not reading them in?

hmm…I’m wondering how difficult it would be to take a .mod file and load it up from a server.

Thanks for the example script. Here is a script that will allow you to see the audio spectrum as bars. Im new to unity and I havent finished this properly, basically Im trying to make it clever enough to deal with a range of different number of samples without lots of the bars disappearing beyond the camera, but its not right yet and Ive run out of time so I shall post it as it. It isnt too bad if you have the number of samples set to 64 and the camera z position set to around -12.

Create a prefab that is just a basic cube. Attach this script to the camera. Drag your cube prefab to Abar. Put your audio Source near to the camera. Set numSamples. Run.

var Audio : AudioListener;
var numSamples : int;
var numberleft = new Array ();
var numberright = new Array ();
var thebarsleft = new Array(GameObject);
var thebarsright = new Array(GameObject);
var abar : GameObject;

function Start () {
	var spacing = 0.4 - (numSamples * 0.001f);
	var width = 0.3 - (numSamples * 0.001f);
	for(i=0; i < numSamples; i++){ 
		var xpos = i*spacing -8.0f;
		var positionleft = Vector3(xpos,3, 0);
		thebarsleft[i] = Instantiate(abar, positionleft, Quaternion.identity);	
		thebarsleft[i].transform.localScale.x = width;
		thebarsleft[i].transform.localScale.z = 0.2;
		var positionright = Vector3(xpos,-3, 0);
		thebarsright[i] = Instantiate(abar, positionright, Quaternion.identity);	
		thebarsright[i].transform.localScale.x = width;
		thebarsright[i].transform.localScale.z = 0.2;
		
	}
}

function Update () {    

var numberleft = Audio.GetSpectrumData(numSamples, 0, FFTWindow.BlackmanHarris);
var numberright = Audio.GetSpectrumData(numSamples, 1, FFTWindow.BlackmanHarris);
for(i=0; i < numSamples; i++){
		thebarsleft[i].transform.localScale.y = numberleft[i]*30;
		thebarsright[i].transform.localScale.y = numberright[i]*30;	
	}
}

What you will likely notice is that the spectrum data is much weaker once you get beyond the first few samples. You can compensate for this by using a larger multiplier as you go through the array of samples, so for example you could change the last few lines of code to something like the following (not a very sophisticated example, Im only just starting this voyage):

thebarsleft[i].transform.localScale.y = numberleft[i]*(20*(i+1));
thebarsright[i].transform.localScale.y = numberright[i]*(20*(i+1));

Also what you really need to do to make this stuff visually appealing is to smooth out the values quite a bit over time so that the bars arent flickering so wildly every frame.