No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

149 líneas
4.1KB

  1. package config
  2. import (
  3. "os"
  4. "time"
  5. "gopkg.in/yaml.v3"
  6. )
  7. type Band struct {
  8. Name string `yaml:"name" json:"name"`
  9. StartHz float64 `yaml:"start_hz" json:"start_hz"`
  10. EndHz float64 `yaml:"end_hz" json:"end_hz"`
  11. }
  12. type DetectorConfig struct {
  13. ThresholdDb float64 `yaml:"threshold_db" json:"threshold_db"`
  14. MinDurationMs int `yaml:"min_duration_ms" json:"min_duration_ms"`
  15. HoldMs int `yaml:"hold_ms" json:"hold_ms"`
  16. }
  17. type RecorderConfig struct {
  18. Enabled bool `yaml:"enabled" json:"enabled"`
  19. MinSNRDb float64 `yaml:"min_snr_db" json:"min_snr_db"`
  20. MinDuration string `yaml:"min_duration" json:"min_duration"`
  21. MaxDuration string `yaml:"max_duration" json:"max_duration"`
  22. PrerollMs int `yaml:"preroll_ms" json:"preroll_ms"`
  23. RecordIQ bool `yaml:"record_iq" json:"record_iq"`
  24. RecordAudio bool `yaml:"record_audio" json:"record_audio"`
  25. AutoDemod bool `yaml:"auto_demod" json:"auto_demod"`
  26. OutputDir string `yaml:"output_dir" json:"output_dir"`
  27. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  28. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  29. }
  30. type Config struct {
  31. Bands []Band `yaml:"bands" json:"bands"`
  32. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  33. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  34. FFTSize int `yaml:"fft_size" json:"fft_size"`
  35. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  36. TunerBwKHz int `yaml:"tuner_bw_khz" json:"tuner_bw_khz"`
  37. UseGPUFFT bool `yaml:"use_gpu_fft" json:"use_gpu_fft"`
  38. AGC bool `yaml:"agc" json:"agc"`
  39. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  40. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  41. Detector DetectorConfig `yaml:"detector" json:"detector"`
  42. Recorder RecorderConfig `yaml:"recorder" json:"recorder"`
  43. WebAddr string `yaml:"web_addr" json:"web_addr"`
  44. EventPath string `yaml:"event_path" json:"event_path"`
  45. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  46. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  47. WebRoot string `yaml:"web_root" json:"web_root"`
  48. }
  49. func Default() Config {
  50. return Config{
  51. Bands: []Band{
  52. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  53. },
  54. CenterHz: 100.0e6,
  55. SampleRate: 2_048_000,
  56. FFTSize: 2048,
  57. GainDb: 30,
  58. TunerBwKHz: 1536,
  59. UseGPUFFT: false,
  60. AGC: false,
  61. DCBlock: false,
  62. IQBalance: false,
  63. Detector: DetectorConfig{ThresholdDb: -20, MinDurationMs: 250, HoldMs: 500},
  64. Recorder: RecorderConfig{
  65. Enabled: false,
  66. MinSNRDb: 10,
  67. MinDuration: "1s",
  68. MaxDuration: "300s",
  69. PrerollMs: 500,
  70. RecordIQ: true,
  71. RecordAudio: false,
  72. AutoDemod: true,
  73. OutputDir: "data/recordings",
  74. RingSeconds: 8,
  75. },
  76. WebAddr: ":8080",
  77. EventPath: "data/events.jsonl",
  78. FrameRate: 15,
  79. WaterfallLines: 200,
  80. WebRoot: "web",
  81. }
  82. }
  83. func Load(path string) (Config, error) {
  84. cfg := Default()
  85. b, err := os.ReadFile(path)
  86. if err != nil {
  87. return cfg, err
  88. }
  89. if err := yaml.Unmarshal(b, &cfg); err != nil {
  90. return cfg, err
  91. }
  92. if cfg.Detector.MinDurationMs <= 0 {
  93. cfg.Detector.MinDurationMs = 250
  94. }
  95. if cfg.Detector.HoldMs <= 0 {
  96. cfg.Detector.HoldMs = 500
  97. }
  98. if cfg.FrameRate <= 0 {
  99. cfg.FrameRate = 15
  100. }
  101. if cfg.WaterfallLines <= 0 {
  102. cfg.WaterfallLines = 200
  103. }
  104. if cfg.WebRoot == "" {
  105. cfg.WebRoot = "web"
  106. }
  107. if cfg.WebAddr == "" {
  108. cfg.WebAddr = ":8080"
  109. }
  110. if cfg.EventPath == "" {
  111. cfg.EventPath = "data/events.jsonl"
  112. }
  113. if cfg.SampleRate <= 0 {
  114. cfg.SampleRate = 2_048_000
  115. }
  116. if cfg.FFTSize <= 0 {
  117. cfg.FFTSize = 2048
  118. }
  119. if cfg.TunerBwKHz <= 0 {
  120. cfg.TunerBwKHz = 1536
  121. }
  122. if cfg.CenterHz == 0 {
  123. cfg.CenterHz = 100.0e6
  124. }
  125. if cfg.Recorder.OutputDir == "" {
  126. cfg.Recorder.OutputDir = "data/recordings"
  127. }
  128. if cfg.Recorder.RingSeconds <= 0 {
  129. cfg.Recorder.RingSeconds = 8
  130. }
  131. return cfg, nil
  132. }
  133. func (c Config) FrameInterval() time.Duration {
  134. fps := c.FrameRate
  135. if fps <= 0 {
  136. fps = 15
  137. }
  138. return time.Second / time.Duration(fps)
  139. }