forked from zaf/g711
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g711.go
184 lines (165 loc) · 4.88 KB
/
g711.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Copyright (C) 2016 - 2017, Lefteris Zafiris <[email protected]>
This program is free software, distributed under the terms of
the BSD 3-Clause License. See the LICENSE file
at the top of the source tree.
*/
/*
Package g711 implements encoding and decoding of G711.0 compressed sound data.
G.711 is an ITU-T standard for audio companding.
*/
package g711
import (
"bytes"
"errors"
"io"
)
const (
// Alaw G711 encoded PCM data
Alaw = iota
// Ulaw G711 encoded PCM data
Ulaw
// Lpcm 16bit signed linear data
Lpcm
)
type encoder func(int16) uint8
type decoder func(uint8) int16
type transcoder func(uint8) uint8
// Reader reads G711 PCM data and decodes it to 16bit LPCM or directly transcodes between A-law and u-law
type Reader struct {
input int // source format
output int // output format
r io.Reader // source data
buf *bytes.Buffer // local buffer
}
// Writer encodes 16bit LPCM data to G711 PCM or directly transcodes between A-law and u-law
type Writer struct {
input int // source format
output int // output format
w io.Writer // output data
buf *bytes.Buffer //local buffer
}
// NewAlawReader returns a pointer to a Reader that decodes or trans-codes A-law data.
// It takes as input the source data Reader and the output encoding fomrat.
func NewAlawReader(reader io.Reader, output int) (*Reader, error) {
if output != Ulaw && output != Lpcm {
return nil, errors.New("Invalid output format")
}
b := new(bytes.Buffer)
return &Reader{input: Alaw, output: output, r: reader, buf: b}, nil
}
// NewUlawReader returns a pointer to a Reader that decodes or trans-codes u-law data.
// It takes as input the source data Reader and the output encoding fomrat.
func NewUlawReader(reader io.Reader, output int) (*Reader, error) {
if output != Alaw && output != Lpcm {
return nil, errors.New("Invalid output format")
}
b := new(bytes.Buffer)
return &Reader{input: Ulaw, output: output, r: reader, buf: b}, nil
}
// NewAlawWriter returns a pointer to a Writer that encodes data to A-law.
// It takes as input the destination data Writer and the input encoding fomrat.
func NewAlawWriter(writer io.Writer, input int) (*Writer, error) {
if input != Ulaw && input != Lpcm {
return nil, errors.New("Invalid input format")
}
b := new(bytes.Buffer)
return &Writer{input: input, output: Alaw, w: writer, buf: b}, nil
}
// NewUlawWriter returns a pointer to a Writer that encodes data to u-law.
// It takes as input the destination data Writer and the input encoding fomrat.
func NewUlawWriter(writer io.Writer, input int) (*Writer, error) {
if input != Alaw && input != Lpcm {
return nil, errors.New("Invalid input format")
}
b := new(bytes.Buffer)
return &Writer{input: input, output: Ulaw, w: writer, buf: b}, nil
}
// Reset discards the Reader state. This permits reusing a Reader rather than allocating a new one.
func (r *Reader) Reset(reader io.Reader) {
r.buf.Reset()
r.r = reader
}
// Reset discards the Writer state. This permits reusing a Writer rather than allocating a new one.
func (w *Writer) Reset(writer io.Writer) {
w.buf.Reset()
w.w = writer
}
// Read decodes G711 data. Reads up to len(p) bytes into p, returns the number
// of bytes read and any error encountered.
func (r *Reader) Read(p []byte) (int, error) {
var dec decoder
var tr transcoder
if r.input == Alaw {
dec = DecodeAlaw
tr = Alaw2Ulaw
} else {
dec = DecodeUlaw
tr = Ulaw2Alaw
}
b := make([]byte, 4096)
n, rErr := r.r.Read(b)
var wrErr error
for _, data := range b[0:n] {
if r.output == Lpcm {
lpcm := dec(data) // Decode G711 data to LPCM
_, wrErr = r.buf.Write([]byte{byte(lpcm), byte(lpcm >> 8)})
} else {
wrErr = r.buf.WriteByte(tr(data)) // Trans-code
}
if wrErr != nil {
break
}
}
i, err := r.buf.Read(p)
if err == nil {
if wrErr != nil {
err = wrErr
} else {
err = rErr
}
}
return i, err
}
// 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)/2 due to compression)
// and any error encountered that caused the write to stop early.
func (w *Writer) Write(p []byte) (int, error) {
var err, wrErr error
var enc encoder
var tr transcoder
if w.output == Alaw {
enc = EncodeAlaw
tr = Ulaw2Alaw
} else {
enc = EncodeUlaw
tr = Alaw2Ulaw
}
if w.input == Lpcm { // Encode LPCM data to G711
for i := 0; i <= len(p)-2; i = i + 2 {
wrErr = w.buf.WriteByte(enc(int16(p[i]) | int16(p[i+1])<<8))
if wrErr != nil {
break
}
}
} else { // Trans-code
for _, data := range p {
wrErr = w.buf.WriteByte(tr(data))
if wrErr != nil {
break
}
}
}
i, err := w.w.Write(w.buf.Bytes())
if err == nil {
err = wrErr
}
return i, err
}
// Flush flushes any pending data to the underlying writer.
func (w *Writer) Flush() (err error) {
if w.buf.Len() > 0 {
_, err = w.buf.WriteTo(w.w)
}
return
}