Ejemplo n.º 1
0
        public static new OSCBundle Unpack(byte[] bytes, ref int start, int end, bool extendedMode = false)
        {
            string address = unpackString(bytes, ref start);

            //Console.WriteLine("bundle: " + address);
            if (!address.Equals(BUNDLE))
            {
                return(null);                // TODO
            }
            DateTime  timestamp = unpackTimeTag(bytes, ref start);
            OSCBundle bundle    = new OSCBundle(timestamp, extendedMode);

            while (start < end)
            {
                int length  = unpackInt(bytes, ref start);
                int sub_end = start + length;
                bundle.Append(OSCPacket.Unpack(bytes, ref start, sub_end, extendedMode));
            }

            return(bundle);
        }
Ejemplo n.º 2
0
        public static new OSCBundle Unpack(byte[] bytes, ref int start, int end)
        {
            string address = unpackString(bytes, ref start);

            //Console.WriteLine("bundle: " + address);
            if (!address.Equals(BUNDLE))
            {
                return(null);                                    // TODO
            }
            long      timestamp = unpackLong(bytes, ref start);
            OSCBundle bundle    = new OSCBundle(timestamp);

            while (start < end)
            {
                int length  = unpackInt(bytes, ref start);
                int sub_end = start + length;
                //Console.WriteLine(bytes.Length +" "+ start+" "+length+" "+sub_end);
                bundle.Append(OSCPacket.Unpack(bytes, ref start, sub_end));
            }

            return(bundle);
        }
Ejemplo n.º 3
0
        public static Stream ToOSC(this Message message, bool extendedMode = false)
        {
            OSCBundle bundle = new OSCBundle(message.TimeStamp, extendedMode);
            foreach (string name in message.Attributes)
            {
                string oscAddress = "";

                foreach (string part in message.Address.Split('.'))
                {
                    if (part.Trim() != "") oscAddress += "/" + part;
                }

                foreach (string part in name.Split('.'))
                {
                    if (part.Trim() != "") oscAddress += "/" + part;
                }

                OSCMessage m = new OSCMessage(oscAddress, extendedMode);
                Bin bl = message[name];
                for (int i = 0; i < bl.Count; i++) m.Append(bl[i]);
                bundle.Append(m);
            }
            return new MemoryStream(bundle.BinaryData); // packs implicitly
        }
Ejemplo n.º 4
0
		public void SendToLiveTrack(string addr, string prop, object arg)
		{
			if( (FOSCTransmitter != null) && (addr != "") )
			{
				var bundle = new OSCBundle();
				var message = new OSCMessage(addr);
				
				message.Append(prop);
				
				if(arg is float) message.Append((float)arg);
				else 
					if(arg is double) message.Append((float)arg);
				else 
					if(arg is int) message.Append((int)arg);
				else 
					if(arg is bool) message.Append((bool)arg ? 1 : 0);
				else
					message.Append(arg.ToString());
				
				bundle.Append(message);
				
				try
				{
					FOSCTransmitter.Send(bundle);
					if(Debug) LogDebug(message.ToString());
				}
				catch (Exception ex)
				{
					LastError = ex.Message.ToString();
				}
			} else LastError = "ERROR: FOSCTransmitter is null!";
		}
Ejemplo n.º 5
0
		public void SendToLive(string addr, object[] args)
		{
			if( (FOSCTransmitter != null) && (addr != "") )
			{
				var bundle = new OSCBundle();
				var message = new OSCMessage(addr);
				
				for (int i = 0; i < args.Length; i++)
					message.Append(args[i]);
				
				bundle.Append(message);

				try
				{
					FOSCTransmitter.Send(bundle);
					if(Debug) LogDebug(message.ToString());
				}
				catch (Exception ex)
				{
					LastError = ex.Message.ToString();
				}
			}
		}
Ejemplo n.º 6
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 MemoryComStream();

                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, FExtendedMode[0]);

                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(FExtendedMode[0]);

            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 MemoryComStream((singleBundle.BinaryData)));
            }
            else
            {
                foreach (OSCBundle bundle in bundles.Values)
                {
                    FOutput.Add(new MemoryComStream((bundle.BinaryData)));
                }
            }
            FOutput.Flush();
        }
