Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

185 rindas
6.1KB

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