Esempio n. 1
0
        public new static OscBundle Parse(ref OscStringReader reader, IFormatProvider provider = null, OscSerializationToken endToken = OscSerializationToken.End)
        {
            if (provider == null)
            {
                provider = CultureInfo.InvariantCulture;
            }

            string ident = reader.ReadAddress(false)
                           .Trim();

            if (BundleIdent.Equals(ident, StringComparison.Ordinal) == false)
            {
                throw new Exception($"Invalid bundle ident '{ident}'");
            }

            reader.ReadSeparator();

            if (reader.ReadNextToken(out string timeStampStr) != OscSerializationToken.Literal)
            {
                throw new Exception("Invalid bundle timestamp");
            }

            OscTimeTag timeStamp = OscTimeTag.Parse(timeStampStr.Trim(), provider);

            if (reader.ReadSeparatorOrEnd() == endToken)
            {
                return(new OscBundle(timeStamp));
            }

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

            OscSerializationToken token = OscSerializationToken.None;

            token = reader.ReadNextToken(out string _);

            while (token != endToken && token != OscSerializationToken.End)
            {
                if (token != OscSerializationToken.ObjectStart)
                {
                    throw new Exception("Invalid bundle token");
                }

                packets.Add(OscPacket.Parse(ref reader, provider, OscSerializationToken.ObjectEnd));

                token = reader.ReadNextToken(out string _);

                if (token == OscSerializationToken.Separator)
                {
                    token = reader.ReadNextToken(out string _);
                }
            }

            if (token != endToken)
            {
                throw new OscException(OscError.UnexpectedToken, $"Unexpected token {token}");
            }

            return(new OscBundle(timeStamp, packets.ToArray()));
        }
Esempio n. 2
0
        public new static OscMessage Parse(ref OscStringReader reader, IFormatProvider provider = null, OscSerializationToken endToken = OscSerializationToken.End)
        {
            string address = reader.ReadAddress(true);

            // parse arguments
            List <object> arguments = new List <object>(reader.ReadArguments(provider, endToken));

            return(new OscMessage(address, arguments.ToArray()));
        }
Esempio n. 3
0
        public static OscPacket Parse(ref OscStringReader reader, IFormatProvider provider = null, OscSerializationToken endToken = OscSerializationToken.End)
        {
            if (reader.PeekChar() == '#')
            {
                return(OscBundle.Parse(ref reader, provider, endToken));
            }

            return(OscMessage.Parse(ref reader, provider, endToken));
        }
Esempio n. 4
0
        public static OscPacket Parse(string str, IFormatProvider provider = null)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                throw new ArgumentNullException(nameof(str));
            }

            OscStringReader reader = new OscStringReader(str);

            return(Parse(ref reader, provider, OscSerializationToken.End));
        }
Esempio n. 5
0
        public void Literal()
        {
            string original      = "hello";
            string expectedValue = "hello";
            OscSerializationToken expectedToken = OscSerializationToken.Literal;

            OscStringReader reader = new OscStringReader(original);

            OscSerializationToken token = reader.ReadNextToken(out string value);

            Assert.Equal(expectedToken, token);
            Assert.Equal(expectedValue, value);
        }
Esempio n. 6
0
        public void End()
        {
            string original      = @"";
            string expectedValue = null;
            OscSerializationToken expectedToken = OscSerializationToken.End;

            OscStringReader reader = new OscStringReader(original);

            OscSerializationToken token = reader.ReadNextToken(out string value);

            Assert.Equal(expectedToken, token);
            Assert.Equal(expectedValue, value);
        }
