Skip to content

Cleanups #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 120 additions & 128 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,186 +1,178 @@
# g711
import "github.com/zaf/g711"

Package g711 implements encoding and decoding of G711 PCM sound data. G.711 is
an ITU-T standard for audio companding.
[![GoDoc](https://img.shields.io/badge/pkg.go.dev-doc-blue)](http://pkg.go.dev/.)

For usage details please see the code snippets in the cmd folder.
Package g711 implements encoding and decoding of G711 PCM sound data.
G.711 is an ITU-T standard for audio companding.

## Usage
The package exposes a high level API for encoding and decoding through
an io.WriteCloser. But also a low level API using preallocated buffers
for cases where performance and memory handling are critical.

```go
For usage details please see the code snippet in the cmd folder.

## Constants

```golang
const (
// Input and output formats
Alaw = iota // Alaw G711 encoded PCM data
Ulaw // Ulaw G711 encoded PCM data
Lpcm // Lpcm 16bit signed linear data
// Input and output formats
Alaw = iota + 1 // Alaw G711 encoded PCM data
Ulaw // Ulaw G711 encoded PCM data
Lpcm // Lpcm 16bit signed linear data
)
```

#### func Alaw2Ulaw
## Types

### type [Coder](/g711.go#L32)

`type Coder struct { ... }`

Coder encodes 16bit 8000Hz LPCM data to G711 PCM, or
decodes G711 PCM data to 16bit 8000Hz LPCM data, or
directly transcodes between A-law and u-law

#### func (*Coder) [Close](/g711.go#L96)

`func (w *Coder) Close() error`

Close closes the Encoder, it implements the io.Closer interface.

#### func (*Coder) [Reset](/g711.go#L105)

`func (w *Coder) Reset(writer io.Writer) error`

Reset discards the Encoder state. This permits reusing an Encoder rather than allocating a new one.

#### func (*Coder) [Write](/g711.go#L119)

`func (w *Coder) Write(p []byte) (int, error)`

Write encodes/decodes/transcodes sound data. Writes len(p) bytes from p to the underlying data stream,
returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered
that caused the write to stop early.


## Functions

### func [Alaw2Ulaw](/alaw.go#L129)

`func Alaw2Ulaw(alaw []byte) []byte`

```go
func Alaw2Ulaw(alaw []byte) []byte
```
Alaw2Ulaw performs direct A-law to u-law data conversion

#### func Alaw2UlawFrame
### func [Alaw2UlawFrame](/alaw.go#L145)

`func Alaw2UlawFrame(frame uint8) uint8`

```go
func Alaw2UlawFrame(frame uint8) uint8
```
Alaw2UlawFrame directly converts an A-law frame to u-law

#### func DecodeAlaw
### func [Alaw2UlawTo](/alaw.go#L138)

`func Alaw2UlawTo(alaw, ulaw []byte)`

Alaw2UlawTo performs direct A-law to u-law data conversion
using an already allocated buffer provided by the user.
The user is responsible for ensuring that the buffer is large enough (the size of the A-law data).

### func [DecodeAlaw](/alaw.go#L106)

`func DecodeAlaw(pcm []byte) []byte`

```go
func DecodeAlaw(pcm []byte) []byte
```
DecodeAlaw decodes A-law PCM data to 16bit LPCM

#### func DecodeAlawFrame
### func [DecodeAlawFrame](/alaw.go#L124)

`func DecodeAlawFrame(frame uint8) int16`

```go
func DecodeAlawFrame(frame uint8) int16
```
DecodeAlawFrame decodes an A-law PCM frame to 16bit LPCM

#### func DecodeUlaw
### func [DecodeAlawTo](/alaw.go#L115)

```go
func DecodeUlaw(pcm []byte) []byte
```
DecodeUlaw decodes u-law PCM data to 16bit LPCM
`func DecodeAlawTo(pcm, lpcm []byte)`

#### func DecodeUlawFrame
DecodeAlawTo decodes A-law PCM data to 16bit LPCM
using an already allocated buffer provided by the user.
The user is responsible for ensuring that the buffer is large enough (double the size of the PCM data).

```go
func DecodeUlawFrame(frame uint8) int16
```
DecodeUlawFrame decodes a u-law PCM frame to 16bit LPCM
### func [DecodeUlaw](/ulaw.go#L110)

#### func EncodeAlaw
`func DecodeUlaw(pcm []byte) []byte`

```go
func EncodeAlaw(lpcm []byte) []byte
```
EncodeAlaw encodes 16bit LPCM data to G711 A-law PCM
DecodeUlaw decodes u-law PCM data to 16bit LPCM

#### func EncodeAlawFrame
### func [DecodeUlawFrame](/ulaw.go#L128)

```go
func EncodeAlawFrame(frame int16) uint8
```
EncodeAlawFrame encodes a 16bit LPCM frame to G711 A-law PCM
`func DecodeUlawFrame(frame uint8) int16`

#### func EncodeUlaw
DecodeUlawFrame decodes a u-law PCM frame to 16bit LPCM

```go
func EncodeUlaw(lpcm []byte) []byte
```
EncodeUlaw encodes 16bit LPCM data to G711 u-law PCM
### func [DecodeUlawTo](/ulaw.go#L119)

#### func EncodeUlawFrame
`func DecodeUlawTo(pcm, lpcm []byte)`

```go
func EncodeUlawFrame(frame int16) uint8
```
EncodeUlawFrame encodes a 16bit LPCM frame to G711 u-law PCM
DecodeUlawTo decodes u-law PCM data to 16bit LPCM
using an already allocated buffer provided by the user.
The user is responsible for ensuring that the buffer is large enough (double the size of the PCM data).

#### func Ulaw2Alaw
### func [EncodeAlaw](/alaw.go#L74)

```go
func Ulaw2Alaw(ulaw []byte) []byte
```
Ulaw2Alaw performs direct u-law to A-law data conversion
`func EncodeAlaw(lpcm []byte) []byte`

#### func Ulaw2AlawFrame
EncodeAlaw encodes 16bit LPCM data to G711 A-law PCM

```go
func Ulaw2AlawFrame(frame uint8) uint8
```
Ulaw2AlawFrame directly converts a u-law frame to A-law
### func [EncodeAlawFrame](/alaw.go#L90)

#### type Decoder
`func EncodeAlawFrame(frame int16) uint8`

```go
type Decoder struct {
}
```
EncodeAlawFrame encodes a 16bit LPCM frame to G711 A-law PCM

Decoder reads G711 PCM data and decodes it to 16bit 8000Hz LPCM
### func [EncodeAlawTo](/alaw.go#L83)

#### func NewAlawDecoder
`func EncodeAlawTo(lpcm, alaw []byte)`

```go
func NewAlawDecoder(reader io.Reader) (*Decoder, error)
```
NewAlawDecoder returns a pointer to a Decoder that implements an io.Reader. It
takes as input the source data Reader.
EncodeAlawTo encodes 16bit LPCM data to G711 A-law PCM
using an already allocated buffer provided by the user.
The user is responsible for ensuring that the buffer is large enough (half the size of the LPCM data).

#### func NewUlawDecoder
### func [EncodeUlaw](/ulaw.go#L79)

```go
func NewUlawDecoder(reader io.Reader) (*Decoder, error)
```
NewUlawDecoder returns a pointer to a Decoder that implements an io.Reader. It
takes as input the source data Reader.
`func EncodeUlaw(lpcm []byte) []byte`

#### func (*Decoder) Read
EncodeUlaw encodes 16bit LPCM data to G711 u-law PCM

```go
func (r *Decoder) Read(p []byte) (i int, err error)
```
Read decodes G711 data. Reads up to len(p) bytes into p, returns the number of
bytes read and any error encountered.
### func [EncodeUlawFrame](/ulaw.go#L95)

#### func (*Decoder) Reset
`func EncodeUlawFrame(frame int16) uint8`

```go
func (r *Decoder) Reset(reader io.Reader) error
```
Reset discards the Decoder state. This permits reusing a Decoder rather than
allocating a new one.
EncodeUlawFrame encodes a 16bit LPCM frame to G711 u-law PCM

#### type Encoder
### func [EncodeUlawTo](/ulaw.go#L88)

```go
type Encoder struct {
}
```
`func EncodeUlawTo(lpcm, ulaw []byte)`

Encoder encodes 16bit 8000Hz LPCM data to G711 PCM or directly transcodes
between A-law and u-law
EncodeUlawTo encodes 16bit LPCM data to G711 u-law PCM
using an already allocated buffer provided by the user.
The user is responsible for ensuring that the buffer is large enough (half the size of the LPCM data).

#### func NewAlawEncoder
### func [Ulaw2Alaw](/ulaw.go#L133)

```go
func NewAlawEncoder(writer io.Writer, input int) (*Encoder, error)
```
NewAlawEncoder returns a pointer to an Encoder that implements an io.Writer. It
takes as input the destination data Writer and the input encoding format.
`func Ulaw2Alaw(ulaw []byte) []byte`

#### func NewUlawEncoder
Ulaw2Alaw performs direct u-law to A-law data conversion

```go
func NewUlawEncoder(writer io.Writer, input int) (*Encoder, error)
```
NewUlawEncoder returns a pointer to an Encoder that implements an io.Writer. It
takes as input the destination data Writer and the input encoding format.
### func [Ulaw2AlawFrame](/ulaw.go#L149)

#### func (*Encoder) Reset
`func Ulaw2AlawFrame(frame uint8) uint8`

```go
func (w *Encoder) Reset(writer io.Writer) error
```
Reset discards the Encoder state. This permits reusing an Encoder rather than
allocating a new one.
Ulaw2AlawFrame directly converts a u-law frame to A-law

#### func (*Encoder) Write
### func [Ulaw2AlawTo](/ulaw.go#L142)

```go
func (w *Encoder) Write(p []byte) (i int, err error)
```
Write encodes G711 Data. Writes len(p) bytes from p to the underlying data
stream, returns the number of bytes written from p (0 <= n <= len(p)) and any
error encountered that caused the write to stop early.
`func Ulaw2AlawTo(ulaw, alaw []byte)`

Ulaw2AlawTo performs direct u-law to A-law data conversion
using an already allocated buffer provided by the user.
The user is responsible for ensuring that the buffer is large enough (the size of the A-law data).

---
49 changes: 33 additions & 16 deletions alaw.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,29 @@ var (

// EncodeAlaw encodes 16bit LPCM data to G711 A-law PCM
func EncodeAlaw(lpcm []byte) []byte {
if len(lpcm) < 2 {
return []byte{}
}
alaw := make([]byte, len(lpcm)/2)
for i, j := 0, 0; j <= len(lpcm)-2; i, j = i+1, j+2 {
alaw[i] = EncodeAlawFrame(int16(lpcm[j]) | int16(lpcm[j+1])<<8)
}
alaw := make([]byte, len(lpcm)>>1)
EncodeAlawTo(lpcm, alaw)
return alaw
}

// EncodeAlawTo encodes 16bit LPCM data to G711 A-law PCM
// using an already allocated buffer provided by the user.
// The user is responsible for ensuring that the buffer is large enough (half the size of the LPCM data).
func EncodeAlawTo(lpcm, alaw []byte) {
for i := 0; i < len(lpcm)-1; i += 2 {
alaw[i>>1] = EncodeAlawFrame(int16(lpcm[i]) | int16(lpcm[i+1])<<8)
}
}

// EncodeAlawFrame encodes a 16bit LPCM frame to G711 A-law PCM
func EncodeAlawFrame(frame int16) uint8 {
var compressedByte, seg, sign int16
sign = ((^frame) >> 8) & 0x80
sign := ((^frame) >> 8) & 0x80
if sign == 0 {
frame = ^frame
}
compressedByte = frame >> 4
compressedByte := frame >> 4
if compressedByte > 15 {
seg = int16(12 - bits.LeadingZeros16(uint16(compressedByte)))
seg := int16(12 - bits.LeadingZeros16(uint16(compressedByte)))
compressedByte >>= seg - 1
compressedByte -= 16
compressedByte += seg << 4
Expand All @@ -102,12 +105,19 @@ func EncodeAlawFrame(frame int16) uint8 {
// DecodeAlaw decodes A-law PCM data to 16bit LPCM
func DecodeAlaw(pcm []byte) []byte {
lpcm := make([]byte, len(pcm)*2)
for i, j := 0, 0; i < len(pcm); i, j = i+1, j+2 {
DecodeAlawTo(pcm, lpcm)
return lpcm
}

// DecodeAlawTo decodes A-law PCM data to 16bit LPCM
// using an already allocated buffer provided by the user.
// The user is responsible for ensuring that the buffer is large enough (double the size of the PCM data).
func DecodeAlawTo(pcm, lpcm []byte) {
for i := 0; i < len(pcm); i++ {
frame := alaw2lpcm[pcm[i]]
lpcm[j] = byte(frame)
lpcm[j+1] = byte(frame >> 8)
lpcm[i*2] = byte(frame)
lpcm[i*2+1] = byte(frame >> 8)
}
return lpcm
}

// DecodeAlawFrame decodes an A-law PCM frame to 16bit LPCM
Expand All @@ -118,10 +128,17 @@ func DecodeAlawFrame(frame uint8) int16 {
// Alaw2Ulaw performs direct A-law to u-law data conversion
func Alaw2Ulaw(alaw []byte) []byte {
ulaw := make([]byte, len(alaw))
Alaw2UlawTo(alaw, ulaw)
return ulaw
}

// Alaw2UlawTo performs direct A-law to u-law data conversion
// using an already allocated buffer provided by the user.
// The user is responsible for ensuring that the buffer is large enough (the size of the A-law data).
func Alaw2UlawTo(alaw, ulaw []byte) {
for i := 0; i < len(alaw); i++ {
ulaw[i] = alaw2ulaw[alaw[i]]
}
return ulaw
}

// Alaw2UlawFrame directly converts an A-law frame to u-law
Expand Down
Loading
Loading