voicemeeter/examples/vm-cli/main.go

97 lines
1.9 KiB
Go
Raw Normal View History

2022-09-17 03:11:53 +01:00
package main
import (
"flag"
2022-09-17 03:11:53 +01:00
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/onyx-and-iris/voicemeeter"
2022-09-17 03:11:53 +01:00
)
var (
kind string
delay int
verbose bool
)
2022-09-17 03:11:53 +01:00
func main() {
flag.StringVar(&kind, "kind", "banana", "kind of voicemeeter")
flag.StringVar(&kind, "k", "banana", "kind of voicemeeter (shorthand)")
flag.IntVar(&delay, "delay", 20, "delay between commands")
flag.IntVar(&delay, "d", 20, "delay between commands (shorthand)")
flag.BoolVar(&verbose, "verbose", false, "toggle console output")
flag.BoolVar(&verbose, "v", false, "toggle console output (shorthand)")
flag.Parse()
vm, err := vmConnect(kind, delay)
2022-09-17 03:11:53 +01:00
if err != nil {
log.Fatal(err)
}
defer vm.Logout()
err = runCommands(vm, verbose)
if err != nil {
fmt.Println(err)
2022-09-17 03:11:53 +01:00
}
}
func vmConnect(kind string, delay int) (*voicemeeter.Remote, error) {
vm, err := voicemeeter.NewRemote(kind, delay)
2022-09-17 03:11:53 +01:00
if err != nil {
return nil, err
}
err = vm.Login()
if err != nil {
return nil, err
}
return vm, nil
}
func runCommands(vm *voicemeeter.Remote, verbose bool) error {
for _, arg := range flag.Args() {
if arg[0] == '!' {
val, err := vm.GetFloat(arg[1:])
if err != nil {
err = fmt.Errorf("unable to toggle %s", arg[1:])
return err
}
vm.SetFloat(arg[1:], 1-val)
if verbose {
fmt.Println("Toggling", arg[1:])
}
} else {
if strings.Contains(arg, "=") {
if verbose {
fmt.Println("Running command", arg)
}
err := vm.SendText(arg)
if err != nil {
err = fmt.Errorf("unable to set %s", arg)
return err
}
} else {
2022-09-21 07:30:14 +01:00
valF, err := vm.GetFloat(arg)
if err != nil {
2022-09-21 07:30:14 +01:00
valS, err := vm.GetString(arg)
if err != nil {
err = fmt.Errorf("unable to get %s", arg)
return err
}
if verbose {
fmt.Println("Value of", arg, "is:", valS)
}
} else {
if verbose {
fmt.Println("Value of", arg, "is:", valF)
}
}
}
}
}
return nil
}