login() is now tested for up to {Remote}.timeout seconds

if timeout exceeded return an error

runVoicemeeter() now promotes types to x64bit on 64-bit OS unless overridden.

Option functions WithTimeout() and WithBits() added.
This commit is contained in:
onyx-and-iris 2024-07-01 07:20:05 +01:00
parent 07018d1703
commit 0558e8f81d
2 changed files with 78 additions and 25 deletions

34
base.go
View File

@ -2,8 +2,10 @@ package voicemeeter
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"math" "math"
"runtime"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -45,16 +47,29 @@ 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) error { func login(kindId string, timeout, bits int) error {
res, _, _ := vmLogin.Call() res, _, _ := vmLogin.Call()
if res == 1 { if res == 1 {
runVoicemeeter(kindId) runVoicemeeter(kindId, bits)
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)
return err return err
} }
log.Info("Logged into Voicemeeter ", kindId)
var ver_s string
start := time.Now()
var err error
for time.Since(start).Seconds() < float64(timeout) {
time.Sleep(time.Duration(100) * time.Millisecond)
if ver_s, err = getVersion(); err == nil {
log.Infof("Logged into Voicemeeter %s v%s", kindMap[kindId], ver_s)
log.Debugf("Log in time: %.2f", time.Since(start).Seconds())
break
}
}
if err != nil {
return errors.New("timeout logging into the API")
}
clear() clear()
return nil return nil
} }
@ -68,18 +83,22 @@ func logout(kindId string) error {
err := fmt.Errorf("VBVMR_Logout returned %d", int32(res)) err := fmt.Errorf("VBVMR_Logout returned %d", int32(res))
return err return err
} }
log.Info("Logged out of Voicemeeter ", kindId) log.Infof("Logged out of Voicemeeter %s", kindMap[kindId])
return nil 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) error { func runVoicemeeter(kindId string, bits int) error {
vals := map[string]uint64{ vals := map[string]uint64{
"basic": 1, "basic": 1,
"banana": 2, "banana": 2,
"potato": 3, "potato": 3,
} }
res, _, _ := vmRunvm.Call(uintptr(vals[kindId])) val := vals[kindId]
if strings.Contains(runtime.GOARCH, "64") && bits == 64 {
val += 3
}
res, _, _ := vmRunvm.Call(uintptr(val))
if int32(res) != 0 { if int32(res) != 0 {
err := fmt.Errorf("VBVMR_RunVoicemeeter returned %d", int32(res)) err := fmt.Errorf("VBVMR_RunVoicemeeter returned %d", int32(res))
return err return err
@ -93,6 +112,7 @@ func getVersion() (string, error) {
res, _, _ := vmGetvmVersion.Call(uintptr(unsafe.Pointer(&ver))) res, _, _ := vmGetvmVersion.Call(uintptr(unsafe.Pointer(&ver)))
if int32(res) != 0 { if int32(res) != 0 {
err := fmt.Errorf("VBVMR_GetVoicemeeterVersion returned %d", int32(res)) err := fmt.Errorf("VBVMR_GetVoicemeeterVersion returned %d", int32(res))
log.Error(err.Error())
return "", err return "", err
} }
v1 := (ver & 0xFF000000) >> 24 v1 := (ver & 0xFF000000) >> 24

View File

@ -20,7 +20,9 @@ type Remote struct {
Recorder *recorder Recorder *recorder
Midi *midi_t Midi *midi_t
pooler *pooler pooler *pooler
timeout int
bits int
} }
// String implements the fmt.stringer interface // String implements the fmt.stringer interface
@ -31,7 +33,7 @@ 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() error { func (r *Remote) Login() error {
err := login(r.Kind.Name) err := login(r.Kind.Name, r.timeout, r.bits)
if err != nil { if err != nil {
return err return err
} }
@ -57,12 +59,10 @@ func (r *Remote) InitPooler() {
// Run launches the Voicemeeter GUI for a kind. // Run launches the Voicemeeter GUI for a kind.
func (r *Remote) Run(kindId string) error { func (r *Remote) Run(kindId string) error {
err := runVoicemeeter(kindId) err := runVoicemeeter(kindId, r.bits)
if err != nil { if err != nil {
return err return err
} }
time.Sleep(time.Second)
clear()
return nil return nil
} }
@ -173,6 +173,7 @@ type remoteBuilder interface {
makeDevice() remoteBuilder makeDevice() remoteBuilder
makeRecorder() remoteBuilder makeRecorder() remoteBuilder
makeMidi() remoteBuilder makeMidi() remoteBuilder
setDefaults() remoteBuilder
Build() remoteBuilder Build() remoteBuilder
Get() *Remote Get() *Remote
} }
@ -212,7 +213,7 @@ func (b *genericBuilder) setKind() remoteBuilder {
// makeStrip makes a strip slice and assigns it to remote.Strip // makeStrip makes a strip slice and assigns it to remote.Strip
// []iStrip comprises of both physical and virtual strip types // []iStrip comprises of both physical and virtual strip types
func (b *genericBuilder) makeStrip() remoteBuilder { func (b *genericBuilder) makeStrip() remoteBuilder {
log.Info("building strip") log.Debug("building strip")
strip := make([]iStrip, b.k.NumStrip()) strip := make([]iStrip, b.k.NumStrip())
for i := 0; i < b.k.NumStrip(); i++ { for i := 0; i < b.k.NumStrip(); i++ {
if i < b.k.PhysIn { if i < b.k.PhysIn {
@ -228,7 +229,7 @@ func (b *genericBuilder) makeStrip() remoteBuilder {
// makeBus makes a bus slice and assigns it to remote.Bus // makeBus makes a bus slice and assigns it to remote.Bus
// []t_bus comprises of both physical and virtual bus types // []t_bus comprises of both physical and virtual bus types
func (b *genericBuilder) makeBus() remoteBuilder { func (b *genericBuilder) makeBus() remoteBuilder {
log.Info("building bus") log.Debug("building bus")
bus := make([]iBus, b.k.NumBus()) bus := make([]iBus, b.k.NumBus())
for i := 0; i < b.k.NumBus(); i++ { for i := 0; i < b.k.NumBus(); i++ {
if i < b.k.PhysOut { if i < b.k.PhysOut {
@ -243,7 +244,7 @@ func (b *genericBuilder) makeBus() remoteBuilder {
// makeButton makes a button slice and assigns it to remote.Button // makeButton makes a button slice and assigns it to remote.Button
func (b *genericBuilder) makeButton() remoteBuilder { func (b *genericBuilder) makeButton() remoteBuilder {
log.Info("building button") log.Debug("building button")
button := make([]button, 80) button := make([]button, 80)
for i := 0; i < 80; i++ { for i := 0; i < 80; i++ {
button[i] = newButton(i) button[i] = newButton(i)
@ -254,39 +255,46 @@ func (b *genericBuilder) makeButton() remoteBuilder {
// makeCommand makes a command type and assigns it to remote.Command // makeCommand makes a command type and assigns it to remote.Command
func (b *genericBuilder) makeCommand() remoteBuilder { func (b *genericBuilder) makeCommand() remoteBuilder {
log.Info("building command") log.Debug("building command")
b.r.Command = newCommand() b.r.Command = newCommand()
return b return b
} }
// makeVban makes a vban type and assigns it to remote.Vban // makeVban makes a vban type and assigns it to remote.Vban
func (b *genericBuilder) makeVban() remoteBuilder { func (b *genericBuilder) makeVban() remoteBuilder {
log.Info("building vban") log.Debug("building vban")
b.r.Vban = newVban(b.k) b.r.Vban = newVban(b.k)
return b return b
} }
// makeDevice makes a device type and assigns it to remote.Device // makeDevice makes a device type and assigns it to remote.Device
func (b *genericBuilder) makeDevice() remoteBuilder { func (b *genericBuilder) makeDevice() remoteBuilder {
log.Info("building device") log.Debug("building device")
b.r.Device = newDevice() b.r.Device = newDevice()
return b return b
} }
// makeRecorder makes a recorder type and assigns it to remote.Recorder // makeRecorder makes a recorder type and assigns it to remote.Recorder
func (b *genericBuilder) makeRecorder() remoteBuilder { func (b *genericBuilder) makeRecorder() remoteBuilder {
log.Info("building recorder") log.Debug("building recorder")
b.r.Recorder = newRecorder() b.r.Recorder = newRecorder()
return b return b
} }
// makeMidi makes a midi type and assigns it to remote.Midi // makeMidi makes a midi type and assigns it to remote.Midi
func (b *genericBuilder) makeMidi() remoteBuilder { func (b *genericBuilder) makeMidi() remoteBuilder {
log.Info("building midi") log.Debug("building midi")
b.r.Midi = newMidi() b.r.Midi = newMidi()
return b return b
} }
// setDefaults sets defaults for optional members
func (b *genericBuilder) setDefaults() remoteBuilder {
b.r.bits = 64
b.r.timeout = 2
return b
}
// Get returns a fully constructed remote type for a kind // Get returns a fully constructed remote type for a kind
func (b *genericBuilder) Get() *Remote { func (b *genericBuilder) Get() *Remote {
return &b.r return &b.r
@ -306,7 +314,8 @@ func (basb *genericBuilder) Build() remoteBuilder {
makeCommand(). makeCommand().
makeVban(). makeVban().
makeDevice(). makeDevice().
makeMidi() makeMidi().
setDefaults()
} }
// bananaBuilder represents a builder specific to banana type // bananaBuilder represents a builder specific to banana type
@ -324,7 +333,8 @@ func (banb *bananaBuilder) Build() remoteBuilder {
makeVban(). makeVban().
makeDevice(). makeDevice().
makeRecorder(). makeRecorder().
makeMidi() makeMidi().
setDefaults()
} }
// potatoBuilder represents a builder specific to potato type // potatoBuilder represents a builder specific to potato type
@ -342,7 +352,8 @@ func (potb *potatoBuilder) Build() remoteBuilder {
makeVban(). makeVban().
makeDevice(). makeDevice().
makeRecorder(). makeRecorder().
makeMidi() makeMidi().
setDefaults()
} }
var ( var (
@ -350,6 +361,22 @@ var (
vmdelay int vmdelay int
) )
type Option func(*Remote)
func WithTimeout(timeout int) Option {
return func(r *Remote) {
r.timeout = timeout
}
}
func WithBits(bits int) Option {
return func(r *Remote) {
if bits == 32 || bits == 64 {
r.bits = bits
}
}
}
func init() { func init() {
log.SetOutput(os.Stdout) log.SetOutput(os.Stdout)
log.SetLevel(log.WarnLevel) log.SetLevel(log.WarnLevel)
@ -357,7 +384,7 @@ func init() {
// NewRemote returns a Remote type for a kind // NewRemote returns a Remote type for a kind
// this is the interface entry point // this is the interface entry point
func NewRemote(kindId string, delay int) (*Remote, error) { func NewRemote(kindId string, delay int, opts ...Option) (*Remote, error) {
kind, ok := kindMap[kindId] kind, ok := kindMap[kindId]
if !ok { if !ok {
err := fmt.Errorf("unknown Voicemeeter kind '%s'", kindId) err := fmt.Errorf("unknown Voicemeeter kind '%s'", kindId)
@ -380,5 +407,11 @@ func NewRemote(kindId string, delay int) (*Remote, error) {
director.SetBuilder(&potatoBuilder{genericBuilder{kind, Remote{}}}) director.SetBuilder(&potatoBuilder{genericBuilder{kind, Remote{}}})
} }
director.Construct() director.Construct()
return director.Get(), nil r := director.Get()
for _, opt := range opts {
opt(r)
}
return r, nil
} }