audio/mp3: Refactoring getByte(s)

This commit is contained in:
Hajime Hoshi 2017-06-17 00:34:54 +09:00
parent e41c0d3514
commit e5fbcd50de
3 changed files with 20 additions and 16 deletions

View File

@ -59,7 +59,7 @@ func Get_Byte() C.unsigned {
} }
readerCache = buf[:n] readerCache = buf[:n]
} }
if readerEOF { if len(readerCache) == 0 {
return eof return eof
} }
b := readerCache[0] b := readerCache[0]
@ -68,16 +68,15 @@ func Get_Byte() C.unsigned {
return C.unsigned(b) return C.unsigned(b)
} }
func getBytes(num int) ([]int, error) { func getBytes(buf []int) (int, error) {
r := make([]int, num) for i := range buf {
for i := 0; i < num; i++ {
v := Get_Byte() v := Get_Byte()
if v == eof { 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 //export Get_Filepos

View File

@ -25,6 +25,7 @@ import "C"
import ( import (
"fmt" "fmt"
"io"
) )
var mpeg1_scalefac_sizes = [16][2]int{ 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 // 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 // read the main_data bits from the bitstream in case they are needed
// for decoding the next frame. // for decoding the next frame.
b, err := getBytes(size) buf := make([]int, size)
if err != nil { n, err := getBytes(buf)
if err != nil && err != io.EOF {
g_error = err g_error = err
return C.ERROR return C.ERROR
} }
copy(theMainDataBytes.vec[theMainDataBytes.top:], b) copy(theMainDataBytes.vec[theMainDataBytes.top:], buf[:n])
/* Set up pointers */ /* Set up pointers */
theMainDataBytes.ptr = theMainDataBytes.vec[0:] theMainDataBytes.ptr = theMainDataBytes.vec[0:]
theMainDataBytes.pos = 0 theMainDataBytes.pos = 0
@ -201,12 +203,13 @@ func getMainData(size int, begin int) int {
theMainDataBytes.vec[i] = theMainDataBytes.vec[theMainDataBytes.top-begin+i] theMainDataBytes.vec[i] = theMainDataBytes.vec[theMainDataBytes.top-begin+i]
} }
/* Read the main_data from file */ /* Read the main_data from file */
b, err := getBytes(int(size)) buf := make([]int, size)
if err != nil { n, err := getBytes(buf)
if err != nil && err != io.EOF {
g_error = err g_error = err
return C.ERROR return C.ERROR
} }
copy(theMainDataBytes.vec[begin:], b) copy(theMainDataBytes.vec[begin:], buf[:n])
/* Set up pointers */ /* Set up pointers */
theMainDataBytes.ptr = theMainDataBytes.vec[0:] theMainDataBytes.ptr = theMainDataBytes.vec[0:]
theMainDataBytes.pos = 0 theMainDataBytes.pos = 0

View File

@ -24,6 +24,7 @@ import "C"
import ( import (
"fmt" "fmt"
"io"
) )
var g_mpeg1_bitrates = [3][15]int{ var g_mpeg1_bitrates = [3][15]int{
@ -138,13 +139,14 @@ type sideInfo struct {
var theSideInfo sideInfo var theSideInfo sideInfo
func getSideinfo(size int) { func getSideinfo(size int) {
var err error buf := make([]int, size)
theSideInfo.vec, err = getBytes(int(size)) n, err := getBytes(buf)
if err != nil { if err != nil && err != io.EOF {
g_error = fmt.Errorf("mp3: couldn't read sideinfo %d bytes at pos %d: %v", g_error = fmt.Errorf("mp3: couldn't read sideinfo %d bytes at pos %d: %v",
size, Get_Filepos(), err) size, Get_Filepos(), err)
return return
} }
theSideInfo.vec = buf[:n]
theSideInfo.idx = 0 theSideInfo.idx = 0
} }