From c05bf500eea4e986398d6f62df51432809c1c1a9 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Sun, 18 Sep 2022 05:38:22 +0100 Subject: [PATCH] obs example now reads conn info from toml. vm, obs connect logic moved into separate functions. README added for obs example. --- examples/obs/README.md | 19 ++++++++++++++ examples/obs/go.mod | 1 + examples/obs/go.sum | 2 ++ examples/obs/main.go | 58 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 examples/obs/README.md diff --git a/examples/obs/README.md b/examples/obs/README.md new file mode 100644 index 0000000..4a9201b --- /dev/null +++ b/examples/obs/README.md @@ -0,0 +1,19 @@ +## Requirements + +- [OBS Studio](https://obsproject.com/) +- [GOOBS Go Client for Websocket v5](https://github.com/andreykaipov/goobs) + +## About + +A simple demonstration showing how to sync OBS scene switches to Voicemeeter states. The script assumes you have connection info saved in +a config file named `config.toml` placed next to `main.go`. It also assumes you have scenes named `START` `BRB` `END` and `LIVE`. + +A valid `config.toml` file might look like this: + +```toml +[connection] +Host="localhost" +Port=4455 +Password="mystrongpass" + +``` diff --git a/examples/obs/go.mod b/examples/obs/go.mod index f0ce769..98b3c36 100644 --- a/examples/obs/go.mod +++ b/examples/obs/go.mod @@ -3,6 +3,7 @@ module main go 1.18 require ( + github.com/BurntSushi/toml v1.2.0 github.com/andreykaipov/goobs v0.10.0 github.com/onyx-and-iris/voicemeeter-api-go v1.7.0 ) diff --git a/examples/obs/go.sum b/examples/obs/go.sum index f74d6d3..c01051a 100644 --- a/examples/obs/go.sum +++ b/examples/obs/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= +github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/andreykaipov/goobs v0.10.0 h1:wa4CxbYu/NqwUmx5E4/baDqYRYEmfHwg2T23RAg3jlU= github.com/andreykaipov/goobs v0.10.0/go.mod h1:EqG73Uu/4npyhXIWWszgRelNkEeIz+d0slUT6NKWYs4= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= diff --git a/examples/obs/main.go b/examples/obs/main.go index 808f8a1..6f593ee 100644 --- a/examples/obs/main.go +++ b/examples/obs/main.go @@ -4,11 +4,14 @@ import ( "fmt" "log" "time" + "os" "github.com/onyx-and-iris/voicemeeter-api-go" "github.com/andreykaipov/goobs" "github.com/andreykaipov/goobs/api/events" + + "github.com/BurntSushi/toml" ) func onStart(vm *voicemeeter.Remote) { @@ -39,18 +42,13 @@ func onEnd(vm *voicemeeter.Remote) { } func main() { - vm, err := voicemeeter.NewRemote("potato", 0) - if err != nil { - log.Fatal(err) - } - - err = vm.Login() + vm, err := vmConnect() if err != nil { log.Fatal(err) } defer vm.Logout() - obs, err := goobs.New("localhost:4455", goobs.WithPassword("mystrongpass")) + obs, err := obsConnect() if err != nil { log.Fatal(err) } @@ -79,3 +77,49 @@ func main() { time.Sleep(30 * time.Second) } + +func vmConnect() (*voicemeeter.Remote, error) { + vm, err := voicemeeter.NewRemote("potato", 0) + if err != nil { + return nil, err + } + + err = vm.Login() + if err != nil { + return nil, err + } + + return vm, nil +} + +func obsConnect() (*goobs.Client, error) { + type ( + connection struct { + Host string + Port int + Password string + } + + config struct { + Connection map[string]connection + } + ) + + f := "config.toml" + if _, err := os.Stat(f); err != nil { + f = "./config.toml" + } + + var c config + _, err := toml.DecodeFile(f, &c.Connection) + if err != nil { + return nil, err + } + conn := c.Connection["connection"] + + obs, err := goobs.New(fmt.Sprintf("%s:%d", conn.Host, conn.Port), goobs.WithPassword(conn.Password)) + if err != nil { + return nil, err + } + return obs, nil +}