Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

243 wiersze
5.7KB

  1. package recorder
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "sync"
  9. "time"
  10. "sdr-visual-suite/internal/demod/gpudemod"
  11. "sdr-visual-suite/internal/detector"
  12. )
  13. type Policy struct {
  14. Enabled bool `yaml:"enabled" json:"enabled"`
  15. MinSNRDb float64 `yaml:"min_snr_db" json:"min_snr_db"`
  16. MinDuration time.Duration `yaml:"min_duration" json:"min_duration"`
  17. MaxDuration time.Duration `yaml:"max_duration" json:"max_duration"`
  18. PrerollMs int `yaml:"preroll_ms" json:"preroll_ms"`
  19. RecordIQ bool `yaml:"record_iq" json:"record_iq"`
  20. RecordAudio bool `yaml:"record_audio" json:"record_audio"`
  21. AutoDemod bool `yaml:"auto_demod" json:"auto_demod"`
  22. AutoDecode bool `yaml:"auto_decode" json:"auto_decode"`
  23. MaxDiskMB int `yaml:"max_disk_mb" json:"max_disk_mb"`
  24. OutputDir string `yaml:"output_dir" json:"output_dir"`
  25. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  26. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  27. }
  28. type Manager struct {
  29. mu sync.RWMutex
  30. policy Policy
  31. ring *Ring
  32. sampleRate int
  33. blockSize int
  34. centerHz float64
  35. decodeCommands map[string]string
  36. queue chan detector.Event
  37. gpuDemod *gpudemod.Engine
  38. closed bool
  39. closeOnce sync.Once
  40. workerWG sync.WaitGroup
  41. }
  42. func New(sampleRate int, blockSize int, policy Policy, centerHz float64, decodeCommands map[string]string) *Manager {
  43. if policy.OutputDir == "" {
  44. policy.OutputDir = "data/recordings"
  45. }
  46. if policy.RingSeconds <= 0 {
  47. policy.RingSeconds = 8
  48. }
  49. m := &Manager{policy: policy, ring: NewRing(sampleRate, blockSize, policy.RingSeconds), sampleRate: sampleRate, blockSize: blockSize, centerHz: centerHz, decodeCommands: decodeCommands, queue: make(chan detector.Event, 64)}
  50. m.initGPUDemod(sampleRate, blockSize)
  51. m.workerWG.Add(1)
  52. go m.worker()
  53. return m
  54. }
  55. func (m *Manager) Update(sampleRate int, blockSize int, policy Policy, centerHz float64, decodeCommands map[string]string) {
  56. m.mu.Lock()
  57. defer m.mu.Unlock()
  58. m.policy = policy
  59. m.sampleRate = sampleRate
  60. m.blockSize = blockSize
  61. m.centerHz = centerHz
  62. m.decodeCommands = decodeCommands
  63. m.initGPUDemodLocked(sampleRate, blockSize)
  64. if m.ring == nil {
  65. m.ring = NewRing(sampleRate, blockSize, policy.RingSeconds)
  66. return
  67. }
  68. m.ring.Reset(sampleRate, blockSize, policy.RingSeconds)
  69. }
  70. func (m *Manager) Ingest(t0 time.Time, samples []complex64) {
  71. if m == nil {
  72. return
  73. }
  74. m.mu.RLock()
  75. ring := m.ring
  76. m.mu.RUnlock()
  77. if ring == nil {
  78. return
  79. }
  80. ring.Push(t0, samples)
  81. }
  82. func (m *Manager) OnEvents(events []detector.Event) {
  83. if m == nil || len(events) == 0 {
  84. return
  85. }
  86. m.mu.RLock()
  87. enabled := m.policy.Enabled
  88. closed := m.closed
  89. m.mu.RUnlock()
  90. if !enabled || closed {
  91. return
  92. }
  93. for _, ev := range events {
  94. select {
  95. case m.queue <- ev:
  96. default:
  97. // drop if queue full
  98. }
  99. }
  100. }
  101. func (m *Manager) worker() {
  102. defer m.workerWG.Done()
  103. for ev := range m.queue {
  104. _ = m.recordEvent(ev)
  105. }
  106. }
  107. func (m *Manager) initGPUDemod(sampleRate int, blockSize int) {
  108. m.mu.Lock()
  109. defer m.mu.Unlock()
  110. m.initGPUDemodLocked(sampleRate, blockSize)
  111. }
  112. func (m *Manager) gpuEngine() *gpudemod.Engine {
  113. m.mu.RLock()
  114. defer m.mu.RUnlock()
  115. return m.gpuDemod
  116. }
  117. func (m *Manager) initGPUDemodLocked(sampleRate int, blockSize int) {
  118. if m.gpuDemod != nil {
  119. m.gpuDemod.Close()
  120. m.gpuDemod = nil
  121. }
  122. if !gpudemod.Available() {
  123. return
  124. }
  125. eng, err := gpudemod.New(blockSize, sampleRate)
  126. if err != nil {
  127. return
  128. }
  129. m.gpuDemod = eng
  130. }
  131. func (m *Manager) Close() {
  132. if m == nil {
  133. return
  134. }
  135. m.closeOnce.Do(func() {
  136. m.mu.Lock()
  137. m.closed = true
  138. if m.queue != nil {
  139. close(m.queue)
  140. }
  141. gpu := m.gpuDemod
  142. m.gpuDemod = nil
  143. m.mu.Unlock()
  144. m.workerWG.Wait()
  145. if gpu != nil {
  146. gpu.Close()
  147. }
  148. })
  149. }
  150. func (m *Manager) recordEvent(ev detector.Event) error {
  151. m.mu.RLock()
  152. policy := m.policy
  153. ring := m.ring
  154. sampleRate := m.sampleRate
  155. centerHz := m.centerHz
  156. m.mu.RUnlock()
  157. if !policy.Enabled {
  158. return nil
  159. }
  160. if ev.SNRDb < policy.MinSNRDb {
  161. return nil
  162. }
  163. dur := ev.End.Sub(ev.Start)
  164. if policy.MinDuration > 0 && dur < policy.MinDuration {
  165. return nil
  166. }
  167. if policy.MaxDuration > 0 && dur > policy.MaxDuration {
  168. return nil
  169. }
  170. if len(policy.ClassFilter) > 0 && ev.Class != nil {
  171. match := false
  172. for _, c := range policy.ClassFilter {
  173. if strings.EqualFold(c, string(ev.Class.ModType)) {
  174. match = true
  175. break
  176. }
  177. }
  178. if !match {
  179. return nil
  180. }
  181. }
  182. if !policy.RecordIQ && !policy.RecordAudio {
  183. return nil
  184. }
  185. start := ev.Start.Add(-time.Duration(policy.PrerollMs) * time.Millisecond)
  186. end := ev.End
  187. if start.After(end) {
  188. return errors.New("invalid event window")
  189. }
  190. if ring == nil {
  191. return errors.New("no ring buffer")
  192. }
  193. segment := ring.Slice(start, end)
  194. if len(segment) == 0 {
  195. return errors.New("no iq in ring")
  196. }
  197. dir := filepath.Join(policy.OutputDir, fmt.Sprintf("%s_%0.fHz_evt%d", ev.Start.Format("2006-01-02T15-04-05"), ev.CenterHz, ev.ID))
  198. if err := os.MkdirAll(dir, 0o755); err != nil {
  199. return err
  200. }
  201. files := map[string]any{}
  202. var iqPath string
  203. if policy.RecordIQ {
  204. iqPath = filepath.Join(dir, "signal.cf32")
  205. if err := writeCF32(iqPath, segment); err != nil {
  206. return err
  207. }
  208. files["iq"] = "signal.cf32"
  209. files["iq_format"] = "cf32"
  210. files["iq_sample_rate"] = sampleRate
  211. }
  212. if policy.RecordAudio && policy.AutoDemod && ev.Class != nil {
  213. if err := m.demodAndWrite(dir, ev, segment, files); err != nil {
  214. return err
  215. }
  216. }
  217. if policy.AutoDecode && iqPath != "" && ev.Class != nil {
  218. m.runDecodeIfConfigured(string(ev.Class.ModType), iqPath, sampleRate, files, dir)
  219. }
  220. _ = centerHz
  221. return writeMeta(dir, ev, sampleRate, files)
  222. }