public static string UnknownMessageToCsv(OscTimeTag firstTimestamp, OscMessage message) { string timeString = ""; if (message.TimeTag.HasValue == true) { timeString = DataBase.CreateTimestampString(message.TimeTag.Value, firstTimestamp); } else { timeString = DataBase.CreateTimestampString(firstTimestamp, firstTimestamp); } StringBuilder sb = new StringBuilder(); string seperatorString = new string(',', 1); sb.Append(timeString); sb.Append(seperatorString); foreach (object obj in message) { CellToString(sb, obj); sb.Append(seperatorString); } string final = sb.ToString(); // strip of the final separator string final = final.Substring(0, final.Length - seperatorString.Length); return(final); }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), Gyroscope, Accelerometer, Magnetometer )); }
public void addGestures(VisualGestureBuilderFrame gestureFrame, ulong id, List <Gesture> discrete, List <Gesture> continuous) { DateTime reference = new DateTime(2001, 01, 01, 0, 0, 0, DateTimeKind.Utc); TimeSpan duration = new TimeSpan(DateTime.UtcNow.Ticks - reference.Ticks); ulong minutesCount = Convert.ToUInt64(duration.TotalMinutes); OscTimeTag timestamp = new OscTimeTag(minutesCount); List <OscMessage> messages = new List <OscMessage>(); foreach (Gesture gest in discrete) { DiscreteGestureResult result = gestureFrame.DiscreteGestureResults[gest]; messages.Add(messageBuilder.BuildGestureMessage(id.ToString(), gest.Name, result.Detected, result.Confidence)); } foreach (Gesture gest in continuous) { ContinuousGestureResult result = gestureFrame.ContinuousGestureResults[gest]; messages.Add(messageBuilder.BuildGestureMessage(id.ToString(), gest.Name, result.Progress)); } OscBundle target = new OscBundle(timestamp, messages.ToArray()); this.Broadcast(target); }
void Update() { // If we are going to send 'as bundle', then we would like OSC io // to add the timetag to the messeges contained in the bundle. oscIn.addTimeTagsToBundledMessages = sendAsBundle; // Create a messege OscMessage message = new OscMessage( address ); // Create a timetag. Default time is DateTime.Now. OscTimeTag timetag = new OscTimeTag(); // Make it 1 milisecond into the future. timetag.time = timetag.time.AddMilliseconds( 1 ); // Two possible methods for sending timetags ... if( sendAsBundle ) { // Either create a bundle with the timetag, add the message and send. OscBundle bundle = new OscBundle( timetag ); bundle.Add( message ); oscOut.Send( bundle ); } else { // Or add the timetag to message and send it. message.Add( timetag ); oscOut.Send( message ); } // Update label. sendLabel.text = timetag.time + ":" + timetag.time.Millisecond; }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), LinearAcceleration.X, LinearAcceleration.Y, LinearAcceleration.Z )); }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), EulerAngles.Roll, EulerAngles.Pitch, EulerAngles.Yaw )); }
public void ReadOffsetTest() { Random random = new Random(); for (int i = 0; i < 1000; i++) { OscTimeTag timestamp = new OscTimeTag(14236589681638796952); List <OscPacket> messages = new List <OscPacket>(); for (int j = 0; j < 10; j++) { messages.Add(new OscMessage("/" + j, (float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble())); } OscBundle expected = new OscBundle(timestamp, messages.ToArray()); int index = random.Next(1, 32); int endPadding = random.Next(0, 32); int count = expected.SizeInBytes; byte[] bytes = new byte[count + index + endPadding]; random.NextBytes(bytes); expected.ToByteArray().CopyTo(bytes, index); OscBundle actual; actual = OscBundle.Read(bytes, index, count); UnitTestHelper.AreEqual(expected, actual); } }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), State )); }
private object ParseObject(string value, IFormatProvider provider) { string name = ReadObjectNameToken(); if (name.Length == 0) { throw new Exception(@"Malformed object missing type name"); } string nameLower = name.ToLowerInvariant(); switch (nameLower) { case "midi": case "m": return(OscMidiMessage.Parse(ref this, provider)); case "time": case "t": return(OscTimeTag.Parse(ref this, provider)); case "color": case "c": return(OscColor.Parse(ref this, provider)); case "blob": case "b": case "data": case "d": return(ParseBlob(provider)); default: throw new Exception($@"Unknown object type '{name}'"); } }
void Awake() { _defaultColor = emptyInputImage.color; _timetag = new OscTimeTag(new System.DateTime(1900, 1, 1)); _midiMessage = new OscMidiMessage(); _sb = new StringBuilder(); }
private void DeconstructPacket(OscPacket oscPacket) { if (oscPacket is OscBundle) { OscBundle oscBundle = (OscBundle)oscPacket; oscTimeTagStack.Push(oscBundle.Timestamp); foreach (OscPacket bundleElement in oscBundle) { DeconstructPacket(bundleElement); } oscTimeTagStack.Pop(); } else if (oscPacket is OscMessage) { OscMessage oscMessage = (OscMessage)oscPacket; if (oscTimeTagStack.Count > 0) { OscTimeTag oscTimeTag = oscTimeTagStack.Peek(); textBoxBuffer.WriteLine(oscTimeTag.ToString() + " " + oscMessage.ToString()); } else { textBoxBuffer.WriteLine(oscMessage.ToString()); } receiveCounter.Increment(); } }
private bool SetFirstAndLastTimestamps(OscTimeTag?timetag) { if (timetag == null) { return(true); } session.SetFirstTimestamp(timetag); if (hasFirstTimestamp == true) { // if the time-tag is from the past then do not process it if (timetag.Value.Value < firstTimestamp.Value) { return(false); } lastTimestamp = timetag.Value; return(true); } lastTimestamp = timetag.Value; firstTimestamp = timetag.Value; hasFirstTimestamp = true; return(true); }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), Quaternion.W, Quaternion.X, Quaternion.Y, Quaternion.Z )); }
void Update() { // Create a messege. OscMessage message = new OscMessage(address); // Create a timetag. Default time is DateTime.Now. OscTimeTag timetag = new OscTimeTag(); // Make it 1 milisecond into the future. timetag.time = timetag.time.AddMilliseconds(1); // Two possible methods for sending timetags ... if (sendTimeTagWithBundle) { // Either create a bundle with the timetag, add the message and send. OscBundle bundle = new OscBundle(timetag); bundle.Add(message); oscOut.Send(bundle); } else { // Or add the timetag to message and send it. message.Add(timetag); oscOut.Send(message); } // Incoming bundles are unpacked automatically and are never exposed. // In the case where we send the timetag with a bundle and want to // access them through incoming messages, we can ask OscIn to add // the timetags from bundles to each of their contained messages. oscIn.addTimeTagsToBundledMessages = sendTimeTagWithBundle; // Update label. sendLabel.text = timetag.time + ":" + timetag.time.Millisecond; }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), Power, Percentage )); }
public bool Equals(OscTimeTag value) { if ((object)value == null) { return(false); } return(_oscNtp.Equals(value._oscNtp)); }
public void WriteTimeTag(ref OscTimeTag value) { CheckWriterState(WriterState.Arguments); Write(unchecked ((long)value.Value)); Flush(); }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), RotationMatrix.XX, RotationMatrix.XY, RotationMatrix.XZ, RotationMatrix.YX, RotationMatrix.YY, RotationMatrix.YZ, RotationMatrix.ZX, RotationMatrix.ZY, RotationMatrix.ZZ )); }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), GyroscopeAndAccelerometer, EnvironmentalSensor )); }
public void FromDataTimeTest() { DateTime datetime = new DateTime(632413223390120000, DateTimeKind.Utc); OscTimeTag expected = new OscTimeTag(14236589681638796952); OscTimeTag actual; actual = OscTimeTag.FromDataTime(datetime); Assert.IsTrue((expected.Value <= actual.Value + 1) && (expected.Value >= actual.Value - 1)); }
public void OscBundleConstructorTest() { OscTimeTag timestamp = new OscTimeTag(14236589681638796952); OscMessage[] messages = new OscMessage[] { UnitTestHelper.Message_Array_Ints(), UnitTestHelper.Message_Array_Ints() }; OscBundle target = new OscBundle(timestamp, messages); Assert.AreEqual(timestamp, target.Timestamp); UnitTestHelper.AreEqual(messages, target.ToArray()); }
public void ToDataTimeTest() { OscTimeTag target = new OscTimeTag(14236589681638796952); DateTime expected = new DateTime(632413223390120000, DateTimeKind.Utc); DateTime actual; actual = target.ToDataTime(); Assert.Equal(expected, actual); }
/// <summary> /// Set argument at specified index, expanding message capacity if necessary. /// </summary> public OscMessage Set(int index, OscTimeTag value) { int dataStartIndex = AdaptiveSet(index, OscArgInfo.timeTagInfo); if (!new EightByteOscData(value).TryWriteTo(_argData, ref dataStartIndex)) { Debug.Log(OscDebug.FailedWritingBytesWarning(this)); } return(this); }
public void OnReceiveTimeTag(OscTimeTag timetag) { timetagYearInputField.text = timetag.time.Year.ToString(); timetagMonthInputField.text = timetag.time.Month.ToString(); timetagDayInputField.text = timetag.time.Day.ToString(); timetagHourInputField.text = timetag.time.Hour.ToString(); timetagMinuteInputField.text = timetag.time.Minute.ToString(); timetagSecondInputField.text = timetag.time.Second.ToString(); timetagMillisecondInputField.text = timetag.time.Millisecond.ToString(); timetagImmediateIinputToggle.isOn = timetag.immediately; }
void Update() { // Create a timetag with the current time. OscTimeTag timetag = new OscTimeTag(System.DateTime.Now); // Make it 1 milisecond into the future. timetag.time = timetag.time.AddMilliseconds(1); // Send it off. _oscOut.Send(address, timetag); }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), Percentage, TimeToEmpty, Voltage, Current, IsChargerConnected )); }
public void TestTimeTagSerializer() { var stream = new MemoryStream(); var expected = new OscTimeTag(); SerializerFactory.TimeTagSerializer.Encode(stream, expected); var data = stream.ToArray(); var actual = SerializerFactory.TimeTagSerializer.Decode(data, 0, out _); actual.Should().Be(expected); }
public void OscTimeTagConstructorTest() { DateTime expected = new DateTime(632413223390120000, DateTimeKind.Utc); ulong value = 14236589681638796952; OscTimeTag target = new OscTimeTag(value); DateTime datetime = target.ToDataTime(); string valueString = datetime.ToString("dd/MM/yyyy HH:mm:ss") + " " + datetime.Millisecond; string expectedString = expected.ToString("dd/MM/yyyy HH:mm:ss") + " " + datetime.Millisecond; Assert.Equal(expectedString, valueString); // , "Date resolved to '{0}'", valueString); }
public void TestTimeTagSerializerEncodeImmediateValue() { var stream = new MemoryStream(); var timetag = new OscTimeTag(); var actual = SerializerFactory.TimeTagSerializer.Encode(stream, timetag); var data = stream.ToArray(); actual.Should().Be(8); data.Take(7).Should().BeEquivalentTo(Enumerable.Repeat(0, 7)); data.Last().Should().Be(1); }
public override bool Equals(object value) { if (value == null) { return(false); } OscTimeTag rhs = value as OscTimeTag; if (rhs == null) { return(false); } return(_oscNtp.Equals(rhs._oscNtp)); }
internal override string ToCsv(OscTimeTag firstTimestamp) { return(Helper.ToCsv( CreateTimestampString(firstTimestamp), Channel1, Channel2, Channel3, Channel4, Channel5, Channel6, Channel7, Channel8 )); }
public bool Equals( OscTimeTag value ) { if( (object) value == null ) return false; return _oscNtp.Equals(value._oscNtp); }
/// <summary> /// Constructor for creating a bundle with specified timetag. /// </summary> public OscBundle( OscTimeTag timeTag ) { _timeTag = timeTag; _packets = new List<OscPacket>(); }
public static new bool FromBytes( byte[] data, ref int pos, int end, out OscPacket packet ) { // Check header size int bundleByteSize = end - pos; if( bundleByteSize < headerByteSize ){ Debug.LogWarning( "[OSC io] OscBundle with invalid header was ignored." + Environment.NewLine ); packet = null; return false; } // Check prefix string prefixInput = Encoding.ASCII.GetString( data, pos, prefixByteSize ); if( prefixInput != prefix ){ Debug.LogWarning( "[OSC io] OscBundle with invalid header was ignored." + Environment.NewLine ); packet = null; return false; } pos += prefixByteSize + 1; // + 1 to ensure bytes multiple of 4 // Get timetag byte[] oscNtpBytes = new byte[OscTimeTag.byteSize]; Array.Copy( data, pos, oscNtpBytes, 0, OscTimeTag.byteSize ); OscTimeTag timeTag = new OscTimeTag( oscNtpBytes ); pos += OscTimeTag.byteSize; // Create and fill bundle packet = new OscBundle( timeTag ); OscBundle bundle = packet as OscBundle; while( pos < end ) { int length = BitConverter.ToInt32( data, 0 ); pos += 4; int packetEnd = pos + length; OscPacket subPacket; if( pos < end && OscPacket.FromBytes( data, ref pos, packetEnd, out subPacket ) ) bundle.Add( subPacket ); } return true; }
public void OnReceiveTimeTag( OscTimeTag timetag ) { timetagYearInputField.text = timetag.time.Year.ToString(); timetagMonthInputField.text = timetag.time.Month.ToString(); timetagDayInputField.text = timetag.time.Day.ToString(); timetagHourInputField.text = timetag.time.Hour.ToString(); timetagMinuteInputField.text = timetag.time.Minute.ToString(); timetagSecondInputField.text = timetag.time.Second.ToString(); timetagMillisecondInputField.text = timetag.time.Millisecond.ToString(); timetagImmediateIinputToggle.isOn = timetag.immediately; }
void Awake() { defaultColor = emptyInputImage.color; timetag = new OscTimeTag( new System.DateTime( 1900, 1, 1 ) ); }
public bool TryGet( int index, out OscTimeTag value ) { if( index >= _args.Count || index < 0 ){ value = new OscTimeTag( OscTimeTag.epochTime ); LogOscMessageArgBoundsWarning( index ); return false; } object a = _args[index]; if( !( a is OscTimeTag ) ){ value = new OscTimeTag( OscTimeTag.epochTime ); LogOscMessageArgTypeWarning( typeof( OscTimeTag ), index ); return false; } value = (OscTimeTag) a; return true; }
public override bool ToBytes( out byte[] bytes ) { bytes = null; // Estimate byte length assuming address is "/address1" and all arguments are floats. int estimatedByteLength = 9 + 1+_args.Count + _args.Count * 4; // TODO A good optimisation here would be to precompute the packet byte length and use an Array instead of a List. // Create temp buffer for collecting the content List<byte> byteList = new List<byte>( estimatedByteLength ); // Add address byteList.AddRange( StringToBytes( _address ) ); // Add type tag char[] typeTags = new char[1+_args.Count]; typeTags[0] = tagPrefix; for( int a=0; a<_args.Count; a++ ) { object o = _args[a]; if( o == null ){ typeTags[1+a] = tagNull; } else if( o is float ){ typeTags[1+a] = tagFloat; } else if( o is double ){ typeTags[1+a] = tagDouble; } else if( o is int ){ typeTags[1+a] = tagInt; } else if( o is long ){ typeTags[1+a] = tagLong; } else if( o is string ){ typeTags[1+a] = tagString; } else if( o is char ){ typeTags[1+a] = tagChar; } else if ( o is bool ){ typeTags[1+a] = ( (bool) o ) ? tagTrue : tagFalse; } else if( o is byte[] ){ typeTags[1+a] = tagBlob; } else if( o is Color32 || o is Color ){ typeTags[1+a] = tagColor; } else if( o is OscTimeTag || o is DateTime ){ typeTags[1+a] = tagTimetag; } else if( o is OscImpulse ){ typeTags[1+a] = tagImpulse; } else { Debug.LogWarning( "[OscMessage] ToBytes ignored. Argument type '" + a.GetType() + "' at index " + a + " is not supported." + Environment.NewLine ); return false; } } byteList.AddRange( StringToBytes( typeTags ) ); // Add arguments foreach( object arg in _args ) { byte[] argDat = null; // TODO when GetTypeCode() becomes available in Unity, use that. // http://stackoverflow.com/questions/17774255/what-is-the-fastest-way-to-check-a-type // For now, we order the types by what we anticipate to be the most used. if( arg is string ){ argDat = StringToBytes( (string) arg ); } else if( arg is float ){ argDat = BitConverter.GetBytes( (float) arg ); if( BitConverter.IsLittleEndian ) Reverse4Bytes( argDat ); } else if( arg is int ){ argDat = BitConverter.GetBytes( (int) arg ); if( BitConverter.IsLittleEndian ) Reverse4Bytes( argDat ); } else if( arg is Color32 ){ Color32 col = (Color32) arg; argDat = new byte[]{ col.r, col.g, col.b, col.a }; if( BitConverter.IsLittleEndian ) Reverse4Bytes( argDat ); } else if( arg is Color ){ Color32 col = (Color32) (Color) arg; argDat = new byte[]{ col.r, col.g, col.b, col.a }; if( BitConverter.IsLittleEndian ) Reverse4Bytes( argDat ); } else if( arg is long ){ argDat = BitConverter.GetBytes( (long) arg ); if( BitConverter.IsLittleEndian ) Reverse8Bytes( argDat ); } else if( arg is double ){ argDat = BitConverter.GetBytes( (double) arg ); if( BitConverter.IsLittleEndian ) Reverse8Bytes( argDat ); } else if( arg is byte[] ){ byte[] rawBlob = (byte[]) arg; int blobLength = 4 + rawBlob.Length; blobLength += 4 - ( blobLength % 4 ); // ensure multiple of 4 bytes argDat = new byte[blobLength]; byte[] lengthBytes = BitConverter.GetBytes( rawBlob.Length ); if( BitConverter.IsLittleEndian ) Reverse4Bytes( lengthBytes ); Array.Copy( lengthBytes, argDat, 4 ); Array.Copy( rawBlob, 0, argDat, 4, rawBlob.Length ); } else if( arg is OscTimeTag ){ argDat = ( (OscTimeTag) arg ).ToByteArray(); // OscTimeTag ensures big-endian byte oder internally. } else if( arg is DateTime ){ argDat = new OscTimeTag( (DateTime) arg ).ToByteArray(); // OscTimeTag ensures big-endian byte oder internally. } else if( arg is char ){ argDat = BitConverter.GetBytes( Convert.ToInt32( (char) arg ) ); if( BitConverter.IsLittleEndian ) Reverse4Bytes( argDat ); } else if( arg is bool ) { // This type has no content (it's state is represented in the OSC tag) } else if( arg is OscImpulse ){ // This type has no content } else if( arg == null ){ // This type has no content (it's state is represented in the OSC tag) } else { Debug.LogWarning( "[OscMessage] ToBytes failed. Unsupported argument type: " + arg.GetType() + Environment.NewLine ); bytes = null; return false; } if( argDat != null ) byteList.AddRange( argDat ); } bytes = byteList.ToArray(); return true; }
public void Add( OscTimeTag value ) { _args.Add( value ); }
public static bool FromBytes( byte[] data, ref int pos, out OscPacket packet ) { // Get address string address = StringFromBytes( data, ref pos ); if( address.Length == 0 || address[0] != addressPrefix ){ Debug.LogWarning( "[OscMessage] Ignoring message because address \"" + address + "\" is not starting with \"" + addressPrefix + "\"." + Environment.NewLine + ( "pos: " + pos + ", data: " + data.Length ) ); packet = null; return false; } // Get type tags char[] tags = StringFromBytes( data, ref pos ).ToCharArray(); int tagCount = tags.Length; // including prefix if( tags.Length == 0 || tags[0] != tagPrefix ){ Debug.LogWarning( "[OscMessage] Ignoring message because typetag is missing." + Environment.NewLine ); packet = null; return false; } // Construct message packet = new OscMessage( address, tagCount ); OscMessage message = packet as OscMessage; // Get and add arguments for( int i=1; i<tagCount; i++ ) { char tag = tags[i]; object value; switch( tag ) { case tagFloat: if( BitConverter.IsLittleEndian ) Array.Reverse( data, pos, 4 ); value = BitConverter.ToSingle( data, pos ); pos += 4; break; case tagDouble: if( BitConverter.IsLittleEndian ) Array.Reverse( data, pos, 8 ); value = BitConverter.ToDouble( data, pos ); pos += 8; break; case tagInt: if( BitConverter.IsLittleEndian ) Array.Reverse( data, pos, 4 ); value = BitConverter.ToInt32( data, pos ); pos += 4; break; case tagLong: if( BitConverter.IsLittleEndian ) Array.Reverse( data, pos, 8 ); value = BitConverter.ToInt64( data, pos ); pos += 8; break; case tagString: case tagSymbol: value = StringFromBytes( data, ref pos ); break; case tagChar: if( BitConverter.IsLittleEndian ) Array.Reverse( data, pos, 4 ); int charInt = BitConverter.ToInt32( data, pos ); value = Convert.ToChar( charInt ); break; case tagBlob: if( BitConverter.IsLittleEndian ) Array.Reverse( data, pos, 4 ); int length = BitConverter.ToInt32( data, pos ); pos += 4; byte[] blob = new byte[length]; Array.Copy( data, pos, blob, 0, length ); value = blob; pos += length; pos += 4 - ( pos % 4 ); break; case tagTimetag: byte[] oscNtpBytes = new byte[OscTimeTag.byteSize]; Array.Copy( data, pos, oscNtpBytes, 0, OscTimeTag.byteSize ); value = new OscTimeTag( oscNtpBytes ); // OscTimeTag ensures big-endin byte oder internally. pos += 8; break; case tagColor: if( BitConverter.IsLittleEndian ) Array.Reverse( data, pos, 4 ); value = new Color32( data[pos], data[pos+1], data[pos+2], data[pos+3] ); pos += 4; break; case tagTrue: value = true; break; case tagFalse: value = false; break; case tagNull: value = null; break; case tagImpulse: value = new OscImpulse(); break; default: Debug.LogWarning( "[OscMessage] FromBytes failed. Argument tag '" + tag + "' is not supported." + Environment.NewLine ); return false; } message.args.Add( value ); } return true; }
public static bool Equals( OscTimeTag lhs, OscTimeTag rhs ) { return lhs.Equals( rhs ); }