Wideband autonomous SDR analysis engine forked from sdr-visual-suite
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

50 lines
1.4KB

  1. package decoder
  2. import (
  3. "errors"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. )
  8. type Result struct {
  9. Stdout string `json:"stdout"`
  10. Stderr string `json:"stderr"`
  11. Code int `json:"code"`
  12. }
  13. // Run executes an external decoder command. If cmdTemplate contains {iq}/{sr}/{audio}, they are replaced.
  14. // Otherwise, --iq and --sample-rate args are appended.
  15. func Run(cmdTemplate string, iqPath string, sampleRate int, audioPath string) (Result, error) {
  16. if cmdTemplate == "" {
  17. return Result{}, errors.New("decoder command empty")
  18. }
  19. cmdStr := strings.ReplaceAll(cmdTemplate, "{iq}", iqPath)
  20. cmdStr = strings.ReplaceAll(cmdStr, "{sr}", strconv.Itoa(sampleRate))
  21. if strings.Contains(cmdTemplate, "{audio}") {
  22. if audioPath == "" {
  23. return Result{}, errors.New("audio path required for decoder")
  24. }
  25. cmdStr = strings.ReplaceAll(cmdStr, "{audio}", audioPath)
  26. }
  27. parts := strings.Fields(cmdStr)
  28. if len(parts) == 0 {
  29. return Result{}, errors.New("invalid decoder command")
  30. }
  31. cmd := exec.Command(parts[0], parts[1:]...)
  32. if !strings.Contains(cmdTemplate, "{iq}") {
  33. cmd.Args = append(cmd.Args, "--iq", iqPath)
  34. }
  35. if !strings.Contains(cmdTemplate, "{sr}") {
  36. cmd.Args = append(cmd.Args, "--sample-rate", strconv.Itoa(sampleRate))
  37. }
  38. out, err := cmd.CombinedOutput()
  39. res := Result{Stdout: string(out), Code: 0}
  40. if err != nil {
  41. res.Stderr = err.Error()
  42. res.Code = 1
  43. return res, err
  44. }
  45. return res, nil
  46. }