-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
169 lines (159 loc) · 3.72 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/Wulfheart/brahms/score"
"github.com/Wulfheart/brahms/viz"
"github.com/mingrammer/cfmt"
"github.com/urfave/cli/v2"
"os"
"path/filepath"
"strings"
)
func main() {
defer func() {
if err := recover(); err != nil {
cfmt.Errorln(err)
fmt.Println("")
}
}()
app := &cli.App{
Name: "brahms",
Usage: "visualize your music",
Version: "v0.2.1",
HideHelpCommand: true,
UsageText: "brahms -i path/to/midi [global options]",
OnUsageError: func(context *cli.Context, err error, isSubcommand bool) error {
cfmt.Errorln(err)
cfmt.Infoln("Here again the help text. I hope it helps.🧙\n")
cli.ShowAppHelp(context)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "in",
Aliases: []string{"i"},
Usage: "`infile`",
Required: true,
},
&cli.StringFlag{
Name: "out",
Aliases: []string{"o"},
Usage: "`outfile` ",
},
&cli.StringFlag{
Name: "colors",
Aliases: []string{"c"},
Usage: "case-insensitive comma-separated string of `hex`colors, e.g. '#40e0d0,#ff8c00,#ff0080'",
},
&cli.Float64Flag{
Name: "width",
Value: 800,
Hidden: true,
},
&cli.Float64Flag{
Name: "height",
Value: 800,
Hidden: true,
},
&cli.Float64Flag{
Name: "max-r",
Value: 350,
Hidden: true,
},
&cli.Float64Flag{
Name: "min-r",
Value: 0,
Hidden: true,
},
&cli.Float64Flag{
Name: "max-r-node",
Value: 15,
Hidden: true,
},
&cli.Float64Flag{
Name: "fill-opacity",
Value: 0.5,
// Hidden: true,
},
&cli.StringFlag{
Name: "midi2csv",
Value: "midicsv",
Usage: "Provide a custom location for your midicsv command",
// Hidden: true,
},
},
Action: func(c *cli.Context) error {
input, err := existingFilepath(c.String("in"), false)
if err != nil {
return err
}
sc := score.Read(input, score.ReadMidi, c.String("midi2csv"))
buf := new(bytes.Buffer)
// TODO: Make error handling for wrong formatted strings
if strings.Contains(c.String("colors"), " ") {
return fmt.Errorf("don't use a space in your hexcolor string")
}
colors, err := viz.MakePalette(len(sc), strings.Split(c.String("colors"), ",")...)
colorsEmpty := c.String("colors") == ""
viz.RenderCircle(buf, sc, viz.CircleConfig{
MaxR: c.Float64("max-r"),
MinR: c.Float64("min-r"),
MaxRNode: c.Float64("max-r-node"),
Width: c.Float64("width"),
Height: c.Float64("height"),
FillOpacity: c.Float64("fill-opacity"),
Colors: colors,
Filled: !colorsEmpty,
Stroke: colorsEmpty,
})
// No output defined
if c.String("out") == "" {
f := bufio.NewWriter(os.Stdout)
defer f.Flush()
_, err = f.Write(buf.Bytes())
if err != nil {
return err
}
} else {
out, err := existingFilepath(c.String("out"), true)
if err != nil {
return err
}
f, err := os.Create(out)
if err != nil {
return err
}
_, err = f.Write(buf.Bytes())
if err != nil {
return err
}
cfmt.Successf("Midi rendered as svg at %s\n", out)
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
func existingFilepath(p string, createIfNotExists bool) (path string, err error) {
path, err = filepath.Abs(p)
if err != nil {
return "", err
}
// File does not exist
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) && createIfNotExists{
_, e := os.Create(path)
if e != nil {
return "", e
}
} else {
return "", fmt.Errorf("it seems like there was an issue with the file %s:\n%s", path, err)
}
}
return path, nil
}