您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

146 行
4.0KB

  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. OutputDir string `yaml:"output_dir" json:"output_dir"`
  22. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  23. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  24. }
  25. type Manager struct {
  26. policy Policy
  27. ring *Ring
  28. sampleRate int
  29. blockSize int
  30. centerHz float64
  31. decodeCommands map[string]string
  32. }
  33. func New(sampleRate int, blockSize int, policy Policy, centerHz float64, decodeCommands map[string]string) *Manager {
  34. if policy.OutputDir == "" {
  35. policy.OutputDir = "data/recordings"
  36. }
  37. if policy.RingSeconds <= 0 {
  38. policy.RingSeconds = 8
  39. }
  40. return &Manager{policy: policy, ring: NewRing(sampleRate, blockSize, policy.RingSeconds), sampleRate: sampleRate, blockSize: blockSize, centerHz: centerHz, decodeCommands: decodeCommands}
  41. }
  42. func (m *Manager) Update(sampleRate int, blockSize int, policy Policy, centerHz float64, decodeCommands map[string]string) {
  43. m.policy = policy
  44. m.sampleRate = sampleRate
  45. m.blockSize = blockSize
  46. m.centerHz = centerHz
  47. m.decodeCommands = decodeCommands
  48. if m.ring == nil {
  49. m.ring = NewRing(sampleRate, blockSize, policy.RingSeconds)
  50. return
  51. }
  52. m.ring.Reset(sampleRate, blockSize, policy.RingSeconds)
  53. }
  54. func (m *Manager) Ingest(t0 time.Time, samples []complex64) {
  55. if m == nil || m.ring == nil {
  56. return
  57. }
  58. m.ring.Push(t0, samples)
  59. }
  60. func (m *Manager) OnEvents(events []detector.Event) {
  61. if m == nil || !m.policy.Enabled || len(events) == 0 {
  62. return
  63. }
  64. for _, ev := range events {
  65. _ = m.recordEvent(ev)
  66. }
  67. }
  68. func (m *Manager) recordEvent(ev detector.Event) error {
  69. if !m.policy.Enabled {
  70. return nil
  71. }
  72. if ev.SNRDb < m.policy.MinSNRDb {
  73. return nil
  74. }
  75. dur := ev.End.Sub(ev.Start)
  76. if m.policy.MinDuration > 0 && dur < m.policy.MinDuration {
  77. return nil
  78. }
  79. if m.policy.MaxDuration > 0 && dur > m.policy.MaxDuration {
  80. return nil
  81. }
  82. if len(m.policy.ClassFilter) > 0 && ev.Class != nil {
  83. match := false
  84. for _, c := range m.policy.ClassFilter {
  85. if strings.EqualFold(c, string(ev.Class.ModType)) {
  86. match = true
  87. break
  88. }
  89. }
  90. if !match {
  91. return nil
  92. }
  93. }
  94. if !m.policy.RecordIQ && !m.policy.RecordAudio {
  95. return nil
  96. }
  97. start := ev.Start.Add(-time.Duration(m.policy.PrerollMs) * time.Millisecond)
  98. end := ev.End
  99. if start.After(end) {
  100. return errors.New("invalid event window")
  101. }
  102. segment := m.ring.Slice(start, end)
  103. if len(segment) == 0 {
  104. return errors.New("no iq in ring")
  105. }
  106. 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))
  107. if err := os.MkdirAll(dir, 0o755); err != nil {
  108. return err
  109. }
  110. files := map[string]any{}
  111. var iqPath string
  112. if m.policy.RecordIQ {
  113. iqPath = filepath.Join(dir, "signal.cf32")
  114. if err := writeCF32(iqPath, segment); err != nil {
  115. return err
  116. }
  117. files["iq"] = "signal.cf32"
  118. files["iq_format"] = "cf32"
  119. files["iq_sample_rate"] = m.sampleRate
  120. }
  121. // Optional demod + audio
  122. if m.policy.RecordAudio && m.policy.AutoDemod && ev.Class != nil {
  123. if err := m.demodAndWrite(dir, ev, segment, files); err != nil {
  124. return err
  125. }
  126. }
  127. if m.policy.AutoDecode && iqPath != "" && ev.Class != nil {
  128. m.runDecodeIfConfigured(string(ev.Class.ModType), iqPath, m.sampleRate, files, dir)
  129. }
  130. return writeMeta(dir, ev, m.sampleRate, files)
  131. }