Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

398 Zeilen
14KB

  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. Runtime RuntimeConfig `json:"runtime"`
  16. Ingest IngestConfig `json:"ingest"`
  17. }
  18. type AudioConfig struct {
  19. InputPath string `json:"inputPath"`
  20. Gain float64 `json:"gain"`
  21. ToneLeftHz float64 `json:"toneLeftHz"`
  22. ToneRightHz float64 `json:"toneRightHz"`
  23. ToneAmplitude float64 `json:"toneAmplitude"`
  24. }
  25. type RDSConfig struct {
  26. Enabled bool `json:"enabled"`
  27. PI string `json:"pi"`
  28. PS string `json:"ps"`
  29. RadioText string `json:"radioText"`
  30. PTY int `json:"pty"`
  31. }
  32. type FMConfig struct {
  33. FrequencyMHz float64 `json:"frequencyMHz"`
  34. StereoEnabled bool `json:"stereoEnabled"`
  35. PilotLevel float64 `json:"pilotLevel"` // fraction of ±75kHz deviation (0.09 = 9%, ITU standard)
  36. RDSInjection float64 `json:"rdsInjection"` // fraction of ±75kHz deviation (0.04 = 4%, typical)
  37. PreEmphasisTauUS float64 `json:"preEmphasisTauUS"` // time constant in µs: 50 (EU) or 75 (US), 0=off
  38. OutputDrive float64 `json:"outputDrive"`
  39. CompositeRateHz int `json:"compositeRateHz"` // internal DSP/MPX sample rate
  40. MaxDeviationHz float64 `json:"maxDeviationHz"`
  41. LimiterEnabled bool `json:"limiterEnabled"`
  42. LimiterCeiling float64 `json:"limiterCeiling"`
  43. FMModulationEnabled bool `json:"fmModulationEnabled"`
  44. MpxGain float64 `json:"mpxGain"` // hardware calibration: scales entire composite output (default 1.0)
  45. BS412Enabled bool `json:"bs412Enabled"` // ITU-R BS.412 MPX power limiter (EU requirement)
  46. BS412ThresholdDBr float64 `json:"bs412ThresholdDBr"` // power limit in dBr (0 = standard, +3 = relaxed)
  47. }
  48. type BackendConfig struct {
  49. Kind string `json:"kind"`
  50. Driver string `json:"driver,omitempty"`
  51. Device string `json:"device"`
  52. URI string `json:"uri,omitempty"`
  53. DeviceArgs map[string]string `json:"deviceArgs,omitempty"`
  54. OutputPath string `json:"outputPath"`
  55. DeviceSampleRateHz float64 `json:"deviceSampleRateHz"` // actual SDR device rate; 0 = same as compositeRateHz
  56. }
  57. type ControlConfig struct {
  58. ListenAddress string `json:"listenAddress"`
  59. }
  60. type RuntimeConfig struct {
  61. FrameQueueCapacity int `json:"frameQueueCapacity"`
  62. }
  63. type IngestConfig struct {
  64. Kind string `json:"kind"`
  65. PrebufferMs int `json:"prebufferMs"`
  66. StallTimeoutMs int `json:"stallTimeoutMs"`
  67. Reconnect IngestReconnectConfig `json:"reconnect"`
  68. Stdin IngestPCMConfig `json:"stdin"`
  69. HTTPRaw IngestPCMConfig `json:"httpRaw"`
  70. Icecast IngestIcecastConfig `json:"icecast"`
  71. SRT IngestSRTConfig `json:"srt"`
  72. AES67 IngestAES67Config `json:"aes67"`
  73. }
  74. type IngestReconnectConfig struct {
  75. Enabled bool `json:"enabled"`
  76. InitialBackoffMs int `json:"initialBackoffMs"`
  77. MaxBackoffMs int `json:"maxBackoffMs"`
  78. }
  79. type IngestPCMConfig struct {
  80. SampleRateHz int `json:"sampleRateHz"`
  81. Channels int `json:"channels"`
  82. Format string `json:"format"`
  83. }
  84. type IngestIcecastConfig struct {
  85. URL string `json:"url"`
  86. Decoder string `json:"decoder"`
  87. RadioText IngestIcecastRadioTextConfig `json:"radioText"`
  88. }
  89. type IngestIcecastRadioTextConfig struct {
  90. Enabled bool `json:"enabled"`
  91. Prefix string `json:"prefix"`
  92. MaxLen int `json:"maxLen"`
  93. OnlyOnChange bool `json:"onlyOnChange"`
  94. }
  95. type IngestSRTConfig struct {
  96. URL string `json:"url"`
  97. Mode string `json:"mode"`
  98. SampleRateHz int `json:"sampleRateHz"`
  99. Channels int `json:"channels"`
  100. }
  101. type IngestAES67Config struct {
  102. SDPPath string `json:"sdpPath"`
  103. SDP string `json:"sdp"`
  104. MulticastGroup string `json:"multicastGroup"`
  105. Port int `json:"port"`
  106. InterfaceName string `json:"interfaceName"`
  107. PayloadType int `json:"payloadType"`
  108. SampleRateHz int `json:"sampleRateHz"`
  109. Channels int `json:"channels"`
  110. Encoding string `json:"encoding"`
  111. PacketTimeMs int `json:"packetTimeMs"`
  112. JitterDepthPackets int `json:"jitterDepthPackets"`
  113. ReadBufferBytes int `json:"readBufferBytes"`
  114. }
  115. func Default() Config {
  116. return Config{
  117. Audio: AudioConfig{Gain: 1.0, ToneLeftHz: 1000, ToneRightHz: 1600, ToneAmplitude: 0.4},
  118. RDS: RDSConfig{Enabled: true, PI: "1234", PS: "FMRTX", RadioText: "fm-rds-tx", PTY: 0},
  119. FM: FMConfig{
  120. FrequencyMHz: 100.0,
  121. StereoEnabled: true,
  122. PilotLevel: 0.09,
  123. RDSInjection: 0.04,
  124. PreEmphasisTauUS: 50,
  125. OutputDrive: 0.5,
  126. CompositeRateHz: 228000,
  127. MaxDeviationHz: 75000,
  128. LimiterEnabled: true,
  129. LimiterCeiling: 1.0,
  130. FMModulationEnabled: true,
  131. MpxGain: 1.0,
  132. },
  133. Backend: BackendConfig{Kind: "file", OutputPath: "build/out/composite.f32"},
  134. Control: ControlConfig{ListenAddress: "127.0.0.1:8088"},
  135. Runtime: RuntimeConfig{FrameQueueCapacity: 3},
  136. Ingest: IngestConfig{
  137. Kind: "none",
  138. PrebufferMs: 1500,
  139. StallTimeoutMs: 3000,
  140. Reconnect: IngestReconnectConfig{
  141. Enabled: true,
  142. InitialBackoffMs: 1000,
  143. MaxBackoffMs: 15000,
  144. },
  145. Stdin: IngestPCMConfig{
  146. SampleRateHz: 44100,
  147. Channels: 2,
  148. Format: "s16le",
  149. },
  150. HTTPRaw: IngestPCMConfig{
  151. SampleRateHz: 44100,
  152. Channels: 2,
  153. Format: "s16le",
  154. },
  155. Icecast: IngestIcecastConfig{
  156. Decoder: "auto",
  157. RadioText: IngestIcecastRadioTextConfig{
  158. Enabled: false,
  159. MaxLen: 64,
  160. OnlyOnChange: true,
  161. },
  162. },
  163. SRT: IngestSRTConfig{
  164. Mode: "listener",
  165. SampleRateHz: 48000,
  166. Channels: 2,
  167. },
  168. AES67: IngestAES67Config{
  169. PayloadType: 97,
  170. SampleRateHz: 48000,
  171. Channels: 2,
  172. Encoding: "L24",
  173. PacketTimeMs: 1,
  174. JitterDepthPackets: 8,
  175. ReadBufferBytes: 1 << 20,
  176. },
  177. },
  178. }
  179. }
  180. // ParsePI parses a hex PI code string. Returns an error for invalid input.
  181. func ParsePI(pi string) (uint16, error) {
  182. trimmed := strings.TrimSpace(pi)
  183. if trimmed == "" {
  184. return 0, fmt.Errorf("rds.pi is required")
  185. }
  186. trimmed = strings.TrimPrefix(trimmed, "0x")
  187. trimmed = strings.TrimPrefix(trimmed, "0X")
  188. v, err := strconv.ParseUint(trimmed, 16, 16)
  189. if err != nil {
  190. return 0, fmt.Errorf("invalid rds.pi: %q", pi)
  191. }
  192. return uint16(v), nil
  193. }
  194. func Load(path string) (Config, error) {
  195. cfg := Default()
  196. if path == "" {
  197. return cfg, cfg.Validate()
  198. }
  199. data, err := os.ReadFile(path)
  200. if err != nil {
  201. return Config{}, err
  202. }
  203. if err := json.Unmarshal(data, &cfg); err != nil {
  204. return Config{}, err
  205. }
  206. return cfg, cfg.Validate()
  207. }
  208. func (c Config) Validate() error {
  209. if c.Audio.Gain < 0 || c.Audio.Gain > 4 {
  210. return fmt.Errorf("audio.gain out of range")
  211. }
  212. if c.Audio.ToneLeftHz <= 0 || c.Audio.ToneRightHz <= 0 {
  213. return fmt.Errorf("audio tone frequencies must be positive")
  214. }
  215. if c.Audio.ToneAmplitude < 0 || c.Audio.ToneAmplitude > 1 {
  216. return fmt.Errorf("audio.toneAmplitude out of range")
  217. }
  218. if c.FM.FrequencyMHz < 65 || c.FM.FrequencyMHz > 110 {
  219. return fmt.Errorf("fm.frequencyMHz out of range")
  220. }
  221. if c.FM.PilotLevel < 0 || c.FM.PilotLevel > 0.2 {
  222. return fmt.Errorf("fm.pilotLevel out of range")
  223. }
  224. if c.FM.RDSInjection < 0 || c.FM.RDSInjection > 0.15 {
  225. return fmt.Errorf("fm.rdsInjection out of range")
  226. }
  227. if c.FM.OutputDrive < 0 || c.FM.OutputDrive > 10 {
  228. return fmt.Errorf("fm.outputDrive out of range (0..10)")
  229. }
  230. if c.FM.CompositeRateHz < 96000 || c.FM.CompositeRateHz > 1520000 {
  231. return fmt.Errorf("fm.compositeRateHz out of range")
  232. }
  233. if c.FM.PreEmphasisTauUS < 0 || c.FM.PreEmphasisTauUS > 100 {
  234. return fmt.Errorf("fm.preEmphasisTauUS out of range (0=off, 50=EU, 75=US)")
  235. }
  236. if c.FM.MaxDeviationHz < 0 || c.FM.MaxDeviationHz > 150000 {
  237. return fmt.Errorf("fm.maxDeviationHz out of range")
  238. }
  239. if c.FM.LimiterCeiling < 0 || c.FM.LimiterCeiling > 2 {
  240. return fmt.Errorf("fm.limiterCeiling out of range")
  241. }
  242. if c.FM.MpxGain < 0.1 || c.FM.MpxGain > 5 {
  243. return fmt.Errorf("fm.mpxGain out of range (0.1..5)")
  244. }
  245. if c.Backend.Kind == "" {
  246. return fmt.Errorf("backend.kind is required")
  247. }
  248. if c.Backend.DeviceSampleRateHz < 0 {
  249. return fmt.Errorf("backend.deviceSampleRateHz must be >= 0")
  250. }
  251. if c.Control.ListenAddress == "" {
  252. return fmt.Errorf("control.listenAddress is required")
  253. }
  254. if c.Runtime.FrameQueueCapacity <= 0 {
  255. return fmt.Errorf("runtime.frameQueueCapacity must be > 0")
  256. }
  257. if c.Ingest.Kind == "" {
  258. c.Ingest.Kind = "none"
  259. }
  260. ingestKind := strings.ToLower(strings.TrimSpace(c.Ingest.Kind))
  261. switch ingestKind {
  262. case "none", "stdin", "stdin-pcm", "http-raw", "icecast", "srt", "aes67", "aoip", "aoip-rtp":
  263. default:
  264. return fmt.Errorf("ingest.kind unsupported: %s", c.Ingest.Kind)
  265. }
  266. if c.Ingest.PrebufferMs < 0 {
  267. return fmt.Errorf("ingest.prebufferMs must be >= 0")
  268. }
  269. if c.Ingest.StallTimeoutMs < 0 {
  270. return fmt.Errorf("ingest.stallTimeoutMs must be >= 0")
  271. }
  272. if c.Ingest.Reconnect.InitialBackoffMs < 0 || c.Ingest.Reconnect.MaxBackoffMs < 0 {
  273. return fmt.Errorf("ingest.reconnect backoff must be >= 0")
  274. }
  275. if c.Ingest.Reconnect.Enabled && c.Ingest.Reconnect.InitialBackoffMs <= 0 {
  276. return fmt.Errorf("ingest.reconnect.initialBackoffMs must be > 0 when reconnect is enabled")
  277. }
  278. if c.Ingest.Reconnect.Enabled && c.Ingest.Reconnect.MaxBackoffMs <= 0 {
  279. return fmt.Errorf("ingest.reconnect.maxBackoffMs must be > 0 when reconnect is enabled")
  280. }
  281. if c.Ingest.Reconnect.MaxBackoffMs > 0 && c.Ingest.Reconnect.InitialBackoffMs > c.Ingest.Reconnect.MaxBackoffMs {
  282. return fmt.Errorf("ingest.reconnect.initialBackoffMs must be <= maxBackoffMs")
  283. }
  284. if c.Ingest.Stdin.SampleRateHz <= 0 || c.Ingest.HTTPRaw.SampleRateHz <= 0 {
  285. return fmt.Errorf("ingest pcm sampleRateHz must be > 0")
  286. }
  287. if (c.Ingest.Stdin.Channels != 1 && c.Ingest.Stdin.Channels != 2) || (c.Ingest.HTTPRaw.Channels != 1 && c.Ingest.HTTPRaw.Channels != 2) {
  288. return fmt.Errorf("ingest pcm channels must be 1 or 2")
  289. }
  290. if strings.ToLower(strings.TrimSpace(c.Ingest.Stdin.Format)) != "s16le" || strings.ToLower(strings.TrimSpace(c.Ingest.HTTPRaw.Format)) != "s16le" {
  291. return fmt.Errorf("ingest pcm format must be s16le")
  292. }
  293. if ingestKind == "icecast" && strings.TrimSpace(c.Ingest.Icecast.URL) == "" {
  294. return fmt.Errorf("ingest.icecast.url is required when ingest.kind=icecast")
  295. }
  296. if ingestKind == "srt" && strings.TrimSpace(c.Ingest.SRT.URL) == "" {
  297. return fmt.Errorf("ingest.srt.url is required when ingest.kind=srt")
  298. }
  299. if ingestKind == "aes67" || ingestKind == "aoip" || ingestKind == "aoip-rtp" {
  300. hasSDP := strings.TrimSpace(c.Ingest.AES67.SDP) != ""
  301. hasSDPPath := strings.TrimSpace(c.Ingest.AES67.SDPPath) != ""
  302. if hasSDP && hasSDPPath {
  303. return fmt.Errorf("ingest.aes67.sdp and ingest.aes67.sdpPath are mutually exclusive")
  304. }
  305. if !hasSDP && !hasSDPPath {
  306. if strings.TrimSpace(c.Ingest.AES67.MulticastGroup) == "" {
  307. return fmt.Errorf("ingest.aes67.multicastGroup is required when ingest.kind=%s", ingestKind)
  308. }
  309. if c.Ingest.AES67.Port <= 0 || c.Ingest.AES67.Port > 65535 {
  310. return fmt.Errorf("ingest.aes67.port must be 1..65535")
  311. }
  312. }
  313. if c.Ingest.AES67.PayloadType < 0 || c.Ingest.AES67.PayloadType > 127 {
  314. return fmt.Errorf("ingest.aes67.payloadType must be 0..127")
  315. }
  316. if c.Ingest.AES67.SampleRateHz <= 0 {
  317. return fmt.Errorf("ingest.aes67.sampleRateHz must be > 0")
  318. }
  319. if c.Ingest.AES67.Channels != 1 && c.Ingest.AES67.Channels != 2 {
  320. return fmt.Errorf("ingest.aes67.channels must be 1 or 2")
  321. }
  322. if strings.ToUpper(strings.TrimSpace(c.Ingest.AES67.Encoding)) != "L24" {
  323. return fmt.Errorf("ingest.aes67.encoding must be L24")
  324. }
  325. if c.Ingest.AES67.PacketTimeMs <= 0 {
  326. return fmt.Errorf("ingest.aes67.packetTimeMs must be > 0")
  327. }
  328. if c.Ingest.AES67.JitterDepthPackets < 1 {
  329. return fmt.Errorf("ingest.aes67.jitterDepthPackets must be >= 1")
  330. }
  331. if c.Ingest.AES67.ReadBufferBytes < 0 {
  332. return fmt.Errorf("ingest.aes67.readBufferBytes must be >= 0")
  333. }
  334. }
  335. switch strings.ToLower(strings.TrimSpace(c.Ingest.SRT.Mode)) {
  336. case "", "listener", "caller", "rendezvous":
  337. default:
  338. return fmt.Errorf("ingest.srt.mode unsupported: %s", c.Ingest.SRT.Mode)
  339. }
  340. if c.Ingest.SRT.SampleRateHz <= 0 {
  341. return fmt.Errorf("ingest.srt.sampleRateHz must be > 0")
  342. }
  343. if c.Ingest.SRT.Channels != 1 && c.Ingest.SRT.Channels != 2 {
  344. return fmt.Errorf("ingest.srt.channels must be 1 or 2")
  345. }
  346. switch strings.ToLower(strings.TrimSpace(c.Ingest.Icecast.Decoder)) {
  347. case "", "auto", "native", "ffmpeg", "fallback":
  348. default:
  349. return fmt.Errorf("ingest.icecast.decoder unsupported: %s", c.Ingest.Icecast.Decoder)
  350. }
  351. if c.Ingest.Icecast.RadioText.MaxLen < 0 || c.Ingest.Icecast.RadioText.MaxLen > 64 {
  352. return fmt.Errorf("ingest.icecast.radioText.maxLen out of range (0-64)")
  353. }
  354. // Fail-loud PI validation
  355. if c.RDS.Enabled {
  356. if _, err := ParsePI(c.RDS.PI); err != nil {
  357. return fmt.Errorf("rds config: %w", err)
  358. }
  359. }
  360. if c.RDS.PTY < 0 || c.RDS.PTY > 31 {
  361. return fmt.Errorf("rds.pty out of range (0-31)")
  362. }
  363. if len(c.RDS.PS) > 8 {
  364. return fmt.Errorf("rds.ps must be <= 8 characters")
  365. }
  366. if len(c.RDS.RadioText) > 64 {
  367. return fmt.Errorf("rds.radioText must be <= 64 characters")
  368. }
  369. return nil
  370. }
  371. // EffectiveDeviceRate returns the device sample rate, falling back to composite rate.
  372. func (c Config) EffectiveDeviceRate() float64 {
  373. if c.Backend.DeviceSampleRateHz > 0 {
  374. return c.Backend.DeviceSampleRateHz
  375. }
  376. return float64(c.FM.CompositeRateHz)
  377. }