-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
158 lines (140 loc) · 3.85 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
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"time"
log "github.com/schollz/logger"
"github.com/schollz/teoperator/src/convert"
"github.com/schollz/teoperator/src/download"
"github.com/schollz/teoperator/src/ffmpeg"
"github.com/schollz/teoperator/src/server"
cli "github.com/urfave/cli/v2"
)
func main() {
log.SetLevel("info")
if !ffmpeg.IsInstalled() {
fmt.Println("ffmpeg not installed")
fmt.Println("you can install it here: https://www.ffmpeg.org/download.html")
os.Exit(1)
}
drumUsage := `
create a drum patch from one-shot files (spliced at file endpoints):
teoperator drum kick.mp3 snare.wav hihat1.wav hihat2.wav
create a drum patch from one file (spliced at transients):
teoperator drum fullset.wav
create a drum patch from one file, spliced at even intervals:
teoperator drum --slices 16 fullset.wav
`
synthUsage := `
create a synth patch from a sample:
teoperator synth trumpet.wav
create a synth patch from a sample with known frequency:
teoperator synth --freq 220 trumpet_a2.wav`
app := &cli.App{
Name: "teoperator",
Usage: "create patches for the op-1 or op-z",
UsageText: drumUsage + synthUsage,
}
app.UseShortOptionHandling = true
app.Flags = []cli.Flag{
&cli.BoolFlag{Name: "debug"},
}
app.Action = func(c *cli.Context) error {
download.Duct = ""
download.ServerName = "http://localhost:8053"
go func() {
time.Sleep(100 * time.Millisecond)
openbrowser("http://localhost:8053")
}()
return server.Run(8053, download.ServerName)
}
app.Commands = []*cli.Command{
{
Name: "drum",
Usage: "create drum patch from file(s)",
UsageText: drumUsage,
Flags: []cli.Flag{
&cli.IntFlag{Name: "slices", Usage: "number of slices", Value: 0},
},
Action: func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel("debug")
}
fnames := make([]string, c.Args().Len())
for i, _ := range fnames {
fnames[i] = c.Args().Get(i)
}
if len(fnames) == 0 {
return fmt.Errorf("need to specify filename")
}
return convert.ToDrum(fnames, c.Int("slices"))
},
},
{
Name: "synth",
Usage: "create synth patch from file",
UsageText: synthUsage,
Flags: []cli.Flag{
&cli.Float64Flag{Name: "freq", Aliases: []string{"s"}, Value: 440, Usage: "base frequency"},
},
Action: func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel("debug")
}
fnames := make([]string, c.Args().Len())
for i, _ := range fnames {
fnames[i] = c.Args().Get(i)
}
if len(fnames) == 0 {
return fmt.Errorf("need to specify filename")
}
return convert.ToSynth(fnames[0], c.Float64("freq"))
},
},
{
Name: "server",
Usage: "run server interface",
UsageText: "",
Flags: []cli.Flag{
&cli.IntFlag{Name: "port", Value: 8053, Usage: "local port"},
&cli.StringFlag{Name: "name", Value: "http://localhost:8053", Usage: "name of server"},
&cli.StringFlag{Name: "duct", Value: "", Usage: "duct name for spanning multiple workers"},
&cli.BoolFlag{Name: "worker", Usage: "initiate a worker for the server"},
},
Action: func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel("debug")
}
download.Duct = c.String("duct")
download.ServerName = c.String("name")
if c.Bool("worker") {
return download.Work()
} else {
return server.Run(c.Int("port"), c.String("name"))
}
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Error(err)
}
}
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
fmt.Println(err)
}
}