From f54089e072d75b4be6a458e54f3c693c0176459d Mon Sep 17 00:00:00 2001 From: Sergey Stepanov Date: Sat, 7 Dec 2024 00:47:27 +0300 Subject: [PATCH] Stretch samples a bit better with the GBA's 32768Hz --- pkg/worker/media/media.go | 3 ++- pkg/worker/media/media_test.go | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/worker/media/media.go b/pkg/worker/media/media.go index 3c8c87133..ed356308a 100644 --- a/pkg/worker/media/media.go +++ b/pkg/worker/media/media.go @@ -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. diff --git a/pkg/worker/media/media_test.go b/pkg/worker/media/media_test.go index 756688def..ab27f7fa8 100644 --- a/pkg/worker/media/media_test.go +++ b/pkg/worker/media/media_test.go @@ -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) } } } @@ -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) {