audio/mp3: Refactoring: layer member usage

This commit is contained in:
Hajime Hoshi 2017-06-17 18:20:43 +09:00
parent b8a965b471
commit 0e4c949471
3 changed files with 14 additions and 16 deletions

View File

@ -35,7 +35,7 @@ func readMainL3() error {
/* Calculate header audio data size */ /* Calculate header audio data size */
framesize := (144* framesize := (144*
g_mpeg1_bitrates[theMPEG1FrameHeader.layer-1][theMPEG1FrameHeader.bitrate_index])/ g_mpeg1_bitrates[theMPEG1FrameHeader.layer][theMPEG1FrameHeader.bitrate_index])/
g_sampling_frequency[theMPEG1FrameHeader.sampling_frequency] + g_sampling_frequency[theMPEG1FrameHeader.sampling_frequency] +
int(theMPEG1FrameHeader.padding_bit) int(theMPEG1FrameHeader.padding_bit)

View File

@ -52,9 +52,8 @@ func readFrame() error {
return err return err
} }
} }
if theMPEG1FrameHeader.layer != mpeg1Layer3 {
if theMPEG1FrameHeader.layer != 3 { return fmt.Errorf("mp3: only layer3 (want %d; got %d) is supported!", mpeg1Layer3, theMPEG1FrameHeader.layer)
return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", theMPEG1FrameHeader.layer)
} }
// Get side info // Get side info
if err := readAudioL3(); err != nil { if err := readAudioL3(); err != nil {
@ -140,26 +139,25 @@ func readHeader() error {
theMPEG1FrameHeader.emphasis = int((header & 0x00000003) >> 0) theMPEG1FrameHeader.emphasis = int((header & 0x00000003) >> 0)
/* Check for invalid values and impossible combinations */ /* Check for invalid values and impossible combinations */
if theMPEG1FrameHeader.id != 3 { if theMPEG1FrameHeader.id != 3 {
return fmt.Errorf("mp3: ID must be 3\nHeader word is 0x%08x at file pos %d", return fmt.Errorf("mp3: ID must be 3. Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if theMPEG1FrameHeader.bitrate_index == 0 { if theMPEG1FrameHeader.bitrate_index == 0 {
return fmt.Errorf("mp3: Free bitrate format NIY!\nHeader word is 0x%08x at file pos %d", return fmt.Errorf("mp3: Free bitrate format NIY! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if theMPEG1FrameHeader.bitrate_index == 15 { if theMPEG1FrameHeader.bitrate_index == 15 {
return fmt.Errorf("mp3: bitrate_index = 15 is invalid!\nHeader word is 0x%08x at file pos %d", return fmt.Errorf("mp3: bitrate_index = 15 is invalid! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if theMPEG1FrameHeader.sampling_frequency == 3 { if theMPEG1FrameHeader.sampling_frequency == 3 {
return fmt.Errorf("mp3: sampling_frequency = 3 is invalid! Header word is 0x%08x at file pos %d", return fmt.Errorf("mp3: sampling_frequency = 3 is invalid! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if theMPEG1FrameHeader.layer == 0 { if theMPEG1FrameHeader.layer == mpeg1LayerReserved {
return fmt.Errorf("mp3: layer = 0 is invalid! Header word is 0x%08x at file pos %d", return fmt.Errorf("mp3: layer = %d is invalid! Header word is 0x%08x at file pos %d",
header, getFilepos()) mpeg1LayerReserved, header, getFilepos())
} }
theMPEG1FrameHeader.layer = 4 - theMPEG1FrameHeader.layer
return nil return nil
} }

View File

@ -21,16 +21,16 @@ import (
"io" "io"
) )
var g_mpeg1_bitrates = [3][15]int{ var g_mpeg1_bitrates = map[mpeg1Layer][15]int{
{ /* Layer 1 */ mpeg1Layer1: {
0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000,
256000, 288000, 320000, 352000, 384000, 416000, 448000, 256000, 288000, 320000, 352000, 384000, 416000, 448000,
}, },
{ /* Layer 2 */ mpeg1Layer2: {
0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000,
128000, 160000, 192000, 224000, 256000, 320000, 384000, 128000, 160000, 192000, 224000, 256000, 320000, 384000,
}, },
{ /* Layer 3 */ mpeg1Layer3: {
0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000,
112000, 128000, 160000, 192000, 224000, 256000, 320000, 112000, 128000, 160000, 192000, 224000, 256000, 320000,
}, },
@ -45,7 +45,7 @@ func readAudioL3() error {
} }
/* Calculate header audio data size */ /* Calculate header audio data size */
framesize := (144* framesize := (144*
g_mpeg1_bitrates[theMPEG1FrameHeader.layer-1][theMPEG1FrameHeader.bitrate_index])/ g_mpeg1_bitrates[theMPEG1FrameHeader.layer][theMPEG1FrameHeader.bitrate_index])/
g_sampling_frequency[theMPEG1FrameHeader.sampling_frequency] + g_sampling_frequency[theMPEG1FrameHeader.sampling_frequency] +
int(theMPEG1FrameHeader.padding_bit) int(theMPEG1FrameHeader.padding_bit)
if framesize > 2000 { if framesize > 2000 {