audio/mp3: Make Get_Byte a Go function

This commit is contained in:
Hajime Hoshi 2017-06-17 01:36:58 +09:00
parent bdb87b0257
commit b08897a4e5
3 changed files with 47 additions and 22 deletions

View File

@ -39,14 +39,28 @@ var (
//export Read_CRC
func Read_CRC() C.int {
if Get_Byte() == eof || Get_Byte() == eof {
return C.FALSE
buf := make([]int, 2)
n := 0
var err error
for n < 2 && err == nil {
nn, err2 := getBytes(buf[n:])
n += nn
err = err2
}
if err == io.EOF {
if n < 2 {
return C.ERROR
}
return C.OK
}
if err != nil {
g_error = err
return C.ERROR
}
return C.OK
}
//export Get_Byte
func Get_Byte() C.unsigned {
func getByte() (uint8, error) {
for len(readerCache) == 0 && !readerEOF {
buf := make([]uint8, 4096)
n, err := reader.Read(buf)
@ -55,26 +69,26 @@ func Get_Byte() C.unsigned {
if err == io.EOF {
readerEOF = true
} else {
panic(err)
return 0, err
}
}
}
if len(readerCache) == 0 {
return eof
return 0, io.EOF
}
b := readerCache[0]
readerCache = readerCache[1:]
readerPos++
return C.unsigned(b)
return b, nil
}
func getBytes(buf []int) (int, error) {
for i := range buf {
v := Get_Byte()
if v == eof {
v, err := getByte()
buf[i] = int(v)
if err == io.EOF {
return i, io.EOF
}
buf[i] = int(v)
}
return len(buf), nil
}

View File

@ -84,8 +84,6 @@ typedef struct { /* Scale factor band indices,for long and short windows */
}
t_sf_band_indices;
unsigned Get_Byte(void);
unsigned Get_Bytes(unsigned num, unsigned* data_vec);
unsigned Get_Filepos(void);
int Decode_L3(void);
int Read_Frame(void);

View File

@ -25,6 +25,7 @@ import "C"
import (
"fmt"
"io"
)
func isHeader(header uint32) bool {
@ -46,14 +47,24 @@ func isHeader(header uint32) bool {
//export Read_Header
func Read_Header() C.int {
/* Get the next four bytes from the bitstream */
b1 := uint32(Get_Byte())
b2 := uint32(Get_Byte())
b3 := uint32(Get_Byte())
b4 := uint32(Get_Byte())
/* If we got an End Of File condition we're done */
if (b1 == eof) || (b2 == eof) || (b3 == eof) || (b4 == eof) {
buf := make([]int, 4)
n := 0
var err error
for n < 4 && err == nil {
nn, err2 := getBytes(buf[n:])
n += nn
err = err2
}
if n < 4 {
if err != io.EOF {
g_error = err
}
return C.ERROR
}
b1 := uint32(buf[0])
b2 := uint32(buf[1])
b3 := uint32(buf[2])
b4 := uint32(buf[3])
header := (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)
for !isHeader(uint32(header)) {
/* No,so scan the bitstream one byte at a time until we find it or EOF */
@ -62,12 +73,14 @@ func Read_Header() C.int {
b2 = b3
b3 = b4
/* Get one new byte from the bitstream */
b4 = uint32(Get_Byte())
/* If we got an End Of File condition we're done */
if b4 == eof {
b, err := getByte()
if err != nil {
if err != io.EOF {
g_error = err
}
return C.ERROR
}
/* Make up the new header */
b4 = uint32(b)
header = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)
} /* while... */
/* If we get here we've found the sync word,and can decode the header