diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d7c5cb..c102cb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Upgrade Go version to 1.21 ([#2](https://github.com/gopxl/beep/pull/2)) - Upgrade Oto version to 3.1 ([#3](https://github.com/gopxl/beep/pull/3)) +- Panic when `Resampler` is given a ratio of `Inf` or `NaN`. ([#120](https://github.com/gopxl/beep/pull/120)) ## [v1.0.0] 2023-10-07 - Forked [faiface/beep](https://github.com/faiface/beep) to [gopxl/beep](https://github.com/gopxl/beep). diff --git a/resample.go b/resample.go index 2082ac9..afead3e 100644 --- a/resample.go +++ b/resample.go @@ -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. @@ -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, @@ -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 }