From 1623b53cffd5ab9fa58570045e39b7eeb21a9847 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 1 Feb 2026 04:15:59 +0000 Subject: [PATCH] headamp gain now accepts a --duration flag for safe gain staging. --- README.md | 6 ++-- cmd/bus.go | 16 +++++----- cmd/headamp.go | 84 +++++++++++++++++++++++++++++++++++++++++++++----- cmd/main.go | 16 +++++----- cmd/strip.go | 14 ++++----- 5 files changed, 103 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 3530266..d4b79e9 100644 --- a/README.md +++ b/README.md @@ -44,14 +44,14 @@ Use "xair-cli [command] --help" for more information about a command. xair-cli main fadeout ``` -*enable phantom power and set the gain to 28dB for strip 09* +*enable phantom power and set the gain to 28.0dB over a 10s duration for strip 09* ```console xair-cli headamp 9 phantom on -xair-cli headamp 9 gain 28 +xair-cli headamp 9 gain 28.0 --duration 10s ``` -*adjust strip 09 send level to bus 5* +*set strip 09 send level for bus 5 to -18.0dB* ```console xair-cli strip send 9 5 -- -18.0 ``` diff --git a/cmd/bus.go b/cmd/bus.go index 442bf37..883b4d0 100644 --- a/cmd/bus.go +++ b/cmd/bus.go @@ -104,7 +104,7 @@ var busFadeOutCmd = &cobra.Command{ Long: "Fade out the bus fader to minimum level over a specified duration in seconds.", Use: "fadeout [bus number] --duration [seconds] [target level in dB]", Example: ` # Fade out bus 1 over 5 seconds - xair-cli bus fadeout 1 --duration 5 -- -90.0`, + xair-cli bus fadeout 1 --duration 5s -- -90.0`, RunE: func(cmd *cobra.Command, args []string) error { client := ClientFromContext(cmd.Context()) if client == nil { @@ -117,7 +117,7 @@ var busFadeOutCmd = &cobra.Command{ busIndex := mustConvToInt(args[0]) - duration, err := cmd.Flags().GetFloat64("duration") + duration, err := cmd.Flags().GetDuration("duration") if err != nil { return fmt.Errorf("Error getting duration flag: %w", err) } @@ -139,7 +139,7 @@ var busFadeOutCmd = &cobra.Command{ return nil } - stepDelay := time.Duration(duration*1000/totalSteps) * time.Millisecond + stepDelay := time.Duration(duration.Seconds()*1000/totalSteps) * time.Millisecond for currentFader > target { currentFader -= 1.0 @@ -161,7 +161,7 @@ var busFadeInCmd = &cobra.Command{ Long: "Fade in the bus fader to maximum level over a specified duration in seconds.", Use: "fadein [bus number] --duration [seconds] [target level in dB]", Example: ` # Fade in bus 1 over 5 seconds - xair-cli bus fadein 1 --duration 5 -- 0.0`, + xair-cli bus fadein 1 --duration 5s -- 0.0`, RunE: func(cmd *cobra.Command, args []string) error { client := ClientFromContext(cmd.Context()) if client == nil { @@ -174,7 +174,7 @@ var busFadeInCmd = &cobra.Command{ busIndex := mustConvToInt(args[0]) - duration, err := cmd.Flags().GetFloat64("duration") + duration, err := cmd.Flags().GetDuration("duration") if err != nil { return fmt.Errorf("Error getting duration flag: %w", err) } @@ -196,7 +196,7 @@ var busFadeInCmd = &cobra.Command{ return nil } - stepDelay := time.Duration(duration*1000/totalSteps) * time.Millisecond + stepDelay := time.Duration(duration.Seconds()*1000/totalSteps) * time.Millisecond for currentFader < target { currentFader += 1.0 @@ -261,9 +261,9 @@ func init() { busCmd.AddCommand(busFaderCmd) busCmd.AddCommand(busFadeOutCmd) - busFadeOutCmd.Flags().Float64P("duration", "d", 5.0, "Duration for fade out in seconds") + busFadeOutCmd.Flags().DurationP("duration", "d", 5*time.Second, "Duration for fade out in seconds") busCmd.AddCommand(busFadeInCmd) - busFadeInCmd.Flags().Float64P("duration", "d", 5.0, "Duration for fade in in seconds") + busFadeInCmd.Flags().DurationP("duration", "d", 5*time.Second, "Duration for fade in in seconds") busCmd.AddCommand(busNameCmd) } diff --git a/cmd/headamp.go b/cmd/headamp.go index 223f14d..fb307fa 100644 --- a/cmd/headamp.go +++ b/cmd/headamp.go @@ -2,7 +2,10 @@ package cmd import ( "fmt" + "time" + "github.com/charmbracelet/log" + "github.com/onyx-and-iris/xair-cli/internal/xair" "github.com/spf13/cobra" ) @@ -23,12 +26,16 @@ var headampGainCmd = &cobra.Command{ Use: "gain", Short: "Get or set headamp gain level", Long: `Get or set the gain level for a specified headamp index. +When setting gain, it will gradually increase from the current level to prevent +sudden jumps that could cause feedback or equipment damage. Examples: # Get gain level for headamp index 1 - xairctl headamp gain 1 - # Set gain level for headamp index 1 to 3.5 dB - xairctl headamp gain 1 3.5`, + xair-cli headamp gain 1 + # Set gain level for headamp index 1 to 3.5 dB (gradually over 5 seconds) + xair-cli headamp gain 1 3.5 + # Set gain level for headamp index 1 to 3.5 dB over 10 seconds + xair-cli headamp gain 1 3.5 --duration 10s`, Args: cobra.RangeArgs(1, 2), RunE: func(cmd *cobra.Command, args []string) error { client := ClientFromContext(cmd.Context()) @@ -55,18 +62,79 @@ Examples: return fmt.Errorf("Please provide a gain level in dB") } - level := mustConvToFloat64(args[1]) + targetLevel := mustConvToFloat64(args[1]) - err := client.HeadAmp.SetGain(index, level) + currentGain, err := client.HeadAmp.Gain(index) if err != nil { - return fmt.Errorf("Error setting headamp gain level: %w", err) + return fmt.Errorf("Error getting current headamp gain level: %w", err) } - cmd.Printf("Headamp %d Gain set to %.2f dB\n", index, level) + duration, err := cmd.Flags().GetDuration("duration") + if err != nil { + return fmt.Errorf("Error getting duration flag: %w", err) + } + + if currentGain == targetLevel { + cmd.Printf("Headamp %d Gain already at %.2f dB\n", index, targetLevel) + return nil + } + + if err := gradualGainAdjust(client, cmd, index, currentGain, targetLevel, duration); err != nil { + return fmt.Errorf("Error adjusting headamp gain level: %w", err) + } + + cmd.Printf("Headamp %d Gain set to %.2f dB\n", index, targetLevel) return nil }, } +// gradualGainAdjust gradually adjusts gain from current to target over specified duration +func gradualGainAdjust( + client *xair.Client, + cmd *cobra.Command, + index int, + currentGain, targetGain float64, + duration time.Duration, +) error { + gainDiff := targetGain - currentGain + + stepInterval := 100 * time.Millisecond + totalSteps := int(duration / stepInterval) + + if totalSteps < 1 { + totalSteps = 1 + stepInterval = duration + } + + stepIncrement := gainDiff / float64(totalSteps) + + log.Debugf("Adjusting Headamp %d gain from %.2f dB to %.2f dB over %v...\n", + index, currentGain, targetGain, duration) + + for step := 1; step <= totalSteps; step++ { + newGain := currentGain + (stepIncrement * float64(step)) + + if step == totalSteps { + newGain = targetGain + } + + err := client.HeadAmp.SetGain(index, newGain) + if err != nil { + return err + } + + if step%10 == 0 || step == totalSteps { + log.Debugf(" Step %d/%d: %.2f dB\n", step, totalSteps, newGain) + } + + if step < totalSteps { + time.Sleep(stepInterval) + } + } + + return nil +} + // headampPhantomPowerCmd represents the headamp phantom power command var headampPhantomPowerCmd = &cobra.Command{ Use: "phantom", @@ -137,5 +205,7 @@ func init() { rootCmd.AddCommand(headampCmd) headampCmd.AddCommand(headampGainCmd) + headampGainCmd.Flags().DurationP("duration", "d", 5*time.Second, "Duration over which to gradually adjust gain") + headampCmd.AddCommand(headampPhantomPowerCmd) } diff --git a/cmd/main.go b/cmd/main.go index 1c683ce..69479d8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -116,7 +116,7 @@ This command will fade out the main output to the specified dB level. `, Use: "fadeout --duration [seconds] [target_db]", Example: ` # Fade out main output over 5 seconds - xair-cli main fadeout --duration 5 -- -90.0`, + xair-cli main fadeout --duration 5s -- -90.0`, Run: func(cmd *cobra.Command, args []string) { client := ClientFromContext(cmd.Context()) if client == nil { @@ -124,7 +124,7 @@ This command will fade out the main output to the specified dB level. return } - duration, err := cmd.Flags().GetFloat64("duration") + duration, err := cmd.Flags().GetDuration("duration") if err != nil { cmd.PrintErrln("Error getting duration flag:", err) return @@ -150,7 +150,7 @@ This command will fade out the main output to the specified dB level. } // Calculate delay per step to achieve exact duration - stepDelay := time.Duration(duration*1000/totalSteps) * time.Millisecond + stepDelay := time.Duration(duration.Seconds()*1000/totalSteps) * time.Millisecond for currentFader > target { currentFader -= 1.0 @@ -175,7 +175,7 @@ This command will fade in the main output to the specified dB level. `, Use: "fadein --duration [seconds] [target_db]", Example: ` # Fade in main output over 5 seconds - xair-cli main fadein --duration 5 -- 0.0`, + xair-cli main fadein --duration 5s -- 0.0`, Run: func(cmd *cobra.Command, args []string) { client := ClientFromContext(cmd.Context()) if client == nil { @@ -183,7 +183,7 @@ This command will fade in the main output to the specified dB level. return } - duration, err := cmd.Flags().GetFloat64("duration") + duration, err := cmd.Flags().GetDuration("duration") if err != nil { cmd.PrintErrln("Error getting duration flag:", err) return @@ -208,7 +208,7 @@ This command will fade in the main output to the specified dB level. } // Calculate delay per step to achieve exact duration - stepDelay := time.Duration(duration*1000/totalSteps) * time.Millisecond + stepDelay := time.Duration(duration.Seconds()*1000/totalSteps) * time.Millisecond for currentFader < target { currentFader += 1.0 @@ -231,7 +231,7 @@ func init() { mainCmd.AddCommand(mainFaderCmd) mainCmd.AddCommand(mainFadeOutCmd) - mainFadeOutCmd.Flags().Float64P("duration", "d", 5, "Duration for fade out in seconds") + mainFadeOutCmd.Flags().DurationP("duration", "d", 5*time.Second, "Duration for fade out in seconds") mainCmd.AddCommand(mainFadeInCmd) - mainFadeInCmd.Flags().Float64P("duration", "d", 5, "Duration for fade in in seconds") + mainFadeInCmd.Flags().DurationP("duration", "d", 5*time.Second, "Duration for fade in in seconds") } diff --git a/cmd/strip.go b/cmd/strip.go index f302ec2..9e56955 100644 --- a/cmd/strip.go +++ b/cmd/strip.go @@ -134,7 +134,7 @@ var stripFadeOutCmd = &cobra.Command{ Long: "Fade out the strip over a specified duration in seconds.", Use: "fadeout [strip number] --duration [seconds] [target level in dB]", Example: ` # Fade out strip 1 over 5 seconds - xair-cli strip fadeout 1 --duration 5.0 -- -90.0`, + xair-cli strip fadeout 1 --duration 5s -- -90.0`, RunE: func(cmd *cobra.Command, args []string) error { client := ClientFromContext(cmd.Context()) if client == nil { @@ -147,7 +147,7 @@ var stripFadeOutCmd = &cobra.Command{ stripIndex := mustConvToInt(args[0]) - duration, err := cmd.Flags().GetFloat64("duration") + duration, err := cmd.Flags().GetDuration("duration") if err != nil { return fmt.Errorf("Error getting duration flag: %w", err) } @@ -168,7 +168,7 @@ var stripFadeOutCmd = &cobra.Command{ return nil } - stepDelay := time.Duration(duration*1000/totalSteps) * time.Millisecond + stepDelay := time.Duration(duration.Seconds()*1000/totalSteps) * time.Millisecond for currentFader > target { currentFader -= 1.0 @@ -179,7 +179,7 @@ var stripFadeOutCmd = &cobra.Command{ time.Sleep(stepDelay) } - cmd.Printf("Strip %d faded out to %.2f dB over %.2f seconds\n", stripIndex, target, duration) + cmd.Printf("Strip %d faded out to %.2f dB over %.2f seconds\n", stripIndex, target, duration.Seconds()) return nil }, } @@ -190,7 +190,7 @@ var stripFadeInCmd = &cobra.Command{ Long: "Fade in the strip over a specified duration in seconds.", Use: "fadein [strip number] --duration [seconds] [target level in dB]", Example: ` # Fade in strip 1 over 5 seconds - xair-cli strip fadein 1 --duration 5.0 0`, + xair-cli strip fadein 1 --duration 5s 0`, RunE: func(cmd *cobra.Command, args []string) error { client := ClientFromContext(cmd.Context()) if client == nil { @@ -340,9 +340,9 @@ func init() { stripCmd.AddCommand(stripFaderCmd) stripCmd.AddCommand(stripFadeOutCmd) - stripFadeOutCmd.Flags().Float64P("duration", "d", 5.0, "Duration of the fade out in seconds") + stripFadeOutCmd.Flags().DurationP("duration", "d", 5*time.Second, "Duration of the fade out in seconds") stripCmd.AddCommand(stripFadeInCmd) - stripFadeInCmd.Flags().Float64P("duration", "d", 5.0, "Duration of the fade in in seconds") + stripFadeInCmd.Flags().DurationP("duration", "d", 5*time.Second, "Duration of the fade in in seconds") stripCmd.AddCommand(stripSendCmd)