Exemplo n.º 1
0
// Serializes a message to OSC wire format.
// If a unsupported type is encountered, serialization
// will be stopped.
func Serialize(m *Message) ([]byte, error) {
	msg := C.lo_message_new()
	for i, param := range m.Params {
		switch x := param.(type) {
		case int32:
			C.lo_message_add_int32(msg, C.int32_t(x))
		case int64:
			C.lo_message_add_int64(msg, C.int64_t(x))
		case float32:
			C.lo_message_add_float(msg, C.float(x))
		case float64:
			C.lo_message_add_double(msg, C.double(x))
		case string:
			cstr := C.CString(x)
			defer C.free(unsafe.Pointer(cstr))
			C.lo_message_add_string(msg, cstr)
		default:
			return nil, fmt.Errorf("Parameter %d has invalid type", i)
		}
	}

	cpath := C.CString(m.Path)
	defer C.free(unsafe.Pointer(cpath))
	var size int

	tmpbuffer := C.lo_message_serialise(msg, cpath, unsafe.Pointer(nil), (*C.size_t)(unsafe.Pointer((&size))))
	defer C.free(unsafe.Pointer(tmpbuffer))
	longbuffer := C.GoBytes(tmpbuffer, C.int(size))

	shortbuffer := make([]byte, size)
	copy(shortbuffer, longbuffer)
	return shortbuffer, nil
}
Exemplo n.º 2
0
Arquivo: osc.go Projeto: mkb218/go-osc
func (this Message) build_lo_message() (m C.lo_message, ret int) {
	m = C.lo_message_new()
	if m == nil {
		ret = -1
		return
	}
	for _, arg := range this {
		//        fmt.Printf("%d %s %v\n", n, arg.GetType(), arg.GetValue())
		switch arg.GetType() {
		case Int32:
			ret = int(C.lo_message_add_int32(m, C.int32_t(arg.GetValue().(Int32Type))))
		case Float:
			ret = int(C.lo_message_add_float(m, C.float(arg.GetValue().(FloatType))))
		case BlobCode:
			a, i := arg.GetValue().(Blob)
			if !i {
				ret = -2
				break
			}

			b := C.lo_blob_new(C.int32_t(len(a)), unsafe.Pointer(&(a[0])))
			if b == nil {
				ret = -3
				break
			}
			defer C.lo_blob_free(C.lo_blob(b))
			ret = int(C.lo_message_add_blob(m, b))
		case Int64:
			ret = int(C.lo_message_add_int64(m, C.int64_t(arg.GetValue().(Int64Type))))
		case TimetagCode:
			ret = int(C.lo_message_add_timetag(m, C.lo_timetag{arg.GetValue().(Timetag).Sec, arg.GetValue().(Timetag).Frac}))
		case Double:
			ret = int(C.lo_message_add_double(m, C.double(arg.GetValue().(DoubleType))))
		case Symbol:
			s := C.CString(string(arg.GetValue().(SymbolType)))
			if s == nil {
				ret = -4
				break
			}
			defer C.free(unsafe.Pointer(s))
			ret = int(C.lo_message_add_symbol(m, s))
		case String:
			s := C.CString(string(arg.GetValue().(StringType)))
			if s == nil {
				ret = -4
				break
			}
			defer C.free(unsafe.Pointer(s))
			ret = int(C.lo_message_add_string(m, s))
		case Char:
			ret = int(C.lo_message_add_char(m, arg.GetValue().(C.char)))
		case MidiMsgCode:
			mm := arg.GetValue().(MidiMsg)
			ret = int(C.lo_message_add_midi(m, (*C.uint8_t)(unsafe.Pointer(&mm))))
		case True:
			ret = int(C.lo_message_add_true(m))
		case False:
			ret = int(C.lo_message_add_false(m))
		case Nil:
			ret = int(C.lo_message_add_nil(m))
		case Infinitum:
			ret = int(C.lo_message_add_infinitum(m))
		default:
			ret = -5
		}
		if ret < 0 {
			C.lo_message_free(m)
			m = nil
			return
		}
	}
	return
}