audio/internal/convert: Refactoring

This commit is contained in:
Hajime Hoshi 2021-01-04 21:42:00 +09:00
parent b7abde7f00
commit 3fb304e5a8

View File

@ -32,6 +32,13 @@ type f32Reader struct {
buf *byte buf *byte
} }
func max(a, b int) int {
if a < b {
return b
}
return a
}
func (f *f32Reader) Read(buf []byte) (int, error) { func (f *f32Reader) Read(buf []byte) (int, error) {
if f.eof { if f.eof {
return 0, io.EOF return 0, io.EOF
@ -45,10 +52,7 @@ func (f *f32Reader) Read(buf []byte) (int, error) {
return 1, nil return 1, nil
} }
bf := make([]float32, len(buf)/2) bf := make([]float32, max(len(buf)/2, 1))
if len(buf) == 1 {
bf = make([]float32, 1)
}
n, err := f.r.Read(bf) n, err := f.r.Read(bf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {