public override void Start()
        {
            FileStream stream = File.OpenRead(filePath);

            bool finished = false;

            byte[] size = new byte[4];

            List <OSCPacket> packets = new List <OSCPacket>();

            while (!finished)
            {
                int bytesRead = stream.Read(size, 0, 4);
                if (bytesRead == 0)
                {
                    finished = true;
                }
                else
                {
                    int    packetSize = BitConverter.ToInt32(size, 0);
                    byte[] bytes      = new byte[packetSize];
                    stream.Read(bytes, 0, packetSize);
                    OSCPacket p = OSCPacket.Unpack(bytes);
                    packets.Add(p);
                }
            }

            stream.Close();

            foreach (OSCPacket packet in packets)
            {
                PacketQueue.Enqueue(packet);
            }

            Finished = true;
        }
Exemple #2
0
    //receive from p5js (WebSocket client)
    protected override void OnMessage(MessageEventArgs e)
    {
        if (e.IsText)
        {
            // Do something with e.Data.
            Debug.Log(e.Data);

            return;
        }

        if (e.IsBinary)
        {
            // Do something with e.RawData.

            //Debug.Log(e.RawData);

            OSCPacket msg = OSCPacket.Unpack(e.RawData);
            //Debug.Log(msg);

            Debug.Log(string.Format("received OSC address: {0}, value: {1}", msg.Address, msg.Values[0]));

            return;
        }
    }
Exemple #3
0
        public void Evaluate(int SpreadMax)
        {
            if (!FInput.IsChanged && !FSelect.IsChanged && !FDistribute.IsChanged)
            {
                return;
            }

            if ((FInput.SliceCount == 0) || (FInput[0] == null) || (FInput[0].Length <= 1))
            {
                return;
            }

            SpreadMax = FInput.SliceCount;

            var isFound  = new Dictionary <string, bool>();
            var messages = new List <OSCMessage>();

            for (int i = 0; i < SpreadMax; i++)
            {
                var ms = new MemoryStream();

                int index = FSelect[0] == SelectEnum.Last ? SpreadMax - i - 1 : i;
                FInput[index].Position = 0; // rewind stream
                FInput[index].CopyTo(ms);
                byte[] bytes = ms.ToArray();
                int    start = 0;

                OSCPacket packet = OSCPacket.Unpack(bytes, ref start, (int)FInput[index].Length);

                if (packet.IsBundle())
                {
                    var packets = ((OSCBundle)packet).Values;
                    for (int j = 0; j < packets.Count; j++)
                    {
                        int innerIndex   = FSelect[0] == SelectEnum.Last ? packets.Count - j - 1 : j;
                        var innerMessage = (OSCMessage)packets[innerIndex];

                        if (FSelect[0] == SelectEnum.All)
                        {
                            messages.Add(innerMessage);
                        }
                        else
                        {
                            if (isFound.ContainsKey(innerMessage.Address))
                            {
                                // do nothing.
                            }
                            else
                            {
                                isFound.Add(innerMessage.Address, true);
                                messages.Add(innerMessage);
                            }
                        }
                    }
                }
                else
                {
                    if (FSelect[0] == SelectEnum.All)
                    {
                        messages.Add((OSCMessage)packet);
                    }
                    else
                    {
                        if (isFound.ContainsKey(packet.Address))
                        {
                            // do nothing.
                        }
                        else
                        {
                            isFound.Add(packet.Address, true);
                            messages.Add((OSCMessage)packet);
                        }
                    }
                }
            }

            // all unnecessary messages are gone. now try to distribute them.

            FOutput.SliceCount = 0;
            var bundles      = new Dictionary <string, OSCBundle>();
            var singleBundle = new OSCBundle();

            foreach (var message in messages)
            {
                if (!FDistribute[0])
                {
                    singleBundle.Append(message);
                }
                else
                {
                    var a = MainAddress(message.Address);
                    if (!bundles.ContainsKey(a))
                    {
                        bundles.Add(a, new OSCBundle());
                    }
                    bundles[a].Append(message);
                }
            }

            FOutput.SliceCount = 0;

            if (!FDistribute[0])
            {
                FOutput.Add(new MemoryStream((singleBundle.BinaryData)));
            }
            else
            {
                foreach (OSCBundle bundle in bundles.Values)
                {
                    FOutput.Add(new MemoryStream((bundle.BinaryData)));
                }
            }
            FOutput.Flush();
        }
