Skip to content

Commit

Permalink
Stretch samples a bit better with the GBA's 32768Hz
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Dec 6, 2024
1 parent 6bb82b2 commit f54089e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/worker/media/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ func DefaultOpus() (*opus.Encoder, error) {
}

// frame calculates an audio stereo frame size, i.e. 48k*frame/1000*2
// with round(x / 2) * 2 for the closest even number
func frame(hz int, frame float32) int {
return int(math.Round(float64(hz) * float64(frame) / 1000 * 2))
return int(math.Round(float64(hz)*float64(frame)/1000/2) * 2 * 2)
}

// stretch does a simple stretching of audio samples.
Expand Down
11 changes: 7 additions & 4 deletions pkg/worker/media/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestBufferWrite(t *testing.T) {
buf.write(samplesOf(w.sample, w.len), func(s samples) { lastResult = s })
}
if !reflect.DeepEqual(test.expect, lastResult) {
t.Errorf("not expted buffer, %v != %v", lastResult, test.expect)
t.Errorf("not expted buffer, %v != %v, %v", lastResult, test.expect, buf.s)
}
}
}
Expand All @@ -217,17 +217,20 @@ func samplesOf(v int16, len int) (s samples) {
return
}

func Test_frame(t *testing.T) {
func TestFrame(t *testing.T) {
type args struct {
hz int
frame int
frame float32
}
tests := []struct {
name string
args args
want int
}{
{name: "mGBA", args: args{hz: 32768, frame: 10}, want: 654},
{name: "mGBA", args: args{hz: 32768, frame: 10}, want: 656},
{name: "mGBA", args: args{hz: 32768, frame: 5}, want: 328},
{name: "mGBA", args: args{hz: 32768, frame: 2.5}, want: 164},
{name: "nes", args: args{hz: 48000, frame: 2.5}, want: 240},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit f54089e

Please sign in to comment.