From e5fbcd50de6ca3024db587b80e2d33c424e80e02 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 17 Jun 2017 00:34:54 +0900 Subject: [PATCH] audio/mp3: Refactoring getByte(s) --- audio/mp3/decode_notjs.go | 13 ++++++------- audio/mp3/maindata.go | 15 +++++++++------ audio/mp3/sideinfo.go | 8 +++++--- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/audio/mp3/decode_notjs.go b/audio/mp3/decode_notjs.go index 76f0a0e50..6ee24768c 100644 --- a/audio/mp3/decode_notjs.go +++ b/audio/mp3/decode_notjs.go @@ -59,7 +59,7 @@ func Get_Byte() C.unsigned { } readerCache = buf[:n] } - if readerEOF { + if len(readerCache) == 0 { return eof } b := readerCache[0] @@ -68,16 +68,15 @@ func Get_Byte() C.unsigned { return C.unsigned(b) } -func getBytes(num int) ([]int, error) { - r := make([]int, num) - for i := 0; i < num; i++ { +func getBytes(buf []int) (int, error) { + for i := range buf { v := Get_Byte() if v == eof { - return r, io.EOF + return i, io.EOF } - r[i] = int(v) + buf[i] = int(v) } - return r, nil + return len(buf), nil } //export Get_Filepos diff --git a/audio/mp3/maindata.go b/audio/mp3/maindata.go index 451994a4e..553bdc212 100644 --- a/audio/mp3/maindata.go +++ b/audio/mp3/maindata.go @@ -25,6 +25,7 @@ import "C" import ( "fmt" + "io" ) var mpeg1_scalefac_sizes = [16][2]int{ @@ -183,12 +184,13 @@ func getMainData(size int, begin int) int { // No,there is not,so we skip decoding this frame,but we have to // read the main_data bits from the bitstream in case they are needed // for decoding the next frame. - b, err := getBytes(size) - if err != nil { + buf := make([]int, size) + n, err := getBytes(buf) + if err != nil && err != io.EOF { g_error = err return C.ERROR } - copy(theMainDataBytes.vec[theMainDataBytes.top:], b) + copy(theMainDataBytes.vec[theMainDataBytes.top:], buf[:n]) /* Set up pointers */ theMainDataBytes.ptr = theMainDataBytes.vec[0:] theMainDataBytes.pos = 0 @@ -201,12 +203,13 @@ func getMainData(size int, begin int) int { theMainDataBytes.vec[i] = theMainDataBytes.vec[theMainDataBytes.top-begin+i] } /* Read the main_data from file */ - b, err := getBytes(int(size)) - if err != nil { + buf := make([]int, size) + n, err := getBytes(buf) + if err != nil && err != io.EOF { g_error = err return C.ERROR } - copy(theMainDataBytes.vec[begin:], b) + copy(theMainDataBytes.vec[begin:], buf[:n]) /* Set up pointers */ theMainDataBytes.ptr = theMainDataBytes.vec[0:] theMainDataBytes.pos = 0 diff --git a/audio/mp3/sideinfo.go b/audio/mp3/sideinfo.go index dab63c221..7ed3fafad 100644 --- a/audio/mp3/sideinfo.go +++ b/audio/mp3/sideinfo.go @@ -24,6 +24,7 @@ import "C" import ( "fmt" + "io" ) var g_mpeg1_bitrates = [3][15]int{ @@ -138,13 +139,14 @@ type sideInfo struct { var theSideInfo sideInfo func getSideinfo(size int) { - var err error - theSideInfo.vec, err = getBytes(int(size)) - if err != nil { + buf := make([]int, size) + n, err := getBytes(buf) + if err != nil && err != io.EOF { g_error = fmt.Errorf("mp3: couldn't read sideinfo %d bytes at pos %d: %v", size, Get_Filepos(), err) return } + theSideInfo.vec = buf[:n] theSideInfo.idx = 0 }