Skip to content

Commit

Permalink
Merge pull request #64 from MarkKremer/master
Browse files Browse the repository at this point in the history
Fixed buffer pop # of samples instead of bytes.
  • Loading branch information
faiface authored Aug 25, 2019
2 parents 8fd2689 + b5d8476 commit 76df446
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (b *Buffer) Len() int {
//
// Existing Streamers are not affected.
func (b *Buffer) Pop(n int) {
b.data = b.data[n:]
b.data = b.data[n*b.f.Width():]
}

// Append adds all audio data from the given Streamer to the end of the Buffer.
Expand Down
26 changes: 26 additions & 0 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,29 @@ func TestFormatEncodeDecode(t *testing.T) {
}
}
}

func TestBufferAppendPop(t *testing.T) {
formats := make(chan beep.Format)
go func() {
defer close(formats)
for _, numChannels := range []int{1, 2, 3, 4} {
formats <- beep.Format{
SampleRate: 44100,
NumChannels: numChannels,
Precision: 2,
}
}
}()

for format := range formats {
b := beep.NewBuffer(format)
b.Append(beep.Silence(768))
if b.Len() != 768 {
t.Fatalf("buffer length isn't equal to appended stream length: expected: %v, actual: %v (NumChannels: %v)", 768, b.Len(), format.NumChannels)
}
b.Pop(512)
if b.Len() != 768-512 {
t.Fatalf("buffer length isn't as expected after Pop: expected: %v, actual: %v (NumChannels: %v)", 768-512, b.Len(), format.NumChannels)
}
}
}

0 comments on commit 76df446

Please sign in to comment.