Esempio n. 7
0
        public void StringWithEscape()
        {
            string original      = @"""let's say \""hello\""""";
            string expectedValue = @"let's say \""hello\""";
            OscSerializationToken expectedToken = OscSerializationToken.String;

            OscStringReader reader = new OscStringReader(original);

            OscSerializationToken token = reader.ReadNextToken(out string value);

            Assert.Equal(expectedToken, token);
            Assert.Equal(expectedValue, value);
        }
Esempio n. 8
0
        /// <summary>
        ///     parse a message from a string using a supplied format provider
        /// </summary>
        /// <param name="str">a string containing a message</param>
        /// <param name="provider">the format provider to use</param>
        /// <returns>the parsed message</returns>
        public new static OscMessage Parse(string str, IFormatProvider provider = null)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                throw new ArgumentNullException("str");
            }

            if (provider == null)
            {
                provider = CultureInfo.InvariantCulture;
            }

            OscStringReader reader = new OscStringReader(str);

            return(Parse(ref reader, provider, OscSerializationToken.End));
        }
Esempio n. 9
0
        public static OscColor Parse(ref OscStringReader reader, IFormatProvider provider)
        {
            string[] pieces = new string[4];

            OscSerializationToken token = OscSerializationToken.None;

            for (int i = 0; i < 4; i++)
            {
                token     = reader.ReadNextToken(out string value);
                pieces[i] = value;
                token     = reader.ReadNextToken(out string _);
            }

            if (token != OscSerializationToken.ObjectEnd)
            {
                throw new Exception("Invalid color");
            }

            byte a, r, g, b;

            r = byte.Parse(
                pieces[0]
                .Trim(),
                NumberStyles.None,
                provider
                );
            g = byte.Parse(
                pieces[1]
                .Trim(),
                NumberStyles.None,
                provider
                );
            b = byte.Parse(
                pieces[2]
                .Trim(),
                NumberStyles.None,
                provider
                );
            a = byte.Parse(
                pieces[3]
                .Trim(),
                NumberStyles.None,
                provider
                );

            return(FromArgb(a, r, g, b));
        }
Esempio n. 10
0
        public static OscMidiMessage Parse(ref OscStringReader reader, IFormatProvider provider)
        {
//            string[] pieces = new string[4];
//
//            OscSerializationToken token = OscSerializationToken.None;
//
//            for (int i = 0; i < 4; i++)
//            {
//                token = reader.ReadNextToken(out string value);
//                pieces[i] = value;
//                token = reader.ReadNextToken(out string _);
//            }
//
//            if (token != OscSerializationToken.ObjectEnd)
//            {
//                throw new Exception($"Invalid color");
//            }
//
//            byte a, r, g, b;
//
//            r = byte.Parse(pieces[0].Trim(), System.Globalization.NumberStyles.None, provider);
//            g = byte.Parse(pieces[1].Trim(), System.Globalization.NumberStyles.None, provider);
//            b = byte.Parse(pieces[2].Trim(), System.Globalization.NumberStyles.None, provider);
//            a = byte.Parse(pieces[3].Trim(), System.Globalization.NumberStyles.None, provider);
//
//            return OscColor.FromArgb(a, r, g, b);
//
//            return new OscMidiMessage(0, OscMidiMessageType.ControlChange, 0, 0);

            List <string> parts = new List <string>();

            OscSerializationToken token = OscSerializationToken.None;

            do
            {
                token = reader.ReadNextToken(out string value);
                parts.Add(value);
                token = reader.ReadNextToken(out string _);
            }while (token != OscSerializationToken.ObjectEnd);

            if (parts.Count < 4)
            {
                throw new Exception($"Not a midi message '{parts.Count}'");
            }

            int  index  = 0;
            byte portID = byte.Parse(
                parts[index++]
                .Trim(),
                provider
                );

            byte statusByte;
            OscMidiMessageType messageType;

            if (byte.TryParse(
                    parts[index]
                    .Trim(),
                    NumberStyles.Integer,
                    provider,
                    out statusByte
                    ) == false)
            {
                OscMidiSystemMessageType systemMessage;

                if (Enum.TryParse(
                        parts[index]
                        .Trim(),
                        true,
                        out systemMessage
                        ))
                {
                    messageType = OscMidiMessageType.SystemExclusive;
                    statusByte  = (byte)((int)messageType | (int)systemMessage);
                    index++;
                }
                else if (Enum.TryParse(
                             parts[index]
                             .Trim(),
                             true,
                             out messageType
                             ))
                {
                    index++;
                    byte channel = byte.Parse(
                        parts[index++]
                        .Trim(),
                        NumberStyles.Integer,
                        provider
                        );

                    if (channel > 15)
                    {
                        throw new ArgumentOutOfRangeException(nameof(channel));
                    }

                    statusByte = (byte)((int)messageType | channel);

                    if (parts.Count < 5)
                    {
                        throw new Exception($"Not a midi message '{parts.Count}'");
                    }
                }
                else
                {
                    throw new Exception($"Not a midi message '{parts.Count}'");
                }
            }

            byte data1 = byte.Parse(
                parts[index++]
                .Trim(),
                NumberStyles.Integer,
                provider
                );

            if (data1 > 0x7F)
            {
                throw new ArgumentOutOfRangeException(nameof(data1));
            }

            byte data2 = byte.Parse(
                parts[index++]
                .Trim(),
                NumberStyles.Integer,
                provider
                );

            if (data2 > 0x7F)
            {
                throw new ArgumentOutOfRangeException(nameof(data2));
            }

            if (index != parts.Count)
            {
                throw new Exception($"Not a midi message '{parts.Count}'");
            }

            return(new OscMidiMessage(portID, statusByte, data1, data2));
        }
Esempio n. 11
0
        public static OscTimeTag Parse(ref OscStringReader reader, IFormatProvider provider)
        {
            if (reader.ReadNextToken(out string str) != OscSerializationToken.Literal)
            {
                throw new Exception(@"Invalid osc-timetag string");
            }

            if (reader.ReadNextToken(out string _) != OscSerializationToken.ObjectEnd)
            {
                throw new Exception(@"Invalid osc-timetag string");
            }

            DateTimeStyles style = DateTimeStyles.AdjustToUniversal;

            if (str.Trim()
                .EndsWith("Z"))
            {
                style = DateTimeStyles.AssumeUniversal;

                str = str.Trim()
                      .TrimEnd('Z');
            }

            if (DateTime.TryParseExact(str, Formats, provider, style, out DateTime datetime))
            {
                return(FromDataTime(datetime));
            }

            if (str.StartsWith("0x") &&
                ulong.TryParse(str.Substring(2), NumberStyles.HexNumber, provider, out ulong value))
            {
                return(new OscTimeTag(value));
            }

            if (ulong.TryParse(str, NumberStyles.Integer, provider, out value))
            {
                return(new OscTimeTag(value));
            }

            throw new Exception($@"Invalid osc-timetag string ""{str}""");

            //return new OscTimeTag(0);

