public void insert(T obj, float value)
 {
     Eppy.Tuple <T, float> n = new Eppy.Tuple <T, float>(obj, value);
     if (queue.Count > 1)
     {
         queue.Add(default(Eppy.Tuple <T, float>));
         int pos = queue.Count - 1;
         while (pos > 1)
         {
             if (n.Item2 < queue[pos / 2].Item2)
             {
                 queue[pos] = queue[pos / 2];
             }
             else
             {
                 break;
             }
         }
         queue[pos] = n;
     }
     else
     {
         queue.Add(n);
     }
 }
    public T get()
    {
        T ret;

        if (queue.Count == 1)
        {
            return(default(T));
        }
        if (queue.Count == 2)
        {
            ret = queue[1].Item1;
            queue.RemoveAt(1);
            return(ret);
        }
        ret = queue[1].Item1;
        Eppy.Tuple <T, float> last_element = queue[queue.Count - 1];
        queue.RemoveAt(queue.Count - 1);
        queue[1] = default(Eppy.Tuple <T, float>);
        int pos = 1;

        if (queue.Count == 2)
        {
            queue[1] = last_element;
        }
        else if (queue.Count == 3)
        {
            if (last_element.Item2 > queue[2].Item2)
            {
                queue[1] = queue[2];
                queue[2] = last_element;
            }
            else
            {
                queue[1] = last_element;
            }
        }
        else
        {
            while (pos * 2 < queue.Count - 1)
            {
                if (queue[pos * 2].Item2 < queue[pos * 2 + 1].Item2)
                {
                    queue[pos] = queue[pos * 2];
                    pos        = pos * 2;
                }
                else
                {
                    queue[pos] = queue[pos * 2 + 1];
                    pos        = pos * 2 + 1;
                }
            }
            queue[pos] = last_element;
        }
        return(ret);
    }
示例#3
0
    }         //Receive data

    private double AddAvgVal(string str, double ts, List <Eppy.Tuple <double, double> > list)
    {
        list.RemoveAt(0);
        list.Add(Tuple.Create(ts, double.Parse(str, NumberStyles.Float, CultureInfo.InvariantCulture)));
        // beregn average

        double avg = 0;

        for (int i = 0; i < list.Count - 1; i++)
        {
            avg += (list[i].Item2 - list[i + 1].Item2) / (list[i].Item1 - list[i + 1].Item1);
        }
        // overfør average
        return(avg / list.Count);
    }
