Skip to content

Instantly share code, notes, and snippets.

@drawcode
Created January 6, 2014 08:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drawcode/8279954 to your computer and use it in GitHub Desktop.
Save drawcode/8279954 to your computer and use it in GitHub Desktop.
AudioSource
using UnityEngine;
using System.Collections;
public class CSBars : MonoBehaviour {
// Public Variables
public AudioListener Audio;
public int numSamples;
public GameObject abar;
// Private Varaibles
float[] numberleft;
float[] numberright;
GameObject[] thebarsleft;
GameObject[] thebarsright;
float spacing;
float width;
// Use this for initialization
void Start () {
thebarsleft = new GameObject[numSamples];
thebarsright = new GameObject[numSamples];
spacing = 0.4f - (numSamples * 0.001f);
width = 0.3f - (numSamples * 0.001f);
for(int i=0; i < numSamples; i++){
float xpos = i*spacing -8.0f;
Vector3 positionleft = new Vector3(xpos,3, 0);
thebarsleft[i] = (GameObject)Instantiate(abar, positionleft, Quaternion.identity) as GameObject;
thebarsleft[i].transform.localScale = new Vector3(width,1,0.2f);
Vector3 positionright = new Vector3(xpos,-3, 0);
thebarsright[i] = (GameObject)Instantiate(abar, positionright, Quaternion.identity) as GameObject;
thebarsright[i].transform.localScale = new Vector3(width,1,0.2f);
}
}
// Update is called once per frame
void Update () {
numberleft = Audio.GetSpectrumData(numSamples, 0, FFTWindow.BlackmanHarris);
numberright = Audio.GetSpectrumData(numSamples, 1, FFTWindow.BlackmanHarris);
for(int i=0; i < numSamples; i++){
if (float.IsInfinity(numberleft[i]*30) || float.IsNaN(numberleft[i]*30)){
}else{
thebarsleft[i].transform.localScale = new Vector3(width, numberleft[i]*30,0.2f);
thebarsright[i].transform.localScale = new Vector3(width, numberright[i]*30,0.2f);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment