// Creates an array of bytes from a single OscMessage. Used internally. private static int OscMessageToPacket(OscMessageSend oscM, byte[] packet, int start, int length) { int index = start; index = InsertString(oscM.address, packet, index, length); if (oscM.values.Count > 0) { StringBuilder tag = new StringBuilder(); tag.Append(","); int tagIndex = index; index += PadSize(2 + oscM.values.Count); foreach (object o in oscM.values) { if (o is int) { int i = (int)o; tag.Append("i"); packet [index++] = (byte)((i >> 24) & 0xFF); packet [index++] = (byte)((i >> 16) & 0xFF); packet [index++] = (byte)((i >> 8) & 0xFF); packet [index++] = (byte)((i) & 0xFF); } else { if (o is float) { float f = (float)o; tag.Append("f"); byte[] buffer = new byte[4]; MemoryStream ms = new MemoryStream(buffer); BinaryWriter bw = new BinaryWriter(ms); bw.Write(f); packet [index++] = buffer [3]; packet [index++] = buffer [2]; packet [index++] = buffer [1]; packet [index++] = buffer [0]; } else { if (o is string) { tag.Append("s"); index = InsertString(o.ToString(), packet, index, length); } else { tag.Append("?"); } } } } InsertString(tag.ToString(), packet, tagIndex, length); } return(index); }
//Sends Osc message public static void SendData(string data) { OscMessageSend oscMessage = OscMessageSend.StringToOscMessage(data); byte[] packet = new byte[1000]; int length = OscMessageSend.OscMessageToPacket(oscMessage, packet, 1000); //Debug.Log(packet); // /Debug.Log(length); client.SendPacket(packet, length); }
public static OscMessageSend StringToOscMessage(string message) { OscMessageSend oM = new OscMessageSend(); // Debug.Log(oM.values.Count); // Console.WriteLine("Splitting " + message); string[] ss = message.Split(new char[] { ' ' }); IEnumerator sE = ss.GetEnumerator(); if (sE.MoveNext()) { oM.address = (string)sE.Current; } while (sE.MoveNext()) { string s = (string)sE.Current; // Console.WriteLine(" <" + s + ">"); if (s.StartsWith("\"")) { StringBuilder quoted = new StringBuilder(); bool looped = false; if (s.Length > 1) { quoted.Append(s.Substring(1)); } else { looped = true; } while (sE.MoveNext()) { string a = (string)sE.Current; // Console.WriteLine(" q:<" + a + ">"); if (looped) { quoted.Append(" "); } if (a.EndsWith("\"")) { quoted.Append(a.Substring(0, a.Length - 1)); break; } else { if (a.Length == 0) { quoted.Append(" "); } else { quoted.Append(a); } } looped = true; } oM.values.Add(quoted.ToString()); } else { if (s.Length > 0) { try { int i = int.Parse(s); Console.WriteLine(" i:" + i); oM.values.Add(i); } catch { try { float f = float.Parse(s); Console.WriteLine(" f:" + f); oM.values.Add(f); } catch { Console.WriteLine(" s:" + s); oM.values.Add(s); } } } } } return(oM); }
public static int OscMessageToPacket(OscMessageSend oscM, byte[] packet, int length) { return(OscMessageToPacket(oscM, packet, 0, length)); }