示例#4
0
    public void ReceiveData()
    {
        byte[] data = new byte[0];

        while (true)
        {
            data = client.Receive(ref ep);
            string json = Encoding.ASCII.GetString(data);

            if (json != "")
            {
                ts = double.Parse(json.Substring(6, 10));

                if (json.Contains("gd"))
                {
                    if (json.Contains("left"))
                    {
                        string   gdl    = json.Substring(json.IndexOf('[') + 1, 19);
                        string[] values = gdl.Split(',');

                        //gdlX = double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        //gdlY = double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                        //gdlZ = double.Parse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture);


                        //x
                        if (gdlXBuff.Count < 10)
                        {
                            gdlXBuff.Add(Tuple.Create(ts, double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture)));
                        }
                        else
                        {
                            gdlX = Math.Abs(AddAvgVal(values[0], ts, gdlXBuff));
                        }

                        //y
                        if (gdlYBuff.Count < 10)
                        {
                            gdlYBuff.Add(Tuple.Create(ts, double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture)));
                        }
                        else
                        {
                            gdlY = Math.Abs(AddAvgVal(values[1], ts, gdlYBuff));
                        }

                        //z
                        if (gdlZBuff.Count < 10)
                        {
                            gdlZBuff.Add(Tuple.Create(ts, double.Parse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture)));
                        }
                        else
                        {
                            gdlZ = Math.Abs(AddAvgVal(values[2], ts, gdlZBuff));
                        }
                    }
                    else
                    {
                        //right
                        string   gdl    = json.Substring(json.IndexOf('[') + 1, 19);
                        string[] values = gdl.Split(',');

                        // gdrX = double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        // gdrY = double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                        // gdrZ = double.Parse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture);


                        //x
                        if (gdrXBuff.Count < 10)
                        {
                            gdrXBuff.Add(Tuple.Create(ts, double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture)));
                        }
                        else
                        {
                            gdrX = Math.Abs(AddAvgVal(values[0], ts, gdrXBuff));
                        }

                        //y
                        if (gdrYBuff.Count < 10)
                        {
                            gdrYBuff.Add(Tuple.Create(ts, double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture)));
                        }
                        else
                        {
                            gdrY = Math.Abs(AddAvgVal(values[1], ts, gdrYBuff));
                        }

                        //z
                        if (gdrZBuff.Count < 10)
                        {
                            gdrZBuff.Add(Tuple.Create(ts, double.Parse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture)));
                        }
                        else
                        {
                            gdrZ = Math.Abs(AddAvgVal(values[2], ts, gdrZBuff));
                        }
                    }
                } //gd found

                if (json.Contains("pd"))
                {
                    string pdString = json.Substring(json.IndexOf('p') + 4, 4);

                    if (json.Contains("left"))
                    {
                        /*
                         * if (pdlBuff.Count < 10)
                         * {
                         *   pdlBuff.Add(Tuple.Create(ts, double.Parse(pdString, NumberStyles.Float, CultureInfo.InvariantCulture)));
                         * }
                         * else if (pdlBuff.Count == 10)
                         * {
                         *   pdlBuff.RemoveAt(0);
                         *   pdlBuff.Add(Tuple.Create(ts, double.Parse(pdString, NumberStyles.Float, CultureInfo.InvariantCulture)));
                         *
                         *   // beregn average
                         *   double avg = 0;
                         *   for (int i = 0; i < pdlBuff.Count - 1; i++)
                         *   {
                         *       avg += (pdlBuff[i].Item2 - pdlBuff[i + 1].Item2) / (pdlBuff[i].Item1 - pdlBuff[i + 1].Item1);
                         *   }
                         *   // overfør average
                         *   pdl = avg / pdlBuff.Count;
                         * }
                         */
                        pdl = double.Parse(pdString, NumberStyles.Float, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        pdr = double.Parse(pdString, NumberStyles.Float, CultureInfo.InvariantCulture);
                        //pdr

                        /*
                         * if(pdrBuff.Count < 10)
                         * {
                         *  pdrBuff.Add(Tuple.Create(ts,double.Parse(pdString, NumberStyles.Float, CultureInfo.InvariantCulture)));
                         *
                         * }else if(pdrBuff.Count == 10){
                         *  pdrBuff.RemoveAt(0);
                         *  pdrBuff.Add(Tuple.Create(ts,double.Parse(pdString, NumberStyles.Float, CultureInfo.InvariantCulture)));
                         *  // beregn average
                         * double avg = 0;
                         * for (int i = 0; i < pdrBuff.Count-1; i++)
                         * {
                         *   avg +=  (pdrBuff[i].Item2-pdrBuff[i+1].Item2)/(pdrBuff[i].Item1-pdrBuff[i+1].Item1);
                         * }
                         *  // overfør average
                         *  pdr = avg/pdlBuff.Count;
                         *
                         * }
                         */
                    }
                } //pd

                if (json.Contains("gy"))
                {
                    string gyString = json.Substring(json.IndexOf('[') + 1);
                    gyString = gyString.Substring(0, gyString.Length - 3);

                    string[] values = gyString.Split(',');

                    gyX = float.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                    gyY = float.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                    gyZ = float.Parse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture);
                }
            } //end json
        }     //while true
    }         //Receive data
