2022-09-17 03:11:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-10-04 20:56:38 +01:00
|
|
|
"bufio"
|
2022-09-18 05:39:42 +01:00
|
|
|
"flag"
|
2022-09-17 03:11:53 +01:00
|
|
|
"fmt"
|
2022-09-29 19:04:32 +01:00
|
|
|
"log"
|
2022-10-04 20:56:38 +01:00
|
|
|
"os"
|
2022-09-17 03:11:53 +01:00
|
|
|
"strings"
|
|
|
|
|
2022-09-22 22:55:08 +01:00
|
|
|
"github.com/onyx-and-iris/voicemeeter"
|
2022-09-17 03:11:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-10-04 20:56:38 +01:00
|
|
|
var (
|
|
|
|
kind string
|
|
|
|
delay int
|
|
|
|
verbose bool
|
|
|
|
interactive bool
|
|
|
|
)
|
|
|
|
|
2022-09-29 18:05:21 +01:00
|
|
|
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)")
|
2022-10-04 20:56:38 +01:00
|
|
|
flag.BoolVar(&interactive, "interactive", false, "toggle interactive mode")
|
|
|
|
flag.BoolVar(&interactive, "i", false, "toggle interactive mode (shorthand)")
|
2022-09-18 05:39:42 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-09-29 18:05:21 +01:00
|
|
|
vm, err := vmConnect(kind, delay)
|
2022-09-17 03:11:53 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer vm.Logout()
|
|
|
|
|
2022-10-04 20:56:38 +01:00
|
|
|
err = runCommands(vm, verbose, interactive)
|
2022-09-18 05:39:42 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2022-09-17 03:11:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 18:05:21 +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
|
|
|
|
}
|
2022-09-18 05:39:42 +01:00
|
|
|
|
2022-10-04 20:56:38 +01:00
|
|
|
func runCommands(vm *voicemeeter.Remote, verbose, interactive bool) error {
|
|
|
|
if interactive {
|
|
|
|
return interactiveMode(vm, verbose)
|
|
|
|
}
|
2022-09-18 05:39:42 +01:00
|
|
|
for _, arg := range flag.Args() {
|
2022-10-04 20:56:38 +01:00
|
|
|
err := parse(vm, arg, verbose)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func interactiveMode(vm *voicemeeter.Remote, verbose bool) error {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
input := scanner.Text()
|
|
|
|
if input == "q" || input == "quit" || input == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, cmd := range strings.Split(input, " ") {
|
|
|
|
err := parse(vm, cmd, verbose)
|
2022-09-18 05:39:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-04 20:56:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(vm *voicemeeter.Remote, cmd string, verbose bool) error {
|
|
|
|
if cmd[0] == '!' {
|
|
|
|
err := toggleCmd(vm, cmd[1:], verbose)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if strings.Contains(cmd, "=") {
|
|
|
|
err := setCmd(vm, cmd, verbose)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-29 18:05:21 +01:00
|
|
|
}
|
2022-09-18 05:39:42 +01:00
|
|
|
} else {
|
2022-10-04 20:56:38 +01:00
|
|
|
err := getCmd(vm, cmd, verbose)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-18 05:39:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-04 20:56:38 +01:00
|
|
|
|
|
|
|
func toggleCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
|
|
|
|
val, err := vm.GetFloat(cmd)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("unable to toggle %s", cmd)
|
|
|
|
return err
|
|
|
|
}
|
2022-10-05 13:12:12 +01:00
|
|
|
vm.SetFloat(cmd, 1-val)
|
2022-10-04 20:56:38 +01:00
|
|
|
if verbose {
|
|
|
|
fmt.Println("Toggling", cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
|
|
|
|
if verbose {
|
|
|
|
fmt.Println("Running command", cmd)
|
|
|
|
}
|
|
|
|
err := vm.SendText(cmd)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("unable to set %s", cmd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
|
|
|
|
valF, err := vm.GetFloat(cmd)
|
|
|
|
if err != nil {
|
|
|
|
valS, err := vm.GetString(cmd)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("unable to get %s", cmd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if verbose {
|
|
|
|
fmt.Println("Value of", cmd, "is:", valS)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if verbose {
|
|
|
|
fmt.Println("Value of", cmd, "is:", valF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|