//            DateTimeStyles style = DateTimeStyles.AdjustToUniversal;
//
//            if (str.Trim().EndsWith("Z") == true)
//            {
//                style = DateTimeStyles.AssumeUniversal;
//
//                str = str.Trim().TrimEnd('Z');
//            }
//
//            if (DateTime.TryParseExact(str, formats, provider, style, out DateTime datetime) == true)
//            {
//                return FromDataTime(datetime);
//            }
//
//            if (str.StartsWith("0x") == true &&
//                ulong.TryParse(str.Substring(2), NumberStyles.HexNumber, provider, out ulong value) == true)
//            {
//                return new OscTimeTag(value);
//            }
//
//            if (ulong.TryParse(str, NumberStyles.Integer, provider, out value) == true)
//            {
//                return new OscTimeTag(value);
//            }
//
//            throw new Exception($@"Invalid osc-timetag string ""{str}""");
        }
Esempio n. 12
0
        public void SimpleString()
        {
            List <string> originals = new List <string>
            {
                @"/moop, ""thing"", 123, '!', [ 1, 2, 3, 4, 5]",
                @"/moop,""thing"",123,'!',[1,2,3,4,5]"
            };

            List <OscSerializationToken> expectedTokens = new List <OscSerializationToken>
            {
                OscSerializationToken.Literal,
                OscSerializationToken.Separator,
                OscSerializationToken.String,
                OscSerializationToken.Separator,
                OscSerializationToken.Literal,
                OscSerializationToken.Separator,
                OscSerializationToken.Char,
                OscSerializationToken.Separator,

                OscSerializationToken.ArrayStart,

                OscSerializationToken.Literal,
                OscSerializationToken.Separator,
                OscSerializationToken.Literal,
                OscSerializationToken.Separator,
                OscSerializationToken.Literal,
                OscSerializationToken.Separator,
                OscSerializationToken.Literal,
                OscSerializationToken.Separator,
                OscSerializationToken.Literal,

                OscSerializationToken.ArrayEnd,
                OscSerializationToken.End
            };

            List <string> expectedValues = new List <string>
            {
                "/moop",
                null,
                "thing",
                null,
                "123",
                null,
                "!",
                null,

                null,

                "1",
                null,
                "2",
                null,
                "3",
                null,
                "4",
                null,
                "5",
                null,

                null,
                null
            };

            foreach (string orginal in originals)
            {
                OscStringReader reader = new OscStringReader(orginal);

                OscSerializationToken token;

                int index = 0;

                do
                {
                    token = reader.ReadNextToken(out string value);

                    Assert.Equal(expectedTokens[index], token);
                    Assert.Equal(expectedValues[index], value);

                    index++;
                }while (token != OscSerializationToken.End);
            }
        }