Exemple #4
0
#pragma warning restore

        #endregion fields & pins



        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            if (FInput.IsAnyInvalid())
            {
                if (FOutput.SliceCount > 0)
                {
                    FOutput.SliceCount = 0;
                    FOutput.Flush();
                }
                if (FOther.SliceCount > 0)
                {
                    FOther.SliceCount = 0;
                    FOther.Flush();
                }
                return;
            }

            FOther.SliceCount = FOutput.SliceCount = 0;
            SpreadMax         = FInput.SliceCount;

            if ((FInput.SliceCount == 0) || (FInput[0] == null) || (FInput[0].Length == 0))
            {
                return;
            }

            for (int i = 0; i < SpreadMax; i++)
            {
                MemoryStream ms = new MemoryComStream();
                FInput[i].Position = 0;
                FInput[i].CopyTo(ms);
                byte[] bytes = ms.ToArray();
                int    start = 0;

                OSCPacket packet = OSCPacket.Unpack(bytes, ref start, (int)ms.Length, FExtendedMode[0]);


                bool matches = false;
                for (int j = 0; j < FPinInAddress.SliceCount; j++)
                {
                    switch (FPinInFilter[0])
                    {
                    case Filter.Matches:
                        matches |= packet.Address == FPinInAddress[j];
                        break;

                    case Filter.Contains:
                        matches |= packet.Address.Contains(FPinInAddress[j]);
                        break;

                    case Filter.Starts:
                        matches |= packet.Address.StartsWith(FPinInAddress[j]);
                        break;

                    case Filter.Ends:
                        matches |= packet.Address.EndsWith(FPinInAddress[j]);
                        break;

                    case Filter.All:
                        matches = true;
                        break;
                    }
                }

                if (matches)
                {
                    FOutput.Add(ms);
                }
                else
                {
                    FOther.Add(ms);
                }
            }

            FOutput.Flush();
            FOther.Flush();
        }
        public static Message FromOSC(Stream stream, bool extendedMode = false, string messagePrefix = "", int contractAddress = 1)
        {
            if (stream == null || stream.Length <= 0)
            {
                return(null);
            }

            stream.Position = 0;
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, (int)stream.Length);

            if (bytes.Length == 0)
            {
                return(null);
            }

            Message message = new Message();
            var     pack    = OSCPacket.Unpack(bytes, extendedMode);
            bool    useNesting;

            OSCBundle bundle;

            if (pack.IsBundle())
            {
                bundle            = (OSCBundle)pack;
                message.TimeStamp = bundle.getTimeStamp(); // if not set, will be 1.1.1900
                useNesting        = true;
            }
            else
            {
                bundle = new OSCBundle(extendedMode);
                var m = (OSCMessage)pack;
                bundle.Append(m);
                message.TimeStamp = Time.CurrentTime();
                useNesting        = false;
            }


            foreach (OSCMessage m in bundle.Values)
            {
                string[] address = m.Address.Trim(new char[] { '/' }).Split('/');

                string messageAddress = string.Join(".", address.Take(Math.Max(1, address.Length - contractAddress)).ToArray());

                if (messagePrefix.Trim() == "")
                {
                    message.Topic = messageAddress;
                }
                else
                {
                    message.Topic = messagePrefix + "." + messageAddress;
                }

                // empty messages are usually used for requesting data
                if (m.Values.Count <= 0)
                {
                    // leave message emtpy, cannot infer type.
                }
                else
                {
                    var usedTypes = (
                        from v in m.Values.ToArray()
                        select v.GetType()
                        ).Distinct();

                    string attribName = string.Join(".", address.Skip(Math.Max(0, address.Length - contractAddress)).ToArray());

                    if (usedTypes.Count() > 1)
                    {
                        var inner = new Message(message.Topic + "." + attribName);
                        var max   = m.Values.Count;

                        for (int i = 0; i < max; i++)
                        {
                            var item = m.Values[i];
                            var num  = i.ToString();
                            inner[num] = BinFactory.New(item.GetType());
                            inner[num].Add(item);
                        }

                        if (useNesting)
                        {
                            message[attribName] = BinFactory.New(inner);
                        }
                        else
                        {
                            message = inner;
                        }
                    }
                    else
                    {
                        var bin = BinFactory.New(usedTypes.First());
                        bin.AssignFrom(m.Values);
                        message[attribName] = bin;
                    }
                }
            }

            return(message);
        }
        public static Message FromOSC(Stream stream, Dictionary <string, IEnumerable <FormularFieldDescriptor> > parser, bool extendedMode = false)
        {
            if (stream == null || stream.Length == 0)
            {
                return(null);
            }

            var size  = stream.Length;
            var bytes = new byte[size];

            stream.Position = 0;
            stream.Read(bytes, 0, (int)size);
            OSCPacket oscMessage = OSCPacket.Unpack(bytes, extendedMode);

            // seems slightly slower.
            //OSCPacket oscMessage;
            //var reader = new BinaryReader(stream);
            //{
            //    var bytes = reader.ReadBytes((int)stream.Length);
            //    oscMessage = OSCPacket.Unpack(bytes, extendedMode);
            //}

            if (oscMessage == null || oscMessage.IsBundle())
            {
                return(null);
            }

            if (!parser.ContainsKey(oscMessage.Address))
            {
                return(null); // skip if unknown address
            }
            Message message = new Message();

            message.TimeStamp = Time.CurrentTime();
            message.Topic     = AsTopic(oscMessage.Address);

            var fields = parser[oscMessage.Address].ToList();

            int pointer = 0;

            for (int i = 0; i < fields.Count(); i++)
            {
                var field = fields[i];

                var count = field.DefaultSize; // todo: how to deal with -1 ?
                if (count < 0)
                {
                    count = 1;
                }

                var bin = BinFactory.New(field.Type, count);

                for (int j = 0; (j < count) && (pointer < oscMessage.Values.Count); j++, pointer++) // stop short if running out of parser fields OR data from osc
                {
                    bin.Add(oscMessage.Values[pointer]);                                            // implicit conversion
                }
                message[field.Name] = bin;
            }

            return(message);
        }