Ejemplo n.º 7
0
        public Stream ToOSC()
        {
            OSCBundle bundle = new OSCBundle(this.TimeStamp.ToFileTime());
            foreach (string name in MessageData.Keys)  {
                string oscAddress = "";

                foreach (string part in Address.Split('.')) {
                    if (part.Trim() != "") oscAddress += "/" + part;
                }

                foreach (string part in name.Split('.'))
                {
                    if (part.Trim() != "") oscAddress += "/" + part;
                }

                OSCMessage m = new OSCMessage(oscAddress);
                SpreadList bl = MessageData[name];
                for (int i=0;i<bl.Count;i++) m.Append(bl[i]);
                bundle.Append(m);
            }
            return new MemoryStream(bundle.BinaryData); // packs implicitly
        }
Ejemplo n.º 8
0
		public static new OSCBundle Unpack (byte[] bytes, ref int start, int end, bool extendedMode = false)
		{

			string address = unpackString (bytes, ref start);
			//Console.WriteLine("bundle: " + address);
			if (!address.Equals (BUNDLE))
				return null; // TODO

			DateTime timestamp = unpackTimeTag (bytes, ref start);
			OSCBundle bundle = new OSCBundle (timestamp, extendedMode);
			
			while (start < end) {
				int length = unpackInt (bytes, ref start);
				int sub_end = start + length;
				bundle.Append (OSCPacket.Unpack (bytes, ref start, sub_end, extendedMode));
			}

			return bundle;
		}
Ejemplo n.º 9
0
        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;
        }
Ejemplo n.º 10
0
        public static Stream ToOSC(this Message message, bool extendedMode = false)
        {
            OSCBundle bundle = new OSCBundle(message.TimeStamp, extendedMode);
            foreach (string name in message.Fields)
            {
                string oscAddress = "";

                foreach (string part in message.Topic.Split('.'))
                {
                    if (part.Trim() != "") oscAddress += "/" + part;
                }

                foreach (string part in name.Split('.'))
                {
                    if (part.Trim() != "") oscAddress += "/" + part;
                }

                Bin bin = message[name];
                var typeRecord = TypeIdentity.Instance[bin.GetInnerType()];
                if (typeRecord == null || typeRecord.CloneMethod == CloneBehaviour.Null) continue;

                OSCMessage msg = new OSCMessage(oscAddress, extendedMode);
                for (int i = 0; i < bin.Count; i++) msg.Append(bin[i]);
                bundle.Append(msg);
            }
            return new MemoryStream(bundle.BinaryData); // packs implicitly
        }
Ejemplo n.º 11
0
        public Stream ToOSC()
        {
            var bundle = new OSCBundle(TimeStamp.ToFileTime());

            foreach (var name in FDictionary.Keys)
            {
                var address = Address.Split('.');
                var oscAddress = "";

                foreach (var part in address) {
                    if (part.Trim() != "") oscAddress += "/" + part;
                }

                var message = new OSCMessage(oscAddress+"/"+name);
                var binList = FDictionary[name];
                foreach (var obj in binList)
                    message.Append(obj);
                bundle.Append(message);
            }

            return new MemoryStream(bundle.BinaryData); // packs implicitly
        }
Ejemplo n.º 12
0
        void timer1_Tick(object sender, EventArgs e)
        {
            var hosttime = Clock.ElapsedMilliseconds / 1000f;
            var bundle = new OSCBundle();

            var prefix = "/" + PrefixTextBox.Text.Trim('/') + "/";
            if (prefix == "//")
                prefix = "/";

            lock(FPoshTimeliners)
                foreach (var timeliner in FPoshTimeliners)
                {
                    timeliner.Evaluate(hosttime);
                    var msg = new OSCMessage(prefix + "time");
                    msg.Append(timeliner.Timeliner.Timer.Time);
                    bundle.Append(msg);

                    foreach (var tl in timeliner.Timeliner.TimelineModel.Tracks)
                    {
                        var label = tl.Label.Value;

                        //TODO: ask track to generate osc message
                        var val = tl.GetCurrentValueAsObject();

                        msg = new OSCMessage(prefix + label);
                        msg.Append(val);
                        bundle.Append(msg);
                    }

                    //set interval for next round
                    timer1.Interval = 1000/timeliner.Timeliner.Timer.FPS;
                }

            if (FOSCTransmitter != null)
                FOSCTransmitter.Send(bundle);
        }