Skip to content

Commit

Permalink
Added list devices option
Browse files Browse the repository at this point in the history
  • Loading branch information
ncirocco committed Aug 22, 2020
1 parent 7c5837d commit 36f30cd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
29 changes: 22 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"

Expand All @@ -13,20 +14,34 @@ import (
)

func main() {
if len(os.Args) == 0 {
log.Fatal("Missing file or playlist argument.")
if len(os.Args[1]) == 2 && os.Args[1] == "-l" {
err := midiplayer.ListDevices()
if err != nil {
log.Fatal(err)
}

return
}

p, err := midiplayer.NewPlayer()
if len(os.Args) != 3 {
log.Fatal("Missing DeviceID and file or playlist argument.")
}

deviceID, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal(err)
panic(err)
}

p, err := midiplayer.NewPlayer(int64(deviceID))
if err != nil {
log.Fatal("Invalid deviceID. ")
}
defer p.Close()

registerInterrupSignal(p)

if strings.HasSuffix(strings.ToLower(os.Args[1]), ".mid") {
midi, err := midiparser.ParseMidiFile(os.Args[1])
if strings.HasSuffix(strings.ToLower(os.Args[2]), ".mid") {
midi, err := midiparser.ParseMidiFile(os.Args[2])
if err != nil {
log.Fatal(err)
}
Expand All @@ -35,7 +50,7 @@ func main() {
return
}

err = playPlaylist(os.Args[1], p)
err = playPlaylist(os.Args[2], p)
if err != nil {
log.Fatal(err)
}
Expand Down
27 changes: 25 additions & 2 deletions midiplayer/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type Player struct {
}

// NewPlayer returns an instance of a Player
func NewPlayer() (*Player, error) {
func NewPlayer(deviceID int64) (*Player, error) {
portmidi.Initialize()
out, err := portmidi.NewOutputStream(2, 1024, 0)
out, err := portmidi.NewOutputStream(portmidi.DeviceID(deviceID), 1024, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -137,3 +137,26 @@ func muteAllNotes(out *portmidi.Stream) {
)
}
}

// ListDevices lists all the available output MIDI devices and their IDs
func ListDevices() error {
err := portmidi.Initialize()
if err != nil {
return err
}

defer portmidi.Terminate()

numDevices := portmidi.CountDevices()

fmt.Println("Available output MIDI devices")
for i := 0; i < numDevices; i++ {
info := portmidi.Info(portmidi.DeviceID(i))

if info.IsOutputAvailable {
fmt.Println("ID:", i, "Name:", info.Name)
}
}

return nil
}

0 comments on commit 36f30cd

Please sign in to comment.