changes to error handling

readme, changelog updated to reflect changes

version bump
This commit is contained in:
onyx-and-iris 2022-09-07 20:59:55 +01:00
parent 505b5969a2
commit 6ed4e38dae
9 changed files with 158 additions and 82 deletions

View File

@ -11,6 +11,14 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
- [x] - [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 ## [1.3.0] - 2022-08-22
### Added ### Added

View File

@ -53,9 +53,12 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) 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].SetLabel("rode podmic")
vm.Strip[0].SetMute(true) vm.Strip[0].SetMute(true)

109
base.go
View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"math" "math"
"os"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -44,36 +43,36 @@ var (
// login logs into the API, // login logs into the API,
// attempts to launch Voicemeeter if it's not running, // attempts to launch Voicemeeter if it's not running,
// initializes dirty parameters. // initializes dirty parameters.
func login(kindId string) { func login(kindId string) error {
res, _, _ := vmLogin.Call() res, _, _ := vmLogin.Call()
if res == 1 { if res == 1 {
runVoicemeeter(kindId) runVoicemeeter(kindId)
time.Sleep(time.Second) time.Sleep(time.Second)
} else if res != 0 { } else if res != 0 {
err := fmt.Errorf("VBVMR_Login returned %d", res) err := fmt.Errorf("VBVMR_Login returned %d", res)
fmt.Println(err) return err
os.Exit(1)
} }
fmt.Printf("Logged into Voicemeeter %s\n", kindId) fmt.Printf("Logged into Voicemeeter %s\n", kindId)
for pdirty() || mdirty() { for pdirty() || mdirty() {
} }
return nil
} }
// logout logs out of the API, // logout logs out of the API,
// delayed for 100ms to allow final operation to complete. // delayed for 100ms to allow final operation to complete.
func logout(kindId string) { func logout(kindId string) error {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
res, _, _ := vmLogout.Call() res, _, _ := vmLogout.Call()
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_Logout returned %d", res) err := fmt.Errorf("VBVMR_Logout returned %d", res)
fmt.Println(err) return err
os.Exit(1)
} }
fmt.Printf("Logged out of Voicemeeter %s\n", kindId) fmt.Printf("Logged out of Voicemeeter %s\n", kindId)
return nil
} }
// runVoicemeeter attempts to launch a Voicemeeter GUI of a kind. // runVoicemeeter attempts to launch a Voicemeeter GUI of a kind.
func runVoicemeeter(kindId string) { func runVoicemeeter(kindId string) error {
vals := map[string]uint64{ vals := map[string]uint64{
"basic": 1, "basic": 1,
"banana": 2, "banana": 2,
@ -82,25 +81,24 @@ func runVoicemeeter(kindId string) {
res, _, _ := vmRunvm.Call(uintptr(vals[kindId])) res, _, _ := vmRunvm.Call(uintptr(vals[kindId]))
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_RunVoicemeeter returned %d", res) err := fmt.Errorf("VBVMR_RunVoicemeeter returned %d", res)
fmt.Println(err) return err
os.Exit(1)
} }
return nil
} }
// getVersion returns the version of Voicemeeter as a string // getVersion returns the version of Voicemeeter as a string
func getVersion() string { func getVersion() (string, error) {
var ver uint64 var ver uint64
res, _, _ := vmGetvmVersion.Call(uintptr(unsafe.Pointer(&ver))) res, _, _ := vmGetvmVersion.Call(uintptr(unsafe.Pointer(&ver)))
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_GetVoicemeeterVersion returned %d", res) err := fmt.Errorf("VBVMR_GetVoicemeeterVersion returned %d", res)
fmt.Println(err) return "", err
os.Exit(1)
} }
v1 := (ver & 0xFF000000) >> 24 v1 := (ver & 0xFF000000) >> 24
v2 := (ver & 0x00FF0000) >> 16 v2 := (ver & 0x00FF0000) >> 16
v3 := (ver & 0x0000FF00) >> 8 v3 := (ver & 0x0000FF00) >> 8
v4 := ver & 0x000000FF 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 // 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()) _levelCache.busLevelsBuff = make([]float32, 8*k.NumBus())
for i := 0; i < (2*k.PhysIn)+(8*k.VirtIn); i++ { 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] _levelCache.stripComp[i] = _levelCache.stripLevelsBuff[i] == _levelCache.stripLevels[i]
} }
for i := 0; i < 8*k.NumBus(); 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] _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())) 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 // getVMType returns the type of Voicemeeter, as a string
func getVMType() string { func getVMType() (string, error) {
var type_ uint64 var type_ uint64
res, _, _ := vmGetvmType.Call( res, _, _ := vmGetvmType.Call(
uintptr(unsafe.Pointer(&type_)), uintptr(unsafe.Pointer(&type_)),
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_GetVoicemeeterType returned %d", res) err := fmt.Errorf("VBVMR_GetVoicemeeterType returned %d", res)
fmt.Println(err) return "", err
os.Exit(1)
} }
vals := map[uint64]string{ vals := map[uint64]string{
1: "basic", 1: "basic",
2: "banana", 2: "banana",
3: "potato", 3: "potato",
} }
return vals[type_] return vals[type_], nil
} }
// getParameterFloat gets the value of a float parameter // getParameterFloat gets the value of a float parameter
func getParameterFloat(name string) float64 { func getParameterFloat(name string) (float64, error) {
var value float32 var value float32
b := append([]byte(name), 0) b := append([]byte(name), 0)
res, _, _ := vmGetParamFloat.Call( res, _, _ := vmGetParamFloat.Call(
@ -160,14 +159,13 @@ func getParameterFloat(name string) float64 {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_GetParameterFloat returned %d", res) err := fmt.Errorf("VBVMR_GetParameterFloat returned %d", res)
fmt.Println(err) return 0, err
os.Exit(1)
} }
return math.Round(float64(value)*10) / 10 return math.Round(float64(value)*10) / 10, nil
} }
// setParameterFloat sets the value of a float parameter // 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) b1 := append([]byte(name), 0)
b2 := math.Float32bits(value) b2 := math.Float32bits(value)
res, _, _ := vmSetParamFloat.Call( res, _, _ := vmSetParamFloat.Call(
@ -176,13 +174,13 @@ func setParameterFloat(name string, value float32) {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_SetParameterFloat returned %d", res) err := fmt.Errorf("VBVMR_SetParameterFloat returned %d", res)
fmt.Println(err) return err
os.Exit(1)
} }
return nil
} }
// getParameterString gets the value of a string parameter // getParameterString gets the value of a string parameter
func getParameterString(name string) string { func getParameterString(name string) (string, error) {
b1 := append([]byte(name), 0) b1 := append([]byte(name), 0)
var b2 [512]byte var b2 [512]byte
res, _, _ := vmGetParamString.Call( res, _, _ := vmGetParamString.Call(
@ -191,15 +189,14 @@ func getParameterString(name string) string {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_GetParameterStringA returned %d", res) err := fmt.Errorf("VBVMR_GetParameterStringA returned %d", res)
fmt.Println(err) return "", err
os.Exit(1)
} }
str := bytes.Trim(b2[:], "\x00") str := bytes.Trim(b2[:], "\x00")
return string(str) return string(str), nil
} }
// getParameterString sets the value of a string parameter // setParameterString sets the value of a string parameter
func setParameterString(name, value string) { func setParameterString(name, value string) error {
b1 := append([]byte(name), 0) b1 := append([]byte(name), 0)
b2 := append([]byte(value), 0) b2 := append([]byte(value), 0)
res, _, _ := vmSetParamString.Call( res, _, _ := vmSetParamString.Call(
@ -208,26 +205,26 @@ func setParameterString(name, value string) {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_SetParameterStringA returned %d", res) err := fmt.Errorf("VBVMR_SetParameterStringA returned %d", res)
fmt.Println(err) return err
os.Exit(1)
} }
return nil
} }
// setParametersMulti sets multiple parameters with a script // setParametersMulti sets multiple parameters with a script
func setParametersMulti(script string) { func setParametersMulti(script string) error {
b1 := append([]byte(script), 0) b1 := append([]byte(script), 0)
res, _, _ := vmSetParameters.Call( res, _, _ := vmSetParameters.Call(
uintptr(unsafe.Pointer(&b1[0])), uintptr(unsafe.Pointer(&b1[0])),
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_SetParameters returned %d", res) err := fmt.Errorf("VBVMR_SetParameters returned %d", res)
fmt.Println(err) return err
os.Exit(1)
} }
return nil
} }
// getMacroStatus gets a macrobutton value // getMacroStatus gets a macrobutton value
func getMacroStatus(id, mode int) float32 { func getMacroStatus(id, mode int) (float32, error) {
var state float32 var state float32
res, _, _ := vmGetMacroStatus.Call( res, _, _ := vmGetMacroStatus.Call(
uintptr(id), uintptr(id),
@ -236,14 +233,13 @@ func getMacroStatus(id, mode int) float32 {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_MacroButton_GetStatus returned %d", res) err := fmt.Errorf("VBVMR_MacroButton_GetStatus returned %d", res)
fmt.Println(err) return 0, err
os.Exit(1)
} }
return state return state, nil
} }
// setMacroStatus sets a macrobutton value // setMacroStatus sets a macrobutton value
func setMacroStatus(id, state, mode int) { func setMacroStatus(id, state, mode int) error {
res, _, _ := vmSetMacroStatus.Call( res, _, _ := vmSetMacroStatus.Call(
uintptr(id), uintptr(id),
uintptr(state), uintptr(state),
@ -251,9 +247,9 @@ func setMacroStatus(id, state, mode int) {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_MacroButton_SetStatus returned %d", res) err := fmt.Errorf("VBVMR_MacroButton_SetStatus returned %d", res)
fmt.Println(err) return err
os.Exit(1)
} }
return nil
} }
// getNumDevices returns the number of hardware input/output devices // 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 // 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 t_ uint64
var b1 [512]byte var b1 [512]byte
var b2 [512]byte var b2 [512]byte
@ -281,8 +277,7 @@ func getDeviceDescription(i int, dir string) (string, uint64, string) {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_Input_GetDeviceDescA returned %d", res) err := fmt.Errorf("VBVMR_Input_GetDeviceDescA returned %d", res)
fmt.Println(err) return "", 0, "", err
os.Exit(1)
} }
} else { } else {
res, _, _ := vmGetDevDescOut.Call( res, _, _ := vmGetDevDescOut.Call(
@ -293,17 +288,16 @@ func getDeviceDescription(i int, dir string) (string, uint64, string) {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_Output_GetDeviceDescA returned %d", res) err := fmt.Errorf("VBVMR_Output_GetDeviceDescA returned %d", res)
fmt.Println(err) return "", 0, "", err
os.Exit(1)
} }
} }
name := bytes.Trim(b1[:], "\x00") name := bytes.Trim(b1[:], "\x00")
hwid := bytes.Trim(b2[:], "\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] // 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 var val float32
res, _, _ := vmGetLevelFloat.Call( res, _, _ := vmGetLevelFloat.Call(
uintptr(type_), uintptr(type_),
@ -312,10 +306,9 @@ func getLevel(type_, i int) float32 {
) )
if res != 0 { if res != 0 {
err := fmt.Errorf("VBVMR_GetLevel returned %d", res) err := fmt.Errorf("VBVMR_GetLevel returned %d", res)
fmt.Println(err) return 0, err
os.Exit(1)
} }
return val return val, nil
} }
// getMidiMessage gets midi channel, pitch and velocity for a single midi input // getMidiMessage gets midi channel, pitch and velocity for a single midi input
@ -329,8 +322,10 @@ func getMidiMessage() bool {
x := int(res) x := int(res)
if x < 0 { if x < 0 {
err := fmt.Errorf("VBVMR_GetMidiMessage returned %d", res) err := fmt.Errorf("VBVMR_GetMidiMessage returned %d", res)
fmt.Println(err) if err != nil {
os.Exit(1) fmt.Println(err)
}
return false
} }
msg := bytes.Trim(b1[:], "\x00") msg := bytes.Trim(b1[:], "\x00")
if len(msg) > 0 { if len(msg) > 0 {

View File

@ -14,7 +14,11 @@ func newButton(i int) button {
// getter returns the value of a macrobutton parameter // getter returns the value of a macrobutton parameter
func (m *button) getter(mode int) bool { 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 // setter sets the value of a macrobutton parameter

View File

@ -1,5 +1,7 @@
package voicemeeter package voicemeeter
import "fmt"
type devDesc struct { type devDesc struct {
Name, Type, Hwid string Name, Type, Hwid string
} }
@ -22,7 +24,10 @@ func (d *device) Outs() int {
} }
func (d *device) Input(i int) devDesc { 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{ vals := map[uint64]string{
1: "mme", 1: "mme",
3: "wdm", 3: "wdm",
@ -33,7 +38,10 @@ func (d *device) Input(i int) devDesc {
} }
func (d *device) Output(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{ vals := map[uint64]string{
1: "mme", 1: "mme",
3: "wdm", 3: "wdm",

View File

@ -43,9 +43,12 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) 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")) obs, err := goobs.New("localhost:4455", goobs.WithPassword("mystrongpass"))
if err != nil { if err != nil {

View File

@ -48,9 +48,12 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) 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) // enable level updates (disabled by default)
vm.EventAdd("ldirty") vm.EventAdd("ldirty")

View File

@ -19,7 +19,11 @@ func (ir *iRemote) identifier() string {
// getter_bool returns the value of a boolean parameter // getter_bool returns the value of a boolean parameter
func (ir *iRemote) getter_bool(p string) bool { func (ir *iRemote) getter_bool(p string) bool {
param := fmt.Sprintf("%s.%s", ir.identifier(), p) 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 // setter_bool sets the value of a boolean parameter
@ -31,41 +35,65 @@ func (ir *iRemote) setter_bool(p string, v bool) {
} else { } else {
value = 0 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 // getter_int returns the value of an int parameter p
func (ir *iRemote) getter_int(p string) int { func (ir *iRemote) getter_int(p string) int {
param := fmt.Sprintf("%s.%s", ir.identifier(), p) 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 // setter_int sets the value v of an int parameter p
func (ir *iRemote) setter_int(p string, v int) { func (ir *iRemote) setter_int(p string, v int) {
param := fmt.Sprintf("%s.%s", ir.identifier(), p) 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 // getter_float returns the value of an int parameter p
func (ir *iRemote) getter_float(p string) float64 { func (ir *iRemote) getter_float(p string) float64 {
param := fmt.Sprintf("%s.%s", ir.identifier(), p) 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 // setter_float sets the value v of an int parameter p
func (ir *iRemote) setter_float(p string, v float32) { func (ir *iRemote) setter_float(p string, v float32) {
param := fmt.Sprintf("%s.%s", ir.identifier(), p) 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 // getter_string returns the value of a string parameter p
func (ir *iRemote) getter_string(p string) string { func (ir *iRemote) getter_string(p string) string {
param := fmt.Sprintf("%s.%s", ir.identifier(), p) 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 // setter_string sets the value v of a string parameter p
func (ir *iRemote) setter_string(p, v string) { func (ir *iRemote) setter_string(p, v string) {
param := fmt.Sprintf("%s.%s", ir.identifier(), p) param := fmt.Sprintf("%s.%s", ir.identifier(), p)
setParameterString(param, v) err := setParameterString(param, v)
if err != nil {
fmt.Println(err)
}
} }

View File

@ -26,26 +26,42 @@ func (r *Remote) String() string {
// Login logs into the API // Login logs into the API
// then it intializes the pooler // then it intializes the pooler
func (r *Remote) Login() { func (r *Remote) Login() error {
login(r.Kind.Name) err := login(r.Kind.Name)
if err != nil {
return err
}
r.pooler = newPooler(r.Kind) r.pooler = newPooler(r.Kind)
return nil
} }
// Logout logs out of the API // Logout logs out of the API
// it also terminates the pooler // it also terminates the pooler
func (r *Remote) Logout() { func (r *Remote) Logout() error {
r.pooler.run = false 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) // Type returns the type of Voicemeeter (basic, banana, potato)
func (r *Remote) Type() string { 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 // Version returns the version of Voicemeeter as a string
func (r *Remote) Version() 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 // Pdirty returns true iff a parameter value has changed
@ -60,7 +76,11 @@ func (r *Remote) Mdirty() bool {
// Gets a float parameter value // Gets a float parameter value
func (r *Remote) GetFloat(name string) float64 { 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 // Sets a float paramter value
@ -70,7 +90,11 @@ func (r *Remote) SetFloat(name string, value float32) {
// Gets a string parameter value // Gets a string parameter value
func (r *Remote) GetString(name string) string { 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 // Sets a string paramter value