Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

44 rindas
1.2KB

  1. package audio
  2. // ResampledSource adapts a WAVSource to a different sample rate using
  3. // linear interpolation between adjacent samples.
  4. type ResampledSource struct {
  5. src *WAVSource
  6. ratio float64 // source rate / target rate
  7. position float64 // fractional position in source frames
  8. }
  9. // NewResampledSource wraps a WAV source with rate conversion.
  10. func NewResampledSource(src *WAVSource, targetSampleRate float64) *ResampledSource {
  11. ratio := 1.0
  12. if src != nil && src.SampleRate > 0 && targetSampleRate > 0 {
  13. ratio = float64(src.SampleRate) / targetSampleRate
  14. }
  15. return &ResampledSource{src: src, ratio: ratio}
  16. }
  17. // NextFrame returns the next interpolated stereo frame.
  18. func (s *ResampledSource) NextFrame() Frame {
  19. if s.src == nil || len(s.src.frames) == 0 {
  20. return NewFrame(0, 0)
  21. }
  22. n := len(s.src.frames)
  23. idx0 := int(s.position) % n
  24. idx1 := (idx0 + 1) % n
  25. frac := s.position - float64(int(s.position))
  26. f0 := s.src.frames[idx0]
  27. f1 := s.src.frames[idx1]
  28. l := float64(f0.L)*(1-frac) + float64(f1.L)*frac
  29. r := float64(f0.R)*(1-frac) + float64(f1.R)*frac
  30. s.position += s.ratio
  31. for s.position >= float64(n) {
  32. s.position -= float64(n)
  33. }
  34. return NewFrame(Sample(l), Sample(r))
  35. }