using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class analyzeSound : MonoBehaviour {

    public int qSamples = 1024;     //array size
    public float refValue = 0.1F;   //RMS value for 0 dB
    public float threshold = 0.02F;  //minimum amplitude to extract pitch
    public float rmsValue;          //sound level - RMS
    public float dbValue;           //sound level - dB
    public float pitchValue;        //sound pitch - Hz

    public Text hzMeter;

    private float[] samples;         //audio samples
    private float[] spectrum;       //audio spectrum
    private float fSample;
    private bool isInitiliazed = false;

	void Start ()
    {
	}

    void AnalyzeSound()
    {
        AudioListener.GetOutputData(samples, 0); // fill array with samples
        float sum = 0;
        for (int i=0; i < qSamples; i++)
        {
            sum += samples[i]*samples[i]; // sum squared samples
        }
        rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
        dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
        if (dbValue < -160)
        {
            dbValue = -160; // clamp it to -160dB min
        }

        // get sound spectrum
        AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
        float maxV = 0;
        int maxN = 0;
        for (int i=0; i < qSamples; i++)
        { // find max 
            if (spectrum[i] > maxV && spectrum[i] > threshold)
            {
                maxV = spectrum[i];
                maxN = i; // maxN is the index of max
            }
        }
        float freqN = maxN; // pass the index to a float variable
        if (maxN > 0 && maxN < qSamples-1)
        { // interpolate index using neighbours
            float dL = spectrum[maxN - 1] / spectrum[maxN];
            float dR = spectrum[maxN + 1] / spectrum[maxN];
            freqN += 0.5F * (dR*dR - dL*dL);
        }

        pitchValue = freqN*(fSample/2)/qSamples; // convert index to frequency
    }
	
	void Update ()
    {
        if (!isInitiliazed)
        {
          //  if (GameManager.Instance.gameStarted)
            {
                samples = new float[qSamples];
                spectrum = new float[qSamples];
                fSample = AudioSettings.outputSampleRate;
                isInitiliazed = true;
            }
            return;
        }
        AnalyzeSound();

        hzMeter.text = "RMS: " + rmsValue.ToString("F2") +
         " (" + dbValue.ToString("F1") + " dB)\n" +
         "Pitch: " + pitchValue.ToString("F0") + " Hz";
    }
}
