예제 #1
0
    void ParseOSC(UnityOSC.OSCPacket packet)
    {
        if (String.Equals(packet.Address, OSCReceiver.throwcmd))
        {
            Debug.Log("throw");
            // TODO: FindObjectOfType is costly with each call
            // store reference and destroy on level load
            FindObjectOfType <DKThrow>().Throw();
        }

        if (String.Equals(packet.Address, OSCReceiver.beatcmd))
        {
            DOTween.ToAlpha(() => bg.color, x => bg.color = x, .6f, .05f).SetEase(Ease.Flash).OnComplete(() => {
                DOTween.ToAlpha(() => bg.color, x => bg.color = x, 0, .05f).SetEase(Ease.Flash);
            });
        }

        if (String.Equals(packet.Address, OSCReceiver.notecmd))
        {
            Debug.Log(packet.Address + " " + OSCReceiver.notecmd + 0);
            receivedPitch = (float)packet.Data[0];           // pitch
            receivedDur   = (float)packet.Data[1];           // duration
            gateNotes[0]  = new Vector2(receivedPitch, receivedDur);
            foreach (var item in cb)
            {
                item.PushNote(0, gateNotes[0]);
            }
        }
        else if (String.Equals(packet.Address, OSCReceiver.notecmd + 1))
        {
            Debug.Log(packet.Address + " " + OSCReceiver.notecmd + 1);
            receivedPitch = (float)packet.Data[0]; // pitch
            receivedDur   = (float)packet.Data[1]; // duration
            gateNotes[1]  = new Vector2(receivedPitch, receivedDur);
            foreach (var item in cb)
            {
                item.PushNote(1, gateNotes[1]);
            }
        }
        else if (String.Equals(packet.Address, OSCReceiver.notecmd + 2))
        {
            receivedPitch = (float)packet.Data[0];           // pitch
            receivedDur   = (float)packet.Data[1];           // duration
            gateNotes[2]  = new Vector2(receivedPitch, receivedDur);
            foreach (var item in cb)
            {
                item.PushNote(2, gateNotes[2]);
            }
        }

        packet.clear();
    }
예제 #2
0
        /// <summary>
        /// Sends an OSC packet to the defined destination and address of the client.
        /// </summary>
        /// <param name="packet">
        /// A <see cref="OSCPacket"/>
        /// </param>
