Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

113 строки
3.3KB

  1. package offline
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "fmt"
  6. "math"
  7. "path/filepath"
  8. "time"
  9. "github.com/jan/fm-rds-tx/internal/audio"
  10. cfgpkg "github.com/jan/fm-rds-tx/internal/config"
  11. "github.com/jan/fm-rds-tx/internal/mpx"
  12. "github.com/jan/fm-rds-tx/internal/output"
  13. "github.com/jan/fm-rds-tx/internal/rds"
  14. "github.com/jan/fm-rds-tx/internal/stereo"
  15. )
  16. type Generator struct {
  17. cfg cfgpkg.Config
  18. }
  19. func NewGenerator(cfg cfgpkg.Config) *Generator {
  20. return &Generator{cfg: cfg}
  21. }
  22. func (g *Generator) GenerateFrame(duration time.Duration) *output.CompositeFrame {
  23. sampleRate := float64(g.cfg.FM.CompositeRateHz)
  24. if sampleRate <= 0 {
  25. sampleRate = 228000
  26. }
  27. samples := int(duration.Seconds() * sampleRate)
  28. if samples <= 0 {
  29. samples = int(sampleRate / 10)
  30. }
  31. frame := &output.CompositeFrame{
  32. Samples: make([]output.IQSample, samples),
  33. SampleRateHz: sampleRate,
  34. Timestamp: time.Now().UTC(),
  35. Sequence: 1,
  36. }
  37. stereoEncoder := stereo.NewStereoEncoder(sampleRate)
  38. combiner := mpx.NewDefaultCombiner()
  39. combiner.PilotGain = g.cfg.FM.PilotLevel
  40. combiner.RDSGain = g.cfg.FM.RDSInjection
  41. rdsEnc, _ := rds.NewEncoder(rds.RDSConfig{
  42. PI: 0x1234,
  43. PS: g.cfg.RDS.PS,
  44. RT: g.cfg.RDS.RadioText,
  45. PTY: uint8(g.cfg.RDS.PTY),
  46. SampleRate: sampleRate,
  47. })
  48. rdsSamples := rdsEnc.Generate(samples)
  49. source := audio.NewToneSource(sampleRate)
  50. for i := 0; i < samples; i++ {
  51. t := float64(i) / sampleRate
  52. in := source.NextFrame()
  53. comps := stereoEncoder.Encode(in)
  54. stereoDSB := comps.Stereo * math.Sin(2*math.Pi*38000.0*t)
  55. rdsValue := 0.0
  56. if g.cfg.RDS.Enabled && i < len(rdsSamples) {
  57. rdsValue = rdsSamples[i]
  58. }
  59. composite := combiner.Combine(comps.Mono, stereoDSB, comps.Pilot, rdsValue) * g.cfg.FM.OutputDrive
  60. frame.Samples[i] = output.IQSample{I: float32(composite), Q: 0}
  61. }
  62. return frame
  63. }
  64. func (g *Generator) WriteFile(path string, duration time.Duration) error {
  65. if path == "" {
  66. path = g.cfg.Backend.OutputPath
  67. }
  68. if path == "" {
  69. path = filepath.Join("build", "offline", "composite.iqf32")
  70. }
  71. backend, err := output.NewFileBackend(path, binary.LittleEndian, output.BackendInfo{
  72. Name: "offline-file",
  73. Description: "offline composite file backend",
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. defer backend.Close(context.Background())
  79. if err := backend.Configure(context.Background(), output.BackendConfig{
  80. SampleRateHz: float64(g.cfg.FM.CompositeRateHz),
  81. Channels: 2,
  82. IQLevel: float32(g.cfg.FM.OutputDrive),
  83. }); err != nil {
  84. return err
  85. }
  86. frame := g.GenerateFrame(duration)
  87. if _, err := backend.Write(context.Background(), frame); err != nil {
  88. return err
  89. }
  90. if err := backend.Flush(context.Background()); err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. func (g *Generator) Summary(duration time.Duration) string {
  96. return fmt.Sprintf("offline frame: freq=%.1fMHz sampleRate=%d duration=%s outputDrive=%.2f stereo=%t rds=%t", g.cfg.FM.FrequencyMHz, g.cfg.FM.CompositeRateHz, duration.String(), g.cfg.FM.OutputDrive, g.cfg.FM.StereoEnabled, g.cfg.RDS.Enabled)
  97. }