using UnityEngine;
using System.Collections;

public class SingGenerator : MonoBehaviour {

    float phase;
    public float freq;
    public float gain;

    FreqOriginScript freq_source;

    void Awake() {
        freq_source = GameObject.FindGameObjectWithTag("Freq").GetComponent<FreqOriginScript>();
    }

    void Update() {
        freq = freq_source.Freq();
        gain = freq_source.Amplitude();
        float s = 0.01f;
        if (freq_source.control == FreqOriginScript.MOUSECONTROL) s = 0.3f;
        GetComponent<AudioSource>().pitch = Mathf.MoveTowards(GetComponent<AudioSource>().pitch, freq / 440, s);
        GetComponent<AudioSource>().volume = Mathf.MoveTowards(GetComponent<AudioSource>().volume, gain*2, s);
    }

    /*void OnAudioFilterRead(float[] data, int channels) {
        // update increment in case frequency has changed
        float increment = freq * 2 * Mathf.PI / 44100;
        for (var i = 0; i < data.Length; i = i + channels) {
            phase = phase + increment;
            // this is where we copy audio data to make them “available” to Unity
            data[i] = (float)(gain * Mathf.Sin(phase) / (freq) * 40);
            // if we have stereo, we copy the mono data to each channel
            if (channels == 2) data[i + 1] = data[i];
            if (phase > 2 * Mathf.PI) phase = 0;
        }
    }*/
}
