Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

168 行
4.5KB

  1. package stereo
  2. import (
  3. "math"
  4. "github.com/jan/fm-rds-tx/internal/audio"
  5. "github.com/jan/fm-rds-tx/internal/dsp"
  6. )
  7. // Mode selects the stereo subcarrier modulation method.
  8. type Mode int
  9. const (
  10. ModeDSB Mode = iota // Standard DSB-SC (FCC §73.322 compliant)
  11. ModeSSB // SSB-SC LSB only (Foti/Tarsio, experimental)
  12. ModeVSB // Vestigial SB (0-200Hz DSB, above SSB; Omnia-style)
  13. )
  14. // ParseMode converts a string to Mode. Returns ModeDSB for unknown values.
  15. func ParseMode(s string) Mode {
  16. switch s {
  17. case "SSB", "ssb":
  18. return ModeSSB
  19. case "VSB", "vsb":
  20. return ModeVSB
  21. default:
  22. return ModeDSB
  23. }
  24. }
  25. // String returns the mode name.
  26. func (m Mode) String() string {
  27. switch m {
  28. case ModeSSB:
  29. return "SSB"
  30. case ModeVSB:
  31. return "VSB"
  32. default:
  33. return "DSB"
  34. }
  35. }
  36. // Components holds the individual MPX components produced by the stereo encoder.
  37. // All outputs are unity-normalized. The combiner controls actual injection levels.
  38. type Components struct {
  39. Mono float64 // (L+R)/2 baseband
  40. Stereo float64 // (L-R)/2 modulated onto 38 kHz subcarrier
  41. Pilot float64 // sin(pilotPhase), unity amplitude
  42. }
  43. // StereoEncoder generates stereo MPX primitives from stereo audio frames.
  44. // Supports DSB-SC (standard), SSB-SC (lower sideband only), and VSB modes.
  45. type StereoEncoder struct {
  46. pilot dsp.PilotGenerator
  47. lastPhase float64
  48. mode Mode
  49. // SSB/VSB: Hilbert transform for quadrature modulation
  50. hilbert *dsp.HilbertFilter
  51. // VSB: crossover filter splits L-R into low (<200Hz, DSB) and high (>200Hz, SSB)
  52. vsbLPF *dsp.FilterChain // 200 Hz LPF for VSB low band
  53. vsbHPF *dsp.FilterChain // 200 Hz HPF for VSB high band (derived from allpass - LPF)
  54. hilbertHi *dsp.HilbertFilter // separate Hilbert for high band
  55. }
  56. // NewStereoEncoder creates a StereoEncoder configured for the provided sample rate.
  57. func NewStereoEncoder(sampleRate float64) StereoEncoder {
  58. return StereoEncoder{
  59. pilot: dsp.NewPilotGenerator(sampleRate),
  60. mode: ModeDSB,
  61. }
  62. }
  63. // SetMode changes the stereo encoding mode. Creates Hilbert filter if needed.
  64. func (s *StereoEncoder) SetMode(mode Mode, sampleRate float64) {
  65. s.mode = mode
  66. if mode == ModeSSB || mode == ModeVSB {
  67. // 127-tap Hilbert FIR at 228kHz: group delay = 63 samples = 0.276ms
  68. s.hilbert = dsp.NewHilbertFilter(127)
  69. }
  70. if mode == ModeVSB {
  71. // Crossover at 200 Hz for vestigial sideband
  72. s.vsbLPF = dsp.NewLPF4(200, sampleRate)
  73. s.vsbHPF = dsp.NewLPF4(200, sampleRate) // we'll subtract to get HPF
  74. s.hilbertHi = dsp.NewHilbertFilter(127)
  75. }
  76. }
  77. // Encode converts a stereo frame into MPX components.
  78. func (s *StereoEncoder) Encode(frame audio.Frame) Components {
  79. pilotPhase := s.pilot.Phase()
  80. s.lastPhase = pilotPhase
  81. pilot := s.pilot.Sample()
  82. sub38sin := math.Sin(2 * math.Pi * 2 * pilotPhase)
  83. sub38cos := math.Cos(2 * math.Pi * 2 * pilotPhase)
  84. diff := float64(frame.Difference())
  85. mono := float64(frame.Mono())
  86. var stereoOut float64
  87. switch s.mode {
  88. case ModeSSB:
  89. if s.hilbert == nil {
  90. // Fallback to DSB if not initialized
  91. stereoOut = diff * sub38sin
  92. break
  93. }
  94. // SSB-LSB: s(t) = m_delayed(t)·sin(ωt) - m̂(t)·cos(ωt)
  95. // The - sign selects LSB (below 38 kHz).
  96. // ×2 compensates for removed USB (+6 dB).
  97. delayed, hilb := s.hilbert.Process(diff)
  98. stereoOut = 2 * (delayed*sub38sin - hilb*sub38cos)
  99. case ModeVSB:
  100. if s.hilbert == nil || s.vsbLPF == nil {
  101. stereoOut = diff * sub38sin
  102. break
  103. }
  104. // VSB: 0-200Hz → DSB, 200Hz+ → SSB-LSB
  105. lo := s.vsbLPF.Process(diff)
  106. hiRef := s.vsbHPF.Process(diff) // same LPF for HPF derivation
  107. hi := diff - hiRef // highpass = original - lowpass (requires matching delay, approximate)
  108. // Low band: standard DSB
  109. dsbPart := lo * sub38sin
  110. // High band: SSB-LSB with Hilbert
  111. delayed, hilb := s.hilbertHi.Process(hi)
  112. ssbPart := 2 * (delayed*sub38sin - hilb*sub38cos)
  113. stereoOut = dsbPart + ssbPart
  114. default: // ModeDSB
  115. stereoOut = diff * sub38sin
  116. }
  117. return Components{
  118. Mono: mono,
  119. Stereo: stereoOut,
  120. Pilot: pilot,
  121. }
  122. }
  123. // Reset restarts the pilot generator and clears filter state.
  124. func (s *StereoEncoder) Reset() {
  125. s.pilot.Reset()
  126. if s.hilbert != nil {
  127. s.hilbert.Reset()
  128. }
  129. if s.hilbertHi != nil {
  130. s.hilbertHi.Reset()
  131. }
  132. }
  133. // PilotPhase returns the pilot phase used in the most recent Encode() call.
  134. func (s *StereoEncoder) PilotPhase() float64 {
  135. return s.lastPhase
  136. }
  137. // RDSCarrier returns sin(3 * pilotPhase * 2π) — the 57 kHz carrier
  138. // phase-locked to the pilot, as required by the RDS standard.
  139. func (s *StereoEncoder) RDSCarrier() float64 {
  140. return math.Sin(2 * math.Pi * 3 * s.lastPhase)
  141. }