Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ikemen support #187

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ jobs:
strategy:
matrix:
go:
- '1.21' # minimum supported version
- 'stable'
- '1.20' # Go version of Ikemen GO
name: "build with Go ${{ matrix.go }}"
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func (f Format) DecodeUnsigned(p []byte) (sample [2]float64, n int) {
func (f Format) encode(signed bool, p []byte, sample [2]float64) (n int) {
switch {
case f.NumChannels == 1:
x := util.Clamp((sample[0]+sample[1])/2, -1, 1)
x := util.ClampFloat64((sample[0]+sample[1])/2, -1, 1)
p = p[encodeFloat(signed, f.Precision, p, x):]
case f.NumChannels >= 2:
for c := range sample {
x := util.Clamp(sample[c], -1, 1)
x := util.ClampFloat64(sample[c], -1, 1)
p = p[encodeFloat(signed, f.Precision, p, x):]
}
for c := len(sample); c < f.NumChannels; c++ {
Expand Down
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gopxl/beep/v2

go 1.21
go 1.20

require (
github.com/ebitengine/oto/v3 v3.2.0
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
12 changes: 8 additions & 4 deletions internal/util/math.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package util

import "cmp"

func Clamp[T cmp.Ordered](x, minV, maxV T) T {
return max(min(x, maxV), minV)
func ClampFloat64(x, minV, maxV float64) float64 {
if x <= minV {
return minV
}
if x >= maxV {
return maxV
}
return x
}
6 changes: 3 additions & 3 deletions internal/util/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestClamp(t *testing.T) {
assert.Equal(t, 0, Clamp(-5, 0, 1))
assert.Equal(t, 1, Clamp(5, 0, 1))
assert.Equal(t, 0.5, Clamp(0.5, 0, 1))
assert.Equal(t, 0.0, ClampFloat64(-5, 0, 1))
assert.Equal(t, 1.0, ClampFloat64(5, 0, 1))
assert.Equal(t, 0.5, ClampFloat64(0.5, 0, 1))
}
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
2 changes: 1 addition & 1 deletion speaker/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (s *sampleReader) Read(buf []byte) (n int, err error) {
for i := range s.buf[:ns] {
for c := range s.buf[i] {
val := s.buf[i][c]
val = util.Clamp(val, -1, 1)
val = util.ClampFloat64(val, -1, 1)
valInt16 := int16(val * (1<<15 - 1))
low := byte(valInt16)
high := byte(valInt16 >> 8)
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