From 3fb304e5a821c92aeb0490374f10193177b5e2c4 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 4 Jan 2021 21:42:00 +0900 Subject: [PATCH] audio/internal/convert: Refactoring --- audio/internal/convert/float32.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/audio/internal/convert/float32.go b/audio/internal/convert/float32.go index 95dcd68ba..bb06dfdb9 100644 --- a/audio/internal/convert/float32.go +++ b/audio/internal/convert/float32.go @@ -32,6 +32,13 @@ type f32Reader struct { buf *byte } +func max(a, b int) int { + if a < b { + return b + } + return a +} + func (f *f32Reader) Read(buf []byte) (int, error) { if f.eof { return 0, io.EOF @@ -45,10 +52,7 @@ func (f *f32Reader) Read(buf []byte) (int, error) { return 1, nil } - bf := make([]float32, len(buf)/2) - if len(buf) == 1 { - bf = make([]float32, 1) - } + bf := make([]float32, max(len(buf)/2, 1)) n, err := f.r.Read(bf) if err != nil && err != io.EOF {