Skip to content

Commit

Permalink
Replace usages of min, max and clear
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkKremer committed Sep 21, 2024
1 parent caac121 commit defe796
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 22 deletions.
16 changes: 13 additions & 3 deletions compositors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ func (t *take) Stream(samples [][2]float64) (n int, ok bool) {
if t.remains <= 0 {
return 0, false
}
toStream := min(t.remains, len(samples))
//toStream := min(t.remains, len(samples))
toStream := t.remains
if len(samples) < toStream {
toStream = len(samples)
}
n, ok = t.s.Stream(samples[:toStream])
t.remains -= n
return n, ok
Expand Down Expand Up @@ -157,7 +161,10 @@ func Loop2(s StreamSeeker, opts ...LoopOption) (Streamer, error) {
if l.start >= l.end {
return nil, errors.New(fmt.Sprintf("invalid argument to Loop2; start position %d must be smaller than the end position %d", l.start, l.end))
}
l.end = min(l.end, n)
//l.end = min(l.end, n)
if n < l.end {
l.end = n
}

return l, nil
}
Expand Down Expand Up @@ -190,7 +197,10 @@ func (l *loop2) Stream(samples [][2]float64) (n int, ok bool) {
continue
}
// Stream only up to the end of the loop.
toStream = min(samplesUntilEnd, toStream)
//toStream = min(samplesUntilEnd, toStream)
if samplesUntilEnd < toStream {
toStream = samplesUntilEnd
}
}

