// ReadSysExBytes reads 4*max sysex bytes from the input stream. func (s *Stream) ReadSysExBytes(max int) ([]byte, error) { if max > maxEventBufferSize { return nil, errMaxBuffer } if max < minEventBufferSize { return nil, errMinBuffer } buffer := make([]C.PmEvent, max) numEvents := C.Pm_Read(unsafe.Pointer(s.pmStream), &buffer[0], C.int32_t(max)) msg := make([]byte, 4*numEvents) for i := 0; i < int(numEvents); i++ { msg[4*i+0] = byte(buffer[i].message & 0xFF) msg[4*i+1] = byte((buffer[i].message >> 8) & 0xFF) msg[4*i+2] = byte((buffer[i].message >> 16) & 0xFF) msg[4*i+3] = byte((buffer[i].message >> 24) & 0xFF) } return msg, nil }
func (s SystemPort) readEvent() (event Event, err error) { if s.IsInputPort { err = errors.New("Can only write, not read from input SystemPort.") return Event{}, err } var buffer C.PmEvent // Only read one event at a time. eventsRead := int(C.Pm_Read(s.stream, &buffer, C.int32_t(1))) if eventsRead > 0 { status := int(buffer.message) & 0xFF event.Channel = int(status & 0x0F) event.Command = int(status & 0xF0) event.Data1 = int((buffer.message >> 8) & 0xFF) event.Data2 = int((buffer.message >> 16) & 0xFF) return event, nil } return Event{}, nil // Nothing to read. }
// Reads from the input stream, the max number events to be read are // determined by max. func (s *Stream) Read(max int) (events []Event, err error) { if max > maxEventBufferSize { return nil, errMaxBuffer } if max < minEventBufferSize { return nil, errMinBuffer } buffer := make([]C.PmEvent, max) numEvents := C.Pm_Read(unsafe.Pointer(s.pmStream), &buffer[0], C.int32_t(max)) events = make([]Event, numEvents) for i := 0; i < int(numEvents); i++ { events[i] = Event{ Timestamp: Timestamp(buffer[i].timestamp), Status: int64(buffer[i].message) & 0xFF, Data1: (int64(buffer[i].message) >> 8) & 0xFF, Data2: (int64(buffer[i].message) >> 16) & 0xFF, } } return }