#if NETFX_CORE
        public async void Send(OSCPacket packet)
        {
            byte[] data = packet.BinaryData;
            //Debug.Log("OSCCLIENT-SEND:" + System.Text.Encoding.ASCII.GetString(data));

            using (var writer = new DataWriter(await socket.GetOutputStreamAsync(_hostname, _port.ToString()))){
                try
                {
                    writer.WriteBytes(data);
                    await writer.StoreAsync();
                }
                catch
                {
                    throw new Exception(String.Format("Can't send OSC packet to client {0} : {1}:Length{2}", _ipAddress, _port, data.Length));
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Unpacks an OSC bundle from a data stream.
        /// </summary>
        /// <param name="data">
        /// A <see cref="System.Byte[]"/>
        /// </param>
        /// <param name="start">
        /// A <see cref="System.Int32"/>
        /// </param>
        /// <param name="end">
        /// A <see cref="System.Int32"/>
        /// </param>
        /// <returns>
        /// A <see cref="OSCBundle"/>
        /// </returns>
        public static OSCBundle Unpack(byte[] data, ref int start, int end)
        {
            string address = OSCPacket.UnpackValue <string>(data, ref start);

            Trace.Assert(address == BUNDLE);

            long      timeStamp = OSCPacket.UnpackValue <long>(data, ref start);
            OSCBundle bundle    = new OSCBundle(timeStamp);

            while (start < end)
            {
                int length    = OSCPacket.UnpackValue <int>(data, ref start);
                int packetEnd = start + length;
                bundle.Append(OSCPacket.Unpack(data, ref start, packetEnd));
            }

            return(bundle);
        }
예제 #4
0
        /// <summary>
        /// Packs a bundle to be transported over an OSC stream.
        /// </summary>
        override public void Pack()
        {
            List <byte> data = new List <byte>();

            data.AddRange(OSCPacket.PackValue(BUNDLE));
            PadNull(data);

            data.AddRange(OSCPacket.PackValue <Int64>(_timeStamp));

            foreach (object value in _data)
            {
                Trace.Assert(value is OSCMessage);
                OSCMessage msg = (OSCMessage)value;

                data.AddRange(OSCPacket.PackValue(msg.BinaryData));
            }

            this._binaryData = data.ToArray();
        }
예제 #5
0
        public void SendTo(OSCPacket packet, string host, int port)
        {
            byte[] data = packet.BinaryData;
            try
            {
                //Debug.Log("SendTo :"+host+"/"+port);
                _ipAddress = IPAddress.Parse(host);
                _port      = port;

                //Connect ();
                var tmpUdpClient = new UdpClient(); //BOF BOF
                tmpUdpClient.Send(data, data.Length, host, port);
                //Debug.Log ("Sent");
            }
            catch (Exception e)
            {
                throw new Exception(String.Format("Can't send OSC packet to client {0} : {1} | {2}", _ipAddress, _port, e.ToString()));
            }
        }
예제 #6
0
        public void SendTo(OSCPacket packet, string host, int port)
        {
            byte[] data = packet.BinaryData;
            try
            {
                //Debug.Log("SendTo :"+host+"/"+port);
                _ipAddress = IPAddress.Parse(host);
                _port      = port;

                //Connect ();

                _udpClient.Send(data, data.Length, host, port);
                //Debug.Log ("Sent");
            }
            catch
            {
                Debug.LogWarning(String.Format("Can't send OSC packet to client {0} : {1}", _ipAddress, _port));
            }
        }
예제 #7
0
        /// <summary>
        /// Receives and unpacks an OSC packet.
        /// A <see cref="OSCPacket"/>
        /// </summary>
        private void Receive()
        {
            IPEndPoint ip = null;

            try {
                byte[] bytes = _udpClient.Receive(ref ip);

                if (bytes != null && bytes.Length > 0)
                {
                    OSCPacket packet = OSCPacket.Unpack(bytes);

                    _lastReceivedPacket = packet;

                    PacketReceivedEvent(this, _lastReceivedPacket);
                }
            }
            catch {
                throw new Exception(String.Format("Can't create server at port {0}", _localPort));
            }
        }
예제 #8
0
        void didRecievedEvent(OSCServer sender, OSCPacket packet)
        {
            lock (_queue)
            {
                if (packet.IsBundle())
                {
                    var bundle = packet as OSCBundle;

                    foreach (object obj in bundle.Data)
                    {
                        OSCMessage msg = obj as OSCMessage;
                        _queue.Enqueue(msg);
                    }
                }
                else
                {
                    _queue.Enqueue(packet as OSCMessage);
                }
            }
        }
예제 #9
0
        /// <summary>
        /// Receives and unpacks an OSC packet.
        /// </summary>
        /// <returns>
        /// A <see cref="OSCPacket"/>
        /// </returns>
        private OSCPacket Receive()
        {
            IPEndPoint ip = null;

            try
            {
                byte[] bytes = _udpClient.Receive(ref ip);

                if (bytes != null && bytes.Length > 0)
                {
                    return(OSCPacket.Unpack(bytes));
                }
            }
            catch
            {
                throw new Exception(String.Format("Can't unpack upcoming OSC data at port {0}", _localPort));
            }

            return(null);
        }
예제 #10
0
    private void Server_PacketReceivedEvent(UnityOSC.OSCServer sender, UnityOSC.OSCPacket packet)
    {
        //Debug.LogError("packet: " + packet.Address + " " + OSCHandler.DataToString(packet.Data));
        if (packet.Address.Contains("touching_forehead"))
        {
            HandleTouchingForehead((int)packet.Data[0] != 0);
        }
        else if (packet.Address.Contains("concentration"))
        {
            HandleConcentrationSample((float)packet.Data[0]);
        }
        else if (packet.Address.Contains("mellow"))
        {
            HandleMellowSample((float)packet.Data[0]);
        }
        else if (packet.Address.Contains("horseshoe"))
        {
            HandleHeadConnectMessage(packet.Data);
        }
        else if (packet.Address.Contains("batt"))
        {
            HandleBatteryStatus(packet.Data);
        }
        else if (packet.Address.Contains("blink"))
        {
            HandleBlinkSample(packet.Data);
        }

        if (SensorMeasureUpdate != null)
        {
            foreach (string metric in sensorMeasuresToTrack)
            {
                if (packet.Address.Contains(metric))
                {
                    SensorMeasureUpdate(metric, (float)packet.Data[0], (float)packet.Data[1], (float)packet.Data[2], (float)packet.Data[3]);
                }
            }
        }

        timeOfLastMessage = DateTime.Now;
    }
예제 #11
0
        void didReceivedEvent(OSCServer sender, OSCPacket packet)
        {
            lock (_queue)
            {
                if (packet.IsBundle())
                {
                    var bundle = packet as OSCBundle;

                    foreach (object obj in bundle.Data)
                    {
                        OSCMessage msg = obj as OSCMessage;

                        if (OSCMaster.Instance.LogIncoming)
                        {
                            Debug.Log("[" + Name + "] " + msg.Address);
                            foreach (var data in msg.Data)
                            {
                                Debug.Log(data);
                            }
                        }

                        _queue.Enqueue(msg);
                    }
                }
                else
                {
                    OSCMessage msg = packet as OSCMessage;

                    if (OSCMaster.Instance.LogIncoming)
                    {
                        Debug.Log("[" + Name + "] " + msg.Address);
                        foreach (var data in msg.Data)
                        {
                            Debug.Log(data);
                        }
                    }

                    _queue.Enqueue(msg);
                }
            }
        }
예제 #12
0
        /// <summary>
        /// Packs the OSC message to binary data.
        /// </summary>
        override public void Pack()
        {
            List <byte> data = new List <byte>();

            data.AddRange(OSCPacket.PackValue(_address));
            OSCPacket.PadNull(data);
            data.AddRange(OSCPacket.PackValue(_typeTag));
            OSCPacket.PadNull(data);

            foreach (object value in _data)
            {
                data.AddRange(OSCPacket.PackValue(value));

                if (value is string || value is byte[])
                {
                    OSCPacket.PadNull(data);
                }
            }

            this._binaryData = data.ToArray();
        }
예제 #13
0
        void OnReceive(IAsyncResult result)
        {
            var        socket = result.AsyncState as UdpClient;
            IPEndPoint ip     = null;

            byte[] bytes = socket.EndReceive(result, ref ip);


            if (bytes != null && bytes.Length > 0)
            {
                OSCPacket packet = OSCPacket.Unpack(bytes);

                _lastReceivedPacket = packet;

                PacketReceivedEvent(this, _lastReceivedPacket);
            }


            // listen to next one
            _udpClient.BeginReceive(new AsyncCallback(OnReceive), _udpClient);
        }
예제 #14
0
        private async void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
                                                  Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
        {
            // lock multi event
            socket.MessageReceived -= Socket_MessageReceived;

            //Debug.Log("OSCSERVER UWP  Socket_MessageReceived");

            //Read the message that was received from the UDP echo client.
            //Stream streamIn = args.GetDataStream().AsStreamForRead();
            DataReader reader = args.GetDataReader();

            //StreamReader reader = new StreamReader(streamIn);
            try
            {
                uint   stringLength = reader.UnconsumedBufferLength;
                byte[] bytes        = new byte[stringLength];
                reader.ReadBytes(bytes);

                //string message = await reader.ReadToEndAsync()
                //                 .ConfigureAwait(continueOnCapturedContext: false);
                //byte[] bytes = System.Text.Encoding.UTF8.GetBytes(message);

                OSCPacket packet = OSCPacket.Unpack(bytes);
                _lastReceivedPacket = packet;

                PacketReceivedEvent(this, _lastReceivedPacket);
            }
            catch (System.Exception e)
            {
                WorldErrors.Print(e.Message);
            }
            finally
            {
                //streamIn.Dispose();
                reader.Dispose();
                // unlock multi event
                socket.MessageReceived += Socket_MessageReceived;
            }
        }
예제 #15
0
파일: OSCServer.cs 프로젝트: thmslhst/Toro
        /// <summary>
        /// Receives and unpacks an OSC packet.
        /// A <see cref="OSCPacket"/>
        /// </summary>
        private void Receive()
        {
            IPEndPoint ip = null;

            try
            {
                byte[] bytes = _udpClient.Receive(ref ip);

                if (bytes != null && bytes.Length > 0)
                {
                    OSCPacket packet = OSCPacket.Unpack(bytes);

                    _lastReceivedPacket = packet;

                    PacketReceivedEvent(this, _lastReceivedPacket);
                }
            }
            catch {
                // Lance une exception à chaque fois qu'on exit le player... bizarre...
                //throw new Exception(String.Format("Can't create server at port {0}", _localPort));
            }
        }
예제 #16
0
        /// <summary>
        /// Packs a bundle to be transported over an OSC stream.
        /// </summary>
        override public void Pack()
        {
            // TODO: Pack bundle with timestamp in NTP format

            //throw new NotImplementedException("OSCBundle.Pack() : Not implemented method.");

            List <byte> data = new List <byte> ();

            data.AddRange(OSCPacket.PackValue(BUNDLE));
            PadNull(data);

            data.AddRange(OSCPacket.PackValue <Int64> (_timeStamp));

            foreach (object value in _data)
            {
                Trace.Assert(value is OSCMessage);
                OSCMessage msg = (OSCMessage)value;

                data.AddRange(OSCPacket.PackValue(msg.BinaryData));
            }

            this._binaryData = data.ToArray();
        }
예제 #17
0
        /// <summary>
        /// Receives and unpacks an OSC packet.
        /// A <see cref="OSCPacket"/>
        /// </summary>
        private void Receive()
        {
            IPEndPoint ip = null;

            try
            {
                byte[] bytes = _udpClient.Receive(ref ip);

                if (bytes != null && bytes.Length > 0)
                {
                    OSCPacket packet = OSCPacket.Unpack(bytes);
                    lock (_lastReceivedPacket);
                    _lastReceivedPacket.Add(packet);
                    if (_lastReceivedPacket.Count > MaxPacketQueue)
                    {
                        _lastReceivedPacket.RemoveAt(0);
                    }
                    recievedFlag = true;
                }
            }
            catch {
                throw new Exception(String.Format("Can't create server at port {0}", _localPort));
            }
        }
예제 #18
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:
                    //UnityEngine.Debug.LogWarning("DEFAULT TAG");
                    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;

                /*
                 * case TRUE:
                 *      value = true;
                 *      break;
                 * case FALSE:
                 *      value = false;
                 *      break;
                 */
                default:
                    //UnityEngine.Debug.LogWarning("Unknown tag: " + tag);
                    Console.WriteLine("Unknown tag: " + tag);
                    continue;
                }

                message.Append(value);
            }

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

            return(message);
        }
예제 #19
0
 public void resetLastReceivedPacket()
 {
     _lastReceivedPacket = null;
 }