Skip to content

Commit

Permalink
Merge pull request faiface#120 from gopxl/check-valid-resampler-ratio
Browse files Browse the repository at this point in the history
Assert the resampler ratio is a valid number
  • Loading branch information
duysqubix authored Oct 18, 2023
2 parents 15ec37a + 33b7435 commit 634b96d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
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 634b96d

Please sign in to comment.