using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class SineGen : MonoBehaviour
{

    public float Period;
    public float Frequency;
    public float AngularFrequency;
    public float Phase;
    public float Amplitude;
    float _elapsedTime;

    public float Multiplier = .1f;

    public string AudioParamName;

    AudioSource _s;
    AudioMixer _mixer;
    void Start()
    {
        _s = GetComponent<AudioSource>();
        _mixer = _s.outputAudioMixerGroup.audioMixer;
    }

    void Update()
    {
        SmoothSineWave();
    }
    void SmoothSineWave()
    {
        // y(t) = A * sin(ωt + θ) [Basic Sine Wave Equation]
        // [A = amplitude | ω = AngularFrequency ((2*PI)f) | f = 1/T | T = [period (s)] | θ = phase | t = elapsedTime]
        // Public/Serialized Variables: amplitude, period, phase
        // Private/Non-serialized Variables: frequency, angularFrequency, elapsedTime
        // Local Variables: omegaProduct, y

        // If the value of period has altered last known frequency...
        if (1 / (Period) != Frequency)
        {
            // Recalculate frequency & omega.
            Frequency = 1 / (Period);
            AngularFrequency = (2 * Mathf.PI) * Frequency;
        }
        // Update elapsed time.
        _elapsedTime += Time.deltaTime;
        // Calculate new omega-time product.
        float omegaProduct = (AngularFrequency * _elapsedTime);
        // Plug in all calculated variables into the complete Sine wave equation.
        float y = (Amplitude * Mathf.Sin(omegaProduct + Phase)) * Multiplier;
        float old = 0;
        _mixer.GetFloat(AudioParamName, out old);
        old += y;
        _mixer.SetFloat(AudioParamName, old);
    }
}
