From 23422f9641a1061e5f78a32afc6337343b65e1de Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sat, 7 Feb 2026 14:23:46 +0000 Subject: [PATCH] DRY up the factory methods use optional functions to set address functions --- internal/xair/bus.go | 5 +++-- internal/xair/client.go | 10 +++++---- internal/xair/comp.go | 36 +++++++------------------------- internal/xair/engine.go | 2 +- internal/xair/eq.go | 36 +++++++------------------------- internal/xair/gate.go | 43 +++++++++++++++++++++++---------------- internal/xair/main.go | 16 +++++++++++---- internal/xair/matrix.go | 4 ++-- internal/xair/option.go | 32 +++++++++++++++++++++++++++-- internal/xair/snapshot.go | 1 + internal/xair/strip.go | 7 ++++--- 11 files changed, 99 insertions(+), 93 deletions(-) diff --git a/internal/xair/bus.go b/internal/xair/bus.go index aa39622..5d9ec7c 100644 --- a/internal/xair/bus.go +++ b/internal/xair/bus.go @@ -9,12 +9,13 @@ type Bus struct { Comp *Comp } +// newBus creates a new Bus instance func newBus(c *Client) *Bus { return &Bus{ client: c, baseAddress: c.addressMap["bus"], - Eq: newEqForBus(c, c.addressMap["bus"]), - Comp: newCompForBus(c, c.addressMap["bus"]), + Eq: newEq(c, c.addressMap["bus"]), + Comp: newComp(c, c.addressMap["bus"]), } } diff --git a/internal/xair/client.go b/internal/xair/client.go index 56ae84f..d1efcef 100644 --- a/internal/xair/client.go +++ b/internal/xair/client.go @@ -13,6 +13,7 @@ type Client struct { *engine } +// XAirClient is a client for controlling XAir mixers type XAirClient struct { Client Main *Main @@ -22,6 +23,7 @@ type XAirClient struct { Snapshot *Snapshot } +// X32Client is a client for controlling X32 mixers type X32Client struct { Client Main *Main @@ -33,8 +35,8 @@ type X32Client struct { Snapshot *Snapshot } -// NewX32Client creates a new X32Client instance -func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, error) { +// NewX32Client creates a new X32Client instance with optional engine configuration +func NewX32Client(mixerIP string, mixerPort int, opts ...EngineOption) (*X32Client, error) { e, err := newEngine(mixerIP, mixerPort, kindX32, opts...) if err != nil { return nil, err @@ -54,8 +56,8 @@ func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, er return c, nil } -// NewXAirClient creates a new XAirClient instance -func NewXAirClient(mixerIP string, mixerPort int, opts ...Option) (*XAirClient, error) { +// NewXAirClient creates a new XAirClient instance with optional engine configuration +func NewXAirClient(mixerIP string, mixerPort int, opts ...EngineOption) (*XAirClient, error) { e, err := newEngine(mixerIP, mixerPort, kindXAir, opts...) if err != nil { return nil, err diff --git a/internal/xair/comp.go b/internal/xair/comp.go index 5c67368..8b1959e 100644 --- a/internal/xair/comp.go +++ b/internal/xair/comp.go @@ -2,48 +2,26 @@ package xair import "fmt" +// Comp represents the compressor parameters. type Comp struct { client *Client baseAddress string AddressFunc func(fmtString string, args ...any) string } -// Factory function to create Comp instance for Main -func newCompForMain(c *Client, baseAddress string) *Comp { - return &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{ +// Factory function to create Comp instance with optional configuration +func newComp(c *Client, baseAddress string, opts ...CompOption) *Comp { + comp := &Comp{ client: c, baseAddress: fmt.Sprintf("%s/dyn", baseAddress), AddressFunc: fmt.Sprintf, } -} -// Factory function to create Comp instance for Bus -func newCompForBus(c *Client, baseAddress string) *Comp { - return &Comp{ - client: c, - baseAddress: fmt.Sprintf("%s/dyn", baseAddress), - AddressFunc: fmt.Sprintf, + for _, opt := range opts { + opt(comp) } -} -// 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, - } + return comp } // On retrieves the on/off status of the Compressor for a specific strip or bus (1-based indexing). diff --git a/internal/xair/engine.go b/internal/xair/engine.go index 9da6e63..bd174d4 100644 --- a/internal/xair/engine.go +++ b/internal/xair/engine.go @@ -26,7 +26,7 @@ type engine struct { 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)) if err != nil { return nil, fmt.Errorf("failed to resolve local address: %v", err) diff --git a/internal/xair/eq.go b/internal/xair/eq.go index 64c73fe..5b31c40 100644 --- a/internal/xair/eq.go +++ b/internal/xair/eq.go @@ -4,48 +4,26 @@ import ( "fmt" ) +// Eq represents the EQ parameters. type Eq struct { client *Client baseAddress string AddressFunc func(fmtString string, args ...any) string } -// Factory function to create Eq instance for Main -func newEqForMain(c *Client, baseAddress string) *Eq { - return &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{ +// Factory function to create Eq instance with optional configuration +func newEq(c *Client, baseAddress string, opts ...EqOption) *Eq { + eq := &Eq{ client: c, baseAddress: fmt.Sprintf("%s/eq", baseAddress), AddressFunc: fmt.Sprintf, } -} -// Factory function to create Eq instance for Bus -func newEqForBus(c *Client, baseAddress string) *Eq { - return &Eq{ - client: c, - baseAddress: fmt.Sprintf("%s/eq", baseAddress), - AddressFunc: fmt.Sprintf, + for _, opt := range opts { + opt(eq) } -} -// 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, - } + return eq } // On retrieves the on/off status of the EQ for a specific strip or bus (1-based indexing). diff --git a/internal/xair/gate.go b/internal/xair/gate.go index c59a4a7..058f061 100644 --- a/internal/xair/gate.go +++ b/internal/xair/gate.go @@ -2,22 +2,31 @@ package xair import "fmt" +// Gate represents the gate parameters. type Gate struct { client *Client baseAddress string + AddressFunc func(fmtString string, args ...any) string } -// Factory function to create Gate instance for Strip -func newGateForStrip(c *Client, baseAddress string) *Gate { - return &Gate{ +// Factory function to create Gate instance with optional configuration +func newGate(c *Client, baseAddress string, opts ...GateOption) *Gate { + gate := &Gate{ client: c, 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). 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) if err != nil { 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). 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 if on { 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). 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) if err != nil { 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). 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"} 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). 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) if err != nil { 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). 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))) } // Range retrieves the range value of the Gate for a specific strip (1-based indexing). 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) if err != nil { 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). 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))) } // Attack retrieves the attack time of the Gate for a specific strip (1-based indexing). 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) if err != nil { 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). 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))) } // Hold retrieves the hold time of the Gate for a specific strip (1-based indexing). 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) if err != nil { 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). 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))) } // Release retrieves the release time of the Gate for a specific strip (1-based indexing). 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) if err != nil { 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). 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))) } diff --git a/internal/xair/main.go b/internal/xair/main.go index 8134faa..2e8f7a0 100644 --- a/internal/xair/main.go +++ b/internal/xair/main.go @@ -11,21 +11,29 @@ type Main struct { // newMainStereo creates a new Main instance for stereo main output func newMainStereo(c *Client) *Main { + addressFunc := func(fmtString string, args ...any) string { + return fmtString + } + return &Main{ client: c, baseAddress: c.addressMap["main"], - Eq: newEqForMain(c, c.addressMap["main"]), - Comp: newCompForMain(c, c.addressMap["main"]), + Eq: newEq(c, c.addressMap["main"], WithEqAddressFunc(addressFunc)), + Comp: newComp(c, c.addressMap["main"], WithCompAddressFunc(addressFunc)), } } // newMainMono creates a new MainMono instance for mono main output (X32 only) func newMainMono(c *Client) *Main { + addressFunc := func(fmtString string, args ...any) string { + return fmtString + } + return &Main{ baseAddress: c.addressMap["mainmono"], client: c, - Eq: newEqForMain(c, c.addressMap["mainmono"]), - Comp: newCompForMain(c, c.addressMap["mainmono"]), + Eq: newEq(c, c.addressMap["mainmono"], WithEqAddressFunc(addressFunc)), + Comp: newComp(c, c.addressMap["mainmono"], WithCompAddressFunc(addressFunc)), } } diff --git a/internal/xair/matrix.go b/internal/xair/matrix.go index 9b2427c..7b4d234 100644 --- a/internal/xair/matrix.go +++ b/internal/xair/matrix.go @@ -14,8 +14,8 @@ func newMatrix(c *Client) *Matrix { return &Matrix{ client: c, baseAddress: c.addressMap["matrix"], - Eq: newEqForMatrix(c, c.addressMap["matrix"]), - Comp: newCompForMatrix(c, c.addressMap["matrix"]), + Eq: newEq(c, c.addressMap["matrix"]), + Comp: newComp(c, c.addressMap["matrix"]), } } diff --git a/internal/xair/option.go b/internal/xair/option.go index 836ef60..57e28f2 100644 --- a/internal/xair/option.go +++ b/internal/xair/option.go @@ -2,10 +2,38 @@ package xair 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) { 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 + } +} diff --git a/internal/xair/snapshot.go b/internal/xair/snapshot.go index cc30f2e..a88b042 100644 --- a/internal/xair/snapshot.go +++ b/internal/xair/snapshot.go @@ -7,6 +7,7 @@ type Snapshot struct { baseAddress string } +// newSnapshot creates a new Snapshot instance func newSnapshot(c *Client) *Snapshot { return &Snapshot{ client: c, diff --git a/internal/xair/strip.go b/internal/xair/strip.go index 65620be..68d273d 100644 --- a/internal/xair/strip.go +++ b/internal/xair/strip.go @@ -10,13 +10,14 @@ type Strip struct { Comp *Comp } +// newStrip creates a new Strip instance func newStrip(c *Client) *Strip { return &Strip{ client: c, baseAddress: c.addressMap["strip"], - Gate: newGateForStrip(c, c.addressMap["strip"]), - Eq: newEqForStrip(c, c.addressMap["strip"]), - Comp: newCompForStrip(c, c.addressMap["strip"]), + Gate: newGate(c, c.addressMap["strip"]), + Eq: newEq(c, c.addressMap["strip"]), + Comp: newComp(c, c.addressMap["strip"]), } }