Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions launchpad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ func TestLaunchpad(t *testing.T) {
// For the launchpad MIDI reference, see https://d19ulaff0trnck.cloudfront.net/sites/default/files/novation/downloads/4080/launchpad-programmers-reference.pdf
// t.SkipNow()

device := &Device{
Name: "hw:0,0,0",
QueueSize: 0,
}
device := &Device{ID: "hw:0"}
if err := device.Open(); err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 19 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package midi

import (
"fmt"
"testing"
)

func TestDevices(t *testing.T) {
devices, err := Devices()
if err != nil {
t.Fatal(err)
}
for i, d := range devices {
if d == nil {
continue
}
fmt.Printf("device %d: %#v\n", i, *d)
}
}
1 change: 0 additions & 1 deletion midi_linux.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// +build cgo
#include <assert.h>
#include <errno.h>
#include <stddef.h>

#include <alsa/asoundlib.h>
Expand Down
177 changes: 174 additions & 3 deletions midi_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// package for talking to midi devices in Go.
package midi

// #include <alsa/asoundlib.h>
// #include <stddef.h>
// #include <stdlib.h>
// #include "midi_linux.h"
Expand All @@ -11,25 +12,41 @@ import "C"
import (
"fmt"
"os"
"unsafe"

"github.com/pkg/errors"
)

// Packet is a MIDI packet.
type Packet [3]byte

// DeviceType is a flag that says if a device is an input, an output, or duplex.
type DeviceType int

const (
DeviceInput DeviceType = iota
DeviceOutput
DeviceDuplex
)

// Device provides an interface for MIDI devices.
type Device struct {
ID string
Name string
QueueSize int
Type DeviceType

conn C.Midi
buf []byte
}

// Open opens a MIDI device.
func (d *Device) Open() error {
result := C.Midi_open(C.CString(d.Name))
var (
id = C.CString(d.ID)
result = C.Midi_open(id)
)
defer C.free(unsafe.Pointer(id))

if result.error != 0 {
return errors.Errorf("error opening device %d", result.error)
}
Expand Down Expand Up @@ -75,6 +92,160 @@ func (d *Device) Read(buf []byte) (int, error) {

// Write writes data to a MIDI device.
func (d *Device) Write(buf []byte) (int, error) {
n, err := C.Midi_write(d.conn, C.CString(string(buf)), C.size_t(len(buf)))
cs := C.CString(string(buf))
n, err := C.Midi_write(d.conn, cs, C.size_t(len(buf)))
C.free(unsafe.Pointer(cs))
return int(n), err
}

type Stream struct {
Name string
}

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

if rc := C.snd_card_next(&card); rc != 0 {
return nil, alsaMidiError(rc)
}
if card < 0 {
return nil, errors.New("no sound card found")
}
devices := []*Device{}

for {
cardDevices, err := getCardDevices(card)
if err != nil {
return nil, err
}
if rc := C.snd_card_next(&card); rc != 0 {
return nil, alsaMidiError(rc)
}
if card < 0 {
break
}
devices = append(devices, cardDevices...)
}
return devices, nil
}

func getCardDevices(card C.int) ([]*Device, error) {
var (
ctl *C.snd_ctl_t
name = C.CString(fmt.Sprintf("hw:%d", card))
)
defer C.free(unsafe.Pointer(name))

if rc := C.snd_ctl_open(&ctl, name, 0); rc != 0 {
return nil, alsaMidiError(rc)
}
var (
cardDevices = []*Device{}
device C.int = -1
)
for {
if rc := C.snd_ctl_rawmidi_next_device(ctl, &device); rc != 0 {
return nil, alsaMidiError(rc)
}
if device < 0 {
break
}
deviceDevices, err := getDeviceDevices(ctl, card, C.uint(device))
if err != nil {
return nil, err
}
cardDevices = append(cardDevices, deviceDevices...)
}
if rc := C.snd_ctl_close(ctl); rc != 0 {
return nil, alsaMidiError(rc)
}
return cardDevices, nil
}

func getDeviceDevices(ctl *C.snd_ctl_t, card C.int, device C.uint) ([]*Device, error) {
var info *C.snd_rawmidi_info_t
C.snd_rawmidi_info_malloc(&info)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs to be freed somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

C.snd_rawmidi_info_set_device(info, device)

defer C.snd_rawmidi_info_free(info)

// Get inputs.
var subsIn C.uint
C.snd_rawmidi_info_set_stream(info, C.SND_RAWMIDI_STREAM_INPUT)
if rc := C.snd_ctl_rawmidi_info(ctl, info); rc != 0 {
return nil, alsaMidiError(rc)
}
subsIn = C.snd_rawmidi_info_get_subdevices_count(info)

// Get outputs.
var subsOut C.uint
C.snd_rawmidi_info_set_stream(info, C.SND_RAWMIDI_STREAM_OUTPUT)
if rc := C.snd_ctl_rawmidi_info(ctl, info); rc != 0 {
return nil, alsaMidiError(rc)
}
subsOut = C.snd_rawmidi_info_get_subdevices_count(info)

// List subdevices.
var subs C.uint
if subsIn > subsOut {
subs = subsIn
} else {
subs = subsOut
}
if subs == C.uint(0) {
return nil, errors.New("no streams")
}
devices := []*Device{}

for sub := C.uint(0); sub < subs; sub++ {
subDevice, err := getSubdevice(ctl, info, card, device, sub, subsIn, subsOut)
if err != nil {
return nil, err
}
devices = append(devices, subDevice)
}
return devices, nil
}

func getSubdevice(ctl *C.snd_ctl_t, info *C.snd_rawmidi_info_t, card C.int, device, sub, subsIn, subsOut C.uint) (*Device, error) {
if sub < subsIn {
C.snd_rawmidi_info_set_stream(info, C.SND_RAWMIDI_STREAM_INPUT)
} else {
C.snd_rawmidi_info_set_stream(info, C.SND_RAWMIDI_STREAM_OUTPUT)
}
C.snd_rawmidi_info_set_subdevice(info, sub)
if rc := C.snd_ctl_rawmidi_info(ctl, info); rc != 0 {
return nil, alsaMidiError(rc)
}
var (
name = C.GoString(C.snd_rawmidi_info_get_name(info))
subName = C.GoString(C.snd_rawmidi_info_get_subdevice_name(info))
)
var dt DeviceType
if sub < subsIn && sub >= subsOut {
dt = DeviceInput
} else if sub >= subsIn && sub < subsOut {
dt = DeviceOutput
} else {
dt = DeviceDuplex
}
if sub == 0 && len(subName) > 0 && subName[0] == 0 {
return &Device{
ID: fmt.Sprintf("hw:%d,%d", card, device),
Name: name,
Type: dt,
}, nil
}
return &Device{
ID: fmt.Sprintf("hw:%d,%d,%d", card, device, sub),
Name: subName,
Type: dt,
}, nil
}

func alsaMidiError(code C.int) error {
if code == C.int(0) {
return nil
}
return errors.New(C.GoString(C.snd_strerror(code)))
}