-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Package g711 contains utilities to work with the G711 codec. | ||
package g711 | ||
|
||
var mulawTable = func() [256]uint16 { | ||
var ret [256]uint16 | ||
for i := 0; i < 256; i++ { | ||
v := ^i | ||
|
||
tmp := (((uint16(v) & 0x0F) << 3) + 0x84) << ((v & 0x70) >> 4) | ||
|
||
if (v & 0x80) != 0 { | ||
ret[i] = 0x84 - tmp | ||
} else { | ||
ret[i] = tmp - 0x84 | ||
} | ||
} | ||
return ret | ||
}() | ||
|
||
var alawTable = func() [256]uint16 { | ||
var ret [256]uint16 | ||
for i := 0; i < 256; i++ { | ||
v := i ^ 0x55 | ||
|
||
t := uint16(v) & 0x0F | ||
seg := (uint16(v) & 0x70) >> 4 | ||
|
||
if seg != 0 { | ||
t = (t*2 + 1 + 32) << (seg + 2) | ||
} else { | ||
t = (t*2 + 1) << 3 | ||
} | ||
|
||
if (v & 0x80) != 0 { | ||
ret[i] = t | ||
} else { | ||
ret[i] = -t | ||
} | ||
} | ||
return ret | ||
}() | ||
|
||
// DecodeMulaw decodes 8-bit G711 samples (MU-law) into 16-bit LPCM samples. | ||
func DecodeMulaw(in []byte) []byte { | ||
out := make([]byte, len(in)*2) | ||
for i, sample := range in { | ||
out[i*2] = uint8(mulawTable[sample] >> 8) | ||
out[(i*2)+1] = uint8(mulawTable[sample]) | ||
} | ||
return out | ||
} | ||
|
||
// DecodeAlaw decodes 8-bit G711 samples (A-law) into 16-bit LPCM samples. | ||
func DecodeAlaw(in []byte) []byte { | ||
out := make([]byte, len(in)*2) | ||
for i, sample := range in { | ||
out[i*2] = uint8(alawTable[sample] >> 8) | ||
out[(i*2)+1] = uint8(alawTable[sample]) | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package g711 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecodeMuLaw(t *testing.T) { | ||
require.Equal(t, | ||
DecodeMulaw([]byte{1, 2, 3, 255, 254, 253}), | ||
[]byte{ | ||
0x86, 0x84, 0x8a, 0x84, 0x8e, 0x84, 0x00, 0x00, | ||
0x00, 0x08, 0x00, 0x10, | ||
}, | ||
) | ||
} | ||
|
||
func TestDecodeALaw(t *testing.T) { | ||
require.Equal(t, | ||
DecodeAlaw([]byte{1, 2, 3, 255, 254, 253}), | ||
[]byte{ | ||
0xeb, 0x80, 0xe8, 0x80, 0xe9, 0x80, 0x03, 0x50, | ||
0x03, 0x70, 0x03, 0x10, | ||
}, | ||
) | ||
} |