-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffer32.go
64 lines (51 loc) · 1.55 KB
/
buffer32.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
package audio
import "time"
type BufferF32 struct {
format Format
offset uint32
frames uint32
data []float32
}
func NewBufferF32(format Format, duration time.Duration) *BufferF32 {
return NewBufferF32Frames(format, format.FrameCount(duration))
}
func NewBufferF32Frames(format Format, frames int) *BufferF32 {
samples := format.ChannelCount * frames
return &BufferF32{
format: format,
offset: 0,
frames: uint32(frames),
data: make([]float32, samples, samples),
}
}
func (b *BufferF32) InternalBuffer() []float32 { return b.data }
func (b *BufferF32) Interleaved() []float32 {
start := int(b.offset)
return b.data[start : start+b.SampleCount()]
}
func (b *BufferF32) SampleRate() int { return b.format.SampleRate }
func (b *BufferF32) ChannelCount() int { return b.format.ChannelCount }
func (b *BufferF32) Empty() bool { return b.frames == 0 }
func (b *BufferF32) FrameCount() int { return int(b.frames) }
func (b *BufferF32) SampleCount() int { return int(b.frames) * b.ChannelCount() }
func (b *BufferF32) Duration() time.Duration {
return time.Duration(int(time.Second) * b.FrameCount() / b.SampleRate())
}
func (b *BufferF32) ShallowCopy() Buffer {
x := *b
return &x
}
func (b *BufferF32) DeepCopy() Buffer {
x := *b
x.data = make([]float32, len(b.data), len(b.data))
copy(x.data, b.data)
return &x
}
func (b *BufferF32) Slice(low, high int) {
b.offset += uint32(low * b.ChannelCount())
b.frames = uint32(high - low)
}
func (b *BufferF32) CutLeading(low int) {
b.offset += uint32(low * b.ChannelCount())
b.frames -= uint32(low)
}