-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathformat.go
More file actions
183 lines (150 loc) · 5.54 KB
/
format.go
File metadata and controls
183 lines (150 loc) · 5.54 KB
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
package pulse
import (
"io"
"reflect"
"unsafe"
"github.com/jfreymuth/pulse/proto"
)
// A Reader provides audio data in a specific format.
type Reader interface {
io.Reader
Format() byte // Format should return one of the format constants defined by the proto package
}
// A Writer accepts audio data in a specific format.
type Writer interface {
io.Writer
Format() byte // Format should return one of the format constants defined by the proto package
}
// Uint8Reader implements the Reader interface.
// The semantics are the same as io.Reader's Read.
type Uint8Reader func([]byte) (int, error)
// Int16Reader implements the Reader interface.
// The semantics are the same as io.Reader's Read, but it returns
// the number of int16 values read, not the number of bytes.
type Int16Reader func([]int16) (int, error)
// Int32Reader implements the Reader interface.
// The semantics are the same as io.Reader's Read, but it returns
// the number of int32 values read, not the number of bytes.
type Int32Reader func([]int32) (int, error)
// Float32Reader implements the Reader interface.
// The semantics are the same as io.Reader's Read, but it returns
// the number of float32 values read, not the number of bytes.
type Float32Reader func([]float32) (int, error)
// NewReader creates a reader from an io.Reader and a format.
// The format must be one of the constants defined in the proto package.
func NewReader(r io.Reader, format byte) Reader {
check(format)
return &reader{r, format}
}
// Uint8Writer implements the Writer interface.
// The semantics are the same as io.Writer's Write.
type Uint8Writer func([]byte) (int, error)
// Int16Writer implements the Writer interface.
// The semantics are the same as io.Writer's Write, but it returns
// the number of int16 values written, not the number of bytes.
type Int16Writer func([]int16) (int, error)
// Int32Writer implements the Writer interface.
// The semantics are the same as io.Writer's Write, but it returns
// the number of int32 values written, not the number of bytes.
type Int32Writer func([]int32) (int, error)
// Float32Writer implements the Writer interface.
// The semantics are the same as io.Writer's Write, but it returns
// the number of float32 values written, not the number of bytes.
type Float32Writer func([]float32) (int, error)
// NewWriter creates a writer from an io.Writer and a format.
// The format must be one of the constants defined in the proto package.
func NewWriter(w io.Writer, format byte) Writer {
check(format)
return &writer{w, format}
}
func (c Uint8Reader) Read(buf []byte) (int, error) { return c(buf) }
func (c Uint8Reader) Format() byte { return proto.FormatUint8 }
func (c Int16Reader) Read(buf []byte) (int, error) {
n, err := c(int16Slice(buf))
return n * 2, err
}
func (c Int16Reader) Format() byte { return formatI16 }
func (c Int32Reader) Read(buf []byte) (int, error) {
n, err := c(int32Slice(buf))
return n * 4, err
}
func (c Int32Reader) Format() byte { return formatI32 }
func (c Float32Reader) Read(buf []byte) (int, error) {
n, err := c(float32Slice(buf))
return n * 4, err
}
func (c Float32Reader) Format() byte { return formatF32 }
func (c Uint8Writer) Write(buf []byte) (int, error) { return c(buf) }
func (c Uint8Writer) Format() byte { return proto.FormatUint8 }
func (c Int16Writer) Write(buf []byte) (int, error) {
n, err := c(int16Slice(buf))
return n * 2, err
}
func (c Int16Writer) Format() byte { return formatI16 }
func (c Int32Writer) Write(buf []byte) (int, error) {
n, err := c(int32Slice(buf))
return n * 4, err
}
func (c Int32Writer) Format() byte { return formatI32 }
func (c Float32Writer) Write(buf []byte) (int, error) {
n, err := c(float32Slice(buf))
return n * 4, err
}
func (c Float32Writer) Format() byte { return formatF32 }
type reader struct {
r io.Reader
f byte
}
func (r *reader) Read(buf []byte) (int, error) { return r.r.Read(buf) }
func (r *reader) Format() byte { return r.f }
type writer struct {
w io.Writer
f byte
}
func (w *writer) Write(buf []byte) (int, error) { return w.w.Write(buf) }
func (w *writer) Format() byte { return w.f }
func bytes(f byte) int {
switch f {
case proto.FormatUint8:
return 1
case proto.FormatInt16LE, proto.FormatInt16BE:
return 2
case proto.FormatInt32LE, proto.FormatInt32BE, proto.FormatFloat32LE, proto.FormatFloat32BE:
return 4
}
panic("pulse: invalid format")
}
func check(f byte) {
switch f {
case proto.FormatUint8, proto.FormatInt16LE, proto.FormatInt16BE,
proto.FormatInt32LE, proto.FormatInt32BE, proto.FormatFloat32LE, proto.FormatFloat32BE:
return
}
panic("pulse: invalid format")
}
var formatI16, formatI32, formatF32 byte
func init() {
i := uint16(1)
littleEndian := *(*byte)(unsafe.Pointer(&i)) == 1
if littleEndian {
formatI16 = proto.FormatInt16LE
formatI32 = proto.FormatInt32LE
formatF32 = proto.FormatFloat32LE
} else {
formatI16 = proto.FormatInt16BE
formatI32 = proto.FormatInt32BE
formatF32 = proto.FormatFloat32BE
}
}
func int16Slice(s []byte) []int16 {
h := *(*reflect.SliceHeader)(unsafe.Pointer(&s))
return *(*[]int16)(unsafe.Pointer(&reflect.SliceHeader{Data: h.Data, Len: h.Len / 2, Cap: h.Len / 2}))
}
func int32Slice(s []byte) []int32 {
h := *(*reflect.SliceHeader)(unsafe.Pointer(&s))
return *(*[]int32)(unsafe.Pointer(&reflect.SliceHeader{Data: h.Data, Len: h.Len / 4, Cap: h.Len / 4}))
}
func float32Slice(s []byte) []float32 {
h := *(*reflect.SliceHeader)(unsafe.Pointer(&s))
return *(*[]float32)(unsafe.Pointer(&reflect.SliceHeader{Data: h.Data, Len: h.Len / 4, Cap: h.Len / 4}))
}