using UnityEngine;
using System.Collections;
using System.Reflection;
using Holoville.HOTween;

public class AudioController : MonoBehaviour {

	public static float value = 0;
	public bool processingVj;

	string oscHost = "127.0.0.1";
	int outPort = 3200;
	int inPort = 121;
	UDPPacketIO udp;
	Osc handler;

	void Start(){
		if(processingVj){
			udp = gameObject.AddComponent<UDPPacketIO>();
			handler = gameObject.AddComponent<Osc>();

			udp.init(oscHost,outPort,inPort);
			handler.init(udp);
			handler.SetAddressHandler("/vj",vjValue);
		}
	}

	void vjValue(OscMessage msg){
		value =(float) msg.Values[3]/100;
	}

	void OnAudioFilterRead (float[] data, int channels)
	{	
		if(processingVj)return;
		value=0;
		for (var i = 0; (i < data.Length||i<256); i = i + channels) {
			float abs = Mathf.Abs(data[i]);
			if(float.IsInfinity(abs)||float.IsNaN(abs))abs=0;
			value = Mathf.Max (value,abs);
		}
		value = Mathf.Pow(value,2)*20;

	}
	
}