From 6ed4e38daef2da123166ae9a5e2f652d9a865a17 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 7 Sep 2022 20:59:55 +0100 Subject: [PATCH] changes to error handling readme, changelog updated to reflect changes version bump --- CHANGELOG.md | 8 +++ README.md | 7 ++- base.go | 109 ++++++++++++++++++-------------------- button.go | 6 ++- device.go | 12 ++++- examples/obs/main.go | 7 ++- examples/observer/main.go | 7 ++- iremote.go | 44 ++++++++++++--- remote.go | 40 +++++++++++--- 9 files changed, 158 insertions(+), 82 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2560d32..7dde6f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass - [x] +## [1.4.0] - 2022-09-07 + +### Changed + +- changes to error handling. + - functions that wrap capi calls now return error types. + - higher level functions print error messages + ## [1.3.0] - 2022-08-22 ### Added diff --git a/README.md b/README.md index 35dc6b8..60d971a 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,12 @@ func main() { if err != nil { log.Fatal(err) } - defer vm.Logout() - vm.Login() + err = vm.Login() + if err != nil { + log.Fatal(err) + } + defer vm.Logout() vm.Strip[0].SetLabel("rode podmic") vm.Strip[0].SetMute(true) diff --git a/base.go b/base.go index 170152d..907aad9 100644 --- a/base.go +++ b/base.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "math" - "os" "strings" "syscall" "time" @@ -44,36 +43,36 @@ var ( // login logs into the API, // attempts to launch Voicemeeter if it's not running, // initializes dirty parameters. -func login(kindId string) { +func login(kindId string) error { res, _, _ := vmLogin.Call() if res == 1 { runVoicemeeter(kindId) time.Sleep(time.Second) } else if res != 0 { err := fmt.Errorf("VBVMR_Login returned %d", res) - fmt.Println(err) - os.Exit(1) + return err } fmt.Printf("Logged into Voicemeeter %s\n", kindId) for pdirty() || mdirty() { } + return nil } // logout logs out of the API, // delayed for 100ms to allow final operation to complete. -func logout(kindId string) { +func logout(kindId string) error { time.Sleep(100 * time.Millisecond) res, _, _ := vmLogout.Call() if res != 0 { err := fmt.Errorf("VBVMR_Logout returned %d", res) - fmt.Println(err) - os.Exit(1) + return err } fmt.Printf("Logged out of Voicemeeter %s\n", kindId) + return nil } // runVoicemeeter attempts to launch a Voicemeeter GUI of a kind. -func runVoicemeeter(kindId string) { +func runVoicemeeter(kindId string) error { vals := map[string]uint64{ "basic": 1, "banana": 2, @@ -82,25 +81,24 @@ func runVoicemeeter(kindId string) { res, _, _ := vmRunvm.Call(uintptr(vals[kindId])) if res != 0 { err := fmt.Errorf("VBVMR_RunVoicemeeter returned %d", res) - fmt.Println(err) - os.Exit(1) + return err } + return nil } // getVersion returns the version of Voicemeeter as a string -func getVersion() string { +func getVersion() (string, error) { var ver uint64 res, _, _ := vmGetvmVersion.Call(uintptr(unsafe.Pointer(&ver))) if res != 0 { err := fmt.Errorf("VBVMR_GetVoicemeeterVersion returned %d", res) - fmt.Println(err) - os.Exit(1) + return "", err } v1 := (ver & 0xFF000000) >> 24 v2 := (ver & 0x00FF0000) >> 16 v3 := (ver & 0x0000FF00) >> 8 v4 := ver & 0x000000FF - return fmt.Sprintf("%d.%d.%d.%d", v1, v2, v3, v4) + return fmt.Sprintf("%d.%d.%d.%d", v1, v2, v3, v4), nil } // pdirty returns true iff a parameter value has changed @@ -121,37 +119,38 @@ func ldirty(k *kind) bool { _levelCache.busLevelsBuff = make([]float32, 8*k.NumBus()) for i := 0; i < (2*k.PhysIn)+(8*k.VirtIn); i++ { - _levelCache.stripLevelsBuff[i] = float32(getLevel(_levelCache.stripMode, i)) + val, _ := getLevel(_levelCache.stripMode, i) + _levelCache.stripLevelsBuff[i] = val _levelCache.stripComp[i] = _levelCache.stripLevelsBuff[i] == _levelCache.stripLevels[i] } for i := 0; i < 8*k.NumBus(); i++ { - _levelCache.busLevelsBuff[i] = float32(getLevel(3, i)) + val, _ := getLevel(3, i) + _levelCache.busLevelsBuff[i] = val _levelCache.busComp[i] = _levelCache.busLevelsBuff[i] == _levelCache.busLevels[i] } return !(allTrue(_levelCache.stripComp, (2*k.PhysIn)+(8*k.VirtIn)) && allTrue(_levelCache.busComp, 8*k.NumBus())) } // getVMType returns the type of Voicemeeter, as a string -func getVMType() string { +func getVMType() (string, error) { var type_ uint64 res, _, _ := vmGetvmType.Call( uintptr(unsafe.Pointer(&type_)), ) if res != 0 { err := fmt.Errorf("VBVMR_GetVoicemeeterType returned %d", res) - fmt.Println(err) - os.Exit(1) + return "", err } vals := map[uint64]string{ 1: "basic", 2: "banana", 3: "potato", } - return vals[type_] + return vals[type_], nil } // getParameterFloat gets the value of a float parameter -func getParameterFloat(name string) float64 { +func getParameterFloat(name string) (float64, error) { var value float32 b := append([]byte(name), 0) res, _, _ := vmGetParamFloat.Call( @@ -160,14 +159,13 @@ func getParameterFloat(name string) float64 { ) if res != 0 { err := fmt.Errorf("VBVMR_GetParameterFloat returned %d", res) - fmt.Println(err) - os.Exit(1) + return 0, err } - return math.Round(float64(value)*10) / 10 + return math.Round(float64(value)*10) / 10, nil } // setParameterFloat sets the value of a float parameter -func setParameterFloat(name string, value float32) { +func setParameterFloat(name string, value float32) error { b1 := append([]byte(name), 0) b2 := math.Float32bits(value) res, _, _ := vmSetParamFloat.Call( @@ -176,13 +174,13 @@ func setParameterFloat(name string, value float32) { ) if res != 0 { err := fmt.Errorf("VBVMR_SetParameterFloat returned %d", res) - fmt.Println(err) - os.Exit(1) + return err } + return nil } // getParameterString gets the value of a string parameter -func getParameterString(name string) string { +func getParameterString(name string) (string, error) { b1 := append([]byte(name), 0) var b2 [512]byte res, _, _ := vmGetParamString.Call( @@ -191,15 +189,14 @@ func getParameterString(name string) string { ) if res != 0 { err := fmt.Errorf("VBVMR_GetParameterStringA returned %d", res) - fmt.Println(err) - os.Exit(1) + return "", err } str := bytes.Trim(b2[:], "\x00") - return string(str) + return string(str), nil } -// getParameterString sets the value of a string parameter -func setParameterString(name, value string) { +// setParameterString sets the value of a string parameter +func setParameterString(name, value string) error { b1 := append([]byte(name), 0) b2 := append([]byte(value), 0) res, _, _ := vmSetParamString.Call( @@ -208,26 +205,26 @@ func setParameterString(name, value string) { ) if res != 0 { err := fmt.Errorf("VBVMR_SetParameterStringA returned %d", res) - fmt.Println(err) - os.Exit(1) + return err } + return nil } // setParametersMulti sets multiple parameters with a script -func setParametersMulti(script string) { +func setParametersMulti(script string) error { b1 := append([]byte(script), 0) res, _, _ := vmSetParameters.Call( uintptr(unsafe.Pointer(&b1[0])), ) if res != 0 { err := fmt.Errorf("VBVMR_SetParameters returned %d", res) - fmt.Println(err) - os.Exit(1) + return err } + return nil } // getMacroStatus gets a macrobutton value -func getMacroStatus(id, mode int) float32 { +func getMacroStatus(id, mode int) (float32, error) { var state float32 res, _, _ := vmGetMacroStatus.Call( uintptr(id), @@ -236,14 +233,13 @@ func getMacroStatus(id, mode int) float32 { ) if res != 0 { err := fmt.Errorf("VBVMR_MacroButton_GetStatus returned %d", res) - fmt.Println(err) - os.Exit(1) + return 0, err } - return state + return state, nil } // setMacroStatus sets a macrobutton value -func setMacroStatus(id, state, mode int) { +func setMacroStatus(id, state, mode int) error { res, _, _ := vmSetMacroStatus.Call( uintptr(id), uintptr(state), @@ -251,9 +247,9 @@ func setMacroStatus(id, state, mode int) { ) if res != 0 { err := fmt.Errorf("VBVMR_MacroButton_SetStatus returned %d", res) - fmt.Println(err) - os.Exit(1) + return err } + return nil } // getNumDevices returns the number of hardware input/output devices @@ -268,7 +264,7 @@ func getNumDevices(dir string) uint64 { } // getDeviceDescription returns name, driver type and hwid for a given device -func getDeviceDescription(i int, dir string) (string, uint64, string) { +func getDeviceDescription(i int, dir string) (string, uint64, string, error) { var t_ uint64 var b1 [512]byte var b2 [512]byte @@ -281,8 +277,7 @@ func getDeviceDescription(i int, dir string) (string, uint64, string) { ) if res != 0 { err := fmt.Errorf("VBVMR_Input_GetDeviceDescA returned %d", res) - fmt.Println(err) - os.Exit(1) + return "", 0, "", err } } else { res, _, _ := vmGetDevDescOut.Call( @@ -293,17 +288,16 @@ func getDeviceDescription(i int, dir string) (string, uint64, string) { ) if res != 0 { err := fmt.Errorf("VBVMR_Output_GetDeviceDescA returned %d", res) - fmt.Println(err) - os.Exit(1) + return "", 0, "", err } } name := bytes.Trim(b1[:], "\x00") hwid := bytes.Trim(b2[:], "\x00") - return string(name), t_, string(hwid) + return string(name), t_, string(hwid), nil } // getLevel returns a single level value of type type_ for channel[i] -func getLevel(type_, i int) float32 { +func getLevel(type_, i int) (float32, error) { var val float32 res, _, _ := vmGetLevelFloat.Call( uintptr(type_), @@ -312,10 +306,9 @@ func getLevel(type_, i int) float32 { ) if res != 0 { err := fmt.Errorf("VBVMR_GetLevel returned %d", res) - fmt.Println(err) - os.Exit(1) + return 0, err } - return val + return val, nil } // getMidiMessage gets midi channel, pitch and velocity for a single midi input @@ -329,8 +322,10 @@ func getMidiMessage() bool { x := int(res) if x < 0 { err := fmt.Errorf("VBVMR_GetMidiMessage returned %d", res) - fmt.Println(err) - os.Exit(1) + if err != nil { + fmt.Println(err) + } + return false } msg := bytes.Trim(b1[:], "\x00") if len(msg) > 0 { diff --git a/button.go b/button.go index 080a891..f700b99 100644 --- a/button.go +++ b/button.go @@ -14,7 +14,11 @@ func newButton(i int) button { // getter returns the value of a macrobutton parameter func (m *button) getter(mode int) bool { - return getMacroStatus(m.index, mode) == 1 + val, err := getMacroStatus(m.index, mode) + if err != nil { + fmt.Println(err) + } + return val == 1 } // setter sets the value of a macrobutton parameter diff --git a/device.go b/device.go index 90a037d..6df0054 100644 --- a/device.go +++ b/device.go @@ -1,5 +1,7 @@ package voicemeeter +import "fmt" + type devDesc struct { Name, Type, Hwid string } @@ -22,7 +24,10 @@ func (d *device) Outs() int { } func (d *device) Input(i int) devDesc { - n, t_, id := getDeviceDescription(i, "in") + n, t_, id, err := getDeviceDescription(i, "in") + if err != nil { + fmt.Println(err) + } vals := map[uint64]string{ 1: "mme", 3: "wdm", @@ -33,7 +38,10 @@ func (d *device) Input(i int) devDesc { } func (d *device) Output(i int) devDesc { - n, t_, id := getDeviceDescription(i, "out") + n, t_, id, err := getDeviceDescription(i, "out") + if err != nil { + fmt.Println(err) + } vals := map[uint64]string{ 1: "mme", 3: "wdm", diff --git a/examples/obs/main.go b/examples/obs/main.go index 2d035df..c5601ad 100644 --- a/examples/obs/main.go +++ b/examples/obs/main.go @@ -43,9 +43,12 @@ func main() { if err != nil { log.Fatal(err) } - defer vm.Logout() - vm.Login() + err = vm.Login() + if err != nil { + log.Fatal(err) + } + defer vm.Logout() obs, err := goobs.New("localhost:4455", goobs.WithPassword("mystrongpass")) if err != nil { diff --git a/examples/observer/main.go b/examples/observer/main.go index 6fe1f58..a576654 100644 --- a/examples/observer/main.go +++ b/examples/observer/main.go @@ -48,9 +48,12 @@ func main() { if err != nil { log.Fatal(err) } - defer vm.Logout() - vm.Login() + err = vm.Login() + if err != nil { + log.Fatal(err) + } + defer vm.Logout() // enable level updates (disabled by default) vm.EventAdd("ldirty") diff --git a/iremote.go b/iremote.go index f69246c..7d91be1 100644 --- a/iremote.go +++ b/iremote.go @@ -19,7 +19,11 @@ func (ir *iRemote) identifier() string { // getter_bool returns the value of a boolean parameter func (ir *iRemote) getter_bool(p string) bool { param := fmt.Sprintf("%s.%s", ir.identifier(), p) - return getParameterFloat(param) == 1 + val, err := getParameterFloat(param) + if err != nil { + fmt.Println(err) + } + return val == 1 } // setter_bool sets the value of a boolean parameter @@ -31,41 +35,65 @@ func (ir *iRemote) setter_bool(p string, v bool) { } else { value = 0 } - setParameterFloat(param, float32(value)) + err := setParameterFloat(param, float32(value)) + if err != nil { + fmt.Println(err) + } } // getter_int returns the value of an int parameter p func (ir *iRemote) getter_int(p string) int { param := fmt.Sprintf("%s.%s", ir.identifier(), p) - return int(getParameterFloat(param)) + val, err := getParameterFloat(param) + if err != nil { + fmt.Println(err) + } + return int(val) } // setter_int sets the value v of an int parameter p func (ir *iRemote) setter_int(p string, v int) { param := fmt.Sprintf("%s.%s", ir.identifier(), p) - setParameterFloat(param, float32(v)) + err := setParameterFloat(param, float32(v)) + if err != nil { + fmt.Println(err) + } } // getter_float returns the value of an int parameter p func (ir *iRemote) getter_float(p string) float64 { param := fmt.Sprintf("%s.%s", ir.identifier(), p) - return getParameterFloat(param) + val, err := getParameterFloat(param) + if err != nil { + fmt.Println(err) + } + return val } // setter_float sets the value v of an int parameter p func (ir *iRemote) setter_float(p string, v float32) { param := fmt.Sprintf("%s.%s", ir.identifier(), p) - setParameterFloat(param, float32(v)) + err := setParameterFloat(param, float32(v)) + if err != nil { + fmt.Println(err) + } } // getter_string returns the value of a string parameter p func (ir *iRemote) getter_string(p string) string { param := fmt.Sprintf("%s.%s", ir.identifier(), p) - return getParameterString(param) + val, err := getParameterString(param) + if err != nil { + fmt.Println(err) + } + return val } // setter_string sets the value v of a string parameter p func (ir *iRemote) setter_string(p, v string) { param := fmt.Sprintf("%s.%s", ir.identifier(), p) - setParameterString(param, v) + err := setParameterString(param, v) + if err != nil { + fmt.Println(err) + } } diff --git a/remote.go b/remote.go index 8fbc826..4bd8ee9 100644 --- a/remote.go +++ b/remote.go @@ -26,26 +26,42 @@ func (r *Remote) String() string { // Login logs into the API // then it intializes the pooler -func (r *Remote) Login() { - login(r.Kind.Name) +func (r *Remote) Login() error { + err := login(r.Kind.Name) + if err != nil { + return err + } r.pooler = newPooler(r.Kind) + return nil } // Logout logs out of the API // it also terminates the pooler -func (r *Remote) Logout() { +func (r *Remote) Logout() error { r.pooler.run = false - logout(r.Kind.Name) + err := logout(r.Kind.Name) + if err != nil { + return err + } + return nil } // Type returns the type of Voicemeeter (basic, banana, potato) func (r *Remote) Type() string { - return getVMType() + val, err := getVMType() + if err != nil { + fmt.Println(err) + } + return val } // Version returns the version of Voicemeeter as a string func (r *Remote) Version() string { - return getVersion() + val, err := getVersion() + if err != nil { + fmt.Println(err) + } + return val } // Pdirty returns true iff a parameter value has changed @@ -60,7 +76,11 @@ func (r *Remote) Mdirty() bool { // Gets a float parameter value func (r *Remote) GetFloat(name string) float64 { - return getParameterFloat(name) + val, err := getParameterFloat(name) + if err != nil { + fmt.Println(err) + } + return val } // Sets a float paramter value @@ -70,7 +90,11 @@ func (r *Remote) SetFloat(name string, value float32) { // Gets a string parameter value func (r *Remote) GetString(name string) string { - return getParameterString(name) + val, err := getParameterString(name) + if err != nil { + fmt.Println(err) + } + return val } // Sets a string paramter value