Skip to content

Commit 01cb19b

Browse files
committed
return []*Device
1 parent 421d991 commit 01cb19b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

list_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ func TestDevices(t *testing.T) {
1010
if err != nil {
1111
t.Fatal(err)
1212
}
13-
fmt.Printf("devices %#v\n", devices)
13+
for i, d := range devices {
14+
if d == nil {
15+
continue
16+
}
17+
fmt.Printf("device %d: %#v\n", i, *d)
18+
}
1419
}

midi_linux.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type Stream struct {
101101
Name string
102102
}
103103

104-
func Devices() ([]Device, error) {
104+
func Devices() ([]*Device, error) {
105105
var card C.int = -1
106106

107107
if rc := C.snd_card_next(&card); rc != 0 {
@@ -110,7 +110,7 @@ func Devices() ([]Device, error) {
110110
if card < 0 {
111111
return nil, errors.New("no sound card found")
112112
}
113-
devices := []Device{}
113+
devices := []*Device{}
114114

115115
for {
116116
cardDevices, err := getCardDevices(card)
@@ -128,7 +128,7 @@ func Devices() ([]Device, error) {
128128
return devices, nil
129129
}
130130

131-
func getCardDevices(card C.int) ([]Device, error) {
131+
func getCardDevices(card C.int) ([]*Device, error) {
132132
var (
133133
ctl *C.snd_ctl_t
134134
name = C.CString(fmt.Sprintf("hw:%d", card))
@@ -139,7 +139,7 @@ func getCardDevices(card C.int) ([]Device, error) {
139139
C.free(unsafe.Pointer(name))
140140

141141
var (
142-
cardDevices = []Device{}
142+
cardDevices = []*Device{}
143143
device C.int = -1
144144
)
145145
for {
@@ -161,7 +161,7 @@ func getCardDevices(card C.int) ([]Device, error) {
161161
return cardDevices, nil
162162
}
163163

164-
func getDeviceDevices(ctl *C.snd_ctl_t, card C.int, device C.uint) ([]Device, error) {
164+
func getDeviceDevices(ctl *C.snd_ctl_t, card C.int, device C.uint) ([]*Device, error) {
165165
var info *C.snd_rawmidi_info_t
166166
C.snd_rawmidi_info_malloc(&info)
167167
C.snd_rawmidi_info_set_device(info, device)
@@ -192,7 +192,7 @@ func getDeviceDevices(ctl *C.snd_ctl_t, card C.int, device C.uint) ([]Device, er
192192
if subs == C.uint(0) {
193193
return nil, errors.New("no streams")
194194
}
195-
devices := []Device{}
195+
devices := []*Device{}
196196

197197
for sub := C.uint(0); sub < subs; sub++ {
198198
subDevice, err := getSubdevice(ctl, info, card, device, sub, subsIn, subsOut)
@@ -204,15 +204,15 @@ func getDeviceDevices(ctl *C.snd_ctl_t, card C.int, device C.uint) ([]Device, er
204204
return devices, nil
205205
}
206206

207-
func getSubdevice(ctl *C.snd_ctl_t, info *C.snd_rawmidi_info_t, card C.int, device, sub, subsIn, subsOut C.uint) (Device, error) {
207+
func getSubdevice(ctl *C.snd_ctl_t, info *C.snd_rawmidi_info_t, card C.int, device, sub, subsIn, subsOut C.uint) (*Device, error) {
208208
if sub < subsIn {
209209
C.snd_rawmidi_info_set_stream(info, C.SND_RAWMIDI_STREAM_INPUT)
210210
} else {
211211
C.snd_rawmidi_info_set_stream(info, C.SND_RAWMIDI_STREAM_OUTPUT)
212212
}
213213
C.snd_rawmidi_info_set_subdevice(info, sub)
214214
if rc := C.snd_ctl_rawmidi_info(ctl, info); rc != 0 {
215-
return Device{}, alsaMidiError(rc)
215+
return nil, alsaMidiError(rc)
216216
}
217217
var (
218218
name = C.GoString(C.snd_rawmidi_info_get_name(info))
@@ -227,13 +227,13 @@ func getSubdevice(ctl *C.snd_ctl_t, info *C.snd_rawmidi_info_t, card C.int, devi
227227
dt = DeviceDuplex
228228
}
229229
if sub == 0 && len(subName) > 0 && subName[0] == 0 {
230-
return Device{
230+
return &Device{
231231
ID: fmt.Sprintf("hw:%d,%d", card, device),
232232
Name: name,
233233
Type: dt,
234234
}, nil
235235
}
236-
return Device{
236+
return &Device{
237237
ID: fmt.Sprintf("hw:%d,%d,%d", card, device, sub),
238238
Name: subName,
239239
Type: dt,

0 commit comments

Comments
 (0)