Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
862B

  1. package audio
  2. import (
  3. "math"
  4. )
  5. type ToneSource struct {
  6. LeftFreq float64
  7. RightFreq float64
  8. SampleRate float64
  9. LeftPhase float64
  10. RightPhase float64
  11. Amplitude float64
  12. }
  13. func NewToneSource(sampleRate float64) *ToneSource {
  14. return &ToneSource{
  15. LeftFreq: 1000,
  16. RightFreq: 1600,
  17. SampleRate: sampleRate,
  18. Amplitude: 0.4,
  19. }
  20. }
  21. func (s *ToneSource) NextFrame() Frame {
  22. left := s.Amplitude * math.Sin(2*math.Pi*s.LeftPhase)
  23. right := s.Amplitude * math.Sin(2*math.Pi*s.RightPhase)
  24. s.LeftPhase += s.LeftFreq / s.SampleRate
  25. s.RightPhase += s.RightFreq / s.SampleRate
  26. if s.LeftPhase >= 1 {
  27. s.LeftPhase -= math.Floor(s.LeftPhase)
  28. }
  29. if s.RightPhase >= 1 {
  30. s.RightPhase -= math.Floor(s.RightPhase)
  31. }
  32. return NewFrame(Sample(left), Sample(right))
  33. }