using UnityEngine;
using System.Collections.Generic;

	public class Sinus : MonoBehaviour
{
	// un-optimized version
	public double frequency = 440;
	public double gain = 0.01;
	private double increment;
	private double phase;
	private double sampling_frequency = 48000;
	private List<float> _randoms = new List<float>();
	private int _curRand = 0;
	
	void Awake()
	{
		for(int i = 0; i < 100000; i++)
		{
			_randoms.Add(Random.value);
		}
		for(int i = 2; i < 99998; i++)
		{
			_randoms[i] = ( (_randoms[i] - 2)	+ (_randoms[i] - 1) + (_randoms[i]) + (_randoms[i] + 1) + (_randoms[i] + 2)) / 5;
		}
	}
	
	void OnAudioFilterRead(float[] data, int channels)
	{
		for (var i = 0; i < data.Length; i = i + channels)
		{
			data[i] = ((float)gain) * (-1.0f + (_randoms[_curRand] * 2.0f));
			_curRand += 1;
			if(_curRand >= 100000)
				_curRand = 0;
			
			// if we have stereo, we copy the mono data to each channel
			if (channels == 2) 
				data[i + 1] = data[i];
		}
	}
} 