Skip to content

Commit

Permalink
Make the resampler window symmetric & center it
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkKremer committed Jul 11, 2024
1 parent 5e1638c commit 7301df4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions resample.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func ResampleRatio(quality int, ratio float64, s Streamer) *Resampler {
ratio: ratio,
buf1: make([][2]float64, resamplerSingleBufferSize),
buf2: make([][2]float64, resamplerSingleBufferSize),
pts: make([]point, quality*2),
pts: make([]point, quality*2+1),
off: -resamplerSingleBufferSize,
pos: 0,
end: math.MaxInt,
Expand Down Expand Up @@ -85,9 +85,8 @@ func (r *Resampler) Stream(samples [][2]float64) (n int, ok bool) {

// Determine the quality*2 closest sample positions for the interpolation.
// The window has length len(r.pts) and is centered around wantPos.
// todo: check if this is actually centered around wantPos
windowStart := int(wantPos) - len(r.pts)/2 + 1 // (inclusive)
windowEnd := int(wantPos) + len(r.pts)/2 + 1 // (exclusive)
windowStart := int(wantPos) - len(r.pts)/2 // (inclusive)
windowEnd := int(wantPos) + len(r.pts)/2 + 1 // (exclusive)

// Prepare the buffers.
if windowEnd >= r.off+resamplerSingleBufferSize {
Expand Down
4 changes: 2 additions & 2 deletions resample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestResample(t *testing.T) {

func resampleCorrect(quality int, old, new beep.SampleRate, p [][2]float64) [][2]float64 {
ratio := float64(old) / float64(new)
pts := make([]point, quality*2)
pts := make([]point, quality*2+1)
var resampled [][2]float64
for i := 0; ; i++ {
j := float64(i) * ratio
Expand All @@ -42,7 +42,7 @@ func resampleCorrect(quality int, old, new beep.SampleRate, p [][2]float64) [][2
var sample [2]float64
for c := range sample {
for k := range pts {
l := int(j) + k - len(pts)/2 + 1
l := int(j) + k - (len(pts)-1)/2
if l >= 0 && l < len(p) {
pts[k] = point{X: float64(l), Y: p[l][c]}
} else {
Expand Down

0 comments on commit 7301df4

Please sign in to comment.