Wideband autonomous SDR analysis engine forked from sdr-visual-suite
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.

160 líneas
4.3KB

  1. package recorder
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "sdr-visual-suite/internal/detector"
  10. )
  11. type Policy struct {
  12. Enabled bool `yaml:"enabled" json:"enabled"`
  13. MinSNRDb float64 `yaml:"min_snr_db" json:"min_snr_db"`
  14. MinDuration time.Duration `yaml:"min_duration" json:"min_duration"`
  15. MaxDuration time.Duration `yaml:"max_duration" json:"max_duration"`
  16. PrerollMs int `yaml:"preroll_ms" json:"preroll_ms"`
  17. RecordIQ bool `yaml:"record_iq" json:"record_iq"`
  18. RecordAudio bool `yaml:"record_audio" json:"record_audio"`
  19. AutoDemod bool `yaml:"auto_demod" json:"auto_demod"`
  20. AutoDecode bool `yaml:"auto_decode" json:"auto_decode"`
  21. MaxDiskMB int `yaml:"max_disk_mb" json:"max_disk_mb"`
  22. OutputDir string `yaml:"output_dir" json:"output_dir"`
  23. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  24. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  25. }
  26. type Manager struct {
  27. policy Policy
  28. ring *Ring
  29. sampleRate int
  30. blockSize int
  31. centerHz float64
  32. decodeCommands map[string]string
  33. queue chan detector.Event
  34. }
  35. func New(sampleRate int, blockSize int, policy Policy, centerHz float64, decodeCommands map[string]string) *Manager {
  36. if policy.OutputDir == "" {
  37. policy.OutputDir = "data/recordings"
  38. }
  39. if policy.RingSeconds <= 0 {
  40. policy.RingSeconds = 8
  41. }
  42. m := &Manager{policy: policy, ring: NewRing(sampleRate, blockSize, policy.RingSeconds), sampleRate: sampleRate, blockSize: blockSize, centerHz: centerHz, decodeCommands: decodeCommands, queue: make(chan detector.Event, 64)}
  43. go m.worker()
  44. return m
  45. }
  46. func (m *Manager) Update(sampleRate int, blockSize int, policy Policy, centerHz float64, decodeCommands map[string]string) {
  47. m.policy = policy
  48. m.sampleRate = sampleRate
  49. m.blockSize = blockSize
  50. m.centerHz = centerHz
  51. m.decodeCommands = decodeCommands
  52. if m.ring == nil {
  53. m.ring = NewRing(sampleRate, blockSize, policy.RingSeconds)
  54. return
  55. }
  56. m.ring.Reset(sampleRate, blockSize, policy.RingSeconds)
  57. }
  58. func (m *Manager) Ingest(t0 time.Time, samples []complex64) {
  59. if m == nil || m.ring == nil {
  60. return
  61. }
  62. m.ring.Push(t0, samples)
  63. }
  64. func (m *Manager) OnEvents(events []detector.Event) {
  65. if m == nil || !m.policy.Enabled || len(events) == 0 {
  66. return
  67. }
  68. for _, ev := range events {
  69. select {
  70. case m.queue <- ev:
  71. default:
  72. // drop if queue full
  73. }
  74. }
  75. }
  76. func (m *Manager) worker() {
  77. for ev := range m.queue {
  78. _ = m.recordEvent(ev)
  79. }
  80. }
  81. func (m *Manager) recordEvent(ev detector.Event) error {
  82. if !m.policy.Enabled {
  83. return nil
  84. }
  85. if ev.SNRDb < m.policy.MinSNRDb {
  86. return nil
  87. }
  88. dur := ev.End.Sub(ev.Start)
  89. if m.policy.MinDuration > 0 && dur < m.policy.MinDuration {
  90. return nil
  91. }
  92. if m.policy.MaxDuration > 0 && dur > m.policy.MaxDuration {
  93. return nil
  94. }
  95. if len(m.policy.ClassFilter) > 0 && ev.Class != nil {
  96. match := false
  97. for _, c := range m.policy.ClassFilter {
  98. if strings.EqualFold(c, string(ev.Class.ModType)) {
  99. match = true
  100. break
  101. }
  102. }
  103. if !match {
  104. return nil
  105. }
  106. }
  107. if !m.policy.RecordIQ && !m.policy.RecordAudio {
  108. return nil
  109. }
  110. start := ev.Start.Add(-time.Duration(m.policy.PrerollMs) * time.Millisecond)
  111. end := ev.End
  112. if start.After(end) {
  113. return errors.New("invalid event window")
  114. }
  115. segment := m.ring.Slice(start, end)
  116. if len(segment) == 0 {
  117. return errors.New("no iq in ring")
  118. }
  119. dir := filepath.Join(m.policy.OutputDir, fmt.Sprintf("%s_%0.fHz_evt%d", ev.Start.Format("2006-01-02T15-04-05"), ev.CenterHz, ev.ID))
  120. if err := os.MkdirAll(dir, 0o755); err != nil {
  121. return err
  122. }
  123. files := map[string]any{}
  124. var iqPath string
  125. if m.policy.RecordIQ {
  126. iqPath = filepath.Join(dir, "signal.cf32")
  127. if err := writeCF32(iqPath, segment); err != nil {
  128. return err
  129. }
  130. files["iq"] = "signal.cf32"
  131. files["iq_format"] = "cf32"
  132. files["iq_sample_rate"] = m.sampleRate
  133. }
  134. // Optional demod + audio
  135. if m.policy.RecordAudio && m.policy.AutoDemod && ev.Class != nil {
  136. if err := m.demodAndWrite(dir, ev, segment, files); err != nil {
  137. return err
  138. }
  139. }
  140. if m.policy.AutoDecode && iqPath != "" && ev.Class != nil {
  141. m.runDecodeIfConfigured(string(ev.Class.ModType), iqPath, m.sampleRate, files, dir)
  142. }
  143. return writeMeta(dir, ev, m.sampleRate, files)
  144. }