DRY up the factory methods

use optional functions to set address functions
This commit is contained in:
onyx-and-iris 2026-02-07 14:23:46 +00:00
parent 3c47d12719
commit daa4d65725
11 changed files with 99 additions and 93 deletions

View File

@ -9,12 +9,13 @@ type Bus struct {
Comp *Comp Comp *Comp
} }
// newBus creates a new Bus instance
func newBus(c *Client) *Bus { func newBus(c *Client) *Bus {
return &Bus{ return &Bus{
client: c, client: c,
baseAddress: c.addressMap["bus"], baseAddress: c.addressMap["bus"],
Eq: newEqForBus(c, c.addressMap["bus"]), Eq: newEq(c, c.addressMap["bus"]),
Comp: newCompForBus(c, c.addressMap["bus"]), Comp: newComp(c, c.addressMap["bus"]),
} }
} }

View File

@ -13,6 +13,7 @@ type Client struct {
*engine *engine
} }
// XAirClient is a client for controlling XAir mixers
type XAirClient struct { type XAirClient struct {
Client Client
Main *Main Main *Main
@ -22,6 +23,7 @@ type XAirClient struct {
Snapshot *Snapshot Snapshot *Snapshot
} }
// X32Client is a client for controlling X32 mixers
type X32Client struct { type X32Client struct {
Client Client
Main *Main Main *Main
@ -33,8 +35,8 @@ type X32Client struct {
Snapshot *Snapshot Snapshot *Snapshot
} }
// NewX32Client creates a new X32Client instance // NewX32Client creates a new X32Client instance with optional engine configuration
func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, error) { func NewX32Client(mixerIP string, mixerPort int, opts ...EngineOption) (*X32Client, error) {
e, err := newEngine(mixerIP, mixerPort, kindX32, opts...) e, err := newEngine(mixerIP, mixerPort, kindX32, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
@ -54,8 +56,8 @@ func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, er
return c, nil return c, nil
} }
// NewXAirClient creates a new XAirClient instance // NewXAirClient creates a new XAirClient instance with optional engine configuration
func NewXAirClient(mixerIP string, mixerPort int, opts ...Option) (*XAirClient, error) { func NewXAirClient(mixerIP string, mixerPort int, opts ...EngineOption) (*XAirClient, error) {
e, err := newEngine(mixerIP, mixerPort, kindXAir, opts...) e, err := newEngine(mixerIP, mixerPort, kindXAir, opts...)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -2,48 +2,26 @@ package xair
import "fmt" import "fmt"
// Comp represents the compressor parameters.
type Comp struct { type Comp struct {
client *Client client *Client
baseAddress string baseAddress string
AddressFunc func(fmtString string, args ...any) string AddressFunc func(fmtString string, args ...any) string
} }
// Factory function to create Comp instance for Main // Factory function to create Comp instance with optional configuration
func newCompForMain(c *Client, baseAddress string) *Comp { func newComp(c *Client, baseAddress string, opts ...CompOption) *Comp {
return &Comp{ comp := &Comp{
client: c,
baseAddress: fmt.Sprintf("%s/dyn", baseAddress),
AddressFunc: func(fmtString string, args ...any) string {
return fmtString
},
}
}
// Factory function to create Comp instance for Strip
func newCompForStrip(c *Client, baseAddress string) *Comp {
return &Comp{
client: c, client: c,
baseAddress: fmt.Sprintf("%s/dyn", baseAddress), baseAddress: fmt.Sprintf("%s/dyn", baseAddress),
AddressFunc: fmt.Sprintf, AddressFunc: fmt.Sprintf,
} }
for _, opt := range opts {
opt(comp)
} }
// Factory function to create Comp instance for Bus return comp
func newCompForBus(c *Client, baseAddress string) *Comp {
return &Comp{
client: c,
baseAddress: fmt.Sprintf("%s/dyn", baseAddress),
AddressFunc: fmt.Sprintf,
}
}
// Factory function to create Comp instance for Matrix
func newCompForMatrix(c *Client, baseAddress string) *Comp {
return &Comp{
client: c,
baseAddress: fmt.Sprintf("%s/dyn", baseAddress),
AddressFunc: fmt.Sprintf,
}
} }
// On retrieves the on/off status of the Compressor for a specific strip or bus (1-based indexing). // On retrieves the on/off status of the Compressor for a specific strip or bus (1-based indexing).

View File

@ -26,7 +26,7 @@ type engine struct {
respChan chan *osc.Message respChan chan *osc.Message
} }
func newEngine(mixerIP string, mixerPort int, kind mixerKind, opts ...Option) (*engine, error) { func newEngine(mixerIP string, mixerPort int, kind mixerKind, opts ...EngineOption) (*engine, error) {
localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0)) localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to resolve local address: %v", err) return nil, fmt.Errorf("failed to resolve local address: %v", err)

View File

@ -4,48 +4,26 @@ import (
"fmt" "fmt"
) )
// Eq represents the EQ parameters.
type Eq struct { type Eq struct {
client *Client client *Client
baseAddress string baseAddress string
AddressFunc func(fmtString string, args ...any) string AddressFunc func(fmtString string, args ...any) string
} }
// Factory function to create Eq instance for Main // Factory function to create Eq instance with optional configuration
func newEqForMain(c *Client, baseAddress string) *Eq { func newEq(c *Client, baseAddress string, opts ...EqOption) *Eq {
return &Eq{ eq := &Eq{
client: c,
baseAddress: fmt.Sprintf("%s/eq", baseAddress),
AddressFunc: func(fmtString string, args ...any) string {
return fmtString
},
}
}
// Factory function to create Eq instance for Strip
func newEqForStrip(c *Client, baseAddress string) *Eq {
return &Eq{
client: c, client: c,
baseAddress: fmt.Sprintf("%s/eq", baseAddress), baseAddress: fmt.Sprintf("%s/eq", baseAddress),
AddressFunc: fmt.Sprintf, AddressFunc: fmt.Sprintf,
} }
for _, opt := range opts {
opt(eq)
} }
// Factory function to create Eq instance for Bus return eq
func newEqForBus(c *Client, baseAddress string) *Eq {
return &Eq{
client: c,
baseAddress: fmt.Sprintf("%s/eq", baseAddress),
AddressFunc: fmt.Sprintf,
}
}
// Factory function to create Eq instance for Matrix
func newEqForMatrix(c *Client, baseAddress string) *Eq {
return &Eq{
client: c,
baseAddress: fmt.Sprintf("%s/eq", baseAddress),
AddressFunc: fmt.Sprintf,
}
} }
// On retrieves the on/off status of the EQ for a specific strip or bus (1-based indexing). // On retrieves the on/off status of the EQ for a specific strip or bus (1-based indexing).

View File

@ -2,22 +2,31 @@ package xair
import "fmt" import "fmt"
// Gate represents the gate parameters.
type Gate struct { type Gate struct {
client *Client client *Client
baseAddress string baseAddress string
AddressFunc func(fmtString string, args ...any) string
} }
// Factory function to create Gate instance for Strip // Factory function to create Gate instance with optional configuration
func newGateForStrip(c *Client, baseAddress string) *Gate { func newGate(c *Client, baseAddress string, opts ...GateOption) *Gate {
return &Gate{ gate := &Gate{
client: c, client: c,
baseAddress: fmt.Sprintf("%s/gate", baseAddress), baseAddress: fmt.Sprintf("%s/gate", baseAddress),
AddressFunc: fmt.Sprintf,
} }
for _, opt := range opts {
opt(gate)
}
return gate
} }
// On retrieves the on/off status of the Gate for a specific strip (1-based indexing). // On retrieves the on/off status of the Gate for a specific strip (1-based indexing).
func (g *Gate) On(index int) (bool, error) { func (g *Gate) On(index int) (bool, error) {
address := fmt.Sprintf(g.baseAddress, index) + "/on" address := g.AddressFunc(g.baseAddress, index) + "/on"
err := g.client.SendMessage(address) err := g.client.SendMessage(address)
if err != nil { if err != nil {
return false, err return false, err
@ -36,7 +45,7 @@ func (g *Gate) On(index int) (bool, error) {
// SetOn sets the on/off status of the Gate for a specific strip (1-based indexing). // SetOn sets the on/off status of the Gate for a specific strip (1-based indexing).
func (g *Gate) SetOn(index int, on bool) error { func (g *Gate) SetOn(index int, on bool) error {
address := fmt.Sprintf(g.baseAddress, index) + "/on" address := g.AddressFunc(g.baseAddress, index) + "/on"
var value int32 var value int32
if on { if on {
value = 1 value = 1
@ -46,7 +55,7 @@ func (g *Gate) SetOn(index int, on bool) error {
// Mode retrieves the current mode of the Gate for a specific strip (1-based indexing). // Mode retrieves the current mode of the Gate for a specific strip (1-based indexing).
func (g *Gate) Mode(index int) (string, error) { func (g *Gate) Mode(index int) (string, error) {
address := fmt.Sprintf(g.baseAddress, index) + "/mode" address := g.AddressFunc(g.baseAddress, index) + "/mode"
err := g.client.SendMessage(address) err := g.client.SendMessage(address)
if err != nil { if err != nil {
return "", err return "", err
@ -67,7 +76,7 @@ func (g *Gate) Mode(index int) (string, error) {
// SetMode sets the mode of the Gate for a specific strip (1-based indexing). // SetMode sets the mode of the Gate for a specific strip (1-based indexing).
func (g *Gate) SetMode(index int, mode string) error { func (g *Gate) SetMode(index int, mode string) error {
address := fmt.Sprintf(g.baseAddress, index) + "/mode" address := g.AddressFunc(g.baseAddress, index) + "/mode"
possibleModes := []string{"exp2", "exp3", "exp4", "gate", "duck"} possibleModes := []string{"exp2", "exp3", "exp4", "gate", "duck"}
return g.client.SendMessage(address, int32(indexOf(possibleModes, mode))) return g.client.SendMessage(address, int32(indexOf(possibleModes, mode)))
@ -75,7 +84,7 @@ func (g *Gate) SetMode(index int, mode string) error {
// Threshold retrieves the threshold value of the Gate for a specific strip (1-based indexing). // Threshold retrieves the threshold value of the Gate for a specific strip (1-based indexing).
func (g *Gate) Threshold(index int) (float64, error) { func (g *Gate) Threshold(index int) (float64, error) {
address := fmt.Sprintf(g.baseAddress, index) + "/thr" address := g.AddressFunc(g.baseAddress, index) + "/thr"
err := g.client.SendMessage(address) err := g.client.SendMessage(address)
if err != nil { if err != nil {
return 0, err return 0, err
@ -94,13 +103,13 @@ func (g *Gate) Threshold(index int) (float64, error) {
// SetThreshold sets the threshold value of the Gate for a specific strip (1-based indexing). // SetThreshold sets the threshold value of the Gate for a specific strip (1-based indexing).
func (g *Gate) SetThreshold(index int, threshold float64) error { func (g *Gate) SetThreshold(index int, threshold float64) error {
address := fmt.Sprintf(g.baseAddress, index) + "/thr" address := g.AddressFunc(g.baseAddress, index) + "/thr"
return g.client.SendMessage(address, float32(linSet(-80, 0, threshold))) return g.client.SendMessage(address, float32(linSet(-80, 0, threshold)))
} }
// Range retrieves the range value of the Gate for a specific strip (1-based indexing). // Range retrieves the range value of the Gate for a specific strip (1-based indexing).
func (g *Gate) Range(index int) (float64, error) { func (g *Gate) Range(index int) (float64, error) {
address := fmt.Sprintf(g.baseAddress, index) + "/range" address := g.AddressFunc(g.baseAddress, index) + "/range"
err := g.client.SendMessage(address) err := g.client.SendMessage(address)
if err != nil { if err != nil {
return 0, err return 0, err
@ -119,13 +128,13 @@ func (g *Gate) Range(index int) (float64, error) {
// SetRange sets the range value of the Gate for a specific strip (1-based indexing). // SetRange sets the range value of the Gate for a specific strip (1-based indexing).
func (g *Gate) SetRange(index int, rangeVal float64) error { func (g *Gate) SetRange(index int, rangeVal float64) error {
address := fmt.Sprintf(g.baseAddress, index) + "/range" address := g.AddressFunc(g.baseAddress, index) + "/range"
return g.client.SendMessage(address, float32(linSet(3, 60, rangeVal))) return g.client.SendMessage(address, float32(linSet(3, 60, rangeVal)))
} }
// Attack retrieves the attack time of the Gate for a specific strip (1-based indexing). // Attack retrieves the attack time of the Gate for a specific strip (1-based indexing).
func (g *Gate) Attack(index int) (float64, error) { func (g *Gate) Attack(index int) (float64, error) {
address := fmt.Sprintf(g.baseAddress, index) + "/attack" address := g.AddressFunc(g.baseAddress, index) + "/attack"
err := g.client.SendMessage(address) err := g.client.SendMessage(address)
if err != nil { if err != nil {
return 0, err return 0, err
@ -144,13 +153,13 @@ func (g *Gate) Attack(index int) (float64, error) {
// SetAttack sets the attack time of the Gate for a specific strip (1-based indexing). // SetAttack sets the attack time of the Gate for a specific strip (1-based indexing).
func (g *Gate) SetAttack(index int, attack float64) error { func (g *Gate) SetAttack(index int, attack float64) error {
address := fmt.Sprintf(g.baseAddress, index) + "/attack" address := g.AddressFunc(g.baseAddress, index) + "/attack"
return g.client.SendMessage(address, float32(linSet(0, 120, attack))) return g.client.SendMessage(address, float32(linSet(0, 120, attack)))
} }
// Hold retrieves the hold time of the Gate for a specific strip (1-based indexing). // Hold retrieves the hold time of the Gate for a specific strip (1-based indexing).
func (g *Gate) Hold(index int) (float64, error) { func (g *Gate) Hold(index int) (float64, error) {
address := fmt.Sprintf(g.baseAddress, index) + "/hold" address := g.AddressFunc(g.baseAddress, index) + "/hold"
err := g.client.SendMessage(address) err := g.client.SendMessage(address)
if err != nil { if err != nil {
return 0, err return 0, err
@ -169,13 +178,13 @@ func (g *Gate) Hold(index int) (float64, error) {
// SetHold sets the hold time of the Gate for a specific strip (1-based indexing). // SetHold sets the hold time of the Gate for a specific strip (1-based indexing).
func (g *Gate) SetHold(index int, hold float64) error { func (g *Gate) SetHold(index int, hold float64) error {
address := fmt.Sprintf(g.baseAddress, index) + "/hold" address := g.AddressFunc(g.baseAddress, index) + "/hold"
return g.client.SendMessage(address, float32(logSet(0.02, 2000, hold))) return g.client.SendMessage(address, float32(logSet(0.02, 2000, hold)))
} }
// Release retrieves the release time of the Gate for a specific strip (1-based indexing). // Release retrieves the release time of the Gate for a specific strip (1-based indexing).
func (g *Gate) Release(index int) (float64, error) { func (g *Gate) Release(index int) (float64, error) {
address := fmt.Sprintf(g.baseAddress, index) + "/release" address := g.AddressFunc(g.baseAddress, index) + "/release"
err := g.client.SendMessage(address) err := g.client.SendMessage(address)
if err != nil { if err != nil {
return 0, err return 0, err
@ -194,6 +203,6 @@ func (g *Gate) Release(index int) (float64, error) {
// SetRelease sets the release time of the Gate for a specific strip (1-based indexing). // SetRelease sets the release time of the Gate for a specific strip (1-based indexing).
func (g *Gate) SetRelease(index int, release float64) error { func (g *Gate) SetRelease(index int, release float64) error {
address := fmt.Sprintf(g.baseAddress, index) + "/release" address := g.AddressFunc(g.baseAddress, index) + "/release"
return g.client.SendMessage(address, float32(logSet(5, 4000, release))) return g.client.SendMessage(address, float32(logSet(5, 4000, release)))
} }

View File

@ -11,21 +11,29 @@ type Main struct {
// newMainStereo creates a new Main instance for stereo main output // newMainStereo creates a new Main instance for stereo main output
func newMainStereo(c *Client) *Main { func newMainStereo(c *Client) *Main {
addressFunc := func(fmtString string, args ...any) string {
return fmtString
}
return &Main{ return &Main{
client: c, client: c,
baseAddress: c.addressMap["main"], baseAddress: c.addressMap["main"],
Eq: newEqForMain(c, c.addressMap["main"]), Eq: newEq(c, c.addressMap["main"], WithEqAddressFunc(addressFunc)),
Comp: newCompForMain(c, c.addressMap["main"]), Comp: newComp(c, c.addressMap["main"], WithCompAddressFunc(addressFunc)),
} }
} }
// newMainMono creates a new MainMono instance for mono main output (X32 only) // newMainMono creates a new MainMono instance for mono main output (X32 only)
func newMainMono(c *Client) *Main { func newMainMono(c *Client) *Main {
addressFunc := func(fmtString string, args ...any) string {
return fmtString
}
return &Main{ return &Main{
baseAddress: c.addressMap["mainmono"], baseAddress: c.addressMap["mainmono"],
client: c, client: c,
Eq: newEqForMain(c, c.addressMap["mainmono"]), Eq: newEq(c, c.addressMap["mainmono"], WithEqAddressFunc(addressFunc)),
Comp: newCompForMain(c, c.addressMap["mainmono"]), Comp: newComp(c, c.addressMap["mainmono"], WithCompAddressFunc(addressFunc)),
} }
} }

View File

@ -14,8 +14,8 @@ func newMatrix(c *Client) *Matrix {
return &Matrix{ return &Matrix{
client: c, client: c,
baseAddress: c.addressMap["matrix"], baseAddress: c.addressMap["matrix"],
Eq: newEqForMatrix(c, c.addressMap["matrix"]), Eq: newEq(c, c.addressMap["matrix"]),
Comp: newCompForMatrix(c, c.addressMap["matrix"]), Comp: newComp(c, c.addressMap["matrix"]),
} }
} }

View File

@ -2,10 +2,38 @@ package xair
import "time" import "time"
type Option func(*engine) type EngineOption func(*engine)
func WithTimeout(timeout time.Duration) Option { // WithTimeout sets the timeout duration for OSC message responses
func WithTimeout(timeout time.Duration) EngineOption {
return func(e *engine) { return func(e *engine) {
e.timeout = timeout e.timeout = timeout
} }
} }
type CompOption func(*Comp)
// WithCompAddressFunc allows customization of the OSC address formatting for Comp parameters
func WithCompAddressFunc(f func(fmtString string, args ...any) string) CompOption {
return func(c *Comp) {
c.AddressFunc = f
}
}
type EqOption func(*Eq)
// WithEqAddressFunc allows customization of the OSC address formatting for Eq parameters
func WithEqAddressFunc(f func(fmtString string, args ...any) string) EqOption {
return func(e *Eq) {
e.AddressFunc = f
}
}
type GateOption func(*Gate)
// WithGateAddressFunc allows customization of the OSC address formatting for Gate parameters
func WithGateAddressFunc(f func(fmtString string, args ...any) string) GateOption {
return func(g *Gate) {
g.AddressFunc = f
}
}

View File

@ -7,6 +7,7 @@ type Snapshot struct {
baseAddress string baseAddress string
} }
// newSnapshot creates a new Snapshot instance
func newSnapshot(c *Client) *Snapshot { func newSnapshot(c *Client) *Snapshot {
return &Snapshot{ return &Snapshot{
client: c, client: c,

View File

@ -10,13 +10,14 @@ type Strip struct {
Comp *Comp Comp *Comp
} }
// newStrip creates a new Strip instance
func newStrip(c *Client) *Strip { func newStrip(c *Client) *Strip {
return &Strip{ return &Strip{
client: c, client: c,
baseAddress: c.addressMap["strip"], baseAddress: c.addressMap["strip"],
Gate: newGateForStrip(c, c.addressMap["strip"]), Gate: newGate(c, c.addressMap["strip"]),
Eq: newEqForStrip(c, c.addressMap["strip"]), Eq: newEq(c, c.addressMap["strip"]),
Comp: newCompForStrip(c, c.addressMap["strip"]), Comp: newComp(c, c.addressMap["strip"]),
} }
} }