|
- package audio
-
- type ResampledSource struct {
- src *WAVSource
- ratio float64
- position float64
- }
-
- func NewResampledSource(src *WAVSource, targetSampleRate float64) *ResampledSource {
- ratio := 1.0
- if src != nil && src.SampleRate > 0 && targetSampleRate > 0 {
- ratio = float64(src.SampleRate) / targetSampleRate
- }
- return &ResampledSource{src: src, ratio: ratio}
- }
-
- func (s *ResampledSource) NextFrame() Frame {
- if s.src == nil || len(s.src.frames) == 0 {
- return NewFrame(0, 0)
- }
- idx := int(s.position) % len(s.src.frames)
- frame := s.src.frames[idx]
- s.position += s.ratio
- for s.position >= float64(len(s.src.frames)) {
- s.position -= float64(len(s.src.frames))
- }
- return frame
- }
|