sn, sok := l.s.Stream(samples[:toStream])
Expand Down
5 changes: 4 additions & 1 deletion compositors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ func TestMix(t *testing.T) {

maxLen := 0
for _, d := range data {
maxLen = max(maxLen, len(d))
//maxLen = max(maxLen, len(d))
if len(d) > maxLen {
maxLen = len(d)
}
}

want := make([][2]float64, maxLen)
Expand Down
5 changes: 4 additions & 1 deletion ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func (c *Ctrl) Stream(samples [][2]float64) (n int, ok bool) {
return 0, false
}
if c.Paused {
clear(samples)
//clear(samples)
for i := range samples {
samples[i] = [2]float64{}
}
return len(samples), true
}
return c.Streamer.Stream(samples)
Expand Down
5 changes: 4 additions & 1 deletion effects/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func (t *TransitionStreamer) Stream(samples [][2]float64) (n int, ok bool) {
for i := 0; i < n; i++ {
pos := t.pos + i
progress := float64(pos) / float64(t.len)
progress = min(progress, 1.0)
//progress = min(progress, 1.0)
if progress > 1 {
progress = 1
}
value := t.transitionFunc(progress)
gain := t.startGain + (t.endGain-t.startGain)*value

Expand Down
20 changes: 16 additions & 4 deletions examples/speedy-player/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ func (ap *audioPanel) handle(event tcell.Event) (changed, quit bool) {
newPos += ap.sampleRate.N(time.Second)
}
// Clamp the position to be within the stream
newPos = max(newPos, 0)
newPos = min(newPos, ap.streamer.Len()-1)
//newPos = max(newPos, 0)
if newPos < 0 {
newPos = 0
}
//newPos = min(newPos, ap.streamer.Len()-1)
if newPos >= ap.streamer.Len() {
newPos = ap.streamer.Len() - 1
}

if err := ap.streamer.Seek(newPos); err != nil {
report(err)
Expand All @@ -128,15 +134,21 @@ func (ap *audioPanel) handle(event tcell.Event) (changed, quit bool) {
case 'z':
speaker.Lock()
newRatio := ap.resampler.Ratio() * 15 / 16
newRatio = max(newRatio, 0.001) // Limit to a reasonable ratio
//newRatio = max(newRatio, 0.001) // Limit to a reasonable ratio
if newRatio < 0.001 {
newRatio = 0.001
}
ap.resampler.SetRatio(newRatio)
speaker.Unlock()
return true, false

case 'x':
speaker.Lock()
newRatio := ap.resampler.Ratio() * 16 / 15
newRatio = min(newRatio, 100) // Limit to a reasonable ratio
//newRatio = min(newRatio, 100) // Limit to a reasonable ratio
if newRatio > 100 {
newRatio = 100
}
ap.resampler.SetRatio(newRatio)
speaker.Unlock()
return true, false
Expand Down
6 changes: 5 additions & 1 deletion flac/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ func (d *decoder) Stream(samples [][2]float64) (n int, ok bool) {
continue
}

toFill := min(samplesLeft, len(samples))
//toFill := min(samplesLeft, len(samples))
toFill := samplesLeft
if toFill > len(samples) {
toFill = len(samples)
}
d.decodeFrameRangeInto(d.frame, d.posInFrame, toFill, samples)
d.posInFrame += toFill
n += toFill
Expand Down
10 changes: 8 additions & 2 deletions generators/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import "github.com/gopxl/beep/v2"
func Silence(num int) beep.Streamer {
if num < 0 {
return beep.StreamerFunc(func(samples [][2]float64) (m int, ok bool) {
clear(samples)
//clear(samples)
for i := range samples {
samples[i] = [2]float64{}
}
return len(samples), true
})
}
Expand All @@ -19,7 +22,10 @@ func Silence(num int) beep.Streamer {
if num < len(samples) {
samples = samples[:num]
}
clear(samples)
//clear(samples)
for i := range samples {
samples[i] = [2]float64{}
}
num -= len(samples)

return len(samples), true
Expand Down
6 changes: 5 additions & 1 deletion internal/testtools/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func AssertStreamerHasCorrectReturnBehaviour(t *testing.T, s beep.Streamer, expe
buf := make([][2]float64, 512)
samplesLeft := expectedSamples - leaveUnreadInFirstCase
for samplesLeft > 0 {
toRead := min(samplesLeft, len(buf))
//toRead := min(samplesLeft, len(buf))
toRead := len(buf)
if toRead > samplesLeft {
toRead = samplesLeft
}
n, ok := s.Stream(buf[:toRead])
if !ok {
t.Fatalf("streamer returned !ok before it was expected to be drained")
Expand Down
6 changes: 5 additions & 1 deletion internal/testtools/streamers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func (e *ErrorStreamer) Stream(samples [][2]float64) (n int, ok bool) {
return 0, false
}

toStream := min(e.samplesLeft, len(samples))
//toStream := min(e.samplesLeft, len(samples))
toStream := e.samplesLeft
if toStream > len(samples) {
toStream = len(samples)
}
n, ok = e.s.Stream(samples[:toStream])
e.samplesLeft -= n

Expand Down
6 changes: 5 additions & 1 deletion midi/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func (d *decoder) Stream(samples [][2]float64) (n int, ok bool) {
}

for len(samples) > 0 {
cn := min(len(d.bufLeft), len(samples))
//cn := min(len(d.bufLeft), len(samples))
cn := len(d.bufLeft)
if cn > len(samples) {
cn = len(samples)
}

d.seq.Render(d.bufLeft[:cn], d.bufRight[:cn])
for i := 0; i < cn; i++ {
Expand Down
11 changes: 9 additions & 2 deletions mixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ func (m *Mixer) Stream(samples [][2]float64) (n int, ok bool) {
var tmp [512][2]float64

for len(samples) > 0 {
toStream := min(len(tmp), len(samples))
//toStream := min(len(tmp), len(samples))
toStream := len(tmp)
if toStream > len(samples) {
toStream = len(samples)
}

// clear the samples
clear(samples[:toStream])
//clear(samples[:toStream])
for i := range samples[:toStream] {
samples[i] = [2]float64{}
}

snMax := 0
for si := 0; si < len(m.streamers); si++ {
Expand Down
10 changes: 8 additions & 2 deletions resample.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ func (r *Resampler) Stream(samples [][2]float64) (n int, ok bool) {
}

// Adjust the window to be within the available buffers.
windowStart = max(windowStart, 0)
windowEnd = min(windowEnd, r.end)
//windowStart = max(windowStart, 0)
if windowStart < 0 {
windowStart = 0
}
//windowEnd = min(windowEnd, r.end)
if windowEnd > r.end {
windowEnd = r.end
}

// For each channel...
for c := range samples[0] {
Expand Down
5 changes: 4 additions & 1 deletion streamers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ func Silence(num int) Streamer {
if 0 < num && num < len(samples) {
samples = samples[:num]
}
clear(samples)
//clear(samples)
for i := range samples {
samples[i] = [2]float64{}
}
if num > 0 {
num -= len(samples)
}
Expand Down
6 changes: 5 additions & 1 deletion wav/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ func (d *decoder) Stream(samples [][2]float64) (n int, ok bool) {
bytesPerFrame := int(d.h.BytesPerFrame)
wantBytes := len(samples) * bytesPerFrame
availableBytes := int(d.h.DataSize - d.pos)
numBytes := min(wantBytes, availableBytes)
//numBytes := min(wantBytes, availableBytes)
numBytes := wantBytes
if numBytes > availableBytes {
numBytes = availableBytes
}
p := make([]byte, numBytes)
n, err := d.r.Read(p)
if err != nil && err != io.EOF {
Expand Down

0 comments on commit defe796

Please sign in to comment.