Skip to content

Commit

Permalink
Assert the resampler ratio isn't Inf or NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkKremer committed Oct 14, 2023
1 parent b1073cf commit f99c74e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion resample.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package beep

import "fmt"
import (
"fmt"
"math"
)

// Resample takes a Streamer which is assumed to stream at the old sample rate and returns a
// Streamer, which streams the data from the original Streamer resampled to the new sample rate.
Expand Down Expand Up @@ -44,6 +47,9 @@ func ResampleRatio(quality int, ratio float64, s Streamer) *Resampler {
if quality < 1 || 64 < quality {
panic(fmt.Errorf("resample: invalid quality: %d", quality))
}
if math.IsInf(ratio, 0) || math.IsNaN(ratio) {
panic(fmt.Errorf("resample: invalid ratio: %d", ratio))
}
return &Resampler{
s: s,
ratio: ratio,
Expand Down Expand Up @@ -148,6 +154,9 @@ func (r *Resampler) Ratio() float64 {

// SetRatio sets the resampling ratio. This does not cause any glitches in the stream.
func (r *Resampler) SetRatio(ratio float64) {
if math.IsInf(ratio, 0) || math.IsNaN(ratio) {
panic(fmt.Errorf("resample: invalid ratio: %d", ratio))
}
r.pos = int(float64(r.pos) * r.ratio / ratio)
r.ratio = ratio
}
Expand Down

0 comments on commit f99c74e

Please sign in to comment.