Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

174 lines
5.3KB

  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"` // linear injection level in composite (e.g. 0.1 = 10%)
  34. RDSInjection float64 `json:"rdsInjection"` // linear injection level in composite (e.g. 0.05 = 5%)
  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. }
  43. type BackendConfig struct {
  44. Kind string `json:"kind"`
  45. Device string `json:"device"`
  46. OutputPath string `json:"outputPath"`
  47. DeviceSampleRateHz float64 `json:"deviceSampleRateHz"` // actual SDR device rate; 0 = same as compositeRateHz
  48. }
  49. type ControlConfig struct {
  50. ListenAddress string `json:"listenAddress"`
  51. }
  52. func Default() Config {
  53. return Config{
  54. Audio: AudioConfig{Gain: 1.0, ToneLeftHz: 1000, ToneRightHz: 1600, ToneAmplitude: 0.4},
  55. RDS: RDSConfig{Enabled: true, PI: "1234", PS: "FMRTX", RadioText: "fm-rds-tx", PTY: 0},
  56. FM: FMConfig{
  57. FrequencyMHz: 100.0,
  58. StereoEnabled: true,
  59. PilotLevel: 0.1,
  60. RDSInjection: 0.05,
  61. PreEmphasisTauUS: 50,
  62. OutputDrive: 0.5,
  63. CompositeRateHz: 228000,
  64. MaxDeviationHz: 75000,
  65. LimiterEnabled: true,
  66. LimiterCeiling: 1.0,
  67. FMModulationEnabled: true,
  68. },
  69. Backend: BackendConfig{Kind: "file", OutputPath: "build/out/composite.f32"},
  70. Control: ControlConfig{ListenAddress: "127.0.0.1:8088"},
  71. }
  72. }
  73. // ParsePI parses a hex PI code string. Returns an error for invalid input.
  74. func ParsePI(pi string) (uint16, error) {
  75. trimmed := strings.TrimSpace(pi)
  76. if trimmed == "" {
  77. return 0, fmt.Errorf("rds.pi is required")
  78. }
  79. trimmed = strings.TrimPrefix(trimmed, "0x")
  80. trimmed = strings.TrimPrefix(trimmed, "0X")
  81. v, err := strconv.ParseUint(trimmed, 16, 16)
  82. if err != nil {
  83. return 0, fmt.Errorf("invalid rds.pi: %q", pi)
  84. }
  85. return uint16(v), nil
  86. }
  87. func Load(path string) (Config, error) {
  88. cfg := Default()
  89. if path == "" {
  90. return cfg, cfg.Validate()
  91. }
  92. data, err := os.ReadFile(path)
  93. if err != nil {
  94. return Config{}, err
  95. }
  96. if err := json.Unmarshal(data, &cfg); err != nil {
  97. return Config{}, err
  98. }
  99. return cfg, cfg.Validate()
  100. }
  101. func (c Config) Validate() error {
  102. if c.Audio.Gain < 0 || c.Audio.Gain > 4 {
  103. return fmt.Errorf("audio.gain out of range")
  104. }
  105. if c.Audio.ToneLeftHz <= 0 || c.Audio.ToneRightHz <= 0 {
  106. return fmt.Errorf("audio tone frequencies must be positive")
  107. }
  108. if c.Audio.ToneAmplitude < 0 || c.Audio.ToneAmplitude > 1 {
  109. return fmt.Errorf("audio.toneAmplitude out of range")
  110. }
  111. if c.FM.FrequencyMHz < 65 || c.FM.FrequencyMHz > 110 {
  112. return fmt.Errorf("fm.frequencyMHz out of range")
  113. }
  114. if c.FM.PilotLevel < 0 || c.FM.PilotLevel > 0.2 {
  115. return fmt.Errorf("fm.pilotLevel out of range")
  116. }
  117. if c.FM.RDSInjection < 0 || c.FM.RDSInjection > 0.15 {
  118. return fmt.Errorf("fm.rdsInjection out of range")
  119. }
  120. if c.FM.OutputDrive < 0 || c.FM.OutputDrive > 1 {
  121. return fmt.Errorf("fm.outputDrive out of range")
  122. }
  123. if c.FM.CompositeRateHz < 96000 || c.FM.CompositeRateHz > 1520000 {
  124. return fmt.Errorf("fm.compositeRateHz out of range")
  125. }
  126. if c.FM.PreEmphasisTauUS < 0 || c.FM.PreEmphasisTauUS > 100 {
  127. return fmt.Errorf("fm.preEmphasisTauUS out of range (0=off, 50=EU, 75=US)")
  128. }
  129. if c.FM.MaxDeviationHz < 0 || c.FM.MaxDeviationHz > 150000 {
  130. return fmt.Errorf("fm.maxDeviationHz out of range")
  131. }
  132. if c.FM.LimiterCeiling < 0 || c.FM.LimiterCeiling > 2 {
  133. return fmt.Errorf("fm.limiterCeiling out of range")
  134. }
  135. if c.Backend.Kind == "" {
  136. return fmt.Errorf("backend.kind is required")
  137. }
  138. if c.Backend.DeviceSampleRateHz < 0 {
  139. return fmt.Errorf("backend.deviceSampleRateHz must be >= 0")
  140. }
  141. if c.Control.ListenAddress == "" {
  142. return fmt.Errorf("control.listenAddress is required")
  143. }
  144. // Fail-loud PI validation
  145. if c.RDS.Enabled {
  146. if _, err := ParsePI(c.RDS.PI); err != nil {
  147. return fmt.Errorf("rds config: %w", err)
  148. }
  149. }
  150. if c.RDS.PTY < 0 || c.RDS.PTY > 31 {
  151. return fmt.Errorf("rds.pty out of range (0-31)")
  152. }
  153. return nil
  154. }
  155. // EffectiveDeviceRate returns the device sample rate, falling back to composite rate.
  156. func (c Config) EffectiveDeviceRate() float64 {
  157. if c.Backend.DeviceSampleRateHz > 0 {
  158. return c.Backend.DeviceSampleRateHz
  159. }
  160. return float64(c.FM.CompositeRateHz)
  161. }