Ejemplo n.º 1
0
        /// <summary>
        /// Creates an OSC Client (sends OSC messages) given an outgoing port and address.
        /// </summary>
        /// <param name="clientId">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="destination">
        /// A <see cref="IPAddress"/>
        /// </param>
        /// <param name="port">
        /// A <see cref="System.Int32"/>
        /// </param>
        public void CreateClient(string clientId, IPAddress destination, int port)
        {
            if (_clients.ContainsKey(clientId))
            {
                return;
            }
            ClientLog clientitem = new ClientLog();

            clientitem.client   = new OSCClient(destination, port);
            clientitem.log      = new List <string>();
            clientitem.messages = new List <OSCMessage>();

            _clients.Add(clientId, clientitem);

            // Send test message
            string     testaddress = "/test/alive/";
            OSCMessage message     = new OSCMessage(testaddress, destination.ToString());

            message.Append(port); message.Append("OK");

            _clients[clientId].log.Add(String.Concat(DateTime.UtcNow.ToString(), ".",
                                                     FormatMilliseconds(DateTime.Now.Millisecond), " : ",
                                                     testaddress, " ", DataToString(message.Data)));
            _clients[clientId].messages.Add(message);

            _clients[clientId].client.Send(message);
        }
Ejemplo n.º 2
0
    //OSC part

    /*
     *  0: pid (int)                        // Personal ID ex : 42th person to enter stage has pid=42
     *  1: oid (int)                        // Ordered ID ex : if 3 person on stage, 43th person might have oid=2
     *  2: age (int)                        // Time on stage (in frame number)
     *  3: centroid.x (float 0:1)           // Position projected to the ground
     *  4: centroid.y (float 0:1)
     *  5: velocity.x (float -1:1)           // Speed and direction vector
     *  6: velocity.y (float -1:1)
     *  7: depth (float 0:1)                // Distance to sensor (in m) (not implemented)
     *  8: boundingRect.x (float 0:1)       // Top view bounding box
     *  9: boundingRect.y (float 0:1)
     *  10: boundingRect.width (float 0:1)
     *  11: boundingRect.height (float 0:1)
     *  12: highest.x (float 0:1)           // Highest point placement
     *  13: highest.y (float 0:1)
     *  14: highest.z (float 0:1)           // Height of the person
     *
     *  /au/scene   args0 arg1 ... argn
     *
     *  0: currentTime (int)                // Time (in frame number)
     *  1: percentCovered (float 0:1)       // Percent covered
     *  2: numPeople (int)                  // Number of person
     *  3: averageMotion.x (float 0:1)          // Average motion
     *  4: averageMotion.y (float 0:1)
     *  5: scene.width (int)                // Scene size
     *  6: scene.height (int)
     *  7: scene.depth (int)
     */

    public void SendSceneUpdated()
    {
        if (Mute)
        {
            return;
        }

        var msg = new UnityOSC.OSCMessage("/au/scene");

        msg.Append(_frameCounter);
        //Compute point size
        msg.Append(InstanceNumber * PointSize.x * PointSize.y);
        msg.Append(NbPoints);
        //Compute average motion
        var velocitySum = Vector3.zero;

        foreach (var element in InstanciatedPoints)
        {
            velocitySum += -element.Value.GetComponent <PointBehaviour>().NormalizedVelocity;
        }
        velocitySum /= InstanciatedPoints.Count;

        msg.Append(velocitySum.x);
        msg.Append(velocitySum.y);
        msg.Append(Width);
        msg.Append(Height);
        msg.Append(100);

        OSCMaster.Clients["AugmentaSimulatorOutput"].Send(msg);
    }
    void SendMessage()
    {
        UnityOSC.OSCMessage msg = new UnityOSC.OSCMessage(messageAddress);
        msg.Append(Time.time);

        client.Send(msg);
        client.Flush();
        Debug.Log("sent");
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Unpacks an OSC message.
        /// </summary>
        /// <param name="data">
        /// A <see cref="System.Byte[]"/>
        /// </param>
        /// <param name="start">
        /// A <see cref="System.Int32"/>
        /// </param>
        /// <returns>
        /// A <see cref="OSCMessage"/>
        /// </returns>
        ///
        public static OSCMessage Unpack(byte[] data, ref int start)
        {
            string     address = OSCPacket.UnpackString(data, ref start);
            OSCMessage message = new OSCMessage(address);

            char[] tags = OSCPacket.UnpackString(data, ref start).ToCharArray();
            if (start == 8)
            {
                message._typeTag = ",";
            }
            foreach (char tag in tags)
            {
                object value;
                switch (tag)
                {
                case FLOAT:
                    value = OSCPacket.UnpackFloat(data, ref start);
                    break;

                case DEFAULT:
                    continue;

                case INTEGER:
                    value = OSCPacket.UnpackInt(data, ref start);
                    break;

                case LONG:
                    value = OSCPacket.UnpackLong(data, ref start);
                    break;

                case DOUBLE:
                    value = OSCPacket.UnpackDouble(data, ref start);
                    break;

                case STRING:
                    value = OSCPacket.UnpackString(data, ref start);
                    break;

                case BYTE:
                    value = OSCPacket.UnpackBytes(data, ref start);
                    break;

                default:
                    Console.WriteLine("Unknown tag: " + tag);
                    continue;
                }

                message.Append(value);
            }

            if (message.TimeStamp == 0)
            {
                message.TimeStamp = DateTime.Now.Ticks;
            }

            return(message);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Unpacks an OSC message.
        /// </summary>
        /// <param name="data">
        /// A <see cref="System.Byte[]"/>
        /// </param>
        /// <param name="start">
        /// A <see cref="System.Int32"/>
        /// </param>
        /// <returns>
        /// A <see cref="OSCMessage"/>
        /// </returns>
        public static OSCMessage Unpack(byte[] data, ref int start)
        {
            string     address = OSCPacket.UnpackValue <string>(data, ref start);
            OSCMessage message = new OSCMessage(address);

            char[] tags = OSCPacket.UnpackValue <string>(data, ref start).ToCharArray();

            foreach (char tag in tags)
            {
                object value;

                switch (tag)
                {
                case DEFAULT:
                    continue;

                case INTEGER:
                    value = OSCPacket.UnpackValue <int>(data, ref start);
                    break;

                case LONG:
                    value = OSCPacket.UnpackValue <long>(data, ref start);
                    break;

                case FLOAT:
                    value = OSCPacket.UnpackValue <float>(data, ref start);
                    break;

                case DOUBLE:
                    value = OSCPacket.UnpackValue <double>(data, ref start);
                    break;

                case STRING:
                    value = OSCPacket.UnpackValue <string>(data, ref start);
                    break;

                case BYTE:
                    value = OSCPacket.UnpackValue <byte[]>(data, ref start);
                    break;

                default:
                    Console.WriteLine("Unknown tag: " + tag);
                    continue;
                }

                message.Append(value);
            }

            if (message.TimeStamp == 0)
            {
                message.TimeStamp = DateTime.Now.Ticks;
            }

            return(message);
        }
Ejemplo n.º 6
0
        IEnumerator MessageLoop()
        {
            while (true)
            {
                yield return(new WaitForSecondsRealtime(messageWait));

                OSCHandler.Instance.UpdateLogs();
                Dictionary <string, ServerLog> servers = OSCHandler.Instance.Servers;

                foreach (KeyValuePair <string, ServerLog> item in servers)
                {
                    if (item.Value.log.Count > 0)                                               // If we have received at least one packet
                    {
                        int    lastPacketIndex = item.Value.packets.Count - 1;
                        string address         = item.Value.packets [lastPacketIndex].Address;

                        try{
                            OscCallback f = callbacks [address];

                            if (f != null)
                            {
                                UnityEngine.Debug.Log("OSC Recieved - Running Function for " + item.Value.packets [lastPacketIndex].Address);
                                OSCMessage mess = new OSCMessage(address);

                                foreach (object d in item.Value.packets[lastPacketIndex].Data)
                                {
                                    mess.Append(d);
                                }

                                f(mess);
                            }
                        }
                        catch (KeyNotFoundException e) {
                            UnityEngine.Debug.Log("Error: Key Not Found in Dictionary: " + address + "\n" + e);
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Sends an OSC message to a specified client, given its clientId (defined at the OSC client construction),
        /// OSC address and a list of values. Also updates the client log.
        /// </summary>
        /// <param name="clientId">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="address">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="values">
        /// A <see cref="List<T>"/>
        /// </param>
        public void SendMessageToClient <T>(string clientId, string address, List <T> values)
        {
            if (_clients.ContainsKey(clientId))
            {
                OSCMessage message = new OSCMessage(address);

                foreach (T msgvalue in values)
                {
                    message.Append(msgvalue);
                }

                if (_clients[clientId].log.Count < _loglength)
                {
                    _clients[clientId].log.Add(String.Concat(DateTime.UtcNow.ToString(), ".",
                                                             FormatMilliseconds(DateTime.Now.Millisecond),
                                                             " : ", address, " ", DataToString(message.Data)));
                    _clients[clientId].messages.Add(message);
                }
                else
                {
                    _clients[clientId].log.RemoveAt(0);
                    _clients[clientId].messages.RemoveAt(0);

                    _clients[clientId].log.Add(String.Concat(DateTime.UtcNow.ToString(), ".",
                                                             FormatMilliseconds(DateTime.Now.Millisecond),
                                                             " : ", address, " ", DataToString(message.Data)));
                    _clients[clientId].messages.Add(message);
                }

                _clients[clientId].client.Send(message);
            }
            else
            {
                Debug.LogError(string.Format("Can't send OSC messages to {0}. Client doesn't exist.", clientId));
            }
        }
Ejemplo n.º 8
0
    public void SendAugmentaMessage(string address, GameObject obj)
    {
        if (Mute)
        {
            return;
        }

        var msg             = new UnityOSC.OSCMessage(address);
        var behaviour       = obj.GetComponent <PointBehaviour>();
        var worldToViewPort = Camera.main.WorldToViewportPoint(obj.transform.position);

        worldToViewPort = new Vector3(worldToViewPort.x, Mathf.Abs(worldToViewPort.y - 1), worldToViewPort.z); //Switch from bot-left (0;0) to top-left(0;0)

        msg.Append(behaviour.pid);
        //oid

        if (CursorPoint != null)
        {
            msg.Append(behaviour.pid);
        }
        else
        {
            msg.Append(behaviour.pid - 1);
        }

        msg.Append((int)behaviour.Age);
        //centroid
        msg.Append(worldToViewPort.x);
        msg.Append(worldToViewPort.y);
        //Velocity
        var normalizedVelocity = behaviour.NormalizedVelocity;

        msg.Append(-normalizedVelocity.x);
        msg.Append(-normalizedVelocity.y);
        msg.Append((float)behaviour.pid);

        //Bounding
        msg.Append(worldToViewPort.x - PointSize.x / 2);
        msg.Append(worldToViewPort.y - PointSize.y / 2);

        msg.Append(PointSize.x);
        msg.Append(PointSize.y);

        msg.Append(worldToViewPort.x);
        msg.Append(worldToViewPort.y);
        msg.Append(0.5f);

        OSCMaster.Clients["AugmentaSimulatorOutput"].Send(msg);
    }