示例#5
0
    // Update is called once per frame
    void Update()
    {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        mousePos.z = 10;
        //print (flag);
        if (Input.GetMouseButtonDown(0))
        {
            this.transform.position = mousePos;

            //mousePos.z = 5;
            if (note == null)
            {
                note = new Note();
                note.AddPoint(mousePos);
                noteStartPos = mousePos;
                trail.time   = 1;

                //nodeArray.Add (mousePos);
                lastActivePoint = mousePos;
            }
            //aud.clip = AssignNote (0);
            //particleNote.GetComponent<AudioSource> ().clip = AssignNote (0);
            //aud.Play ();
            flag = true;

            //particleNote = GameObject.Instantiate (particleSystemNote, mousePos, Quaternion.identity);
            //particleNote.GetComponent<FollowNotePath> ().AddPathNode (note.LastNode());
            Eppy.Tuple <Vector3, string> tuple = new Eppy.Tuple <Vector3, string> (mousePos, "longnote");
            notesInStroke.Add(noteSceneCount, tuple);
            noteSceneCount++;

            aud.clip = notes [Mathf.FloorToInt(mousePos.y)];
            aud.Play();
            //print ("workign falsE");


            //instantiate node here
        }
        else if (Input.GetMouseButtonUp(0))
        {
            flag = false;
            //print ("tworkingrue");
            //mousePos.z = 5;
            if (note.LastNode() != mousePos)
            {
                note.AddPoint(mousePos);
                //nodeArray.Add (mousePos);
                trail.time = 0;
            }
            else
            {
                note.AddPoint(new Vector3(mousePos.x + 0.3f, mousePos.y, mousePos.z));
            }


            //to pass note if pitch and y val does not change

            /*if ((int)noteStartPos.y == (int)mousePos.y) {
             *
             *      /*if (noteEndPos.x < noteStartPos.x) {
             *
             *              Vector3 temp = noteStartPos;
             *              noteStartPos = noteEndPos;
             *              noteEndPos = temp;
             *      }
             *
             *      //float timeStart = Mathf.Abs (noteEndPos.x - SettingsManager.instance.CursorStartPos.x) / SettingsManager.instance.cursorSpeed;
             *
             *
             *
             *
             * }*/


            /*string noteString = note.GetNoteString(noteStartPos, mousePos); //this should be switched so that parameter is passed according to which instrument selected
             * Eppy.Tuple<Vector3, string> tuple = new Eppy.Tuple<Vector3, string>(noteStartPos, noteString);
             * notesInStroke.Add (noteSceneCount, tuple);
             * noteSceneCount++;*/
            Eppy.Tuple <Vector3, string> tuple = new Eppy.Tuple <Vector3, string> (note.LastNode(), "longnote");
            notesInStroke.Add(noteSceneCount, tuple);
            noteSceneCount++;

            //particleNote.GetComponent<FollowNotePath> ().AddPathNode (note.LastNode());
            List <Vector3> nodeList = note.getNodes();
            particleNote = GameObject.Instantiate(particleSystemNote, nodeList[0], Quaternion.identity);
            particleNote.GetComponent <FollowNotePath> ().SetNodes(nodeList);


            //*************  add note here to assign notes to instantiated note
            particleNote.GetComponent <PlayMusic>().SaveNoteReferences(notesInStroke);

            notesInStroke.Clear();

            //GameObject lineRenderer = GameObject.Instantiate(LineRendererSystemNote, nodeList[0], Quaternion.identity);
            //lineRenderer.GetComponent<LineRenderer> ().positionCount = nodeList.Count;
            //lineRenderer.GetComponent<LineRenderer> ().SetPositions (nodeList.ToArray ());
            note = null;
        }

        if (flag)
        {
            this.transform.position = mousePos;

            //mousePos.z = 5;
            if (note == null)
            {
                //print ("null");
            }
            Vector3 lastNodePos  = note.LastNode();
            Vector3 oldDirection = lastActivePoint - lastNodePos;
            Vector3 newDirection = mousePos - lastActivePoint;

            if (newDirection == Vector3.zero)
            {
                return;
            }

            if ((int)lastNodePos.y != (int)mousePos.y)
            {
                noteEndPos = mousePos;
                //get string val and pass startPosition + string
                string noteString = note.GetNoteString(noteStartPos, noteEndPos);

                //vars I have, startPos, endPos,

                /*if (noteEndPos.x < noteStartPos.x) {
                 *
                 *      Vector3 temp = noteStartPos;
                 *      noteStartPos = noteEndPos;
                 *      noteEndPos = temp;
                 * }
                 *
                 * float timeStart = Mathf.Abs (noteEndPos.x - SettingsManager.instance.CursorStartPos.x) / SettingsManager.instance.cursorSpeed;*/

                //d/s

                Eppy.Tuple <Vector3, string> tuple = new Eppy.Tuple <Vector3, string>(noteStartPos, noteString);

                notesInStroke.Add(noteSceneCount, tuple);
                noteSceneCount++;
                noteStartPos = mousePos;
                noteEndPos   = Vector3.zero;
            }

            if (!newDirection.normalized.Equals(oldDirection.normalized))
            {
                if (Mathf.Abs(Vector3.Angle(oldDirection, newDirection)) > 5f)
                {
                    note.AddPoint(lastActivePoint);
                    trail.time++;

                    //nodeArray.Add (lastActivePoint);
                    //print ("last node pos" + lastNodePos);
                    //print ("new dir" + newDirection);
                    //print(Vector3.Angle(lastNodePos, newDirection));
                    //particleNote.GetComponent<FollowNotePath> ().AddPathNode (note.LastNode());
                }
            }
        }

        lastActivePoint = mousePos;



        /*if (Input.GetMouseButtonDown (1)) {
         *
         *      aud.clip = AssignNote (1);
         *      particleNote.GetComponent<AudioSource> ().clip = AssignNote (1);
         *      aud.Play ();
         * }*/
        /*if (flag)
         * {
         *      if (particleNote)
         *      {
         *              Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         *              mousePos.z = 5;
         *              var shape = particleNote.GetComponent<ParticleSystem> ().shape;
         *              Vector3 length = mousePos - startPositionNote;
         *              shape.scale = new Vector3(length.x, 1, 1);
         *              float xPosShift = (endPositionNote.x - startPositionNote.x) / 2;
         *              Vector3 newPos = new Vector3(startPositionNote.x + xPosShift, mousePos.y, mousePos.z);
         *              particleNote.transform.position = newPos;
         *
         *      }
         * }
         *
         * if (Input.GetMouseButtonUp (0))
         * {
         *      flag = false;
         * }*/
    }