-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtmidi.go
More file actions
291 lines (242 loc) · 7.42 KB
/
rtmidi.go
File metadata and controls
291 lines (242 loc) · 7.42 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package abletonlink
/*
#cgo pkg-config: rtmidi
#include <rtmidi_c.h>
#include <stdlib.h>
// Forward declaration for Go callback
extern void rtmidi_callback(double timestamp, unsigned char* message, size_t messageSize, void* userData);
*/
import "C"
import (
"fmt"
"runtime"
"sync"
"unsafe"
)
// Global registry for MIDI callbacks to avoid CGO pointer issues
var (
midiCallbackRegistry = make(map[uintptr]func([]byte))
midiCallbackRegistryMu sync.RWMutex
nextMidiID uintptr = 1
)
// MidiIn represents an RtMidi input device
type MidiIn struct {
ptr C.RtMidiInPtr
id uintptr
}
// MidiOut represents an RtMidi output device
type MidiOut struct {
ptr C.RtMidiOutPtr
}
// NewMidiIn creates a new MIDI input
func NewMidiIn() (*MidiIn, error) {
ptr := C.rtmidi_in_create_default()
if ptr == nil {
return nil, fmt.Errorf("failed to create MIDI input")
}
midiCallbackRegistryMu.Lock()
id := nextMidiID
nextMidiID++
midiCallbackRegistryMu.Unlock()
in := &MidiIn{ptr: ptr, id: id}
runtime.SetFinalizer(in, (*MidiIn).Close)
return in, nil
}
// NewMidiOut creates a new MIDI output
func NewMidiOut() (*MidiOut, error) {
ptr := C.rtmidi_out_create_default()
if ptr == nil {
return nil, fmt.Errorf("failed to create MIDI output")
}
out := &MidiOut{ptr: ptr}
runtime.SetFinalizer(out, (*MidiOut).Close)
return out, nil
}
// OpenVirtualPort opens a virtual MIDI input port
func (in *MidiIn) OpenVirtualPort(name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.rtmidi_open_virtual_port(in.ptr, cname)
if !in.ptr.ok {
return fmt.Errorf("failed to open virtual input port '%s': %s", name, C.GoString(in.ptr.msg))
}
return nil
}
// OpenVirtualPort opens a virtual MIDI output port
func (out *MidiOut) OpenVirtualPort(name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.rtmidi_open_virtual_port(out.ptr, cname)
if !out.ptr.ok {
return fmt.Errorf("failed to open virtual output port '%s': %s", name, C.GoString(out.ptr.msg))
}
return nil
}
// SetCallback sets a callback function for incoming MIDI messages
func (in *MidiIn) SetCallback(callback func([]byte)) {
midiCallbackRegistryMu.Lock()
midiCallbackRegistry[in.id] = callback
midiCallbackRegistryMu.Unlock()
C.rtmidi_in_set_callback(in.ptr, (*[0]byte)(C.rtmidi_callback), unsafe.Pointer(in.id))
}
// IgnoreTypes configures which MIDI message types to ignore
// midiSysex: ignore system exclusive messages
// midiTime: ignore MIDI time messages (including clock)
// midiSense: ignore active sensing messages
func (in *MidiIn) IgnoreTypes(midiSysex, midiTime, midiSense bool) {
C.rtmidi_in_ignore_types(in.ptr, C.bool(midiSysex), C.bool(midiTime), C.bool(midiSense))
}
// OpenPort opens a physical MIDI input port by number
func (in *MidiIn) OpenPort(portNumber uint, portName string) error {
cname := C.CString(portName)
defer C.free(unsafe.Pointer(cname))
C.rtmidi_open_port(in.ptr, C.uint(portNumber), cname)
if !in.ptr.ok {
return fmt.Errorf("failed to open input port %d '%s': %s", portNumber, portName, C.GoString(in.ptr.msg))
}
return nil
}
// OpenPort opens a physical MIDI output port by number
func (out *MidiOut) OpenPort(portNumber uint, portName string) error {
cname := C.CString(portName)
defer C.free(unsafe.Pointer(cname))
C.rtmidi_open_port(out.ptr, C.uint(portNumber), cname)
if !out.ptr.ok {
return fmt.Errorf("failed to open output port %d '%s': %s", portNumber, portName, C.GoString(out.ptr.msg))
}
return nil
}
// GetPortCount returns the number of available MIDI input ports
func (in *MidiIn) GetPortCount() uint {
return uint(C.rtmidi_get_port_count(in.ptr))
}
// GetPortCount returns the number of available MIDI output ports
func (out *MidiOut) GetPortCount() uint {
return uint(C.rtmidi_get_port_count(out.ptr))
}
// GetPortName returns the name of a MIDI input port
func (in *MidiIn) GetPortName(portNumber uint) string {
// Check if the wrapper is valid
if !in.ptr.ok {
return fmt.Sprintf("Port %d (error)", portNumber)
}
// First call to get required buffer length
var bufLen C.int = 0
result := C.rtmidi_get_port_name(in.ptr, C.uint(portNumber), nil, &bufLen)
if result != 0 || bufLen <= 0 {
return fmt.Sprintf("Port %d (no name)", portNumber)
}
// Allocate buffer with extra space for null terminator
buf := (*C.char)(C.malloc(C.size_t(bufLen + 1)))
defer C.free(unsafe.Pointer(buf))
// Zero the buffer
for i := 0; i < int(bufLen+1); i++ {
*(*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(i))) = 0
}
result = C.rtmidi_get_port_name(in.ptr, C.uint(portNumber), buf, &bufLen)
if result == 0 {
name := C.GoString(buf)
if name != "" {
return name
}
}
return fmt.Sprintf("Port %d", portNumber)
}
// GetPortName returns the name of a MIDI output port
func (out *MidiOut) GetPortName(portNumber uint) string {
// Check if the wrapper is valid
if !out.ptr.ok {
return fmt.Sprintf("Port %d (error)", portNumber)
}
// First call to get required buffer length
var bufLen C.int = 0
result := C.rtmidi_get_port_name(out.ptr, C.uint(portNumber), nil, &bufLen)
if result != 0 || bufLen <= 0 {
return fmt.Sprintf("Port %d (no name)", portNumber)
}
// Allocate buffer with extra space for null terminator
buf := (*C.char)(C.malloc(C.size_t(bufLen + 1)))
defer C.free(unsafe.Pointer(buf))
// Zero the buffer
for i := 0; i < int(bufLen+1); i++ {
*(*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(i))) = 0
}
result = C.rtmidi_get_port_name(out.ptr, C.uint(portNumber), buf, &bufLen)
if result == 0 {
name := C.GoString(buf)
if name != "" {
return name
}
}
return fmt.Sprintf("Port %d", portNumber)
}
// SendMessage sends a MIDI message
func (out *MidiOut) SendMessage(data []byte) error {
if len(data) == 0 {
return nil
}
C.rtmidi_out_send_message(out.ptr, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(len(data)))
if !out.ptr.ok {
return fmt.Errorf("failed to send MIDI message: %s", C.GoString(out.ptr.msg))
}
return nil
}
// Close closes the MIDI input port
func (in *MidiIn) Close() {
if in.ptr != nil {
midiCallbackRegistryMu.Lock()
delete(midiCallbackRegistry, in.id)
midiCallbackRegistryMu.Unlock()
C.rtmidi_in_free(in.ptr)
in.ptr = nil
runtime.SetFinalizer(in, nil)
}
}
// Close closes the MIDI output port
func (out *MidiOut) Close() {
if out.ptr != nil {
C.rtmidi_out_free(out.ptr)
out.ptr = nil
runtime.SetFinalizer(out, nil)
}
}
// ListInputPorts returns a list of available MIDI input ports
func ListInputPorts() ([]string, error) {
in, err := NewMidiIn()
if err != nil {
return nil, err
}
defer in.Close()
count := in.GetPortCount()
ports := make([]string, count)
for i := uint(0); i < count; i++ {
ports[i] = in.GetPortName(i)
}
return ports, nil
}
// ListOutputPorts returns a list of available MIDI output ports
func ListOutputPorts() ([]string, error) {
out, err := NewMidiOut()
if err != nil {
return nil, err
}
defer out.Close()
count := out.GetPortCount()
ports := make([]string, count)
for i := uint(0); i < count; i++ {
ports[i] = out.GetPortName(i)
}
return ports, nil
}
//export rtmidi_callback
func rtmidi_callback(timestamp C.double, message *C.uchar, messageSize C.size_t, userData unsafe.Pointer) {
id := uintptr(userData)
midiCallbackRegistryMu.RLock()
callback, exists := midiCallbackRegistry[id]
midiCallbackRegistryMu.RUnlock()
if exists {
// Convert C array to Go slice
data := C.GoBytes(unsafe.Pointer(message), C.int(messageSize))
callback(data)
}
}