Exemple #7
0
        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            bool needsSend = FInFrame.IsChanged;

            if (FInFilename.IsChanged || FInReload[0])
            {
                try
                {
                    var fileText = System.IO.File.ReadAllText(FInFilename[0]);
                    var json     = JsonConvert.DeserializeObject <Dictionary <string, List <string> > > (fileText);

                    this.FFrames.Clear();

                    foreach (var frame in json)
                    {
                        int frameIndex = System.Convert.ToInt32(frame.Key);
                        var packetList = new List <OSCPacket>();
                        foreach (var encodedMessage in frame.Value)
                        {
                            var data   = Convert.FromBase64String(encodedMessage);
                            var packet = OSCPacket.Unpack(data);
                            packetList.Add(packet);
                        }
                        this.FFrames.Add(frameIndex, packetList);
                    }
                    int  firstIndex = this.FFrames.Keys.GetEnumerator().Current;
                    int  checkIndex = firstIndex;                    // get first
                    bool trim       = false;
                    foreach (var frameIndex in this.FFrames.Keys)
                    {
                        if (frameIndex != checkIndex)
                        {
                            trim = true;
                        }
                        checkIndex++;
                    }
                    if (trim)
                    {
                        SortedDictionary <int, List <OSCPacket> > trimmedFrames = new SortedDictionary <int, List <OSCPacket> >();
                        int i = 0;
                        foreach (var frame in this.FFrames)
                        {
                            trimmedFrames.Add(i++, frame.Value);
                        }
                        this.FFrames = trimmedFrames;

                        string exceptionMessage = "Encoded frames are not in order. Please resave. Missing " + checkIndex + ". Found :";
                        foreach (var outFrameIndex in this.FFrames.Keys)
                        {
                            exceptionMessage += outFrameIndex.ToString() + ", ";
                        }
                        FOutStatus[0] = exceptionMessage;
                    }
                    else
                    {
                        FOutStatus[0] = "OK";
                    }
                    needsSend         = true;
                    FOutFrameCount[0] = this.FFrames.Count;
                    FOutStatus[0]     = "OK";
                }
                catch (Exception e)
                {
                    FFrames.Clear();
                    FOutStatus[0] = e.Message;
                }
            }

            int currentFrameIndex = FInFrame[0];

            if (FFrames.Count > 0)
            {
                currentFrameIndex = VMath.Zmod(currentFrameIndex, FFrames.Count);
            }
            needsSend &= FFrames.ContainsKey(currentFrameIndex);
            if (needsSend)
            {
                var currentFrame = this.FFrames[currentFrameIndex];
                FOutput.AssignFrom(currentFrame);
            }
            else
            {
                FOutput.SliceCount = 0;
            }
        }