-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (114 loc) · 2.59 KB
/
main.go
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
package main
import (
"encoding/csv"
"flag"
"github.com/mmm444/alsaxtor/alsa"
"io"
"log"
"os"
)
type Connector struct {
seq *alsa.Seq
possiblePairs [][]string
}
func NewConnector(seq *alsa.Seq) *Connector {
return &Connector{seq, make([][]string, 0)}
}
func (c *Connector) AddPair(from, to string) {
c.possiblePairs = append(c.possiblePairs, []string{from, to})
}
func (c *Connector) ReadPairsCsv(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
r := csv.NewReader(file)
r.FieldsPerRecord = 2
for {
pair, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
c.AddPair(pair[0], pair[1])
}
return nil
}
func (c *Connector) ConnectPossible() {
// wheee
for _, c1 := range c.seq.Clients() {
for _, p1 := range c1.Ports() {
if !p1.HasConnOut() {
for _, poss := range c.possiblePairs {
if p1.Name() == poss[0] {
for _, c2 := range c.seq.Clients() {
for _, p2 := range c2.Ports() {
if p2.Name() == poss[1] && !p2.HasConnIn() {
if c.seq.Connect(&p1, &p2) == nil {
log.Printf("Connected %v to %v", p1.Name(), p2.Name())
} else {
log.Printf("Cannot connect %v to %v", p1.Name(), p2.Name())
}
}
}
}
}
}
}
}
}
}
var (
info = flag.Bool("i", false, "print ALSA sequencer state")
csvFile = flag.String("f", "", "read to be connected port pairs from CSV")
keepRunning = flag.Bool("r", false, "run and connect ports forever")
verbose = flag.Bool("v", false, "be verbose (log received events)")
)
func main() {
flag.Parse()
if flag.NFlag() == 0 {
flag.Usage()
return
}
seq, err := alsa.OpenSeq("alsaxtor")
if err != nil {
log.Fatal(err)
}
defer seq.Close()
if *info {
seq.Dump()
} else if *csvFile != "" {
connector := NewConnector(seq)
err = connector.ReadPairsCsv(*csvFile)
if err != nil {
log.Fatal("Cannot read csv file: ", err)
}
connector.ConnectPossible()
if *keepRunning {
port, err := seq.CreatePort("Listener", alsa.PORT_CAP_WRITE|alsa.PORT_CAP_SUBS_WRITE)
if err != nil {
log.Fatal(err)
}
err = seq.Connect(seq.Port(0, 1), port)
if err != nil {
log.Fatal(err)
}
ch := make(chan alsa.SeqEventType)
go seq.EventLoop(ch)
for t := range ch {
if *verbose {
log.Println("Received", t)
}
if t == alsa.SND_SEQ_EVENT_PORT_START || t == alsa.SND_SEQ_EVENT_CLIENT_EXIT {
seq.Refresh()
connector.ConnectPossible()
}
}
}
} else {
log.Fatal("No to be connected port pairs specified.")
}
}