forked from youpy/go-coremidi
/
packet.go
44 lines (33 loc) · 996 Bytes
/
packet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package coremidi
/*
#cgo LDFLAGS: -framework CoreMIDI
#include <CoreMIDI/CoreMIDI.h>
*/
import "C"
import "errors"
import "fmt"
import "unsafe"
type Packet struct {
packetList C.MIDIPacketList
}
func NewPacket(p []byte) Packet {
var packetList C.MIDIPacketList
var data = (*C.Byte)(unsafe.Pointer(&p[0]))
packet := C.MIDIPacketListInit(&packetList)
packet = C.MIDIPacketListAdd(&packetList, 1024, packet, 0, C.ByteCount(len(p)), data)
return Packet{packetList}
}
func (packet Packet) Send(port *OutputPort, destination *Destination) (err error) {
osStatus := C.MIDISend(port.port, destination.endpoint, &packet.packetList)
if osStatus != C.noErr {
err = errors.New(fmt.Sprintf("%d: failed to send MIDI", int(osStatus)))
}
return
}
func (packet Packet) Received(source *Source) (err error) {
osStatus := C.MIDIReceived(source.endpoint, &packet.packetList)
if osStatus != C.noErr {
err = errors.New(fmt.Sprintf("%d: failed to transmit MIDI", int(osStatus)))
}
return
}