-
Notifications
You must be signed in to change notification settings - Fork 10
/
pcm_buffer.go
461 lines (436 loc) · 11.1 KB
/
pcm_buffer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package audio
import "math"
// PCMDataFormat is an enum type to indicate the underlying data format used.
type PCMDataFormat uint8
const (
// DataTypeUnknown refers to an unknown format
DataTypeUnknown PCMDataFormat = iota
// DataTypeI8 indicates that the content of the audio buffer made of 8-bit integers.
DataTypeI8
// DataTypeI16 indicates that the content of the audio buffer made of 16-bit integers.
DataTypeI16
// DataTypeI32 indicates that the content of the audio buffer made of 32-bit integers.
DataTypeI32
// DataTypeF32 indicates that the content of the audio buffer made of 32-bit floats.
DataTypeF32
// DataTypeF64 indicates that the content of the audio buffer made of 64-bit floats.
DataTypeF64
)
var _ Buffer = (*PCMBuffer)(nil)
// PCMBuffer encapsulates uncompressed audio data
// and provides useful methods to read/manipulate this PCM data.
// It's a more flexible buffer type allowing the developer to handle
// different kind of buffer data formats and convert between underlying
// types.
type PCMBuffer struct {
// Format describes the format of the buffer data.
Format *Format
// I8 is a store for audio sample data as integers.
I8 []int8
// I16 is a store for audio sample data as integers.
I16 []int16
// I32 is a store for audio sample data as integers.
I32 []int32
// F32 is a store for audio samples data as float64.
F32 []float32
// F64 is a store for audio samples data as float64.
F64 []float64
// DataType indicates the primary format used for the underlying data.
// The consumer of the buffer might want to look at this value to know what store
// to use to optimaly retrieve data.
DataType PCMDataFormat
// SourceBitDepth helps us know if the source was encoded on
// 1 (int8), 2 (int16), 3(int24), 4(int32), 8(int64) bytes.
SourceBitDepth uint8
}
// Len returns the length of the underlying data.
func (b *PCMBuffer) Len() int {
if b == nil {
return 0
}
switch b.DataType {
case DataTypeI8:
return len(b.I8)
case DataTypeI16:
return len(b.I16)
case DataTypeI32:
return len(b.I32)
case DataTypeF32:
return len(b.F32)
case DataTypeF64:
return len(b.F64)
default:
return 0
}
}
// PCMFormat returns the buffer format information.
func (b *PCMBuffer) PCMFormat() *Format {
if b == nil {
return nil
}
return b.Format
}
// NumFrames returns the number of frames contained in the buffer.
func (b *PCMBuffer) NumFrames() int {
if b == nil || b.Format == nil {
return 0
}
numChannels := b.Format.NumChannels
if numChannels == 0 {
numChannels = 1
}
return b.Len() / numChannels
}
// AsFloatBuffer returns a copy of this buffer but with data converted to floats.
func (b *PCMBuffer) AsFloatBuffer() *FloatBuffer {
newB := &FloatBuffer{}
newB.Data = b.AsF64()
if b.Format != nil {
newB.Format = &Format{
NumChannels: b.Format.NumChannels,
SampleRate: b.Format.SampleRate,
}
}
return newB
}
// AsFloat32Buffer implements the Buffer interface and returns a float 32 version of itself.
func (b *PCMBuffer) AsFloat32Buffer() *Float32Buffer {
newB := &Float32Buffer{}
newB.Data = b.AsF32()
if b.Format != nil {
newB.Format = &Format{
NumChannels: b.Format.NumChannels,
SampleRate: b.Format.SampleRate,
}
}
return newB
}
// AsIntBuffer returns a copy of this buffer but with data truncated to Ints.
func (b *PCMBuffer) AsIntBuffer() *IntBuffer {
newB := &IntBuffer{}
newB.Data = b.AsInt()
if b.Format != nil {
newB.Format = &Format{
NumChannels: b.Format.NumChannels,
SampleRate: b.Format.SampleRate,
}
}
return newB
}
// AsI8 returns the buffer's samples as int8 sample values.
// If the buffer isn't in this format, a copy is created and converted.
// Note that converting might result in loss of resolution.
func (b *PCMBuffer) AsI8() (out []int8) {
if b == nil {
return nil
}
switch b.DataType {
case DataTypeI8:
return b.I8
case DataTypeI16:
out = make([]int8, len(b.I16))
for i := 0; i < len(b.I16); i++ {
out[i] = int8(b.I16[i])
}
case DataTypeI32:
out = make([]int8, len(b.I32))
for i := 0; i < len(b.I32); i++ {
out[i] = int8(b.I32[i])
}
case DataTypeF32:
out = make([]int8, len(b.F32))
for i := 0; i < len(b.F32); i++ {
out[i] = int8(b.F32[i])
}
case DataTypeF64:
out = make([]int8, len(b.F64))
for i := 0; i < len(b.F64); i++ {
out[i] = int8(b.F64[i])
}
}
return out
}
// AsI16 returns the buffer's samples as int16 sample values.
// If the buffer isn't in this format, a copy is created and converted.
// Note that converting might result in loss of resolution.
func (b *PCMBuffer) AsI16() (out []int16) {
if b == nil {
return nil
}
switch b.DataType {
case DataTypeI8:
out = make([]int16, len(b.I8))
for i := 0; i < len(b.I8); i++ {
out[i] = int16(b.I8[i])
}
case DataTypeI16:
return b.I16
case DataTypeI32:
out = make([]int16, len(b.I32))
for i := 0; i < len(b.I32); i++ {
out[i] = int16(b.I32[i])
}
case DataTypeF32:
out = make([]int16, len(b.F32))
for i := 0; i < len(b.F32); i++ {
out[i] = int16(b.F32[i])
}
case DataTypeF64:
out = make([]int16, len(b.F64))
for i := 0; i < len(b.F64); i++ {
out[i] = int16(b.F64[i])
}
}
return out
}
// AsI32 returns the buffer's samples as int32 sample values.
// If the buffer isn't in this format, a copy is created and converted.
// Note that converting a float to an int might result in unexpected truncations.
func (b *PCMBuffer) AsI32() (out []int32) {
if b == nil {
return nil
}
switch b.DataType {
case DataTypeI8:
out = make([]int32, len(b.I8))
for i := 0; i < len(b.I8); i++ {
out[i] = int32(b.I8[i])
}
case DataTypeI16:
out = make([]int32, len(b.I16))
for i := 0; i < len(b.I16); i++ {
out[i] = int32(b.I16[i])
}
case DataTypeI32:
return b.I32
case DataTypeF32:
out = make([]int32, len(b.F32))
for i := 0; i < len(b.F32); i++ {
out[i] = int32(b.F32[i])
}
case DataTypeF64:
out = make([]int32, len(b.F64))
for i := 0; i < len(b.F64); i++ {
out[i] = int32(b.F64[i])
}
}
return out
}
// AsInt returns the buffer content as integers (int32s).
// It's recommended to avoid this method since it creates
// an extra copy of the buffer content.
func (b *PCMBuffer) AsInt() (out []int) {
int32s := b.AsI32()
out = make([]int, len(int32s))
for i := 0; i < len(int32s); i++ {
out[i] = int(int32s[i])
}
return out
}
// AsF32 returns the buffer's samples as float32 sample values.
// If the buffer isn't in this format, a copy is created and converted.
// Note that converting might result in unexpected truncations.
func (b *PCMBuffer) AsF32() (out []float32) {
if b == nil {
return nil
}
switch b.DataType {
case DataTypeI8:
bitDepth := b.calculateIntBitDepth()
factor := math.Pow(2, 8*float64(bitDepth/8)-1)
out = make([]float32, len(b.I8))
for i := 0; i < len(b.I8); i++ {
out[i] = float32(float64(int64(b.I8[i])) / factor)
}
case DataTypeI16:
bitDepth := b.calculateIntBitDepth()
factor := math.Pow(2, 8*float64(bitDepth/8)-1)
out = make([]float32, len(b.I16))
for i := 0; i < len(b.I16); i++ {
out[i] = float32(float64(int64(b.I16[i])) / factor)
}
case DataTypeI32:
bitDepth := b.calculateIntBitDepth()
factor := math.Pow(2, 8*float64(bitDepth/8)-1)
out = make([]float32, len(b.I16))
for i := 0; i < len(b.I16); i++ {
out[i] = float32(float64(int64(b.I16[i])) / factor)
}
case DataTypeF32:
return b.F32
case DataTypeF64:
out = make([]float32, len(b.F64))
for i := 0; i < len(b.F64); i++ {
out[i] = float32(b.F64[i])
}
}
return out
}
// AsF64 returns the buffer's samples as float64 sample values.
// If the buffer isn't in this format, a copy is created and converted.
// Note that converting might result in unexpected truncations.
func (b *PCMBuffer) AsF64() (out []float64) {
if b == nil {
return nil
}
switch b.DataType {
case DataTypeI8:
bitDepth := b.calculateIntBitDepth()
factor := math.Pow(2, 8*float64(bitDepth/8)-1)
out = make([]float64, len(b.I8))
for i := 0; i < len(b.I8); i++ {
out[i] = float64(int64(b.I8[i])) / factor
}
case DataTypeI16:
bitDepth := b.calculateIntBitDepth()
factor := math.Pow(2, 8*float64(bitDepth/8)-1)
out = make([]float64, len(b.I16))
for i := 0; i < len(b.I16); i++ {
out[i] = float64(int64(b.I16[i])) / factor
}
case DataTypeI32:
bitDepth := b.calculateIntBitDepth()
factor := math.Pow(2, 8*float64(bitDepth/8)-1)
out = make([]float64, len(b.I16))
for i := 0; i < len(b.I16); i++ {
out[i] = float64(int64(b.I16[i])) / factor
}
case DataTypeF32:
out = make([]float64, len(b.F32))
for i := 0; i < len(b.F32); i++ {
out[i] = float64(b.F32[i])
}
case DataTypeF64:
return b.F64
}
return out
}
// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (b *PCMBuffer) Clone() Buffer {
if b == nil {
return nil
}
newB := &PCMBuffer{DataType: b.DataType}
switch b.DataType {
case DataTypeI8:
newB.I8 = make([]int8, len(b.I8))
copy(newB.I8, b.I8)
case DataTypeI16:
newB.I16 = make([]int16, len(b.I16))
copy(newB.I16, b.I16)
case DataTypeI32:
newB.I32 = make([]int32, len(b.I32))
copy(newB.I32, b.I32)
case DataTypeF32:
newB.F32 = make([]float32, len(b.F32))
copy(newB.F32, b.F32)
case DataTypeF64:
newB.F64 = make([]float64, len(b.F64))
copy(newB.F64, b.F64)
}
newB.Format = &Format{
NumChannels: b.Format.NumChannels,
SampleRate: b.Format.SampleRate,
}
return newB
}
// SwitchPrimaryType is a convenience method to switch the primary data type.
// Use this if you process/swap a different type than the original type.
// Notes that conversion might be lossy if you switch to a lower resolution format.
func (b *PCMBuffer) SwitchPrimaryType(t PCMDataFormat) {
if b == nil || t == b.DataType {
return
}
switch t {
case DataTypeI8:
b.I8 = b.AsI8()
b.I16 = nil
b.I32 = nil
b.F32 = nil
b.F64 = nil
case DataTypeI16:
b.I8 = nil
b.I16 = b.AsI16()
b.I32 = nil
b.F32 = nil
b.F64 = nil
case DataTypeI32:
b.I8 = nil
b.I16 = nil
b.I32 = b.AsI32()
b.F32 = nil
b.F64 = nil
case DataTypeF32:
b.I8 = nil
b.I16 = nil
b.I32 = nil
b.F32 = b.AsF32()
b.F64 = nil
case DataTypeF64:
b.I8 = nil
b.I16 = nil
b.I32 = nil
b.F32 = nil
b.F64 = b.AsF64()
}
b.DataType = t
}
// calculateIntBithDepth looks at the int values in the buffer and returns
// the required lowest bit depth.
func (b *PCMBuffer) calculateIntBitDepth() uint8 {
if b == nil {
return 0
}
bitDepth := b.SourceBitDepth
if bitDepth != 0 {
return bitDepth
}
var max int64
switch b.DataType {
case DataTypeI8:
var i8max int8
for _, s := range b.I8 {
if s > i8max {
i8max = s
}
}
max = int64(i8max)
case DataTypeI16:
var i16max int16
for _, s := range b.I16 {
if s > i16max {
i16max = s
}
}
max = int64(i16max)
case DataTypeI32:
var i32max int32
for _, s := range b.I32 {
if s > i32max {
i32max = s
}
}
max = int64(i32max)
default:
// This method is only meant to be used on int buffers.
return bitDepth
}
bitDepth = 8
if max > 127 {
bitDepth = 16
}
// greater than int16, expecting int24
if max > 32767 {
bitDepth = 24
}
// int 32
if max > 8388607 {
bitDepth = 32
}
// int 64
if max > 4294967295 {
bitDepth = 64
}
return bitDepth
}