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.

115 lines
3.6KB

  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. )
  7. type Config struct {
  8. Audio AudioConfig `json:"audio"`
  9. RDS RDSConfig `json:"rds"`
  10. FM FMConfig `json:"fm"`
  11. Backend BackendConfig `json:"backend"`
  12. Control ControlConfig `json:"control"`
  13. }
  14. type AudioConfig struct {
  15. InputPath string `json:"inputPath"`
  16. SampleRate int `json:"sampleRate"`
  17. Gain float64 `json:"gain"`
  18. ToneLeftHz float64 `json:"toneLeftHz"`
  19. ToneRightHz float64 `json:"toneRightHz"`
  20. ToneAmplitude float64 `json:"toneAmplitude"`
  21. }
  22. type RDSConfig struct {
  23. Enabled bool `json:"enabled"`
  24. PI string `json:"pi"`
  25. PS string `json:"ps"`
  26. RadioText string `json:"radioText"`
  27. PTY int `json:"pty"`
  28. }
  29. type FMConfig struct {
  30. FrequencyMHz float64 `json:"frequencyMHz"`
  31. StereoEnabled bool `json:"stereoEnabled"`
  32. PilotLevel float64 `json:"pilotLevel"`
  33. RDSInjection float64 `json:"rdsInjection"`
  34. PreEmphasisUS bool `json:"preEmphasisUS"`
  35. OutputDrive float64 `json:"outputDrive"`
  36. CompositeRateHz int `json:"compositeRateHz"`
  37. }
  38. type BackendConfig struct {
  39. Kind string `json:"kind"`
  40. Device string `json:"device"`
  41. OutputPath string `json:"outputPath"`
  42. }
  43. type ControlConfig struct {
  44. ListenAddress string `json:"listenAddress"`
  45. }
  46. func Default() Config {
  47. return Config{
  48. Audio: AudioConfig{SampleRate: 48000, Gain: 1.0, ToneLeftHz: 1000, ToneRightHz: 1600, ToneAmplitude: 0.4},
  49. RDS: RDSConfig{Enabled: true, PI: "1234", PS: "FMRTX", RadioText: "fm-rds-tx", PTY: 0},
  50. FM: FMConfig{FrequencyMHz: 100.0, StereoEnabled: true, PilotLevel: 0.1, RDSInjection: 0.03, OutputDrive: 0.5, CompositeRateHz: 228000},
  51. Backend: BackendConfig{Kind: "file", OutputPath: "build/out/composite.f32"},
  52. Control: ControlConfig{ListenAddress: "127.0.0.1:8088"},
  53. }
  54. }
  55. func Load(path string) (Config, error) {
  56. cfg := Default()
  57. if path == "" {
  58. return cfg, cfg.Validate()
  59. }
  60. data, err := os.ReadFile(path)
  61. if err != nil {
  62. return Config{}, err
  63. }
  64. if err := json.Unmarshal(data, &cfg); err != nil {
  65. return Config{}, err
  66. }
  67. return cfg, cfg.Validate()
  68. }
  69. func (c Config) Validate() error {
  70. if c.Audio.SampleRate < 8000 || c.Audio.SampleRate > 384000 {
  71. return fmt.Errorf("audio.sampleRate out of range")
  72. }
  73. if c.Audio.Gain < 0 || c.Audio.Gain > 4 {
  74. return fmt.Errorf("audio.gain out of range")
  75. }
  76. if c.Audio.ToneLeftHz <= 0 || c.Audio.ToneRightHz <= 0 {
  77. return fmt.Errorf("audio tone frequencies must be positive")
  78. }
  79. if c.Audio.ToneAmplitude < 0 || c.Audio.ToneAmplitude > 1 {
  80. return fmt.Errorf("audio.toneAmplitude out of range")
  81. }
  82. if c.FM.FrequencyMHz < 65 || c.FM.FrequencyMHz > 110 {
  83. return fmt.Errorf("fm.frequencyMHz out of range")
  84. }
  85. if c.FM.PilotLevel < 0 || c.FM.PilotLevel > 0.2 {
  86. return fmt.Errorf("fm.pilotLevel out of range")
  87. }
  88. if c.FM.RDSInjection < 0 || c.FM.RDSInjection > 0.1 {
  89. return fmt.Errorf("fm.rdsInjection out of range")
  90. }
  91. if c.FM.OutputDrive < 0 || c.FM.OutputDrive > 1 {
  92. return fmt.Errorf("fm.outputDrive out of range")
  93. }
  94. if c.FM.CompositeRateHz < 96000 || c.FM.CompositeRateHz > 1520000 {
  95. return fmt.Errorf("fm.compositeRateHz out of range")
  96. }
  97. if c.Backend.Kind == "" {
  98. return fmt.Errorf("backend.kind is required")
  99. }
  100. if c.Control.ListenAddress == "" {
  101. return fmt.Errorf("control.listenAddress is required")
  102. }
  103. return nil
  104. }