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.

526 líneas
17KB

  1. //go:build cufft && windows
  2. package gpudemod
  3. /*
  4. #cgo windows CFLAGS: -I"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.2/include"
  5. #cgo windows LDFLAGS: -lcudart64_13 -lkernel32
  6. #include <windows.h>
  7. #include <stdlib.h>
  8. #include <cuda_runtime.h>
  9. typedef struct { float x; float y; } gpud_float2;
  10. typedef int (__stdcall *gpud_upload_fir_taps_fn)(const float* taps, int n);
  11. typedef int (__stdcall *gpud_launch_freq_shift_fn)(const gpud_float2* in, gpud_float2* out, int n, double phase_inc, double phase_start);
  12. typedef int (__stdcall *gpud_launch_fm_discrim_fn)(const gpud_float2* in, float* out, int n);
  13. typedef int (__stdcall *gpud_launch_fir_fn)(const gpud_float2* in, gpud_float2* out, int n, int num_taps);
  14. typedef int (__stdcall *gpud_launch_decimate_fn)(const gpud_float2* in, gpud_float2* out, int n_out, int factor);
  15. typedef int (__stdcall *gpud_launch_am_envelope_fn)(const gpud_float2* in, float* out, int n);
  16. typedef int (__stdcall *gpud_launch_ssb_product_fn)(const gpud_float2* in, float* out, int n, double phase_inc, double phase_start);
  17. static HMODULE gpud_mod = NULL;
  18. static gpud_upload_fir_taps_fn gpud_p_upload_fir_taps = NULL;
  19. static gpud_launch_freq_shift_fn gpud_p_launch_freq_shift = NULL;
  20. static gpud_launch_fm_discrim_fn gpud_p_launch_fm_discrim = NULL;
  21. static gpud_launch_fir_fn gpud_p_launch_fir = NULL;
  22. static gpud_launch_decimate_fn gpud_p_launch_decimate = NULL;
  23. static gpud_launch_am_envelope_fn gpud_p_launch_am_envelope = NULL;
  24. static gpud_launch_ssb_product_fn gpud_p_launch_ssb_product = NULL;
  25. static int gpud_cuda_malloc(void **ptr, size_t bytes) { return (int)cudaMalloc(ptr, bytes); }
  26. static int gpud_cuda_free(void *ptr) { return (int)cudaFree(ptr); }
  27. static int gpud_memcpy_h2d(void *dst, const void *src, size_t bytes) { return (int)cudaMemcpy(dst, src, bytes, cudaMemcpyHostToDevice); }
  28. static int gpud_memcpy_d2h(void *dst, const void *src, size_t bytes) { return (int)cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToHost); }
  29. static int gpud_device_sync() { return (int)cudaDeviceSynchronize(); }
  30. static int gpud_load_library(const char* path) {
  31. if (gpud_mod != NULL) return 0;
  32. gpud_mod = LoadLibraryA(path);
  33. if (gpud_mod == NULL) return -1;
  34. gpud_p_upload_fir_taps = (gpud_upload_fir_taps_fn)GetProcAddress(gpud_mod, "gpud_upload_fir_taps_cuda");
  35. gpud_p_launch_freq_shift = (gpud_launch_freq_shift_fn)GetProcAddress(gpud_mod, "gpud_launch_freq_shift_cuda");
  36. gpud_p_launch_fm_discrim = (gpud_launch_fm_discrim_fn)GetProcAddress(gpud_mod, "gpud_launch_fm_discrim_cuda");
  37. gpud_p_launch_fir = (gpud_launch_fir_fn)GetProcAddress(gpud_mod, "gpud_launch_fir_cuda");
  38. gpud_p_launch_decimate = (gpud_launch_decimate_fn)GetProcAddress(gpud_mod, "gpud_launch_decimate_cuda");
  39. gpud_p_launch_am_envelope = (gpud_launch_am_envelope_fn)GetProcAddress(gpud_mod, "gpud_launch_am_envelope_cuda");
  40. gpud_p_launch_ssb_product = (gpud_launch_ssb_product_fn)GetProcAddress(gpud_mod, "gpud_launch_ssb_product_cuda");
  41. if (!gpud_p_upload_fir_taps || !gpud_p_launch_freq_shift || !gpud_p_launch_fm_discrim || !gpud_p_launch_fir || !gpud_p_launch_decimate || !gpud_p_launch_am_envelope || !gpud_p_launch_ssb_product) {
  42. FreeLibrary(gpud_mod);
  43. gpud_mod = NULL;
  44. return -2;
  45. }
  46. return 0;
  47. }
  48. static int gpud_upload_fir_taps(const float* taps, int n) {
  49. if (!gpud_p_upload_fir_taps) return -1;
  50. return gpud_p_upload_fir_taps(taps, n);
  51. }
  52. static int gpud_launch_freq_shift(gpud_float2 *in, gpud_float2 *out, int n, double phase_inc, double phase_start) {
  53. if (!gpud_p_launch_freq_shift) return -1;
  54. return gpud_p_launch_freq_shift(in, out, n, phase_inc, phase_start);
  55. }
  56. static int gpud_launch_fm_discrim(gpud_float2 *in, float *out, int n) {
  57. if (!gpud_p_launch_fm_discrim) return -1;
  58. return gpud_p_launch_fm_discrim(in, out, n);
  59. }
  60. static int gpud_launch_fir(gpud_float2 *in, gpud_float2 *out, int n, int num_taps) {
  61. if (!gpud_p_launch_fir) return -1;
  62. return gpud_p_launch_fir(in, out, n, num_taps);
  63. }
  64. static int gpud_launch_decimate(gpud_float2 *in, gpud_float2 *out, int n_out, int factor) {
  65. if (!gpud_p_launch_decimate) return -1;
  66. return gpud_p_launch_decimate(in, out, n_out, factor);
  67. }
  68. static int gpud_launch_am_envelope(gpud_float2 *in, float *out, int n) {
  69. if (!gpud_p_launch_am_envelope) return -1;
  70. return gpud_p_launch_am_envelope(in, out, n);
  71. }
  72. static int gpud_launch_ssb_product(gpud_float2 *in, float *out, int n, double phase_inc, double phase_start) {
  73. if (!gpud_p_launch_ssb_product) return -1;
  74. return gpud_p_launch_ssb_product(in, out, n, phase_inc, phase_start);
  75. }
  76. */
  77. import "C"
  78. import (
  79. "errors"
  80. "fmt"
  81. "math"
  82. "os"
  83. "path/filepath"
  84. "sync"
  85. "unsafe"
  86. "sdr-visual-suite/internal/demod"
  87. "sdr-visual-suite/internal/dsp"
  88. )
  89. type DemodType int
  90. const (
  91. DemodNFM DemodType = iota
  92. DemodWFM
  93. DemodAM
  94. DemodUSB
  95. DemodLSB
  96. DemodCW
  97. )
  98. var loadOnce sync.Once
  99. var loadErr error
  100. func ensureDLLLoaded() error {
  101. loadOnce.Do(func() {
  102. candidates := []string{}
  103. if exe, err := os.Executable(); err == nil {
  104. dir := filepath.Dir(exe)
  105. candidates = append(candidates, filepath.Join(dir, "gpudemod_kernels.dll"))
  106. }
  107. if wd, err := os.Getwd(); err == nil {
  108. candidates = append(candidates,
  109. filepath.Join(wd, "gpudemod_kernels.dll"),
  110. filepath.Join(wd, "internal", "demod", "gpudemod", "build", "gpudemod_kernels.dll"),
  111. )
  112. }
  113. seen := map[string]bool{}
  114. for _, p := range candidates {
  115. if p == "" || seen[p] {
  116. continue
  117. }
  118. seen[p] = true
  119. if _, err := os.Stat(p); err == nil {
  120. cp := C.CString(p)
  121. res := C.gpud_load_library(cp)
  122. C.free(unsafe.Pointer(cp))
  123. if res == 0 {
  124. loadErr = nil
  125. return
  126. }
  127. loadErr = fmt.Errorf("failed to load gpudemod DLL: %s (code %d)", p, int(res))
  128. }
  129. }
  130. if loadErr == nil {
  131. loadErr = errors.New("gpudemod_kernels.dll not found")
  132. }
  133. })
  134. return loadErr
  135. }
  136. type Engine struct {
  137. maxSamples int
  138. sampleRate int
  139. phase float64
  140. bfoPhase float64
  141. firTaps []float32
  142. cudaReady bool
  143. lastShiftUsedGPU bool
  144. lastFIRUsedGPU bool
  145. lastDecimUsedGPU bool
  146. lastDemodUsedGPU bool
  147. dIQIn *C.gpud_float2
  148. dShifted *C.gpud_float2
  149. dFiltered *C.gpud_float2
  150. dDecimated *C.gpud_float2
  151. dAudio *C.float
  152. iqBytes C.size_t
  153. audioBytes C.size_t
  154. }
  155. func Available() bool {
  156. if ensureDLLLoaded() != nil {
  157. return false
  158. }
  159. var count C.int
  160. if C.cudaGetDeviceCount(&count) != C.cudaSuccess {
  161. return false
  162. }
  163. return count > 0
  164. }
  165. func New(maxSamples int, sampleRate int) (*Engine, error) {
  166. if maxSamples <= 0 {
  167. return nil, errors.New("invalid maxSamples")
  168. }
  169. if sampleRate <= 0 {
  170. return nil, errors.New("invalid sampleRate")
  171. }
  172. if err := ensureDLLLoaded(); err != nil {
  173. return nil, err
  174. }
  175. if !Available() {
  176. return nil, errors.New("cuda device not available")
  177. }
  178. e := &Engine{
  179. maxSamples: maxSamples,
  180. sampleRate: sampleRate,
  181. cudaReady: true,
  182. iqBytes: C.size_t(maxSamples) * C.size_t(unsafe.Sizeof(C.gpud_float2{})),
  183. audioBytes: C.size_t(maxSamples) * C.size_t(unsafe.Sizeof(C.float(0))),
  184. }
  185. var ptr unsafe.Pointer
  186. if C.gpud_cuda_malloc(&ptr, e.iqBytes) != C.cudaSuccess {
  187. e.Close()
  188. return nil, errors.New("cudaMalloc dIQIn failed")
  189. }
  190. e.dIQIn = (*C.gpud_float2)(ptr)
  191. ptr = nil
  192. if C.gpud_cuda_malloc(&ptr, e.iqBytes) != C.cudaSuccess {
  193. e.Close()
  194. return nil, errors.New("cudaMalloc dShifted failed")
  195. }
  196. e.dShifted = (*C.gpud_float2)(ptr)
  197. ptr = nil
  198. if C.gpud_cuda_malloc(&ptr, e.iqBytes) != C.cudaSuccess {
  199. e.Close()
  200. return nil, errors.New("cudaMalloc dFiltered failed")
  201. }
  202. e.dFiltered = (*C.gpud_float2)(ptr)
  203. ptr = nil
  204. if C.gpud_cuda_malloc(&ptr, e.iqBytes) != C.cudaSuccess {
  205. e.Close()
  206. return nil, errors.New("cudaMalloc dDecimated failed")
  207. }
  208. e.dDecimated = (*C.gpud_float2)(ptr)
  209. ptr = nil
  210. if C.gpud_cuda_malloc(&ptr, e.audioBytes) != C.cudaSuccess {
  211. e.Close()
  212. return nil, errors.New("cudaMalloc dAudio failed")
  213. }
  214. e.dAudio = (*C.float)(ptr)
  215. return e, nil
  216. }
  217. func (e *Engine) SetFIR(taps []float32) {
  218. if len(taps) == 0 {
  219. e.firTaps = nil
  220. return
  221. }
  222. if len(taps) > 256 {
  223. taps = taps[:256]
  224. }
  225. e.firTaps = append(e.firTaps[:0], taps...)
  226. if e.cudaReady {
  227. _ = C.gpud_upload_fir_taps((*C.float)(unsafe.Pointer(&e.firTaps[0])), C.int(len(e.firTaps)))
  228. }
  229. }
  230. func phaseStatus() string { return "phase1c-validated-shift" }
  231. func (e *Engine) LastShiftUsedGPU() bool { return e != nil && e.lastShiftUsedGPU }
  232. func (e *Engine) LastDemodUsedGPU() bool { return e != nil && e.lastDemodUsedGPU }
  233. func (e *Engine) tryCUDAFreqShift(iq []complex64, offsetHz float64) ([]complex64, bool) {
  234. if e == nil || !e.cudaReady || len(iq) == 0 || e.dIQIn == nil || e.dShifted == nil {
  235. return nil, false
  236. }
  237. bytes := C.size_t(len(iq)) * C.size_t(unsafe.Sizeof(complex64(0)))
  238. if C.gpud_memcpy_h2d(unsafe.Pointer(e.dIQIn), unsafe.Pointer(&iq[0]), bytes) != C.cudaSuccess {
  239. return nil, false
  240. }
  241. phaseInc := -2.0 * math.Pi * offsetHz / float64(e.sampleRate)
  242. if C.gpud_launch_freq_shift(e.dIQIn, e.dShifted, C.int(len(iq)), C.double(phaseInc), C.double(e.phase)) != 0 {
  243. return nil, false
  244. }
  245. if C.gpud_device_sync() != C.cudaSuccess {
  246. return nil, false
  247. }
  248. out := make([]complex64, len(iq))
  249. if C.gpud_memcpy_d2h(unsafe.Pointer(&out[0]), unsafe.Pointer(e.dShifted), bytes) != C.cudaSuccess {
  250. return nil, false
  251. }
  252. e.phase += phaseInc * float64(len(iq))
  253. return out, true
  254. }
  255. func (e *Engine) tryCUDAFIR(iq []complex64, numTaps int) ([]complex64, bool) {
  256. if e == nil || !e.cudaReady || len(iq) == 0 || numTaps <= 0 || e.dShifted == nil || e.dFiltered == nil {
  257. return nil, false
  258. }
  259. iqBytes := C.size_t(len(iq)) * C.size_t(unsafe.Sizeof(complex64(0)))
  260. if C.gpud_memcpy_h2d(unsafe.Pointer(e.dShifted), unsafe.Pointer(&iq[0]), iqBytes) != C.cudaSuccess {
  261. return nil, false
  262. }
  263. if C.gpud_launch_fir(e.dShifted, e.dFiltered, C.int(len(iq)), C.int(numTaps)) != 0 {
  264. return nil, false
  265. }
  266. if C.gpud_device_sync() != C.cudaSuccess {
  267. return nil, false
  268. }
  269. out := make([]complex64, len(iq))
  270. if C.gpud_memcpy_d2h(unsafe.Pointer(&out[0]), unsafe.Pointer(e.dFiltered), iqBytes) != C.cudaSuccess {
  271. return nil, false
  272. }
  273. return out, true
  274. }
  275. func (e *Engine) tryCUDADecimate(filtered []complex64, factor int) ([]complex64, bool) {
  276. if e == nil || !e.cudaReady || len(filtered) == 0 || factor <= 0 || e.dFiltered == nil || e.dDecimated == nil {
  277. return nil, false
  278. }
  279. nOut := len(filtered) / factor
  280. if nOut <= 0 {
  281. return nil, false
  282. }
  283. iqBytes := C.size_t(len(filtered)) * C.size_t(unsafe.Sizeof(complex64(0)))
  284. if C.gpud_memcpy_h2d(unsafe.Pointer(e.dFiltered), unsafe.Pointer(&filtered[0]), iqBytes) != C.cudaSuccess {
  285. return nil, false
  286. }
  287. if C.gpud_launch_decimate(e.dFiltered, e.dDecimated, C.int(nOut), C.int(factor)) != 0 {
  288. return nil, false
  289. }
  290. if C.gpud_device_sync() != C.cudaSuccess {
  291. return nil, false
  292. }
  293. out := make([]complex64, nOut)
  294. outBytes := C.size_t(nOut) * C.size_t(unsafe.Sizeof(complex64(0)))
  295. if C.gpud_memcpy_d2h(unsafe.Pointer(&out[0]), unsafe.Pointer(e.dDecimated), outBytes) != C.cudaSuccess {
  296. return nil, false
  297. }
  298. return out, true
  299. }
  300. func (e *Engine) tryCUDAFMDiscrim(shifted []complex64) ([]float32, bool) {
  301. if e == nil || !e.cudaReady || len(shifted) < 2 || e.dShifted == nil || e.dAudio == nil {
  302. return nil, false
  303. }
  304. iqBytes := C.size_t(len(shifted)) * C.size_t(unsafe.Sizeof(complex64(0)))
  305. if C.gpud_memcpy_h2d(unsafe.Pointer(e.dShifted), unsafe.Pointer(&shifted[0]), iqBytes) != C.cudaSuccess {
  306. return nil, false
  307. }
  308. if C.gpud_launch_fm_discrim(e.dShifted, e.dAudio, C.int(len(shifted))) != 0 {
  309. return nil, false
  310. }
  311. if C.gpud_device_sync() != C.cudaSuccess {
  312. return nil, false
  313. }
  314. out := make([]float32, len(shifted)-1)
  315. outBytes := C.size_t(len(out)) * C.size_t(unsafe.Sizeof(float32(0)))
  316. if C.gpud_memcpy_d2h(unsafe.Pointer(&out[0]), unsafe.Pointer(e.dAudio), outBytes) != C.cudaSuccess {
  317. return nil, false
  318. }
  319. return out, true
  320. }
  321. func (e *Engine) tryCUDAAMEnvelope(shifted []complex64) ([]float32, bool) {
  322. if e == nil || !e.cudaReady || len(shifted) == 0 || e.dShifted == nil || e.dAudio == nil {
  323. return nil, false
  324. }
  325. iqBytes := C.size_t(len(shifted)) * C.size_t(unsafe.Sizeof(complex64(0)))
  326. if C.gpud_memcpy_h2d(unsafe.Pointer(e.dShifted), unsafe.Pointer(&shifted[0]), iqBytes) != C.cudaSuccess {
  327. return nil, false
  328. }
  329. if C.gpud_launch_am_envelope(e.dShifted, e.dAudio, C.int(len(shifted))) != 0 {
  330. return nil, false
  331. }
  332. if C.gpud_device_sync() != C.cudaSuccess {
  333. return nil, false
  334. }
  335. out := make([]float32, len(shifted))
  336. outBytes := C.size_t(len(out)) * C.size_t(unsafe.Sizeof(float32(0)))
  337. if C.gpud_memcpy_d2h(unsafe.Pointer(&out[0]), unsafe.Pointer(e.dAudio), outBytes) != C.cudaSuccess {
  338. return nil, false
  339. }
  340. return out, true
  341. }
  342. func (e *Engine) tryCUDASSBProduct(shifted []complex64, bfoHz float64) ([]float32, bool) {
  343. if e == nil || !e.cudaReady || len(shifted) == 0 || e.dShifted == nil || e.dAudio == nil {
  344. return nil, false
  345. }
  346. iqBytes := C.size_t(len(shifted)) * C.size_t(unsafe.Sizeof(complex64(0)))
  347. if C.gpud_memcpy_h2d(unsafe.Pointer(e.dShifted), unsafe.Pointer(&shifted[0]), iqBytes) != C.cudaSuccess {
  348. return nil, false
  349. }
  350. phaseInc := 2.0 * math.Pi * bfoHz / float64(e.sampleRate)
  351. if C.gpud_launch_ssb_product(e.dShifted, e.dAudio, C.int(len(shifted)), C.double(phaseInc), C.double(e.bfoPhase)) != 0 {
  352. return nil, false
  353. }
  354. if C.gpud_device_sync() != C.cudaSuccess {
  355. return nil, false
  356. }
  357. out := make([]float32, len(shifted))
  358. outBytes := C.size_t(len(out)) * C.size_t(unsafe.Sizeof(float32(0)))
  359. if C.gpud_memcpy_d2h(unsafe.Pointer(&out[0]), unsafe.Pointer(e.dAudio), outBytes) != C.cudaSuccess {
  360. return nil, false
  361. }
  362. e.bfoPhase += phaseInc * float64(len(shifted))
  363. return out, true
  364. }
  365. func (e *Engine) Demod(iq []complex64, offsetHz float64, bw float64, mode DemodType) ([]float32, int, error) {
  366. if e == nil {
  367. return nil, 0, errors.New("nil CUDA demod engine")
  368. }
  369. if !e.cudaReady {
  370. return nil, 0, errors.New("cuda demod engine is not initialized")
  371. }
  372. if len(iq) == 0 {
  373. return nil, 0, nil
  374. }
  375. if len(iq) > e.maxSamples {
  376. return nil, 0, errors.New("sample count exceeds engine capacity")
  377. }
  378. _ = fmt.Sprintf("%s:%0.3f", phaseStatus(), offsetHz)
  379. shifted, ok := e.tryCUDAFreqShift(iq, offsetHz)
  380. e.lastShiftUsedGPU = ok && ValidateFreqShift(iq, e.sampleRate, offsetHz, shifted, 1e-3)
  381. if !e.lastShiftUsedGPU {
  382. shifted = dsp.FreqShift(iq, e.sampleRate, offsetHz)
  383. }
  384. var outRate int
  385. switch mode {
  386. case DemodNFM, DemodAM, DemodUSB, DemodLSB, DemodCW:
  387. outRate = 48000
  388. case DemodWFM:
  389. outRate = 192000
  390. default:
  391. return nil, 0, errors.New("unsupported demod type")
  392. }
  393. cutoff := bw / 2
  394. if cutoff < 200 {
  395. cutoff = 200
  396. }
  397. taps := e.firTaps
  398. if len(taps) == 0 {
  399. base64 := dsp.LowpassFIR(cutoff, e.sampleRate, 101)
  400. taps = make([]float32, len(base64))
  401. for i, v := range base64 {
  402. taps[i] = float32(v)
  403. }
  404. e.SetFIR(taps)
  405. }
  406. filtered, ok := e.tryCUDAFIR(shifted, len(taps))
  407. e.lastFIRUsedGPU = ok && ValidateFIR(shifted, taps, filtered, 1e-3)
  408. if !e.lastFIRUsedGPU {
  409. ftaps := make([]float64, len(taps))
  410. for i, v := range taps {
  411. ftaps[i] = float64(v)
  412. }
  413. filtered = dsp.ApplyFIR(shifted, ftaps)
  414. }
  415. decim := int(math.Round(float64(e.sampleRate) / float64(outRate)))
  416. if decim < 1 {
  417. decim = 1
  418. }
  419. dec, ok := e.tryCUDADecimate(filtered, decim)
  420. e.lastDecimUsedGPU = ok && ValidateDecimate(filtered, decim, dec, 1e-3)
  421. if !e.lastDecimUsedGPU {
  422. dec = dsp.Decimate(filtered, decim)
  423. }
  424. inputRate := e.sampleRate / decim
  425. e.lastDemodUsedGPU = false
  426. switch mode {
  427. case DemodNFM:
  428. if gpuAudio, ok := e.tryCUDAFMDiscrim(dec); ok {
  429. e.lastDemodUsedGPU = true
  430. return gpuAudio, inputRate, nil
  431. }
  432. return demod.NFM{}.Demod(dec, inputRate), inputRate, nil
  433. case DemodWFM:
  434. if gpuAudio, ok := e.tryCUDAFMDiscrim(dec); ok {
  435. e.lastDemodUsedGPU = true
  436. return gpuAudio, inputRate, nil
  437. }
  438. return demod.WFM{}.Demod(dec, inputRate), inputRate, nil
  439. case DemodAM:
  440. if gpuAudio, ok := e.tryCUDAAMEnvelope(dec); ok {
  441. e.lastDemodUsedGPU = true
  442. return gpuAudio, inputRate, nil
  443. }
  444. return demod.AM{}.Demod(dec, inputRate), inputRate, nil
  445. case DemodUSB:
  446. if gpuAudio, ok := e.tryCUDASSBProduct(dec, 700.0); ok {
  447. e.lastDemodUsedGPU = true
  448. return gpuAudio, inputRate, nil
  449. }
  450. return demod.USB{}.Demod(dec, inputRate), inputRate, nil
  451. case DemodLSB:
  452. if gpuAudio, ok := e.tryCUDASSBProduct(dec, -700.0); ok {
  453. e.lastDemodUsedGPU = true
  454. return gpuAudio, inputRate, nil
  455. }
  456. return demod.LSB{}.Demod(dec, inputRate), inputRate, nil
  457. case DemodCW:
  458. if gpuAudio, ok := e.tryCUDASSBProduct(dec, 700.0); ok {
  459. e.lastDemodUsedGPU = true
  460. return gpuAudio, inputRate, nil
  461. }
  462. return demod.CW{}.Demod(dec, inputRate), inputRate, nil
  463. default:
  464. return nil, 0, errors.New("unsupported demod type")
  465. }
  466. }
  467. func (e *Engine) Close() {
  468. if e == nil {
  469. return
  470. }
  471. if e.dIQIn != nil {
  472. _ = C.gpud_cuda_free(unsafe.Pointer(e.dIQIn))
  473. e.dIQIn = nil
  474. }
  475. if e.dShifted != nil {
  476. _ = C.gpud_cuda_free(unsafe.Pointer(e.dShifted))
  477. e.dShifted = nil
  478. }
  479. if e.dFiltered != nil {
  480. _ = C.gpud_cuda_free(unsafe.Pointer(e.dFiltered))
  481. e.dFiltered = nil
  482. }
  483. if e.dDecimated != nil {
  484. _ = C.gpud_cuda_free(unsafe.Pointer(e.dDecimated))
  485. e.dDecimated = nil
  486. }
  487. if e.dAudio != nil {
  488. _ = C.gpud_cuda_free(unsafe.Pointer(e.dAudio))
  489. e.dAudio = nil
  490. }
  491. e.firTaps = nil
  492. e.cudaReady = false
  493. }