Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

264 lignes
8.0KB

  1. package config
  2. import (
  3. "math"
  4. "os"
  5. "time"
  6. "gopkg.in/yaml.v3"
  7. )
  8. type Band struct {
  9. Name string `yaml:"name" json:"name"`
  10. StartHz float64 `yaml:"start_hz" json:"start_hz"`
  11. EndHz float64 `yaml:"end_hz" json:"end_hz"`
  12. }
  13. type DetectorConfig struct {
  14. ThresholdDb float64 `yaml:"threshold_db" json:"threshold_db"`
  15. MinDurationMs int `yaml:"min_duration_ms" json:"min_duration_ms"`
  16. HoldMs int `yaml:"hold_ms" json:"hold_ms"`
  17. EmaAlpha float64 `yaml:"ema_alpha" json:"ema_alpha"`
  18. HysteresisDb float64 `yaml:"hysteresis_db" json:"hysteresis_db"`
  19. MinStableFrames int `yaml:"min_stable_frames" json:"min_stable_frames"`
  20. GapToleranceMs int `yaml:"gap_tolerance_ms" json:"gap_tolerance_ms"`
  21. CFARMode string `yaml:"cfar_mode" json:"cfar_mode"`
  22. CFARGuardHz float64 `yaml:"cfar_guard_hz" json:"cfar_guard_hz"`
  23. CFARTrainHz float64 `yaml:"cfar_train_hz" json:"cfar_train_hz"`
  24. CFARGuardCells int `yaml:"cfar_guard_cells,omitempty" json:"cfar_guard_cells,omitempty"`
  25. CFARTrainCells int `yaml:"cfar_train_cells,omitempty" json:"cfar_train_cells,omitempty"`
  26. CFARRank int `yaml:"cfar_rank" json:"cfar_rank"`
  27. CFARScaleDb float64 `yaml:"cfar_scale_db" json:"cfar_scale_db"`
  28. CFARWrapAround bool `yaml:"cfar_wrap_around" json:"cfar_wrap_around"`
  29. EdgeMarginDb float64 `yaml:"edge_margin_db" json:"edge_margin_db"`
  30. MaxSignalBwHz float64 `yaml:"max_signal_bw_hz" json:"max_signal_bw_hz"`
  31. MergeGapHz float64 `yaml:"merge_gap_hz" json:"merge_gap_hz"`
  32. // Deprecated (backward compatibility)
  33. CFAREnabled *bool `yaml:"cfar_enabled,omitempty" json:"cfar_enabled,omitempty"`
  34. }
  35. type RecorderConfig struct {
  36. Enabled bool `yaml:"enabled" json:"enabled"`
  37. MinSNRDb float64 `yaml:"min_snr_db" json:"min_snr_db"`
  38. MinDuration string `yaml:"min_duration" json:"min_duration"`
  39. MaxDuration string `yaml:"max_duration" json:"max_duration"`
  40. PrerollMs int `yaml:"preroll_ms" json:"preroll_ms"`
  41. RecordIQ bool `yaml:"record_iq" json:"record_iq"`
  42. RecordAudio bool `yaml:"record_audio" json:"record_audio"`
  43. AutoDemod bool `yaml:"auto_demod" json:"auto_demod"`
  44. AutoDecode bool `yaml:"auto_decode" json:"auto_decode"`
  45. MaxDiskMB int `yaml:"max_disk_mb" json:"max_disk_mb"`
  46. OutputDir string `yaml:"output_dir" json:"output_dir"`
  47. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  48. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  49. }
  50. type DecoderConfig struct {
  51. FT8Cmd string `yaml:"ft8_cmd" json:"ft8_cmd"`
  52. WSPRCmd string `yaml:"wspr_cmd" json:"wspr_cmd"`
  53. DMRCmd string `yaml:"dmr_cmd" json:"dmr_cmd"`
  54. DStarCmd string `yaml:"dstar_cmd" json:"dstar_cmd"`
  55. FSKCmd string `yaml:"fsk_cmd" json:"fsk_cmd"`
  56. PSKCmd string `yaml:"psk_cmd" json:"psk_cmd"`
  57. }
  58. type Config struct {
  59. Bands []Band `yaml:"bands" json:"bands"`
  60. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  61. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  62. FFTSize int `yaml:"fft_size" json:"fft_size"`
  63. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  64. TunerBwKHz int `yaml:"tuner_bw_khz" json:"tuner_bw_khz"`
  65. UseGPUFFT bool `yaml:"use_gpu_fft" json:"use_gpu_fft"`
  66. AGC bool `yaml:"agc" json:"agc"`
  67. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  68. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  69. Detector DetectorConfig `yaml:"detector" json:"detector"`
  70. Recorder RecorderConfig `yaml:"recorder" json:"recorder"`
  71. Decoder DecoderConfig `yaml:"decoder" json:"decoder"`
  72. WebAddr string `yaml:"web_addr" json:"web_addr"`
  73. EventPath string `yaml:"event_path" json:"event_path"`
  74. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  75. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  76. WebRoot string `yaml:"web_root" json:"web_root"`
  77. }
  78. func Default() Config {
  79. return Config{
  80. Bands: []Band{
  81. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  82. },
  83. CenterHz: 100.0e6,
  84. SampleRate: 2_048_000,
  85. FFTSize: 2048,
  86. GainDb: 30,
  87. TunerBwKHz: 1536,
  88. UseGPUFFT: false,
  89. AGC: false,
  90. DCBlock: false,
  91. IQBalance: false,
  92. Detector: DetectorConfig{
  93. ThresholdDb: -20,
  94. MinDurationMs: 250,
  95. HoldMs: 500,
  96. EmaAlpha: 0.2,
  97. HysteresisDb: 3,
  98. MinStableFrames: 3,
  99. GapToleranceMs: 500,
  100. CFARMode: "GOSCA",
  101. CFARGuardHz: 500,
  102. CFARTrainHz: 5000,
  103. CFARGuardCells: 3,
  104. CFARTrainCells: 24,
  105. CFARRank: 36,
  106. CFARScaleDb: 6,
  107. CFARWrapAround: true,
  108. EdgeMarginDb: 3.0,
  109. MaxSignalBwHz: 150000,
  110. MergeGapHz: 5000,
  111. },
  112. Recorder: RecorderConfig{
  113. Enabled: false,
  114. MinSNRDb: 10,
  115. MinDuration: "1s",
  116. MaxDuration: "300s",
  117. PrerollMs: 500,
  118. RecordIQ: true,
  119. RecordAudio: false,
  120. AutoDemod: true,
  121. AutoDecode: false,
  122. MaxDiskMB: 0,
  123. OutputDir: "data/recordings",
  124. RingSeconds: 8,
  125. },
  126. Decoder: DecoderConfig{},
  127. WebAddr: ":8080",
  128. EventPath: "data/events.jsonl",
  129. FrameRate: 15,
  130. WaterfallLines: 200,
  131. WebRoot: "web",
  132. }
  133. }
  134. func Load(path string) (Config, error) {
  135. cfg := Default()
  136. if b, err := os.ReadFile(autosavePath(path)); err == nil {
  137. if err := yaml.Unmarshal(b, &cfg); err == nil {
  138. return applyDefaults(cfg), nil
  139. }
  140. }
  141. b, err := os.ReadFile(path)
  142. if err != nil {
  143. return cfg, err
  144. }
  145. if err := yaml.Unmarshal(b, &cfg); err != nil {
  146. return cfg, err
  147. }
  148. return applyDefaults(cfg), nil
  149. }
  150. func applyDefaults(cfg Config) Config {
  151. if cfg.Detector.MinDurationMs <= 0 {
  152. cfg.Detector.MinDurationMs = 250
  153. }
  154. if cfg.Detector.HoldMs <= 0 {
  155. cfg.Detector.HoldMs = 500
  156. }
  157. if cfg.Detector.MinStableFrames <= 0 {
  158. cfg.Detector.MinStableFrames = 3
  159. }
  160. if cfg.Detector.GapToleranceMs <= 0 {
  161. cfg.Detector.GapToleranceMs = cfg.Detector.HoldMs
  162. }
  163. if cfg.Detector.CFARMode == "" {
  164. if cfg.Detector.CFAREnabled != nil {
  165. if *cfg.Detector.CFAREnabled {
  166. cfg.Detector.CFARMode = "OS"
  167. } else {
  168. cfg.Detector.CFARMode = "OFF"
  169. }
  170. } else {
  171. cfg.Detector.CFARMode = "GOSCA"
  172. }
  173. }
  174. if cfg.Detector.CFARGuardHz <= 0 && cfg.Detector.CFARGuardCells > 0 {
  175. cfg.Detector.CFARGuardHz = float64(cfg.Detector.CFARGuardCells) * 62.5
  176. }
  177. if cfg.Detector.CFARTrainHz <= 0 && cfg.Detector.CFARTrainCells > 0 {
  178. cfg.Detector.CFARTrainHz = float64(cfg.Detector.CFARTrainCells) * 62.5
  179. }
  180. if cfg.Detector.CFARGuardHz <= 0 {
  181. cfg.Detector.CFARGuardHz = 500
  182. }
  183. if cfg.Detector.CFARTrainHz <= 0 {
  184. cfg.Detector.CFARTrainHz = 5000
  185. }
  186. if cfg.Detector.CFARGuardCells <= 0 {
  187. cfg.Detector.CFARGuardCells = 3
  188. }
  189. if cfg.Detector.CFARTrainCells <= 0 {
  190. cfg.Detector.CFARTrainCells = 24
  191. }
  192. if cfg.Detector.CFARRank <= 0 || cfg.Detector.CFARRank > 2*cfg.Detector.CFARTrainCells {
  193. cfg.Detector.CFARRank = int(math.Round(0.75 * float64(2*cfg.Detector.CFARTrainCells)))
  194. if cfg.Detector.CFARRank <= 0 {
  195. cfg.Detector.CFARRank = 1
  196. }
  197. }
  198. if cfg.Detector.CFARScaleDb <= 0 {
  199. cfg.Detector.CFARScaleDb = 6
  200. }
  201. if cfg.Detector.EdgeMarginDb <= 0 {
  202. cfg.Detector.EdgeMarginDb = 3.0
  203. }
  204. if cfg.Detector.MaxSignalBwHz <= 0 {
  205. cfg.Detector.MaxSignalBwHz = 150000
  206. }
  207. if cfg.Detector.MergeGapHz <= 0 {
  208. cfg.Detector.MergeGapHz = 5000
  209. }
  210. if cfg.FrameRate <= 0 {
  211. cfg.FrameRate = 15
  212. }
  213. if cfg.WaterfallLines <= 0 {
  214. cfg.WaterfallLines = 200
  215. }
  216. if cfg.WebRoot == "" {
  217. cfg.WebRoot = "web"
  218. }
  219. if cfg.WebAddr == "" {
  220. cfg.WebAddr = ":8080"
  221. }
  222. if cfg.EventPath == "" {
  223. cfg.EventPath = "data/events.jsonl"
  224. }
  225. if cfg.SampleRate <= 0 {
  226. cfg.SampleRate = 2_048_000
  227. }
  228. if cfg.FFTSize <= 0 {
  229. cfg.FFTSize = 2048
  230. }
  231. if cfg.TunerBwKHz <= 0 {
  232. cfg.TunerBwKHz = 1536
  233. }
  234. if cfg.CenterHz == 0 {
  235. cfg.CenterHz = 100.0e6
  236. }
  237. if cfg.Recorder.OutputDir == "" {
  238. cfg.Recorder.OutputDir = "data/recordings"
  239. }
  240. if cfg.Recorder.RingSeconds <= 0 {
  241. cfg.Recorder.RingSeconds = 8
  242. }
  243. return cfg
  244. }
  245. func (c Config) FrameInterval() time.Duration {
  246. fps := c.FrameRate
  247. if fps <= 0 {
  248. fps = 15
  249. }
  250. return time.Second / time.Duration(fps)
  251. }