return values for CAPI calls fixed.

Polling functions now return error types
This commit is contained in:
onyx-and-iris 2022-12-08 10:26:19 +00:00
parent 4c54d924b3
commit 62b4956279

126
base.go
View File

@ -55,8 +55,7 @@ func login(kindId string) error {
return err
}
log.Info("Logged into Voicemeeter ", kindId)
for pdirty() || mdirty() {
}
clear()
return nil
}
@ -65,8 +64,8 @@ func login(kindId string) error {
func logout(kindId string) error {
time.Sleep(100 * time.Millisecond)
res, _, _ := vmLogout.Call()
if res != 0 {
err := fmt.Errorf("VBVMR_Logout returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_Logout returned %d", int32(res))
return err
}
log.Info("Logged out of Voicemeeter ", kindId)
@ -81,8 +80,8 @@ func runVoicemeeter(kindId string) error {
"potato": 3,
}
res, _, _ := vmRunvm.Call(uintptr(vals[kindId]))
if res != 0 {
err := fmt.Errorf("VBVMR_RunVoicemeeter returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_RunVoicemeeter returned %d", int32(res))
return err
}
return nil
@ -92,8 +91,8 @@ func runVoicemeeter(kindId string) error {
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)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_GetVoicemeeterVersion returned %d", int32(res))
return "", err
}
v1 := (ver & 0xFF000000) >> 24
@ -104,33 +103,61 @@ func getVersion() (string, error) {
}
// pdirty returns true iff a parameter value has changed
func pdirty() bool {
func pdirty() (bool, error) {
res, _, _ := vmPdirty.Call()
return int(res) == 1
if int32(res) < 0 {
err := fmt.Errorf("VBVMR_IsParametersDirty returned %d", int32(res))
log.Error(err.Error())
return false, err
}
return int32(res) == 1, nil
}
// mdirty returns true iff a macrobutton value has changed
func mdirty() bool {
func mdirty() (bool, error) {
res, _, _ := vmMdirty.Call()
return int(res) == 1
if int32(res) < 0 {
err := fmt.Errorf("VBVMR_MacroButton_IsDirty returned %d", int32(res))
log.Error(err.Error())
return false, err
}
return int32(res) == 1, nil
}
// ldirty returns true iff a level value has changed
func ldirty(k *kind) bool {
func ldirty(k *kind) (bool, error) {
_levelCache.stripLevelsBuff = make([]float64, (2*k.PhysIn)+(8*k.VirtIn))
_levelCache.busLevelsBuff = make([]float64, 8*k.NumBus())
for i := 0; i < (2*k.PhysIn)+(8*k.VirtIn); i++ {
val, _ := getLevel(_levelCache.stripMode, i)
val, err := getLevel(_levelCache.stripMode, i)
if err != nil {
log.Error(err.Error())
return false, err
}
_levelCache.stripLevelsBuff[i] = val
_levelCache.stripComp[i] = _levelCache.stripLevelsBuff[i] == _levelCache.stripLevels[i]
}
for i := 0; i < 8*k.NumBus(); i++ {
val, _ := getLevel(3, i)
val, err := getLevel(3, i)
if err != nil {
log.Error(err.Error())
return false, err
}
_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()))
return !(allTrue(_levelCache.stripComp, (2*k.PhysIn)+(8*k.VirtIn)) && allTrue(_levelCache.busComp, 8*k.NumBus())), nil
}
func clear() {
for {
pdirty, _ := pdirty()
mdirty, _ := mdirty()
if !(pdirty || mdirty) {
break
}
}
}
// getVMType returns the type of Voicemeeter, as a string
@ -139,8 +166,8 @@ func getVMType() (string, error) {
res, _, _ := vmGetvmType.Call(
uintptr(unsafe.Pointer(&type_)),
)
if res != 0 {
err := fmt.Errorf("VBVMR_GetVoicemeeterType returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_GetVoicemeeterType returned %d", int32(res))
return "", err
}
vals := map[uint64]string{
@ -155,8 +182,7 @@ func getVMType() (string, error) {
func getParameterFloat(name string) (float64, error) {
if vmsync {
time.Sleep(time.Duration(vmdelay) * time.Millisecond)
for pdirty() || mdirty() {
}
clear()
}
var value float32
b := append([]byte(name), 0)
@ -164,8 +190,8 @@ func getParameterFloat(name string) (float64, error) {
uintptr(unsafe.Pointer(&b[0])),
uintptr(unsafe.Pointer(&value)),
)
if res != 0 {
err := fmt.Errorf("VBVMR_GetParameterFloat returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_GetParameterFloat returned %d", int32(res))
return 0, err
}
return math.Round(float64(value)*10) / 10, nil
@ -179,8 +205,8 @@ func setParameterFloat(name string, value float64) error {
uintptr(unsafe.Pointer(&b1[0])),
uintptr(b2),
)
if res != 0 {
err := fmt.Errorf("VBVMR_SetParameterFloat returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_SetParameterFloat returned %d", int32(res))
return err
}
return nil
@ -190,8 +216,7 @@ func setParameterFloat(name string, value float64) error {
func getParameterString(name string) (string, error) {
if vmsync {
time.Sleep(time.Duration(vmdelay) * time.Millisecond)
for pdirty() || mdirty() {
}
clear()
}
b1 := append([]byte(name), 0)
var b2 [512]byte
@ -199,8 +224,8 @@ func getParameterString(name string) (string, error) {
uintptr(unsafe.Pointer(&b1[0])),
uintptr(unsafe.Pointer(&b2[0])),
)
if res != 0 {
err := fmt.Errorf("VBVMR_GetParameterStringA returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_GetParameterStringA returned %d", int32(res))
return "", err
}
str := bytes.Trim(b2[:], "\x00")
@ -215,8 +240,8 @@ func setParameterString(name, value string) error {
uintptr(unsafe.Pointer(&b1[0])),
uintptr(unsafe.Pointer(&b2[0])),
)
if res != 0 {
err := fmt.Errorf("VBVMR_SetParameterStringA returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_SetParameterStringA returned %d", int32(res))
return err
}
return nil
@ -228,8 +253,8 @@ func setParametersMulti(script string) error {
res, _, _ := vmSetParameters.Call(
uintptr(unsafe.Pointer(&b1[0])),
)
if res != 0 {
err := fmt.Errorf("VBVMR_SetParameters returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_SetParameters returned %d", int32(res))
return err
}
return nil
@ -239,8 +264,7 @@ func setParametersMulti(script string) error {
func getMacroStatus(id, mode int) (float64, error) {
if vmsync {
time.Sleep(time.Duration(vmdelay) * time.Millisecond)
for pdirty() || mdirty() {
}
clear()
}
var state float32
res, _, _ := vmGetMacroStatus.Call(
@ -248,8 +272,8 @@ func getMacroStatus(id, mode int) (float64, error) {
uintptr(unsafe.Pointer(&state)),
uintptr(mode),
)
if res != 0 {
err := fmt.Errorf("VBVMR_MacroButton_GetStatus returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_MacroButton_GetStatus returned %d", int32(res))
return 0, err
}
return float64(state), nil
@ -262,8 +286,8 @@ func setMacroStatus(id, state, mode int) error {
uintptr(state),
uintptr(mode),
)
if res != 0 {
err := fmt.Errorf("VBVMR_MacroButton_SetStatus returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_MacroButton_SetStatus returned %d", int32(res))
return err
}
return nil
@ -292,8 +316,8 @@ func getDeviceDescription(i int, dir string) (string, uint64, string, error) {
uintptr(unsafe.Pointer(&b1[0])),
uintptr(unsafe.Pointer(&b2[0])),
)
if res != 0 {
err := fmt.Errorf("VBVMR_Input_GetDeviceDescA returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_Input_GetDeviceDescA returned %d", int32(res))
return "", 0, "", err
}
} else {
@ -303,8 +327,8 @@ func getDeviceDescription(i int, dir string) (string, uint64, string, error) {
uintptr(unsafe.Pointer(&b1[0])),
uintptr(unsafe.Pointer(&b2[0])),
)
if res != 0 {
err := fmt.Errorf("VBVMR_Output_GetDeviceDescA returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_Output_GetDeviceDescA returned %d", int32(res))
return "", 0, "", err
}
}
@ -321,28 +345,30 @@ func getLevel(type_, i int) (float64, error) {
uintptr(i),
uintptr(unsafe.Pointer(&val)),
)
if res != 0 {
err := fmt.Errorf("VBVMR_GetLevel returned %d", res)
if int32(res) != 0 {
err := fmt.Errorf("VBVMR_GetLevel returned %d", int32(res))
return 0, err
}
return float64(val), nil
}
// getMidiMessage gets midi channel, pitch and velocity for a single midi input
func getMidiMessage() bool {
func getMidiMessage() (bool, error) {
var midi = newMidi()
var b1 [1024]byte
res, _, _ := vmGetMidiMessage.Call(
uintptr(unsafe.Pointer(&b1[0])),
uintptr(1024),
)
x := int(res)
x := int32(res)
if x < 0 {
err := fmt.Errorf("VBVMR_GetMidiMessage returned %d", res)
if x == -2 {
err := fmt.Errorf("VBVMR_GetMidiMessage returned %d", int32(res))
if err != nil {
fmt.Println(err)
log.Error(err)
return false, err
}
}
return false
}
msg := bytes.Trim(b1[:], "\x00")
if len(msg) > 0 {
@ -359,5 +385,5 @@ func getMidiMessage() bool {
midi.cache[pitch] = vel
}
}
return len(msg) > 0
return len(msg) > 0, nil
}