Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.7KB

  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. )
  9. type Config struct {
  10. Audio AudioConfig `json:"audio"`
  11. RDS RDSConfig `json:"rds"`
  12. FM FMConfig `json:"fm"`
  13. Backend BackendConfig `json:"backend"`
  14. Control ControlConfig `json:"control"`
  15. }
  16. type AudioConfig struct {
  17. InputPath string `json:"inputPath"`
  18. Gain float64 `json:"gain"`
  19. ToneLeftHz float64 `json:"toneLeftHz"`
  20. ToneRightHz float64 `json:"toneRightHz"`
  21. ToneAmplitude float64 `json:"toneAmplitude"`
  22. }
  23. type RDSConfig struct {
  24. Enabled bool `json:"enabled"`
  25. PI string `json:"pi"`
  26. PS string `json:"ps"`
  27. RadioText string `json:"radioText"`
  28. PTY int `json:"pty"`
  29. }
  30. type FMConfig struct {
  31. FrequencyMHz float64 `json:"frequencyMHz"`
  32. StereoEnabled bool `json:"stereoEnabled"`
  33. PilotLevel float64 `json:"pilotLevel"` // fraction of ±75kHz deviation (0.09 = 9%, ITU standard)
  34. RDSInjection float64 `json:"rdsInjection"` // fraction of ±75kHz deviation (0.04 = 4%, typical)
  35. PreEmphasisTauUS float64 `json:"preEmphasisTauUS"` // time constant in µs: 50 (EU) or 75 (US), 0=off
  36. OutputDrive float64 `json:"outputDrive"`
  37. CompositeRateHz int `json:"compositeRateHz"` // internal DSP/MPX sample rate
  38. MaxDeviationHz float64 `json:"maxDeviationHz"`
  39. LimiterEnabled bool `json:"limiterEnabled"`
  40. LimiterCeiling float64 `json:"limiterCeiling"`
  41. FMModulationEnabled bool `json:"fmModulationEnabled"`
  42. MpxGain float64 `json:"mpxGain"` // hardware calibration: scales entire composite output (default 1.0)
  43. }
  44. type BackendConfig struct {
  45. Kind string `json:"kind"`
  46. Device string `json:"device"`
  47. OutputPath string `json:"outputPath"`
  48. DeviceSampleRateHz float64 `json:"deviceSampleRateHz"` // actual SDR device rate; 0 = same as compositeRateHz
  49. }
  50. type ControlConfig struct {
  51. ListenAddress string `json:"listenAddress"`
  52. }
  53. func Default() Config {
  54. return Config{
  55. Audio: AudioConfig{Gain: 1.0, ToneLeftHz: 1000, ToneRightHz: 1600, ToneAmplitude: 0.4},
  56. RDS: RDSConfig{Enabled: true, PI: "1234", PS: "FMRTX", RadioText: "fm-rds-tx", PTY: 0},
  57. FM: FMConfig{
  58. FrequencyMHz: 100.0,
  59. StereoEnabled: true,
  60. PilotLevel: 0.09,
  61. RDSInjection: 0.04,
  62. PreEmphasisTauUS: 50,
  63. OutputDrive: 0.5,
  64. CompositeRateHz: 228000,
  65. MaxDeviationHz: 75000,
  66. LimiterEnabled: true,
  67. LimiterCeiling: 1.0,
  68. FMModulationEnabled: true,
  69. MpxGain: 1.0,
  70. },
  71. Backend: BackendConfig{Kind: "file", OutputPath: "build/out/composite.f32"},
  72. Control: ControlConfig{ListenAddress: "127.0.0.1:8088"},
  73. }
  74. }
  75. // ParsePI parses a hex PI code string. Returns an error for invalid input.
  76. func ParsePI(pi string) (uint16, error) {
  77. trimmed := strings.TrimSpace(pi)
  78. if trimmed == "" {
  79. return 0, fmt.Errorf("rds.pi is required")
  80. }
  81. trimmed = strings.TrimPrefix(trimmed, "0x")
  82. trimmed = strings.TrimPrefix(trimmed, "0X")
  83. v, err := strconv.ParseUint(trimmed, 16, 16)
  84. if err != nil {
  85. return 0, fmt.Errorf("invalid rds.pi: %q", pi)
  86. }
  87. return uint16(v), nil
  88. }
  89. func Load(path string) (Config, error) {
  90. cfg := Default()
  91. if path == "" {
  92. return cfg, cfg.Validate()
  93. }
  94. data, err := os.ReadFile(path)
  95. if err != nil {
  96. return Config{}, err
  97. }
  98. if err := json.Unmarshal(data, &cfg); err != nil {
  99. return Config{}, err
  100. }
  101. return cfg, cfg.Validate()
  102. }
  103. func (c Config) Validate() error {
  104. if c.Audio.Gain < 0 || c.Audio.Gain > 4 {
  105. return fmt.Errorf("audio.gain out of range")
  106. }
  107. if c.Audio.ToneLeftHz <= 0 || c.Audio.ToneRightHz <= 0 {
  108. return fmt.Errorf("audio tone frequencies must be positive")
  109. }
  110. if c.Audio.ToneAmplitude < 0 || c.Audio.ToneAmplitude > 1 {
  111. return fmt.Errorf("audio.toneAmplitude out of range")
  112. }
  113. if c.FM.FrequencyMHz < 65 || c.FM.FrequencyMHz > 110 {
  114. return fmt.Errorf("fm.frequencyMHz out of range")
  115. }
  116. if c.FM.PilotLevel < 0 || c.FM.PilotLevel > 0.2 {
  117. return fmt.Errorf("fm.pilotLevel out of range")
  118. }
  119. if c.FM.RDSInjection < 0 || c.FM.RDSInjection > 0.15 {
  120. return fmt.Errorf("fm.rdsInjection out of range")
  121. }
  122. if c.FM.OutputDrive < 0 || c.FM.OutputDrive > 10 {
  123. return fmt.Errorf("fm.outputDrive out of range (0..3)")
  124. }
  125. if c.FM.CompositeRateHz < 96000 || c.FM.CompositeRateHz > 1520000 {
  126. return fmt.Errorf("fm.compositeRateHz out of range")
  127. }
  128. if c.FM.PreEmphasisTauUS < 0 || c.FM.PreEmphasisTauUS > 100 {
  129. return fmt.Errorf("fm.preEmphasisTauUS out of range (0=off, 50=EU, 75=US)")
  130. }
  131. if c.FM.MaxDeviationHz < 0 || c.FM.MaxDeviationHz > 150000 {
  132. return fmt.Errorf("fm.maxDeviationHz out of range")
  133. }
  134. if c.FM.LimiterCeiling < 0 || c.FM.LimiterCeiling > 2 {
  135. return fmt.Errorf("fm.limiterCeiling out of range")
  136. }
  137. if c.FM.MpxGain == 0 { c.FM.MpxGain = 1.0 } // default if omitted from JSON
  138. if c.FM.MpxGain < 0.1 || c.FM.MpxGain > 5 {
  139. return fmt.Errorf("fm.mpxGain out of range (0.1..5)")
  140. }
  141. if c.Backend.Kind == "" {
  142. return fmt.Errorf("backend.kind is required")
  143. }
  144. if c.Backend.DeviceSampleRateHz < 0 {
  145. return fmt.Errorf("backend.deviceSampleRateHz must be >= 0")
  146. }
  147. if c.Control.ListenAddress == "" {
  148. return fmt.Errorf("control.listenAddress is required")
  149. }
  150. // Fail-loud PI validation
  151. if c.RDS.Enabled {
  152. if _, err := ParsePI(c.RDS.PI); err != nil {
  153. return fmt.Errorf("rds config: %w", err)
  154. }
  155. }
  156. if c.RDS.PTY < 0 || c.RDS.PTY > 31 {
  157. return fmt.Errorf("rds.pty out of range (0-31)")
  158. }
  159. return nil
  160. }
  161. // EffectiveDeviceRate returns the device sample rate, falling back to composite rate.
  162. func (c Config) EffectiveDeviceRate() float64 {
  163. if c.Backend.DeviceSampleRateHz > 0 {
  164. return c.Backend.DeviceSampleRateHz
  165. }
  166. return float64(c.FM.CompositeRateHz)
  167. }