devNotes 6-15-16 Sound Plot, Circular buffer, Echo Processing

ryujyruj8

Incorporate Sensor Plots

Reticle on Sphere

using UnityEngine;
using System.Collections;
using ProceduralToolkit;

public class Vertical_Plot : MonoBehaviour
{
    MicrophoneInput micInput;
    Mesh mesh;

    //[SerializeField]
    //Material _material;

    //MaterialPropertyBlock _block;

    public int numPlotLines = 1024;

    [Range(1f, 200f)]
    public float scale_plot;

    Vector3 vRight, vUp,v0,v1,v2,v3;
    float xScale;//scale amp to plot

    public Color graphColor;

    private MeshDraft mDraft;
    public MeshDraft MDraft
    {
        get { return mDraft; }
    }

    void Start()
    {
        MeshFilter mf = GetComponent<MeshFilter>();
        if (mf.mesh == null)
            mf.mesh = new Mesh();
        mesh = mf.mesh;
        mDraft = new MeshDraft();

        //_block = new MaterialPropertyBlock();

        micInput = GetComponent<MicrophoneInput>();

        xScale = 450f;
        vRight = transform.right;
        vUp = transform.up;

        initPlot();

        ToMeshInternal();
    }

    float accumTime = 0f;

    private void ToMeshInternal()
    {
        mesh.Clear();
        mesh.vertices = mDraft.vertices.ToArray();
        mesh.triangles = mDraft.triangles.ToArray();
        mesh.normals = mDraft.normals.ToArray();
        mesh.uv = mDraft.uv.ToArray();
        mesh.colors = mDraft.colors.ToArray();
    }

    //951
    //581
    //1532

    //x extent 475

    //x abs 450;

    private void initPlot()
    {
        float dy = 1532f/numPlotLines;
        Vector3 vDeltY = new Vector3(0, dy, 0);
        Vector3 vDeltYHalf = vDeltY / 2;

        float phiRatio = 391f / 1024f;

        int cycleOnce = (int)(phiRatio * numPlotLines);  
        float sampAmp = 0;

        Vector3 posRideY = Vector3.zero + vDeltY/2f;
        int count = 0;

        while(count < numPlotLines)
        {
            sampAmp = 0.5f + 0.5f * Mathf.Sin(2f*Mathf.PI*(float)count / (float)numPlotLines);//test

            v0 = posRideY - sampAmp * xScale * vRight - vDeltYHalf;
            v1 = posRideY - sampAmp * xScale * vRight + vDeltYHalf;
            v2 = posRideY + sampAmp * xScale * vRight + vDeltYHalf;
            v3 = posRideY + sampAmp * xScale * vRight - vDeltYHalf;

            AddQuad(v0, v1, v2, v3, graphColor);

            if (++count == cycleOnce) posRideY -= vDeltY * numPlotLines;

            posRideY += vDeltY; 
        }
    }

    void setBucketQuad(int idx, float val)
    {
        if (idx < MDraft.vertices.Count - 4)
        {
            int id4 = idx * 4;

            v0 = mDraft.vertices[id4];
            v1 = mDraft.vertices[id4 + 1];
            v2 = mDraft.vertices[id4 + 2];
            v3 = mDraft.vertices[id4 + 3];

            v0.x = v1.x = -val * xScale* scale_plot;
            v2.x = v3.x = val * xScale * scale_plot;

            mDraft.vertices[id4] = v0;
            mDraft.vertices[id4 + 1] = v1;
            mDraft.vertices[id4 + 2] = v2;
            mDraft.vertices[id4 + 3] = v3;
        }
     }

    // 1   2  
    // 0   3
    void Update()
    {
        accumTime += Time.deltaTime;

        int numSampsDisplay = (int)(micInput.echoTime*micInput.globalSampleRate*2f);

        int sampsPerBucket = (int)((float)numSampsDisplay / (float)numPlotLines);

        int n = 0;
        int nScan = micInput.Record_Head;

        float accumSamp = 0f;

        float bucketVal;

        int bIdx = 0;

        while(n < numSampsDisplay-1)
        {
            for (int i = 0; i < sampsPerBucket; i++)
            {
                accumSamp += Mathf.Abs(micInput.EchoBuffer[nScan]);
                n++;
                if (--nScan < 0) nScan = micInput.EchoBuffer.Length - 1;   
            }
            bucketVal = accumSamp / (float)sampsPerBucket;
            accumSamp = 0f;
            setBucketQuad(bIdx, bucketVal);

            if (++bIdx > numPlotLines - 1) break;

        }
        ToMeshInternal();

        //ToMeshInternal();
        //Vector3 vH0 = a12horns.v12HORN_Norms[0];
        //Quaternion q;

        //for (int i = 0; i < 12; i++)
        //{
        //    //q = Quaternion.FromToRotation(vH0, a12horns.v12HORN_Norms[i]);

        //    q = Quaternion.LookRotation(a12horns.v12HORN_Norms[i]);
        //    //print(q.ToString());
        //    Graphics.DrawMesh(GetComponent<MeshFilter>().mesh, Vector3.zero, q, _material, gameObject.layer, null, 0, _block);

        //}
    }

    public int GetNumVertices()
    {
        return mDraft.vertices.Count;
    }

    public Vector3 GetVertex(int i)
    {
        return mDraft.vertices[i];
    }

    public void SetVertex(int i, Vector3 v)
    {
        mDraft.vertices[i] = v;
    }

    public void MeshDraft_ToMesh()
    {
        if (mDraft != null)
        {
            GetComponent<MeshFilter>().mesh = mDraft.ToMesh();
        }
    }

    public void initAddMeshDraft(MeshDraft md)
    {
        MeshFilter mf = GetComponent<MeshFilter>();
        if (mf.mesh == null)
            mf.mesh = new Mesh();
        mesh = mf.mesh;
        mDraft = new ArgosMeshDraft();

        if (mDraft != null)
        {
            mDraft.Add(md);
            GetComponent<MeshFilter>().mesh = mDraft.ToMesh();
        }
    }

    public void AddMeshDraft(MeshDraft md)
    {
        if (mDraft != null)
        {
            mDraft.Add(md);
            GetComponent<MeshFilter>().mesh = mDraft.ToMesh();
        }
    }

    public void AddMeshDraft_Only(MeshDraft md)
    {
        if (mDraft != null)
        {
            mDraft.Add(md);
        }
    }

    public void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3)
    {
        if (mDraft != null)
        {
            mDraft.Add(MeshDraft.Quad(v0, v1, v2, v3));
        }
    }

    public void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, Color col)
    {
        if (mDraft != null)
        {
            mDraft.Add(MeshDraft.Quad(v0, v1, v2, v3, col));
        }
    }
}

 

 

 

gfmhfgmhfmg

 

 

Graph_Plate_pfpjpf

 

 

ghdfsghfhg1