mirror of
https://github.com/onyx-and-iris/xair-cli.git
synced 2026-02-26 08:19:11 +00:00
DRY up the factory methods
use optional functions to set address functions
This commit is contained in:
parent
3c47d12719
commit
daa4d65725
@ -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"]),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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).
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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).
|
||||
|
||||
@ -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)))
|
||||
}
|
||||
|
||||
@ -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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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"]),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@ type Snapshot struct {
|
||||
baseAddress string
|
||||
}
|
||||
|
||||
// newSnapshot creates a new Snapshot instance
|
||||
func newSnapshot(c *Client) *Snapshot {
|
||||
return &Snapshot{
|
||||
client: c,
|
||||
|
||||
@ -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"]),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user