Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

using UnityEngine;

using System.Collections;
using System.IO;
public class audioTracks : MonoBehaviour
{
public
public
public
public
public
public
float[]
float[]
float[]
float[]

AudioClip[] audioClips;
int SongNumber;
float offset;
int octaves;
float amplitude;
string seed;
frequency = new float[8192];
amplitudeArray = new float[8192];
SumFreq ;
SumAmpl ;

float[] sumA;
float[] sumF;
int channelS = 0;
float time = 0f;
FFTWindow window = new FFTWindow ();
// Use this for initialization
void Start ()
{
SumAmpl = new float[octaves];
SumFreq = new float[octaves];
sumA = new float[octaves];
sumF = new float[octaves];
if (this.gameObject.GetComponent<AudioSource> ()
== null)
this.gameObject.AddComponent<Aud
ioSource> ();
if (this.gameObject.GetComponent<AudioListener>
() == null)
this.gameObject.AddComponent<Aud
ioListener> ();
this.gameObject.GetComponent<AudioSource> ().cli
p = audioClips [SongNumber];
this.gameObject.GetComponent<AudioSource> ().Pla
y ();
GameObject[] go = GameObject.FindGameObjectsWith
Tag ("cube");
if (go.Length != octaves) {
for (int i= go.Length + 1; i<= o
ctaves; i++) {
GameObject aux =
GameObject.CreatePrimitive (PrimitiveType.Cube);
aux.tag = "cube"
;
aux.name = "c" +
i;

aux.transform.po
sition = new Vector3 ((float)i + i * 0.5f - 1.5f * (octaves / 2f), 0f, 0f);
}
}
}
// Update is called once per frame
void Update ()
{
GameObject[] go = GameObject.FindGameObjectsWith
Tag ("cube");
int k = 0;
int mid = 22;
audio.GetSpectrumData (amplitudeArray, channelS,
FFTWindow.Rectangular);
audio.GetSpectrumData (frequency, channelS, wind
ow);
for (int i = 0; i< frequency.Length; i++) {
if (i <= mid) {
SumFreq [k] += M
athf.Abs (frequency [i]);
SumAmpl [k] += M
athf.Abs (amplitudeArray [i]);
} else {
mid = mid * 2;
if (k < octaves
- 1)
k++;
SumFreq [k] += M
athf.Abs (frequency [i]);
SumAmpl [k] += M
athf.Abs (amplitudeArray [i]);
}
}
for (int i=0; i<go.Length; i++) {
go [i].transform.localScale = ne
w Vector3 (1f, amplitude * Mathf.Sqrt (SumAmpl [i] + SumFreq [i]) + 0.5f, 1f);
sumA [i] += SumAmpl [i];
sumF [i] += SumFreq [i];
}
ClearData (SumAmpl);
ClearData (SumFreq);
time += Time.deltaTime;
if (SongNumber < audioClips.Length - 1 && time >
this.gameObject.GetComponent<AudioSource> ().clip.length + offset) {
SaveInfo ();
time = 0f;
ClearData (sumA);
ClearData (sumF);
SongNumber++;
this.gameObject.GetComponent<Aud
ioSource> ().clip = audioClips [SongNumber];
this.gameObject.GetComponent<Aud
ioSource> ().Play ();
}
}
void ClearData (float[] x)

{
for (int i=0; i<x.Length; i++)
x [i] = 0f;
}
void SaveInfo ()
{
StreamWriter SW = new StreamWriter (@"F:\unityGa
mes\messaround\data\text_" + seed + ".txt", true);
SW.WriteLine (this.gameObject.GetComponent<Audio
Source> ().clip.name.ToString ());
SW.WriteLine ("");
SW.Write ("A: ");
foreach (float i in sumA)
SW.Write (i.ToString () + " - ")
;
SW.WriteLine ("");
SW.WriteLine ("");
SW.Write ("F: ");
foreach (float i in sumF)
SW.Write (i.ToString () + " - ")
;
SW.WriteLine ("");
SW.WriteLine ("");
SW.Write (this.gameObject.GetComponent<AudioSour
ce> ().clip.length.ToString ());
SW.WriteLine ("");
SW.WriteLine ("");
SW.Close ();
}
void OnGUI ()
{
if (GUI.Button (new Rect (50f, 50f, 100f, 50f),
"Reset")) {
SongNumber = 0;
time = 0f;
this.gameObject.GetComponent<Aud
ioSource> ().clip = audioClips [SongNumber];
this.gameObject.GetComponent<Aud
ioSource> ().Play ();
}
if (GUI.Button (new Rect (50f, 100f, 100f, 50f),
"Next")) {
if (SongNumber < audioClips.Leng
th - 1) {
SongNumber++;
time = 0f;
this.gameObject.
GetComponent<AudioSource> ().clip = audioClips [SongNumber];
this.gameObject.
GetComponent<AudioSource> ().Play ();
}
}
if (GUI.Button (new Rect (50f, 150f, 100f, 50f),
"previous")) {
if (SongNumber > 0) {
SongNumber--;
time = 0f;
this.gameObject.

GetComponent<AudioSource> ().clip = audioClips [SongNumber];


this.gameObject.
GetComponent<AudioSource> ().Play ();
}
}
}
}

You might also like