Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

130 lignes
3.9KB

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