Created
June 22, 2014 22:51
-
-
Save adsr/ecab341fef25f0fcf906 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"errors" | |
"github.com/adsr/portmidi" | |
) | |
func main() { | |
if err := run(); err != nil { | |
fmt.Printf("Error: %v\n", err) | |
os.Exit(1) | |
} | |
} | |
func run() error { | |
// Init portmidi | |
if err := portmidi.Initialize(); err != nil { | |
return err | |
} | |
defer portmidi.Terminate() | |
// Get device name from argv | |
if deviceName, err := getDeviceNameFromArgs(); err != nil { | |
printUsage() | |
return err | |
} else if stream, err := getStreamByName(deviceName); err != nil { | |
return err | |
} else { | |
// Do sysex test | |
defer stream.Close() | |
for j := 0; j < 128; j++ { | |
sysexBytes := []byte{0xf0, 0x7f, 0x00, 0x00, 0x00, byte(j), 0xf7} | |
stream.WriteSysEx(0, string(sysexBytes)) // This segfaults without the patch applied | |
fmt.Printf("Sent sysex %v to device '%s'\n", sysexBytes, deviceName) | |
} | |
} | |
return nil | |
} | |
func getDeviceNameFromArgs() (string, error) { | |
if len(os.Args) < 2 { | |
return "", errors.New("Missing midi_out_device argument") | |
} | |
return os.Args[1], nil | |
} | |
func printUsage() { | |
fmt.Printf("Usage: %s <midi_out_device>\n\n", os.Args[0]) | |
fmt.Printf("Possible values for midi_out_device:\n") | |
for i := 0; i < portmidi.CountDevices(); i++ { | |
info := portmidi.GetDeviceInfo(portmidi.DeviceId(i)) | |
if !info.IsOutputAvailable || info.IsOpened { | |
continue | |
} | |
fmt.Printf(" %s\n", info.Name) | |
} | |
} | |
func getStreamByName(deviceName string) (*portmidi.Stream, error) { | |
for i := 0; i < portmidi.CountDevices(); i++ { | |
deviceId := portmidi.DeviceId(i) | |
info := portmidi.GetDeviceInfo(deviceId) | |
if info.Name != deviceName || !info.IsOutputAvailable || info.IsOpened { | |
continue | |
} | |
if stream, err := portmidi.NewOutputStream(deviceId, 1024, 0); err != nil { | |
return nil, err | |
} else { | |
return stream, nil | |
} | |
} | |
return nil, errors.New("Device not found or already opened") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment