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

public class volclocalize : MonoBehaviour
{
    int objno = 2;
    rtcmixmain RTcmix;
    private bool did_start = false;

    getMyTransform myrelativetransform; // to get the position of this object relative to the Main Camera

    private void Awake()
    {
        // find the RTcmixmain object with the RTcmix function definitions
        RTcmix = GameObject.Find("RTcmixmain").GetComponent<rtcmixmain>();
        myrelativetransform = Camera.main.GetComponent<getMyTransform>();
    }


    // Use this for initialization
    void Start()
    {
        // initialize RTcmix
        RTcmix.initRTcmix(objno);

        string score = "rtinput(\"AUDIO\")  " +
            "srcX = makeconnection(\"inlet\", 1, 10) " +
            "srcY = makeconnection(\"inlet\", 2, 10) " +
            "srcZ = makeconnection(\"inlet\", 3, 10) " +
            "LOCALIZE(0, 0, 9999, 500, srcX, srcY, srcZ, 0,0,0, 1.0, 3.0, 0, 1, 2, 0.0001, 100) ";

        RTcmix.SendScore(score, objno);

        did_start = true;
    }


    // Update is called once per frame
    void Update()
    {
        Vector3 srcpos = myrelativetransform.getTransform(gameObject); // this source relative to the 'observer' object

        // note swap of y and z axes
        RTcmix.setpfieldRTcmix(1, srcpos.x, objno);
        RTcmix.setpfieldRTcmix(2, srcpos.z, objno);
        RTcmix.setpfieldRTcmix(3, srcpos.y, objno);
    }

    void OnAudioFilterRead(float[] data, int channels)
    {
        if (!did_start) return;

        // compute sound samples
        RTcmix.runRTcmix(data, objno, 1);
    }


    void OnApplicationQuit()
    {
        did_start = false;
        RTcmix = null;
    }
}
